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:XMLUtils.java

private static DocumentBuilderFactory getDocumentBuilderFactory() {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    if (loader == null) {
        loader = XMLUtils.class.getClassLoader();
    }//from www  .  ja  va2 s  .  c o  m
    if (loader == null) {
        return DocumentBuilderFactory.newInstance();
    }
    DocumentBuilderFactory factory = DOCUMENT_BUILDER_FACTORIES.get(loader);
    if (factory == null) {
        factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DOCUMENT_BUILDER_FACTORIES.put(loader, factory);
    }
    return factory;
}

From source file:edu.cornell.mannlib.vitro.utilities.containerneutral.CheckContainerNeutrality.java

@BeforeClass
public static void createDocBuilder() {
    try {//from  w w w .  ja v a 2  s .  com
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true); // never forget this!
        docBuilder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.prestoprime.p4gui.connection.AccessConnection.java

public static DIP getDIP(P4Service service, String id) {
    try {// ww  w  .  java  2  s .  co m
        String path = service.getURL() + "/access/dip/" + id;
        P4HttpClient client = new P4HttpClient(service.getUserID());
        HttpRequestBase request = new HttpGet(path);
        HttpResponse response = client.executeRequest(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            Node dom = dbf.newDocumentBuilder().parse(is);
            is.close();

            return new DIP(id, dom);
        }
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.pieframework.model.repository.ModelStore.java

/**
 * @param xmlFile//ww  w.j av a  2  s  .  c o  m
 * @param writer
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws TransformerException
 */
protected static void processXIncludes(File xmlFile, Writer writer)
        throws ParserConfigurationException, SAXException, IOException, TransformerException {

    final InputStream xml = new FileInputStream(xmlFile);

    // This sets the base where XIncludes are resolved
    InputSource i = new InputSource(xml);
    i.setSystemId(xmlFile.toURI().toString());

    // Configure Document Builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setXIncludeAware(true);
    factory.setNamespaceAware(true);
    factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
    DocumentBuilder docBuilder = factory.newDocumentBuilder();

    if (!docBuilder.isXIncludeAware()) {
        throw new IllegalStateException();
    }

    // Parse the InputSource
    Document doc = docBuilder.parse(i);

    // output
    Source source = new DOMSource(doc);
    Result result = new StreamResult(writer);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(source, result);
}

From source file:es.gob.afirma.signers.ooxml.be.fedict.eid.applet.service.signer.AbstractXmlSignatureService.java

private static Document loadDocument(final InputStream documentInputStream)
        throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    return documentBuilderFactory.newDocumentBuilder().parse(new InputSource(documentInputStream));
}

From source file:com.wavemaker.tools.pws.install.PwsInstall.java

public static void insertImport(File xmlFile, String resource) throws Exception {
    String content = getTrimmedXML(xmlFile);

    String fromStr1 = "<!DOCTYPE";
    String toStr1 = "<!--!DOCTYPE";
    content = content.replace(fromStr1, toStr1);

    String fromStr2 = "spring-beans-2.0.dtd\">";
    String toStr2 = "spring-beans-2.0.dtd\"-->";
    content = content.replace(fromStr2, toStr2);

    FileUtils.writeStringToFile(xmlFile, content, "UTF-8");

    InputStream is = new FileInputStream(xmlFile);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document doc = docBuilder.parse(is);

    doc = insertImport(doc, resource);/*w  w  w  .  j  ava  2s .  co  m*/

    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    t.transform(new DOMSource(doc), new StreamResult(xmlFile));

    content = FileUtils.readFileToString(xmlFile, "UTF-8");
    content = content.replace(toStr1, fromStr1);

    content = content.replace(toStr2, fromStr2);

    FileUtils.writeStringToFile(xmlFile, content, "UTF-8");
}

From source file:es.gob.afirma.signers.ooxml.be.fedict.eid.applet.service.signer.AbstractXmlSignatureService.java

protected static Document loadDocumentNoClose(final InputStream documentInputStream)
        throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    return documentBuilder.parse(new InputSource(new NoCloseInputStream(documentInputStream)));
}

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   ww  w  .  ja v a 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.wavemaker.tools.pws.install.PwsInstall.java

public static void insertEntryKey(File xmlFile, File[] runtimeJarFiles, File[] toolsJarFiles,
        String partnerName) throws Exception {
    String content = getTrimmedXML(xmlFile);

    String fromStr1 = "<!DOCTYPE";
    String toStr1 = "<!--!DOCTYPE";
    content = content.replace(fromStr1, toStr1);

    String fromStr2 = "spring-beans-2.0.dtd\">";
    String toStr2 = "spring-beans-2.0.dtd\"-->";
    content = content.replace(fromStr2, toStr2);

    FileUtils.writeStringToFile(xmlFile, content, "UTF-8");

    InputStream is = new FileInputStream(xmlFile);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document doc = docBuilder.parse(is);

    insertEntryKey(doc, runtimeJarFiles, toolsJarFiles, partnerName);

    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    t.transform(new DOMSource(doc), new StreamResult(xmlFile));

    content = FileUtils.readFileToString(xmlFile, "UTF-8");
    content = content.replace(toStr1, fromStr1);

    content = content.replace(toStr2, fromStr2);

    FileUtils.writeStringToFile(xmlFile, content, "UTF-8");
}

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//from w  w  w . j  a  va 2  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;
}