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:com.connexta.arbitro.TestUtil.java

/**
 * This creates the expected XACML response from a file
 *
 * @param rootDirectory   root directory of the  response files
 * @param versionDirectory   version directory of the  response files
 * @param responseId  response file name
 * @return ResponseCtx or null if any error
 *//*from www  .  j  a  va 2  s. c  o m*/
public static ResponseCtx createResponse(String rootDirectory, String versionDirectory, String responseId) {

    File file = new File(".");
    try {
        String filePath = file.getCanonicalPath() + File.separator + TestConstants.RESOURCE_PATH
                + File.separator + rootDirectory + File.separator + versionDirectory + File.separator
                + TestConstants.RESPONSE_DIRECTORY + File.separator + responseId;

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        factory.setNamespaceAware(true);
        factory.setValidating(false);
        DocumentBuilder db = factory.newDocumentBuilder();
        Document doc = db.parse(new FileInputStream(filePath));
        return ResponseCtx.getInstance(doc.getDocumentElement());
    } catch (Exception e) {
        log.error("Error while reading expected response from file ", e);
        //ignore any exception and return null
    }

    return null;
}

From source file:com.connexta.arbitro.TestUtil.java

/**
 * This creates the XACML request from a file
 *
 * @param rootDirectory   root directory of the  request files
 * @param versionDirectory   version directory of the  request files
 * @param requestId  request file name//www  .ja  v a2  s .c  o m
 * @return String or null if any error
 */
public static String createRequest(String rootDirectory, String versionDirectory, String requestId) {

    File file = new File(".");
    StringWriter writer = null;
    try {
        String filePath = file.getCanonicalPath() + File.separator + TestConstants.RESOURCE_PATH
                + File.separator + rootDirectory + File.separator + versionDirectory + File.separator
                + TestConstants.REQUEST_DIRECTORY + File.separator + requestId;

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        factory.setNamespaceAware(true);
        DocumentBuilder db = factory.newDocumentBuilder();
        Document doc = db.parse(new FileInputStream(filePath));
        DOMSource domSource = new DOMSource(doc);
        writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        log.error("Error while reading expected response from file ", e);
        //ignore any exception and return null
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                log.error("Error closing stream ", e);
                //ignore any exception and return null
            }
        }
    }
    return null;
}

From source file:gpms.utils.PolicyTestUtil.java

/**
 * This creates the expected XACML response from a file
 *
 * @param rootDirectory//from   w  w w  . j av  a2s. c om
 *            root directory of the response files
 * @param versionDirectory
 *            version directory of the response files
 * @param responseId
 *            response file name
 * @return ResponseCtx or null if any error
 */
public static ResponseCtx createResponse(String rootDirectory, String versionDirectory, String responseId) {

    File file = new File(".");
    try {
        String filePath = file.getCanonicalPath() + File.separator + TestConstants.RESOURCE_PATH
                + File.separator + rootDirectory + File.separator + versionDirectory + File.separator
                + TestConstants.RESPONSE_DIRECTORY + File.separator + responseId;

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        factory.setNamespaceAware(true);
        factory.setValidating(false);
        DocumentBuilder db = factory.newDocumentBuilder();
        Document doc = db.parse(new FileInputStream(filePath));
        return ResponseCtx.getInstance(doc.getDocumentElement());
    } catch (Exception e) {
        log.error("Error while reading expected response from file ", e);
        // ignore any exception and return null
    }

    return null;
}

From source file:gpms.utils.PolicyTestUtil.java

/**
 * This creates the XACML request from a file
 *
 * @param rootDirectory//from   ww  w  . ja  v  a  2s. c  o  m
 *            root directory of the request files
 * @param versionDirectory
 *            version directory of the request files
 * @param requestId
 *            request file name
 * @return String or null if any error
 */
public static String createRequest(String rootDirectory, String versionDirectory, String requestId) {

    File file = new File(".");
    StringWriter writer = null;
    try {
        String filePath = file.getCanonicalPath() + File.separator + TestConstants.RESOURCE_PATH
                + File.separator + rootDirectory + File.separator + versionDirectory + File.separator
                + TestConstants.REQUEST_DIRECTORY + File.separator + requestId;

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        factory.setNamespaceAware(true);
        DocumentBuilder db = factory.newDocumentBuilder();
        Document doc = db.parse(new FileInputStream(filePath));
        DOMSource domSource = new DOMSource(doc);
        writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        log.error("Error while reading expected response from file ", e);
        // ignore any exception and return null
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                log.error("Error closing stream ", e);
                // ignore any exception and return null
            }
        }
    }
    return null;
}

From source file:com.google.appengine.tools.mapreduce.ConfigurationTemplatePreprocessor.java

/**
 * Creates a DocumentBuilder that mirrors the settings used by Hadoop.
 *//* w  ww . j av  a 2s.  c  om*/
static DocumentBuilderFactory createConfigurationDocBuilderFactory() {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

    // Mirroring the settings in org.apache.hadoop.conf.Configuration
    docBuilderFactory.setIgnoringComments(true);
    docBuilderFactory.setNamespaceAware(false);
    try {
        docBuilderFactory.setXIncludeAware(true);
    } catch (UnsupportedOperationException e) {
        // Ignore
    }
    return docBuilderFactory;
}

From source file:de.mpg.escidoc.services.syndication.Utils.java

public static DocumentBuilder createDocumentBuilder() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);/*w  w  w  .j a va2s.c  o  m*/
    dbf.setIgnoringComments(true);
    return dbf.newDocumentBuilder();
}

From source file:dk.dbc.rawrepo.oai.OAIWorker.java

private static DocumentBuilder makeDocumentBuilder() {
    synchronized (DocumentBuilderFactory.class) {
        try {//from w ww.j  a va2  s.  c  om
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setIgnoringComments(true);
            dbf.setIgnoringElementContentWhitespace(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setEntityResolver(new NullResolver());
            return db;
        } catch (ParserConfigurationException ex) {
            throw new RuntimeException(ex);
        }
    }
}

From source file:it.cnr.icar.eric.server.security.authorization.RegistryPolicyFinderModule.java

/**
 * Loads a policy from the DataHandler, using the specified
 * <code>PolicyFinder</code> to help with instantiating PolicySets.
 *
 * @param DataHandler the DataHandler to load the policy from
 * @param finder a PolicyFinder used to help in instantiating PolicySets
 * @param handler an error handler used to print warnings and errors
 *                during parsing/*from  w w w. ja  v  a 2  s.c  o m*/
 *
 * @return a policy associated with the specified DataHandler
 *
 * @throws RegistryException exception thrown if there is a problem reading the DataHandler's input stream
 */
private static AbstractPolicy loadPolicy(DataHandler dh, PolicyFinder finder) throws RegistryException {
    AbstractPolicy policy = null;

    try {
        // create the factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);

        DocumentBuilder db = null;

        // set the factory to work the way the system requires
        // we're not doing any validation
        factory.setNamespaceAware(false);
        factory.setValidating(false);

        db = factory.newDocumentBuilder();

        // try to load the policy file
        Document doc = db.parse(dh.getInputStream());

        // handle the policy, if it's a known type
        Element root = doc.getDocumentElement();
        String name = root.getTagName();

        if (name.equals("Policy")) {
            policy = Policy.getInstance(root);
        } else if (name.equals("PolicySet")) {
            policy = PolicySet.getInstance(root, finder);
        } else {
            // this isn't a root type that we know how to handle
            throw new RegistryException(ServerResourceBundle.getInstance()
                    .getString("message.unknownRootDocumentType", new Object[] { name }));
        }
    } catch (Exception e) {
        log.error(ServerResourceBundle.getInstance().getString("message.FailedToLoadPolicy"), e);
        throw new RegistryException(e);
    }

    return policy;
}

From source file:com.bcmcgroup.flare.client.ClientUtil.java

/**
 * Constructs a DocumentBuilder object for XML documents
 *
 * @return DocumentBuilder object with the proper initializations
 *///from   w w w .j  a v  a2s  .  co m
public static DocumentBuilder generateDocumentBuilder() {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setIgnoringComments(true);
        dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
        dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        logger.error("ParserConfigurationException when attempting to generate a document builder.");
    }
    return null;
}

From source file:com.vmware.identity.SharedUtils.java

/**
 * Read XML as DOM./* w  w w .  jav a 2  s  .c  om*/
 */
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

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

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

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

    return db.parse(is);
}