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:be.fedict.eid.dss.spi.utils.XAdESUtils.java

public static Document loadDocument(byte[] data) {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder domBuilder;/*from  w w  w  . j a  v a 2s.  co  m*/
    try {
        domBuilder = domFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("parser configuration error: " + e.getMessage(), e);
    }
    ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
    InputSource inputSource = new InputSource(inputStream);
    try {
        return domBuilder.parse(inputSource);
    } catch (SAXException e) {
        throw new RuntimeException("SAX error: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException("IO error: " + e.getMessage(), e);
    }
}

From source file:io.personium.core.model.DavRsCmp.java

static final org.apache.wink.webdav.model.Response createDavResponse(final String pathName, final String href,
        final DavCmp dCmp, final Propfind propfind, final boolean isAclRead) {
    ObjectFactory of = new ObjectFactory();
    org.apache.wink.webdav.model.Response ret = of.createResponse();
    ret.getHref().add(href);//from w ww .ja  v  a  2 s  .c o  m

    // TODO v1.1 PROPFIND???????
    if (propfind != null) {

        log.debug("isAllProp:" + propfind.isAllprop());
        log.debug("isPropName:" + propfind.isPropname());
    } else {
        log.debug("propfind is null");
    }

    /*
     * Displayname dn = of.createDisplayname(); dn.setValue(name); ret.setPropertyOk(dn);
     */

    Long updated = dCmp.getUpdated();
    if (updated != null) {
        Getlastmodified lm = of.createGetlastmodified();
        lm.setValue(new Date(updated));
        ret.setPropertyOk(lm);
    }
    Long published = dCmp.getPublished();
    if (published != null) {
        Creationdate cd = of.createCreationdate();
        cd.setValue(new Date(published));
        ret.setPropertyOk(cd);
    }
    String type = dCmp.getType();
    if (DavCmp.TYPE_DAV_FILE.equals(type)) {
        // Dav ?????
        Resourcetype rt1 = of.createResourcetype();
        ret.setPropertyOk(rt1);
        Getcontentlength gcl = new Getcontentlength();
        gcl.setValue(String.valueOf(dCmp.getContentLength()));
        ret.setPropertyOk(gcl);
        String contentType = dCmp.getContentType();
        Getcontenttype gct = new Getcontenttype();
        gct.setValue(contentType);
        ret.setPropertyOk(gct);
    } else if (DavCmp.TYPE_COL_ODATA.equals(type)) {
        // OData ?????
        Resourcetype colRt = of.createResourcetype();
        colRt.setCollection(of.createCollection());
        List<Element> listElement = colRt.getAny();
        QName qname = new QName(PersoniumCoreUtils.XmlConst.NS_PERSONIUM, PersoniumCoreUtils.XmlConst.ODATA,
                PersoniumCoreUtils.XmlConst.NS_PREFIX_PERSONIUM);
        Element element = WebDAVModelHelper.createElement(qname);
        listElement.add(element);
        ret.setPropertyOk(colRt);

    } else if (DavCmp.TYPE_COL_SVC.equals(type)) {
        // Service ?????
        Resourcetype colRt = of.createResourcetype();
        colRt.setCollection(of.createCollection());
        List<Element> listElement = colRt.getAny();
        QName qname = new QName(PersoniumCoreUtils.XmlConst.NS_PERSONIUM, PersoniumCoreUtils.XmlConst.SERVICE,
                PersoniumCoreUtils.XmlConst.NS_PREFIX_PERSONIUM);
        Element element = WebDAVModelHelper.createElement(qname);
        listElement.add(element);
        ret.setPropertyOk(colRt);

    } else {
        // Col ?????
        Resourcetype colRt = of.createResourcetype();
        colRt.setCollection(of.createCollection());
        ret.setPropertyOk(colRt);

    }

    // ACL??
    Acl acl = dCmp.getAcl();
    if (isAclRead && acl != null) {

        Document aclDoc = null;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        try {
            aclDoc = dbf.newDocumentBuilder().newDocument();
            ObjectIo.marshal(acl, aclDoc);
        } catch (Exception e) {
            throw new WebApplicationException(e);
        }
        if (aclDoc != null) {
            Element e = aclDoc.getDocumentElement();
            ret.setPropertyOk(e);
        }
    }

    Map<String, String> props = dCmp.getProperties();
    if (props != null) {
        List<String> nsList = new ArrayList<String>();
        for (Map.Entry<String, String> entry : props.entrySet()) {
            String key = entry.getKey();
            String val = entry.getValue();
            int idx = key.indexOf("@");
            String ns = key.substring(idx + 1, key.length());

            int nsIdx = nsList.indexOf(ns);
            if (nsIdx == -1) {
                nsList.add(ns);
            }

            Element e = parseProp(val);

            ret.setPropertyOk(e);
        }

    }
    return ret;
}

From source file:be.fedict.eid.applet.service.signer.ooxml.OOXMLSignatureFacet.java

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

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

public static Document createXMLDocument(byte[] xmlStream) throws Exception {
    InputStream inStream = new ByteArrayInputStream(xmlStream);
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();

    return builder.parse(inStream);
}

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

/**
 * @param idmClient//ww w  . j  av a2s.  c  o m
 * @param tenant
 * @param resourceName
 * @throws FileNotFoundException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws Exception
 */
public static void importConfiguration(CasIdmClient idmClient, String tenant, String resourceName)
        throws FileNotFoundException, ParserConfigurationException, SAXException, IOException, Exception {
    InputStream is = new FileInputStream(SsoControllerTest.class.getResource(resourceName).getFile());

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

    idmClient.importTenantConfiguration(tenant, doc);
}

From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java

/**
 * Creates the parser instance based on DOM parser factory, using the contents of a XML file.
 * Validation is not done. Return the documentElement of the XML document
 * /*from w  w  w .  j av  a2 s  .  c o  m*/
 * @param xmlFile - XML file to be parsed
 * @return documentElement of the XML file
 */
public static Document getXmlDocumentElement(String xmlFileContents) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Reader reader = new StringReader(xmlFileContents);
    InputSource inputSource = new InputSource(reader);
    factory.setCoalescing(true);
    factory.setNamespaceAware(false);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(inputSource);
}

From source file:Examples.java

/**
 * Show how to transform a DOM tree into another DOM tree.
 * This uses the javax.xml.parsers to parse an XML file into a
 * DOM, and create an output DOM./*from  w  w  w.  j a va2s.co m*/
 */
public static Node exampleDOM2DOM(String sourceID, String xslID) throws TransformerException,
        TransformerConfigurationException, SAXException, IOException, ParserConfigurationException {
    TransformerFactory tfactory = TransformerFactory.newInstance();

    if (tfactory.getFeature(DOMSource.FEATURE)) {
        Templates templates;

        {
            DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
            dfactory.setNamespaceAware(true);
            DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
            org.w3c.dom.Document outNode = docBuilder.newDocument();
            Node doc = docBuilder.parse(new InputSource(xslID));

            DOMSource dsource = new DOMSource(doc);
            // If we don't do this, the transformer won't know how to 
            // resolve relative URLs in the stylesheet.
            dsource.setSystemId(xslID);

            templates = tfactory.newTemplates(dsource);
        }

        Transformer transformer = templates.newTransformer();
        DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
        // Note you must always setNamespaceAware when building .xsl stylesheets
        dfactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
        org.w3c.dom.Document outNode = docBuilder.newDocument();
        Node doc = docBuilder.parse(new InputSource(sourceID));

        transformer.transform(new DOMSource(doc), new DOMResult(outNode));

        Transformer serializer = tfactory.newTransformer();
        serializer.transform(new DOMSource(outNode), new StreamResult(new OutputStreamWriter(System.out)));

        return outNode;
    } else {
        throw new org.xml.sax.SAXNotSupportedException("DOM node processing not supported!");
    }
}

From source file:fr.afcepf.atod.wine.data.parser.XmlParser.java

public static ArrayList<ProductWine> parseSampleXml(String fileName) throws WineException {
    ArrayList<ProductWine> wineList = new ArrayList<ProductWine>();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document xml = null;//from   w w  w  .j  a  v a  2  s. com
    try {
        xml = dbf.newDocumentBuilder()
                .parse(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
    } catch (Exception e) {
        e.printStackTrace();
    }
    NodeList subNodes = xml.getElementsByTagName("Product");
    for (int i = 0; i < subNodes.getLength(); i++) {
        Node node = subNodes.item(i);
        Element tag = (Element) node;
        ProductWine w = setWine(tag);
        if (Files.exists(Paths.get(getResourcePath() + "wine_pictures/" + w.getApiId() + "/" + w.getApiId()
                + "_front.jpg")) == true) {
            wineList.add(w);
        }
    }
    return wineList;
}

From source file:be.fedict.eid.tsl.BelgianTrustServiceListFactory.java

private static Document loadDocumentFromResource(String resourceName) {
    Thread currentThread = Thread.currentThread();
    ClassLoader classLoader = currentThread.getContextClassLoader();
    InputStream documentInputStream = classLoader.getResourceAsStream(resourceName);
    if (null == documentInputStream) {
        throw new IllegalArgumentException("resource not found: " + resourceName);
    }/*from   w  w w  . j a v a 2 s  . co  m*/
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    try {
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document tslDocument = documentBuilder.parse(documentInputStream);
        return tslDocument;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.microsoft.tfs.util.xml.JAXPUtils.java

/**
 * A convenience method to create a new {@link DocumentBuilderFactory}. If
 * the given {@link ClassLoader} is not <code>null</code>, it is temporarily
 * set as the calling thread's context classloader while calling into JAXP
 * to get a {@link DocumentBuilderFactory} instance. The factory is
 * configured to be namespace aware (/*  w  w w  .j  a  va2s .c om*/
 * {@link DocumentBuilderFactory#setNamespaceAware(boolean)} is called with
 * <code>true</code>).
 *
 * @throws XMLException
 *         if a new factory can't be created
 *
 * @param contextClassloader
 *        the context classloader or <code>null</code> if none
 * @return a new {@link DocumentBuilderFactory} (never <code>null</code>)
 */
public static DocumentBuilderFactory newDocumentBuilderFactory(final ClassLoader contextClassloader) {
    final boolean setTCL = contextClassloader != null;
    ClassLoader currentTCL = null;

    if (setTCL) {
        currentTCL = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(contextClassloader);
    }

    try {
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        if (log.isTraceEnabled()) {
            final String messageFormat = "Created a new DocumentBuilderFactory: {0}(loaded from: {1})"; //$NON-NLS-1$
            final String message = MessageFormat.format(messageFormat, factory.getClass().getName(),
                    factory.getClass().getClassLoader());
            log.trace(message);
        }

        factory.setNamespaceAware(true);

        return factory;
    } catch (final FactoryConfigurationError e) {
        throw new XMLException(e);
    } finally {
        if (setTCL) {
            Thread.currentThread().setContextClassLoader(currentTCL);
        }
    }
}