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:eu.smartfp7.terrier.sensor.ParserUtility.java

public static EdgeNodeSnapShot parseShort(InputStream is) throws Exception {

    DocumentBuilderFactory xmlfact = DocumentBuilderFactory.newInstance();
    xmlfact.setNamespaceAware(true);
    Document document = xmlfact.newDocumentBuilder().parse(is);

    XPath xpath = XPathFactory.newInstance().newXPath();

    String time = (String) xpath.compile("//crowd/time/text()").evaluate(document, XPathConstants.STRING);

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    Calendar c = Calendar.getInstance();
    ;/*w  w  w.ja  v a 2 s.  c  om*/
    c.setTime(df.parse(time));

    String density = (String) xpath.compile("//crowd/density/text()").evaluate(document, XPathConstants.STRING);

    NodeList list = (NodeList) xpath.compile("//crowd/colour").evaluate(document, XPathConstants.NODESET);
    double[] colors = new double[list.getLength()];
    for (int i = 0; i < list.getLength(); i++) {
        org.w3c.dom.Node colorNode = list.item(i);
        String v = colorNode.getFirstChild().getTextContent();
        colors[i] = new Double(v);
    }

    String activity = (String) xpath.compile("//activity/name/text()").evaluate(document,
            XPathConstants.STRING);

    CrowdReport crowdReport = new CrowdReport(null, new Double(density), 0.0, colors);

    EdgeNodeSnapShot snapShot = new EdgeNodeSnapShot(null, null, c, crowdReport);
    snapShot.setText((activity != null ? activity : StringUtils.EMPTY));

    return snapShot;

}

From source file:com.sitewhere.configuration.ConfigurationMigrationSupport.java

/**
 * Get configuration bytes as DOM document.
 * // ww w .  j  a  v  a2 s.c o m
 * @param original
 * @return
 * @throws SiteWhereException
 */
protected static Document getConfigurationDocument(byte[] original) throws SiteWhereException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new InputSource(new ByteArrayInputStream(original)));
    } catch (Exception e) {
        throw new SiteWhereException("Unable to parse tenant configuration.", e);
    }
}

From source file:eu.eidas.engine.test.simple.SSETestUtils.java

/**
 * Marshall./*from  w  ww  .j  ava  2  s  . c o  m*/
 *
 * @param samlToken the SAML token
 *
 * @return the byte[]
 *
 * @throws MarshallingException the marshalling exception
 * @throws ParserConfigurationException the parser configuration exception
 * @throws TransformerException the transformer exception
 */
public static byte[] marshall(final XMLObject samlToken)
        throws MarshallingException, ParserConfigurationException, TransformerException {

    final javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringComments(true);
    final javax.xml.parsers.DocumentBuilder docBuild = dbf.newDocumentBuilder();

    // Get the marshaller factory
    final MarshallerFactory marshallerFactory = Configuration.getMarshallerFactory();

    // Get the Subject marshaller
    final Marshaller marshaller = marshallerFactory.getMarshaller(samlToken);

    final Document doc = docBuild.newDocument();

    // Marshall the SAML token
    marshaller.marshall(samlToken, doc);

    // Obtain a byte array representation of the marshalled SAML object
    final DOMSource domSource = new DOMSource(doc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final StreamResult result = new StreamResult(new OutputStreamWriter(baos, Constants.UTF8));
    final TransformerFactory transFact = TransformerFactory.newInstance();
    final Transformer transformer = transFact.newTransformer();
    transformer.transform(domSource, result);

    return baos.toByteArray();
}

From source file:com.c4om.jschematronvalidator.JSchematronValidatorMain.java

/**
 * Reads a {@link org.w3c.dom.Document} from an {@link InputSource}
 * @param inputSource the input source/*from  w ww .  j av a2 s.  c  o m*/
 * @return the read {@link Document}
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document loadW3CDocumentFromInputSource(InputSource inputSource)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    Document resultingDocument = documentBuilder.parse(inputSource);
    return resultingDocument;
}

From source file:net.sf.jasperreports.engine.util.JRXmlUtils.java

/**
 * Creates a XML document builder.// w  ww .j  a  v a2s  .  c  o  m
 * 
 * @return a XML document builder
 * @throws JRException
 */
public static DocumentBuilder createDocumentBuilder(boolean isNamespaceAware) throws JRException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setIgnoringComments(true);
    dbf.setNamespaceAware(isNamespaceAware);
    try {
        if (!allowDoctype()) {
            dbf.setFeature(FEATURE_DISALLOW_DOCTYPE, true);
        }

        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_DOCUMENT_BUILDER_FACTORY_CREATION_FAILURE, null, e);
    }
}

From source file:eu.optimis.mi.aggregator.util.XmlUtil.java

@SuppressWarnings("unused")
private static Document getDocument(String xml) {
    try {// w  ww  .j av a2s .c o m
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
        return null;
    } catch (ParserConfigurationException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}

From source file:com.photon.phresco.impl.WindowsApplicationProcessor.java

private static void updateItemGroup(File path, List<ArtifactGroup> artifactGroups) throws PhrescoException {
    try {//from   w  w  w  .j a v a2s .  c  om
        path = new File(path + File.separator + SOURCE_DIR + File.separator + SRC_DIR + File.separator
                + PROJECT_ROOT + File.separator + PROJECT_ROOT + CSPROJ_FILE);
        if (!path.exists() && artifactGroups == null) {
            return;
        }
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(false);
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(path);
        boolean referenceCheck = referenceCheck(doc);
        if (referenceCheck) {
            updateItemGroups(doc, artifactGroups);
        } else {
            createNewItemGroup(doc, artifactGroups);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(path.toURI().getPath());
        transformer.transform(source, result);
    } catch (ParserConfigurationException e) {
        throw new PhrescoException(e);
    } catch (SAXException e) {
        throw new PhrescoException(e);
    } catch (IOException e) {
        throw new PhrescoException(e);
    } catch (TransformerConfigurationException e) {
        throw new PhrescoException(e);
    } catch (TransformerException e) {
        throw new PhrescoException(e);
    }
}

From source file:de.tub.av.pe.xcapsrv.XMLValidator.java

public static String getWellFormedElement(InputStream input)
        throws NotValidXMLFragmentConflictException, InternalServerErrorException, TransformerException,
        ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    String result = null;/* ww w . j a va 2 s.com*/

    DocumentBuilder parser = factory.newDocumentBuilder();
    Document dummyDocument = parser.parse(input);
    result = TextWriter.toString(dummyDocument.getDocumentElement());

    return result;
}

From source file:com.cablevision.util.sso.UtilSSO.java

/**
 * Converts a JDOM Document to a W3 DOM document.
 * /*from   www. j  av a  2 s  .  com*/
 * @param doc JDOM Document
 * @return W3 DOM Document if converted successfully, null otherwise
 */
public static org.w3c.dom.Document toDom(jwm.jdom.Document doc) throws SamlException {
    try {
        XMLOutputter xmlOutputter = new XMLOutputter();
        StringWriter elemStrWriter = new StringWriter();
        xmlOutputter.output(doc, elemStrWriter);
        byte[] xmlBytes = elemStrWriter.toString().getBytes();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        return dbf.newDocumentBuilder().parse(new ByteArrayInputStream(xmlBytes));
    } catch (IOException e) {
        throw new SamlException("Error converting JDOM document to W3 DOM document: " + e.getMessage());
    } catch (ParserConfigurationException e) {
        throw new SamlException("Error converting JDOM document to W3 DOM document: " + e.getMessage());
    } catch (SAXException e) {
        throw new SamlException("Error converting JDOM document to W3 DOM document: " + e.getMessage());
    }
}

From source file:com.act.lcms.MzMLParser.java

/**
 * Helper function: builds an XML DocumentBuilderFactory that can be used repeatedly in this class.
 * <p>//from   ww  w  .  jav  a 2  s .co  m
 * TODO: move this to an XML utility class, as I'm sure we'll use it again some day.
 *
 * @return An XML DocumentBuilderFactory.
 * @throws ParserConfigurationException
 */
public static DocumentBuilderFactory mkDocBuilderFactory() throws ParserConfigurationException {
    /* This factory must be configured within the context of a method call for exception handling.
     * TODO: can we work around this w/ dependency injection? */
    // from http://stackoverflow.com/questions/155101/make-documentbuilder-parse-ignore-dtd-references
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setValidating(false);
    docFactory.setNamespaceAware(true);
    docFactory.setFeature("http://xml.org/sax/features/namespaces", false);
    docFactory.setFeature("http://xml.org/sax/features/validation", false);
    docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    return docFactory;
}