Java Code Examples for javax.xml.parsers.DocumentBuilderFactory.setXIncludeAware()

The following are Jave code examples for showing how to use setXIncludeAware() of the javax.xml.parsers.DocumentBuilderFactory class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: alfresco-xml-factory   File: FactoryHelper.java   View Source Code Vote up 8 votes
private void setFeature(DocumentBuilderFactory factory, String feature, boolean enable)
{
    try
    {
        if (ADDITIONAL_FEATURE_X_INCLUDE_AWARE.equals(feature))
        {
            factory.setXIncludeAware(enable);
        }
        else if (ADDITIONAL_FEATURE_EXPAND_ENTITY_REFERENCES.equals(feature))
        {
            factory.setExpandEntityReferences(enable);
        }
        else
        {
            factory.setFeature(feature, enable);
        }
        debug(debugCounter+" DocumentBuilderFactory "+feature+" "+enable);
    }
    catch (ParserConfigurationException pce)
    {
        logConfigurationFailure(factory.getClass().getName(), feature, pce);
    }
}
 
Example 2
Project: lams   File: BasicParserPool.java   View Source Code Vote up 6 votes
/**
 * Initializes the pool with a new set of configuration options.
 * 
 * @throws XMLParserException thrown if there is a problem initialzing the pool
 */
protected synchronized void initializePool() throws XMLParserException {
    if (!dirtyBuilderConfiguration) {
        // in case the pool was initialized by some other thread
        return;
    }

    DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
    setAttributes(newFactory, builderAttributes);
    setFeatures(newFactory, builderFeatures);
    newFactory.setCoalescing(coalescing);
    newFactory.setExpandEntityReferences(expandEntityReferences);
    newFactory.setIgnoringComments(ignoreComments);
    newFactory.setIgnoringElementContentWhitespace(ignoreElementContentWhitespace);
    newFactory.setNamespaceAware(namespaceAware);
    newFactory.setSchema(schema);
    newFactory.setValidating(dtdValidating);
    newFactory.setXIncludeAware(xincludeAware);

    poolVersion++;
    dirtyBuilderConfiguration = false;
    builderFactory = newFactory;
    builderPool.clear();
}
 
Example 3
Project: hadoop   File: QueueConfigurationParser.java   View Source Code Vote up 6 votes
/**
 * Method to load the resource file.
 * generates the root.
 * 
 * @param resourceInput InputStream that provides the XML to parse
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
protected Queue loadResource(InputStream resourceInput)
  throws ParserConfigurationException, SAXException, IOException {
  DocumentBuilderFactory docBuilderFactory
    = DocumentBuilderFactory.newInstance();
  //ignore all comments inside the xml file
  docBuilderFactory.setIgnoringComments(true);

  //allow includes in the xml file
  docBuilderFactory.setNamespaceAware(true);
  try {
    docBuilderFactory.setXIncludeAware(true);
  } catch (UnsupportedOperationException e) {
    LOG.info(
      "Failed to set setXIncludeAware(true) for parser "
        + docBuilderFactory
        + NAME_SEPARATOR + e);
  }
  DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
  Document doc = null;
  Element queuesNode = null;

  doc = builder.parse(resourceInput);
  queuesNode = doc.getDocumentElement();
  return this.parseResource(queuesNode);
}
 
Example 4
Project: openjdk-jdk10   File: Bug6794483Test.java   View Source Code Vote up 6 votes
public Document parseXmlFile(String fileName) throws Exception {
    System.out.println("Parsing XML file... " + fileName);
    DocumentBuilder docBuilder = null;
    Document doc = null;
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setCoalescing(true);
    docBuilderFactory.setXIncludeAware(true);
    System.out.println("Include: " + docBuilderFactory.isXIncludeAware());
    docBuilderFactory.setNamespaceAware(true);
    docBuilderFactory.setExpandEntityReferences(true);

    docBuilder = docBuilderFactory.newDocumentBuilder();

    File sourceFile = new File(fileName);
    doc = docBuilder.parse(sourceFile);

    System.out.println("XML file parsed");
    return doc;

}
 
Example 5
Project: openjdk-jdk10   File: AuctionItemRepository.java   View Source Code Vote up 6 votes
/**
 * Test the simple case of including a document using xi:include using a
 * DocumentBuilder.
 *
 * @throws Exception If any errors occur.
 */
@Test(groups = {"readWriteLocalFiles"})
public void testXIncludeDOMPos() throws Exception {
    String resultFile = USER_DIR + "doc_xincludeDOM.out";
    String goldFile = GOLDEN_DIR + "doc_xincludeGold.xml";
    String xmlFile = XML_DIR + "doc_xinclude.xml";
    try (FileOutputStream fos = new FileOutputStream(resultFile)) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setXIncludeAware(true);
        dbf.setNamespaceAware(true);
        Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
        doc.setXmlStandalone(true);
        TransformerFactory.newInstance().newTransformer().
                transform(new DOMSource(doc), new StreamResult(fos));
    }
    assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
 
Example 6
Project: openjdk-jdk10   File: AuctionItemRepository.java   View Source Code Vote up 6 votes
/**
 * Test the simple case of including a document using xi:include within a
 * xi:fallback using a DocumentBuilder.
 *
 * @throws Exception If any errors occur.
 */
@Test(groups = {"readWriteLocalFiles"})
public void testXIncludeFallbackDOMPos() throws Exception {
    String resultFile = USER_DIR + "doc_fallbackDOM.out";
    String goldFile = GOLDEN_DIR + "doc_fallbackGold.xml";
    String xmlFile = XML_DIR + "doc_fallback.xml";
    try (FileOutputStream fos = new FileOutputStream(resultFile)) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setXIncludeAware(true);
        dbf.setNamespaceAware(true);

        Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
        doc.setXmlStandalone(true);
        TransformerFactory.newInstance().newTransformer()
                .transform(new DOMSource(doc), new StreamResult(fos));
    }
    assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
 
Example 7
Project: openjdk-jdk10   File: AuctionItemRepository.java   View Source Code Vote up 6 votes
/**
 * Test for xi:fallback where the fall back text is parsed as text. This
 * test uses a nested xi:include for the fallback test.
 *
 * @throws Exception If any errors occur.
 */
@Test(groups = {"readWriteLocalFiles"})
public void testXIncludeFallbackTextPos() throws Exception {
    String resultFile = USER_DIR + "doc_fallback_text.out";
    String goldFile = GOLDEN_DIR + "doc_fallback_textGold.xml";
    String xmlFile = XML_DIR + "doc_fallback_text.xml";
    try (FileOutputStream fos = new FileOutputStream(resultFile)) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setXIncludeAware(true);
        dbf.setNamespaceAware(true);

        Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
        doc.setXmlStandalone(true);
        TransformerFactory.newInstance().newTransformer()
                .transform(new DOMSource(doc), new StreamResult(fos));
    }
    assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
 
Example 8
Project: openjdk-jdk10   File: AuctionItemRepository.java   View Source Code Vote up 6 votes
/**
 * Test the XPointer element() framework with XInclude.
 *
 * @throws Exception If any errors occur.
 */
@Test(groups = {"readWriteLocalFiles"})
public void testXpointerElementPos() throws Exception {
    String resultFile = USER_DIR + "doc_xpointer_element.out";
    String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml";
    String xmlFile = XML_DIR + "doc_xpointer_element.xml";
    try (FileOutputStream fos = new FileOutputStream(resultFile)) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setXIncludeAware(true);
        dbf.setNamespaceAware(true);

        DocumentBuilder db = dbf.newDocumentBuilder();

        TransformerFactory.newInstance().newTransformer()
                .transform(new DOMSource(db.parse(new File(xmlFile))),
                        new StreamResult(fos));
    }
    assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
 
Example 9
Project: openjdk-jdk10   File: AuctionItemRepository.java   View Source Code Vote up 6 votes
/**
 * Test if xi:include may reference the doc containing the include if the
 * parse type is text.
 *
 * @throws Exception If any errors occur.
 */
@Test(groups = {"readWriteLocalFiles"})
public void testXIncludeLoopPos() throws Exception {
    String resultFile = USER_DIR + "doc_xinc_loops.out";
    String goldFile = GOLDEN_DIR + "doc_xinc_loopGold.xml";
    String xmlFile = XML_DIR + "doc_xinc_loops.xml";

    try (FileOutputStream fos = new FileOutputStream(resultFile)) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setXIncludeAware(true);
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File(xmlFile));
        doc.normalizeDocument();
        doc.setXmlStandalone(true);

        TransformerFactory.newInstance().newTransformer()
                .transform(new DOMSource(doc), new StreamResult(fos));
    }
    assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
 
Example 10
Project: openjdk-jdk10   File: AuctionItemRepository.java   View Source Code Vote up 6 votes
/**
 * Test if two non nested xi:include elements can include the same document
 * with an xi:include statement.
 *
 * @throws Exception If any errors occur.
 */
@Test(groups = {"readWriteLocalFiles"})
public void testXIncludeNestedPos() throws Exception {
    String resultFile = USER_DIR + "schedule.out";
    String goldFile = GOLDEN_DIR + "scheduleGold.xml";
    String xmlFile = XML_DIR + "schedule.xml";

    try (FileOutputStream fos = new FileOutputStream(resultFile)) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setXIncludeAware(true);
        dbf.setNamespaceAware(true);

        Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
        doc.setXmlStandalone(true);
        TransformerFactory.newInstance().newTransformer()
                .transform(new DOMSource(doc), new StreamResult(fos));
    }
    assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
 
Example 11
Project: Android_Code_Arbiter   File: DocumentBuilderSafeProperty.java   View Source Code Vote up 6 votes
/**
 * This implementation allow DTD but disable all its dangerous features.
 * Not sure it can still do something useful with DTD ...
 */
public static void safeManualConfiguration() throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setFeature("http://xml.org/sax/features/external-general-entities",true);
    dbf.setFeature("http://xml.org/sax/features/external-parameter-entities",true);
    dbf.setXIncludeAware(false);
    dbf.setExpandEntityReferences(false);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(getInputFile());
    print(doc);
}
 
Example 12
Project: Android_Code_Arbiter   File: DocumentBuilderSafeProperty.java   View Source Code Vote up 6 votes
public static void unsafeManualConfig4() throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setFeature("http://xml.org/sax/features/external-general-entities",true);
    dbf.setFeature("http://xml.org/sax/features/external-parameter-entities",true);
    dbf.setXIncludeAware(false);
    //dbf.setExpandEntityReferences(false);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(getInputFile());
    print(doc);
}
 
Example 13
Project: incubator-servicecomb-java-chassis   File: FortifyUtils.java   View Source Code Vote up 5 votes
public static DocumentBuilderFactory getSecurityXmlDocumentFactory() throws ParserConfigurationException {
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
  factory.setXIncludeAware(false);
  factory.setExpandEntityReferences(false);
  factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
  factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
  factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
  factory.setValidating(true);

  return factory;
}
 
Example 14
Project: lams   File: StaticBasicParserPool.java   View Source Code Vote up 5 votes
/**
 * Initializes the pool with a new set of configuration options.
 * 
 * @throws XMLParserException thrown if there is a problem initialzing the pool
 */
protected synchronized void initializeFactory() throws XMLParserException {
    DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
    setAttributes(newFactory, builderAttributes);
    setFeatures(newFactory, builderFeatures);
    newFactory.setCoalescing(coalescing);
    newFactory.setExpandEntityReferences(expandEntityReferences);
    newFactory.setIgnoringComments(ignoreComments);
    newFactory.setIgnoringElementContentWhitespace(ignoreElementContentWhitespace);
    newFactory.setNamespaceAware(namespaceAware);
    newFactory.setSchema(schema);
    newFactory.setValidating(dtdValidating);
    newFactory.setXIncludeAware(xincludeAware);
    builderFactory = newFactory;
}
 
Example 15
Project: Android_Code_Arbiter   File: DocumentBuilderSafeProperty.java   View Source Code Vote up 5 votes
public static void unsafeManualConfig1() throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    //dbf.setFeature("http://xml.org/sax/features/external-general-entities",true);
    dbf.setFeature("http://xml.org/sax/features/external-parameter-entities",true);
    dbf.setXIncludeAware(false);
    dbf.setExpandEntityReferences(false);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(getInputFile());
    print(doc);
}
 
Example 16
Project: Android_Code_Arbiter   File: DocumentBuilderSafeProperty.java   View Source Code Vote up 5 votes
public static void unsafeManualConfig2() throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setFeature("http://xml.org/sax/features/external-general-entities",true);
    //dbf.setFeature("http://xml.org/sax/features/external-parameter-entities",true);
    dbf.setXIncludeAware(false);
    dbf.setExpandEntityReferences(false);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(getInputFile());
    print(doc);
}