예제 #1
0
  /**
   * Removes all the given tags from the document.
   *
   * @param dom the document object.
   * @param tagName the tag name, examples: script, style, meta
   * @return the changed dom.
   */
  public static Document removeTags(Document dom, String tagName) {
    if (dom != null) {
      // NodeList list = dom.getElementsByTagName("SCRIPT");

      NodeList list;
      try {
        list = XPathHelper.evaluateXpathExpression(dom, "//" + tagName.toUpperCase());

        while (list.getLength() > 0) {
          Node sc = list.item(0);

          if (sc != null) {
            sc.getParentNode().removeChild(sc);
          }

          list = XPathHelper.evaluateXpathExpression(dom, "//" + tagName.toUpperCase());
          // list = dom.getElementsByTagName("SCRIPT");
        }
      } catch (XPathExpressionException e) {
        LOGGER.error(e.getMessage(), e);
      }

      return dom;
    }

    return null;
  }
예제 #2
0
    public List getTopLevelItemNodes(Document manifest) {
      List items = XPathHelper.selectNodes("//organization/item", manifest);
      if (items != null && items.size() > 1) return items;

      items = XPathHelper.selectNodes("//organization/item/item", manifest);
      if (items != null && items.size() > 1) return items;

      return XPathHelper.selectNodes("//organization/item/item/item", manifest);
    }
예제 #3
0
 public String getTitle(Node resourceNode) {
   String title = null;
   Node itemNode =
       XPathHelper.selectNode(
           "//item[@identifierref='" + this.getId(resourceNode) + "']",
           resourceNode.getOwnerDocument());
   if (itemNode != null) {
     title = XPathHelper.getNodeValue("./title", itemNode);
     if (title == null || "".equals(title)) {
       Document descriptor = getDescriptor(resourceNode);
       if (descriptor != null) {
         title = XPathHelper.getNodeValue("/CONTENT/TITLE", descriptor);
       }
     }
   }
   return title;
 }
  /** Write some objects to files. */
  @Test
  public void testWriteToFile() {
    File out = new File("xml_test_helper_out.xml");

    SimpleXml simpleXml = new SimpleXml();
    simpleXml.setField("testing");

    XmlTestHelper.writeObjectToFile(
        simpleXml, XmlTestHelper.createXmlManagerFor(SimpleXml.class), out);

    // and validate results
    XPathHelper helper = new XPathHelper(out);
    assertEquals(
        "The SimpleXml instance was not correctly unmarshalled.",
        simpleXml.getField(),
        helper.evaluateXPathAsString("//field"));

    // and clean.
    out.delete();
  }
예제 #5
0
    public Document getDescriptor(Node resourceNode) {
      String descriptorFilename =
          XPathHelper.getNodeValue("./file/@href", resourceNode)
              .replaceAll("[/\\\\]+", "\\" + File.separator);
      Document doc = null;
      DocumentBuilder docBuilder;
      InputStream fis = null;

      // There is no reason to attempt to parse the file for a title unless it can be parsed
      String lowerFilename = descriptorFilename.toLowerCase();
      if (!lowerFilename.endsWith(".xml")
          && !lowerFilename.endsWith(".html")
          && !lowerFilename.endsWith(".htm")
          && !lowerFilename.endsWith(".xhtml")) {
        return doc;
      }

      try {
        fis = new FileInputStream(pathToData + File.separator + descriptorFilename);
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        builderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        builderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        docBuilder = builderFactory.newDocumentBuilder();
        doc = (Document) docBuilder.parse(fis);
      } catch (FileNotFoundException e) {
        M_log.warn(
            "getDescriptor() file not found: " + pathToData + File.separator + descriptorFilename);
      } catch (ParserConfigurationException e) {
        M_log.warn("getDescriptor() parser config error: " + e.getMessage());
      } catch (SAXException e) {
        M_log.warn(
            "getDescriptor() SAX parse error on file "
                + pathToData
                + File.separator
                + descriptorFilename
                + ": "
                + e.getMessage());
      } catch (IOException e) {
        M_log.warn("getDescriptor() file IO exception", e);
      } finally {
        if (fis != null) {
          try {
            fis.close();
          } catch (IOException e) {
            // oh well, we tried
          }
        }
      }
      return doc;
    }
예제 #6
0
 public String getFilePathForNode(Node node, String basePath) {
   return XPathHelper.getNodeValue("./@href", node);
 }
예제 #7
0
 public List getResourceNodes(Document manifest) {
   return XPathHelper.selectNodes("//resource", manifest);
 }
예제 #8
0
 public Node getResourceForId(String resourceId, Document manifest) {
   return XPathHelper.selectNode(
       "//resource[@identifier='" + resourceId + "']", archiveManifest);
 }
예제 #9
0
 public List getItemNodes(Document manifest) {
   return XPathHelper.selectNodes("//item", manifest);
 }
예제 #10
0
 public String getDescription(Node itemNode) {
   String resourceId = XPathHelper.getNodeValue("./@identifierref", itemNode);
   Node resourceNode = manifestHelper.getResourceForId(resourceId, archiveManifest);
   return resourceHelper.getDescription(resourceNode);
 }
예제 #11
0
 public String getTitle(Node itemNode) {
   return XPathHelper.getNodeValue("./title", itemNode);
 }
예제 #12
0
 public String getId(Node itemNode) {
   return XPathHelper.getNodeValue("./@identifier", itemNode);
 }
예제 #13
0
 public boolean isFolder(Document resourceDescriptor) {
   return "true"
       .equals(XPathHelper.getNodeValue("/CONTENT/FLAGS/ISFOLDER/@value", resourceDescriptor));
 }
예제 #14
0
 public String getDescription(Node resourceNode) {
   Document descriptor = resourceHelper.getDescriptor(resourceNode);
   String desc = XPathHelper.getNodeValue("//TEXT", descriptor);
   return desc;
 }
예제 #15
0
 public String getId(Node resourceNode) {
   return XPathHelper.getNodeValue("./@identifier", resourceNode);
 }
예제 #16
0
 public String getType(Node resourceNode) {
   return XPathHelper.getNodeValue("./@type", resourceNode);
 }