Example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringComments

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

Introduction

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

Prototype


public void setIgnoringComments(boolean ignoreComments) 

Source Link

Document

Specifies that the parser produced by this code will ignore comments.

Usage

From source file:Main.java

public static Document createDocument() {
    try {//from  w  w  w  .j  av  a  2 s  .c om
        // Use JAXP to create a document builder
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        builderFactory.setExpandEntityReferences(false);
        builderFactory.setValidating(false);
        builderFactory.setNamespaceAware(true);
        builderFactory.setIgnoringComments(true);
        builderFactory.setCoalescing(true);
        builderFactory.setIgnoringElementContentWhitespace(true);

        return builderFactory.newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    }
}

From source file:net.sf.jasperreports.engine.util.JRXmlUtils.java

/**
 * Creates a XML document builder./*from w  w  w.  jav  a 2  s  .  c om*/
 * 
 * @return a XML document builder
 * @throws JRException
 */
public static DocumentBuilder createDocumentBuilder(boolean isNamespaceAware) throws JRException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setIgnoringComments(true);
    dbf.setNamespaceAware(isNamespaceAware);
    try {
        if (!allowDoctype()) {
            dbf.setFeature(FEATURE_DISALLOW_DOCTYPE, true);
        }

        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_DOCUMENT_BUILDER_FACTORY_CREATION_FAILURE, null, e);
    }
}

From source file:Main.java

public static Document parseStreamToXML(InputStream in) {
    try {/* w w  w.ja  v a2 s  .  c  o  m*/
        // Use JAXP to create a document builder
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        builderFactory.setExpandEntityReferences(false);
        builderFactory.setValidating(false);
        builderFactory.setNamespaceAware(true);
        builderFactory.setIgnoringComments(true);
        builderFactory.setCoalescing(true);
        builderFactory.setIgnoringElementContentWhitespace(true);

        return builderFactory.newDocumentBuilder().parse(in);
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    } catch (SAXException errSax) {
        throw new RuntimeException("Error parsing XML files", errSax);
    } catch (IOException errIO) {
        throw new RuntimeException("Error parsing XML files", errIO);
    }
}

From source file:Main.java

public static Document parseStreamToXML(Reader in) {
    try {//from   w w  w.ja  v  a  2s  .com
        // Use JAXP to create a document builder
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        builderFactory.setExpandEntityReferences(false);
        builderFactory.setValidating(false);
        builderFactory.setNamespaceAware(true);
        builderFactory.setIgnoringComments(true);
        builderFactory.setCoalescing(true);
        builderFactory.setIgnoringElementContentWhitespace(true);

        return builderFactory.newDocumentBuilder().parse(new InputSource(in));
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    } catch (SAXException errSax) {
        throw new RuntimeException("Error parsing XML files", errSax);
    } catch (IOException errIO) {
        throw new RuntimeException("Error parsing XML files", errIO);
    }
}

From source file:DomUtil.java

/**
 * Read XML as DOM.//from  w  w  w.  j av a  2 s  . co  m
 */
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());

    Document doc = db.parse(is);
    return doc;
}

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

/**
 * Marshall./*from  www. j a v a 2 s  .  c o 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./*from  ww  w.ja v a2  s . com*/
 *
 * @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:kenh.xscript.ScriptUtils.java

/**
 * Get xScript instance./*from w w  w .  jav a 2  s.  com*/
 * @param url
 * @param env
 * @return
 * @throws UnsupportedScriptException
 */
public static final Element getInstance(URL url, Environment env) throws UnsupportedScriptException {

    if (url == null)
        return null;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        factory.setNamespaceAware(true);
        factory.setIgnoringElementContentWhitespace(true);

        InputStream in = url.openStream();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(in);
        return getInstance(doc, env);
    } catch (UnsupportedScriptException e) {
        throw e;
    } catch (Exception e) {
        throw new UnsupportedScriptException(null, e);
    }
}

From source file:kenh.xscript.ScriptUtils.java

/**
 * Get xScript instance./*from  w  ww .  j av  a2s  .  co  m*/
 * @param file
 * @param env
 * @return
 * @throws UnsupportedScriptException
 */
public static final Element getInstance(File file, Environment env) throws UnsupportedScriptException {

    if (file == null || !file.exists())
        return null;

    if (env == null)
        env = new Environment();

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        factory.setNamespaceAware(true);
        factory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new FileInputStream(file));
        String home = file.getCanonicalFile().getParent();
        if (env != null) {
            if (!env.containsVariable(Constant.VARIABLE_HOME))
                env.setPublicVariable(Constant.VARIABLE_HOME, home, false);
        }
        return getInstance(doc, env);
    } catch (UnsupportedScriptException e) {
        throw e;
    } catch (Exception e) {
        throw new UnsupportedScriptException(null, e);
    }
}

From source file:com.l2jfree.sql.L2DatabaseInstaller.java

public static void check() throws SAXException, IOException, ParserConfigurationException {
    final TreeMap<String, String> tables = new TreeMap<String, String>();
    final TreeMap<Double, String> updates = new TreeMap<Double, String>();

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false); // FIXME add validation
    factory.setIgnoringComments(true);

    final List<Document> documents = new ArrayList<Document>();

    InputStream is = null;/*  w w  w  . j  a va 2 s  .c o m*/
    try {
        // load default database schema from resources
        is = L2DatabaseInstaller.class.getResourceAsStream("database_schema.xml");

        documents.add(factory.newDocumentBuilder().parse(is));
    } finally {
        IOUtils.closeQuietly(is);
    }

    final File f = new File("./config/database_schema.xml");

    // load optional project specific database tables/updates (fails on already existing)
    if (f.exists())
        documents.add(factory.newDocumentBuilder().parse(f));

    for (Document doc : documents) {
        for (Node n1 : L2XML.listNodesByNodeName(doc, "database")) {
            for (Node n2 : L2XML.listNodesByNodeName(n1, "table")) {
                final String name = L2XML.getAttribute(n2, "name");
                final String definition = L2XML.getAttribute(n2, "definition");

                final String oldDefinition = tables.put(name, definition);
                if (oldDefinition != null)
                    throw new RuntimeException("Found multiple tables with name " + name + "!");
            }

            for (Node n2 : L2XML.listNodesByNodeName(n1, "update")) {
                final Double revision = Double.valueOf(L2XML.getAttribute(n2, "revision"));
                final String query = L2XML.getAttribute(n2, "query");

                final String oldQuery = updates.put(revision, query);
                if (oldQuery != null)
                    throw new RuntimeException("Found multiple updates with revision " + revision + "!");
            }
        }
    }

    createRevisionTable();

    final double databaseRevision = getDatabaseRevision();

    if (databaseRevision == -1) // no table exists
    {
        for (Entry<String, String> table : tables.entrySet()) {
            final String tableName = table.getKey();
            final String tableDefinition = table.getValue();

            installTable(tableName, tableDefinition);
        }

        if (updates.isEmpty())
            insertRevision(0);
        else
            insertRevision(updates.lastKey());
    } else
    // check for possibly required updates
    {
        for (Entry<String, String> table : tables.entrySet()) {
            final String tableName = table.getKey();
            final String tableDefinition = table.getValue();

            if (L2Database.tableExists(tableName))
                continue;

            System.err.println("Table '" + tableName + "' is missing, so the server attempts to install it.");
            System.err.println("WARNING! It's highly recommended to check the results manually.");

            installTable(tableName, tableDefinition);
        }

        for (Entry<Double, String> update : updates.entrySet()) {
            final double updateRevision = update.getKey();
            final String updateQuery = update.getValue();

            if (updateRevision > databaseRevision) {
                executeUpdate(updateQuery);

                insertRevision(updateRevision);
            }
        }
    }
}