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:bridge.toolkit.commands.S1000DConverter.java

/**
 * Create the DOM from the file//from  w w w .j a  v a2  s .  co  m
 * 
 * @param filetempXML
 * @return
 * @throws Exception
 */
public static org.w3c.dom.Document getDoc(File filetempXML) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);
    factory.setExpandEntityReferences(false);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(filetempXML);
}

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  ww w  .  j  a  v  a 2 s  .  c  om
 *
 * @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:eu.elf.license.LicenseParser.java

/**
 * Creates DOM Document object from XML string
 *
 * @param xmlString/* w ww  .  j  av  a  2 s .  c o  m*/
 * @return xml document
 * @throws Exception
 */
public static Document createXMLDocumentFromString(String xmlString) throws Exception {
    Document document = null;

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);

        DocumentBuilder builder = dbf.newDocumentBuilder();
        InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
        document = builder.parse(is);

    } catch (SAXParseException spe) {
        Locator2Impl locator = new Locator2Impl();
        spe.printStackTrace();
        throw new SAXParseException("", locator);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return document;
}

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

/**
 * Read XML as DOM./*  ww  w. j a v  a2s .c o 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.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);
}

From source file:com.trsst.Common.java

public static org.w3c.dom.Element fomToDom(Element element) {
    org.w3c.dom.Element dom = null;
    if (element != null) {
        try {//from   www  .j a va  2s . co m
            ByteArrayInputStream in = new ByteArrayInputStream(element.toString().getBytes());
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(false);
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            dom = db.parse(in).getDocumentElement();
        } catch (Exception e) {
        }
    }
    return dom;
}

From source file:com.trsst.Common.java

public static org.w3c.dom.Document fomToDom(Document<Element> doc) {
    org.w3c.dom.Document dom = null;
    if (doc != null) {
        try {/*from   w  w w.  ja  v  a  2 s .c  om*/
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            doc.writeTo(out);
            ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(false);
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            dom = db.parse(in);
        } catch (Exception e) {
        }
    }
    return dom;
}

From source file:edu.virginia.speclab.juxta.author.model.JuxtaXMLParser.java

static public String getIndexBasedXPathForGeneralXPath(String xpathString, String xml) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {//from   w ww  .  j  ava2  s .co  m
        factory.setNamespaceAware(false); // ignore the horrible issues of namespacing
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xml)));
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        Node root = doc.getFirstChild();
        XPathExpression expr = xpath.compile(xpathString);
        Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        return nodeToSimpleXPath(node, root);
    } catch (SAXException ex) {
    } catch (IOException ex) {
    } catch (XPathExpressionException ex) {
    } catch (ParserConfigurationException ex) {
    }
    return null;
}

From source file:eu.stork.peps.auth.engine.SAMLEngineUtils.java

/**
 * Generate requested attribute./*w w  w. j  ava 2  s.co m*/
 * 
 * @param name the name
 * @param friendlyName the friendly name
 * @param isRequired the is required
 * @param value the value
 * 
 * @return the requested attribute
 */
public static RequestedAttribute generateReqAuthnAttributeSimple(final String name, final String friendlyName,
        final String isRequired, final List<String> value) {
    LOG.debug("Generate the requested attribute.");

    final RequestedAttribute requested = (RequestedAttribute) SAMLEngineUtils
            .createSamlObject(RequestedAttribute.DEF_ELEMENT_NAME);
    requested.setName(name);
    requested.setNameFormat(RequestedAttribute.URI_REFERENCE);

    requested.setFriendlyName(friendlyName);

    requested.setIsRequired(isRequired);

    // The value is optional in an authentication request.
    if (!value.isEmpty()) {
        for (int nextValue = 0; nextValue < value.size(); nextValue++) {
            final String valor = value.get(nextValue);
            if (StringUtils.isNotBlank(valor)) {

                if (!name.equals("http://www.stork.gov.eu/1.0/signedDoc")) {

                    // Create the attribute statement
                    final XSAny attrValue = (XSAny) SAMLEngineUtils
                            .createSamlObject(new QName(SAMLCore.STORK10_NS.getValue(), "AttributeValue",
                                    SAMLCore.STORK10_PREFIX.getValue()), XSAny.TYPE_NAME);

                    attrValue.setTextContent(valor.trim());
                    requested.getAttributeValues().add(attrValue);

                } else {

                    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
                    domFactory.setNamespaceAware(true);
                    Document document = null;
                    DocumentBuilder builder;

                    // Parse the signedDoc value into an XML DOM Document
                    try {
                        builder = domFactory.newDocumentBuilder();
                        InputStream is;
                        is = new ByteArrayInputStream(valor.trim().getBytes(UTF_8));
                        document = builder.parse(is);
                        is.close();
                    } catch (SAXException e1) {
                        LOG.error("SAX Error while parsing signModule attribute", e1);
                        throw new STORKSAMLEngineRuntimeException(e1);
                    } catch (ParserConfigurationException e2) {
                        LOG.error("Parser Configuration Error while parsing signModule attribute", e2);
                        throw new STORKSAMLEngineRuntimeException(e2);
                    } catch (UnsupportedEncodingException e3) {
                        LOG.error("Unsupported encoding Error while parsing signModule attribute", e3);
                        throw new STORKSAMLEngineRuntimeException(e3);
                    } catch (IOException e4) {
                        LOG.error("IO Error while parsing signModule attribute", e4);
                        throw new STORKSAMLEngineRuntimeException(e4);
                    }

                    // Create the XML statement(this will be overwritten with the previous DOM structure)
                    final XSAny xmlValue = (XSAny) SAMLEngineUtils
                            .createSamlObject(new QName(SAMLCore.STORK10_NS.getValue(), "XMLValue",
                                    SAMLCore.STORK10_PREFIX.getValue()), XSAny.TYPE_NAME);

                    //Set the signedDoc XML content to this element
                    xmlValue.setDOM(document.getDocumentElement());

                    // Create the attribute statement
                    final XSAny attrValue = (XSAny) SAMLEngineUtils
                            .createSamlObject(new QName(SAMLCore.STORK10_NS.getValue(), "AttributeValue",
                                    SAMLCore.STORK10_PREFIX.getValue()), XSAny.TYPE_NAME);

                    //Add previous signedDocXML to the AttributeValue Element
                    attrValue.getUnknownXMLObjects().add(xmlValue);

                    requested.getAttributeValues().add(attrValue);
                }

            }
        }
    }

    return requested;
}

From source file:com.amalto.webapp.core.util.Util.java

/**
 * Returns a namespaced root element of a document Useful to create a namespace holder element
 * //from w  ww . jav a2  s . co m
 * @return the root Element
 */
public static Element getRootElement(String elementName, String namespace, String prefix) throws Exception {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();
        Document namespaceHolder = impl.createDocument(namespace,
                (prefix == null ? "" : prefix + ":") + elementName, null); //$NON-NLS-1$ //$NON-NLS-2$
        Element rootNS = namespaceHolder.getDocumentElement();
        rootNS.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace); //$NON-NLS-1$ //$NON-NLS-2$
        return rootNS;
    } catch (Exception e) {
        String err = "Error creating a namespace holder document: " + e.getLocalizedMessage(); //$NON-NLS-1$
        throw new Exception(err);
    }
}

From source file:edu.indiana.lib.twinpeaks.util.DomUtils.java

/**
 * Get a DOM Document builder.// w ww  . jav  a  2s. com
 * @param namespaceAware true if we're to handle namespace details
 * @return The DocumentBuilder
 * @throws DomException
 */
public static DocumentBuilder getXmlDocumentBuilder(boolean namespaceAware) throws DomException {
    try {
        DocumentBuilderFactory factory;

        factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(namespaceAware);

        _log.debug("DOM parse: namespace aware = " + namespaceAware);
        return factory.newDocumentBuilder();

    } catch (Exception e) {
        throw new DomException(e.toString());
    }
}