Example usage for javax.xml.parsers DocumentBuilderFactory setNamespaceAware

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

Introduction

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

Prototype


public void setNamespaceAware(boolean awareness) 

Source Link

Document

Specifies that the parser produced by this code will provide support for XML namespaces.

Usage

From source file:org.syncope.console.commons.XMLRolesReader.java

public void init() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    try {/*from   w  w  w  .j a  v a 2s  .co  m*/
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(getClass().getResource("/" + authorizations).openStream());
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        LOG.error("While initializing parsing of {}", authorizations, e);
        doc = null;
    }
}

From source file:com.sdl.odata.unmarshaller.atom.AtomLinkUnmarshaller.java

private Document parseXML(String xml) throws ODataUnmarshallingException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);

    try {//from   www  .  j  a  v a 2s  .c  o  m
        return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
        throw new ODataUnmarshallingException("Error while parsing XML", e);
    } catch (IOException | ParserConfigurationException e) {
        throw new ODataSystemException(e);
    }
}

From source file:com.jkoolcloud.tnt4j.streams.matchers.XPathMatcher.java

private XPathMatcher() throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false);
    builder = domFactory.newDocumentBuilder();
    xPath = StreamsXMLUtils.getStreamsXPath();
}

From source file:org.npr.api.Client.java

public Node execute() throws ClientProtocolException, IOException, SAXException, ParserConfigurationException {
    InputStream data = download();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);/*from   w  w w  .  j a v a 2  s . c om*/
    dbf.setNamespaceAware(false);

    if (data != null) {
        Document document = dbf.newDocumentBuilder().parse(data);
        return document.getDocumentElement();
    }
    return null;
}

From source file:com.amalto.core.save.DOMDocumentTest.java

public void testIncludeXSINamespace() throws Exception {
    String lineSeparator = System.getProperty("line.separator");
    StringBuilder xmlBuilder = new StringBuilder(
            "<Organisation xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
    xmlBuilder.append(lineSeparator);/*from   w  w  w. j a v a  2s  .c o m*/
    xmlBuilder.append("<IdOrganisation xsi:type=\"xsd:string\">5797</IdOrganisation>");
    xmlBuilder.append(lineSeparator);
    xmlBuilder.append("</Organisation>");
    xmlBuilder.append(lineSeparator);
    String xml = xmlBuilder.toString();
    InputStream documentStream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    // Parsing
    MutableDocument userDocument;
    DocumentBuilderFactory DOM_PARSER_FACTORY = DocumentBuilderFactory.newInstance();
    DOM_PARSER_FACTORY.setNamespaceAware(true);
    DOM_PARSER_FACTORY.setIgnoringComments(true);
    DOM_PARSER_FACTORY.setValidating(false);
    try {
        // Don't ignore talend internal attributes when parsing this document
        DocumentBuilder documentBuilder = new SkipAttributeDocumentBuilder(
                DOM_PARSER_FACTORY.newDocumentBuilder(), false);
        InputSource source = new InputSource(documentStream);
        Document userDomDocument = documentBuilder.parse(source);
        userDocument = new DOMDocument(userDomDocument, null, StringUtils.EMPTY, StringUtils.EMPTY);
    } catch (Exception e) {
        throw new RuntimeException("Unable to parse document to save.", e);
    }
    assertNotNull(userDocument);
    String result = userDocument.exportToString();
    assertEquals(xml, result);
}

From source file:org.openengsb.openengsbplugin.tools.OpenEngSBVersionResolver.java

public String resolveVersion(String content) throws NoVersionFoundException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    try {/*from   w  w  w  .j av a 2 s  .  co m*/
        DocumentBuilder builder = factory.newDocumentBuilder();
        File tmpFile = createTmpFile(content);
        Document parse = builder.parse(tmpFile.getAbsolutePath());
        NodeList latestNode = parse.getElementsByTagName("latest");
        if (latestNode == null || latestNode.getLength() < 1) {
            throw new NoVersionFoundException("Could not resolve latest version");
        }
        return latestNode.item(0).getFirstChild().getNodeValue();
    } catch (ParserConfigurationException e) {
        throw new NoVersionFoundException("Could not retrieve latest version", e);
    } catch (SAXException e) {
        throw new NoVersionFoundException("Could not retrieve latest version", e);
    } catch (IOException e) {
        throw new NoVersionFoundException("IOException, tmp folder must be writable", e);
    }
}

From source file:org.objenesis.tck.OsgiTest.java

protected String[] getTestBundlesNames() {
    String version = getImplementationVersion(Objenesis.class);
    // Null means we are an IDE, not in Maven. So we have an IDE project dependency instead
    // of a Maven dependency with the jar. Which explains why the version is null (no Manifest
    // since there's no jar. In that case we get the version from the pom.xml and hope the Maven
    // jar is up-to-date in the local repository
    if (version == null) {
        try {//from   w w  w. ja  va2  s .c  o  m
            XPathFactory xPathFactory = XPathFactory.newInstance();
            final XPath xPath = xPathFactory.newXPath();
            XPathExpression xPathExpression;
            try {
                xPathExpression = xPath.compile("/project/parent/version");
            } catch (final XPathExpressionException e) {
                throw new RuntimeException(e);
            }

            final DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
            xmlFact.setNamespaceAware(false);
            final DocumentBuilder builder = xmlFact.newDocumentBuilder();
            final Document doc = builder.parse(new File("pom.xml"));
            version = xPathExpression.evaluate(doc);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
    return new String[] { "org.objenesis, objenesis, " + version };
}

From source file:ddf.security.ws.policy.impl.FilePolicyLoader.java

/**
 * Loads the policy and converts it into a Document.
 *
 * @param policyFileURL URL that is based in from the bundlecontext.
 * @return The policy in a Document format.
 * @throws IOException If an error occurs while trying to parse the file into a Document.
 *///ww  w  . j a  v a2 s.c  om
protected Document loadFromFile(URL policyFileURL) throws IOException {
    InputStream policyStream = null;
    Document doc = null;
    if (policyFileURL != null) {
        try {
            policyStream = policyFileURL.openStream();
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setNamespaceAware(true);
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(policyStream);
        } catch (IOException e) {
            throw new IOException("Could not read policy file located at " + policyFileURL, e);
        } catch (ParserConfigurationException e) {
            throw new IOException("Could not read policy file located at " + policyFileURL, e);
        } catch (SAXException e) {
            throw new IOException("Could not read policy file located at " + policyFileURL, e);
        } finally {
            IOUtils.closeQuietly(policyStream);
        }
    }
    if (doc == null) {
        throw new IOException("Could not find policy file. No valid location given: " + policyFileURL);
    }
    return doc;
}

From source file:com.espertech.esper.example.autoid.AutoIdSimMain.java

public AutoIdSimMain(int numEvents, String engineURI, boolean continuousSimulation)
        throws ParserConfigurationException {
    this.numEvents = numEvents;
    this.engineURI = engineURI;
    this.continuousSimulation = continuousSimulation;

    // set up DOM parser
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    documentBuilder = builderFactory.newDocumentBuilder();
}

From source file:net.firejack.platform.core.config.meta.parse.XMLStreamDescriptorParser.java

private void uidValidation(String xml)
        throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("//*/@uid");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    Map<String, Integer> uids = new HashMap<String, Integer>();
    for (int i = 0; i < nodes.getLength(); i++) {
        String uid = nodes.item(i).getNodeValue();
        Integer count = uids.get(uid);
        if (count == null) {
            count = 0;//from w ww  . ja va  2s  .c  om
        }
        count++;
        uids.put(uid, count);
    }
    StringBuilder errorMessage = new StringBuilder();
    for (Map.Entry<String, Integer> entry : uids.entrySet()) {
        if (entry.getValue() > 1) {
            errorMessage.append(" [").append(entry.getKey()).append("]");
        }
    }
    if (errorMessage.length() > 0) {
        throw new BusinessFunctionException(
                "UIDs are not unique in package.xml. Check UIDs:" + errorMessage.toString());
    }
}