Example usage for javax.xml.parsers DocumentBuilderFactory setFeature

List of usage examples for javax.xml.parsers DocumentBuilderFactory setFeature

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory setFeature.

Prototype

public abstract void setFeature(String name, boolean value) throws ParserConfigurationException;

Source Link

Document

Set a feature for this DocumentBuilderFactory and DocumentBuilder s created by this factory.

Usage

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilder documentBuilder;

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

    documentBuilderFactory.setValidating(false); // dtd isn't available; would be nice to attempt to validate
    documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilder = documentBuilderFactory.newDocumentBuilder();

    return documentBuilder;
}

From source file:Main.java

public static Document getDoc(String xml) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;//from  w  w  w  . j  a  v  a2s. c o m

    dbf.setValidating(false);
    try {
        dbf.setFeature("http://xml.org/sax/features/namespaces", false);
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        db = dbf.newDocumentBuilder();
        InputSource source = new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")));
        Document doc = db.parse(source);
        return doc;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Source getDomSourceIgnoringDtd(InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // stop the loading of DTD files
    factory.setValidating(false);/*  www .j a v a2 s . c o  m*/
    factory.setNamespaceAware(true);
    factory.setFeature("http://xml.org/sax/features/validation", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    DocumentBuilder docbuilder = factory.newDocumentBuilder();

    Document doc = docbuilder.parse(inputStream);

    Source domSource = new DOMSource(doc.getDocumentElement());

    return domSource;
}

From source file:Main.java

public static Document toDocument(String xml, boolean namespaceAware, boolean ignoreDtd) throws Exception {
    Reader input = new StringReader(xml);
    InputSource is = new InputSource(input);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(namespaceAware);

    // ignore dtd files
    if (ignoreDtd) {
        dbf.setValidating(false);/* w  w  w . j a  va2  s . c om*/
        dbf.setFeature("http://xml.org/sax/features/namespaces", false);
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    }

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException exception) throws SAXException {
            throw exception;

        }

        public void fatalError(SAXParseException exception) throws SAXException {
            throw exception;
        }

        public void error(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    return db.parse(is);
}

From source file:eu.stork.peps.test.simple.SSETestUtils.java

/**
 * Marshall./*from  ww  w. ja va  2  s . co m*/
 * 
 * @param samlToken the SAML token
 * 
 * @return the byte[]
 * 
 * @throws MarshallingException the marshalling exception
 * @throws ParserConfigurationException the parser configuration exception
 * @throws TransformerException the transformer exception
 */
public static byte[] marshall(final XMLObject samlToken)
        throws MarshallingException, ParserConfigurationException, TransformerException {

    final javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringComments(true);
    final javax.xml.parsers.DocumentBuilder docBuild = dbf.newDocumentBuilder();

    // Get the marshaller factory
    final MarshallerFactory marshallerFactory = Configuration.getMarshallerFactory();

    // Get the Subject marshaller
    final Marshaller marshaller = marshallerFactory.getMarshaller(samlToken);

    final Document doc = docBuild.newDocument();

    // Marshall the SAML token
    marshaller.marshall(samlToken, doc);

    // Obtain a byte array representation of the marshalled SAML object
    final DOMSource domSource = new DOMSource(doc);
    final StringWriter writer = new StringWriter();
    final StreamResult result = new StreamResult(writer);
    final TransformerFactory transFact = TransformerFactory.newInstance();
    final Transformer transformer = transFact.newTransformer();
    transformer.transform(domSource, result);

    return writer.toString().getBytes();
}

From source file:eu.eidas.engine.test.simple.SSETestUtils.java

/**
 * Marshall.// w  w  w.j a  v a  2  s  . co m
 *
 * @param samlToken the SAML token
 *
 * @return the byte[]
 *
 * @throws MarshallingException the marshalling exception
 * @throws ParserConfigurationException the parser configuration exception
 * @throws TransformerException the transformer exception
 */
public static byte[] marshall(final XMLObject samlToken)
        throws MarshallingException, ParserConfigurationException, TransformerException {

    final javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringComments(true);
    final javax.xml.parsers.DocumentBuilder docBuild = dbf.newDocumentBuilder();

    // Get the marshaller factory
    final MarshallerFactory marshallerFactory = Configuration.getMarshallerFactory();

    // Get the Subject marshaller
    final Marshaller marshaller = marshallerFactory.getMarshaller(samlToken);

    final Document doc = docBuild.newDocument();

    // Marshall the SAML token
    marshaller.marshall(samlToken, doc);

    // Obtain a byte array representation of the marshalled SAML object
    final DOMSource domSource = new DOMSource(doc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final StreamResult result = new StreamResult(new OutputStreamWriter(baos, Constants.UTF8));
    final TransformerFactory transFact = TransformerFactory.newInstance();
    final Transformer transformer = transFact.newTransformer();
    transformer.transform(domSource, result);

    return baos.toByteArray();
}

From source file:Main.java

/**
 * Creates and returns an new document builder factory. This method tries to
 * configure the namespace support for the builder. If the underlying parser
 * does not support namespaces then this method returns a simple
 * DocumentBuilder object./*from   w  w  w.  j  av a  2  s  . c om*/
 * 
 * @return a new document builder
 * @throws ParserConfigurationException
 */
private static DocumentBuilder getDocumentBuilder(boolean isNamespaceAware)
        throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(isNamespaceAware); // never forget this!
    try {
        factory.setFeature("http://xml.org/sax/features/namespaces", isNamespaceAware);
    } catch (Throwable t) {
        // Just skip it...
    }
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder;
}

From source file:com.mingo.parser.xml.dom.DocumentBuilderFactoryCreator.java

/**
 * Creates DocumentBuilderFactory./*  ww  w  .  ja v a2 s  .  com*/
 *
 * @param parserConfiguration {@link ParserConfiguration}
 * @return DocumentBuilderFactory a factory API that enables applications to obtain a
 *         parser that produces DOM object trees from XML documents
 * @throws ParserConfigurationException {@link ParserConfigurationException}
 */
public static DocumentBuilderFactory createDocumentBuilderFactory(ParserConfiguration parserConfiguration)
        throws ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setValidating(parserConfiguration.isValidate());
    documentBuilderFactory.setNamespaceAware(parserConfiguration.isNamespaceAware());
    documentBuilderFactory.setFeature(DYNAMIC_VALIDATION, true);
    List<Source> sourceList = createSchemaSources(parserConfiguration.getXsdSchemaPaths());
    if (CollectionUtils.isNotEmpty(sourceList)) {
        documentBuilderFactory.setSchema(createSchema(sourceList));
    }
    return documentBuilderFactory;
}

From source file:com.twentyn.patentExtractor.Util.java

public static DocumentBuilderFactory mkDocBuilderFactory() throws ParserConfigurationException {
    /* Try to load the document.  Note that the factory must be configured within the context of a method call
     * for exception handling.  TODO: can we work around this w/ dependency injection? */

    // With help from http://stackoverflow.com/questions/155101/make-documentbuilder-parse-ignore-dtd-references
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setValidating(false);// w ww  . j  a v a  2 s .co m
    docFactory.setNamespaceAware(true);
    docFactory.setFeature("http://xml.org/sax/features/namespaces", false);
    docFactory.setFeature("http://xml.org/sax/features/validation", false);
    docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    return docFactory;
}

From source file:fr.free.movierenamer.utils.URIRequest.java

private static Document getXmlDocument(InputSource source) throws IOException, SAXException {
    try {//from  w w w  .j  ava  2 s.c om
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setFeature("http://xml.org/sax/features/namespaces", false);
        factory.setFeature("http://xml.org/sax/features/validation", false);
        return factory.newDocumentBuilder().parse(source);
    } catch (ParserConfigurationException e) {
        // will never happen
        throw new RuntimeException(e);
    }
}