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:com.openkm.util.FormUtils.java

/**
 * Parse form.xml definitions/*from  w w w . j av  a  2s  . c  o  m*/
 * 
 * @return A Map with all the forms and its form elements.
 */
public static Map<String, List<FormElement>> parseWorkflowForms(InputStream is) throws ParseException {
    log.debug("parseWorkflowForms({})", is);
    long begin = System.currentTimeMillis();
    Map<String, List<FormElement>> forms = new HashMap<String, List<FormElement>>();

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(true);
        ErrorHandler handler = new ErrorHandler();
        // EntityResolver resolver = new LocalResolver(Config.DTD_BASE);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.setEntityResolver(resolver);

        if (is != null) {
            Document doc = db.parse(is);
            doc.getDocumentElement().normalize();
            NodeList nlForm = doc.getElementsByTagName("workflow-form");

            for (int i = 0; i < nlForm.getLength(); i++) {
                Node nForm = nlForm.item(i);

                if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                    String taskName = nForm.getAttributes().getNamedItem("task").getNodeValue();
                    NodeList nlField = nForm.getChildNodes();
                    List<FormElement> fe = parseField(nlField);
                    forms.put(taskName, fe);
                }
            }
        }
    } catch (ParserConfigurationException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ParseException(e.getMessage(), e);
    }

    log.trace("parseWorkflowForms.Time: {}", System.currentTimeMillis() - begin);
    log.debug("parseWorkflowForms: {}", forms);
    return forms;
}

From source file:com.syrup.storage.xml.XmlFactory.java

/**
 * Returns a <code>Document</code> object representing a ???
 * //w  ww.j  ava 2  s  .c  o m
 * @param mockServices List of services to convert into an xml document
  * @return <code>Document</code> object representing a cXML order request
 */
public Document getAsDocument(IStorage store) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document document = docBuilder.newDocument();

        XmlFileConfigurationGenerator xmlGeneratorSupport = new XmlFileConfigurationGenerator();

        Element xmlRootElement = xmlGeneratorSupport.getElement(document, store);
        document.appendChild(xmlRootElement);

        return document;
    } catch (ParserConfigurationException pce) {
        System.out.println(":" + pce.getMessage());

        return null;
    }
}

From source file:marytts.tests.junit4.EnvironmentTest.java

@Test
public void testXMLParserSupportsNamespaces() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document document = docBuilder.parse(this.getClass().getResourceAsStream("test1.namespaces"));
    NodeList nl = document.getElementsByTagNameNS("http://www.w3.org/2001/10/synthesis", "*");
    assertNotNull(nl.item(0));//  w w  w  .j  a  va2s  .co m
    assertTrue(nl.item(0).getNodeName().equals("ssml:emphasis"));
}

From source file:com.openkm.util.FormUtils.java

/**
 * Parse PropertyGroups.xml definitions//from w ww.  j a  v  a 2s  . c o m
 * 
 * @param pgDefFile Path to file where is the Property Groups definition.
 * @return A Map with all the forms and its form elements.
 */
public static synchronized Map<PropertyGroup, List<FormElement>> parsePropertyGroupsForms(String pgDefFile)
        throws IOException, ParseException {
    log.debug("parsePropertyGroupsForms({})", pgDefFile);

    if (pGroups == null) {
        long begin = System.currentTimeMillis();
        pGroups = new HashMap<PropertyGroup, List<FormElement>>();
        FileInputStream fis = null;

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setValidating(true);
            ErrorHandler handler = new ErrorHandler();
            // EntityResolver resolver = new LocalResolver(Config.DTD_BASE);
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setErrorHandler(handler);
            db.setEntityResolver(resolver);
            fis = new FileInputStream(pgDefFile);

            if (fis != null) {
                Document doc = db.parse(fis);
                doc.getDocumentElement().normalize();
                NodeList nlForm = doc.getElementsByTagName("property-group");

                for (int i = 0; i < nlForm.getLength(); i++) {
                    Node nForm = nlForm.item(i);

                    if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                        PropertyGroup pg = new PropertyGroup();

                        Node item = nForm.getAttributes().getNamedItem("label");
                        if (item != null)
                            pg.setLabel(item.getNodeValue());
                        item = nForm.getAttributes().getNamedItem("name");
                        if (item != null)
                            pg.setName(item.getNodeValue());
                        item = nForm.getAttributes().getNamedItem("visible");
                        if (item != null)
                            pg.setVisible(Boolean.valueOf(item.getNodeValue()));
                        item = nForm.getAttributes().getNamedItem("readonly");
                        if (item != null)
                            pg.setReadonly(Boolean.valueOf(item.getNodeValue()));

                        NodeList nlField = nForm.getChildNodes();
                        List<FormElement> fe = parseField(nlField);
                        pGroups.put(pg, fe);
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            throw new ParseException(e.getMessage());
        } catch (SAXException e) {
            throw new ParseException(e.getMessage());
        } catch (IOException e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(fis);
        }

        log.trace("parsePropertyGroupsForms.Time: {}", System.currentTimeMillis() - begin);
    }

    log.debug("parsePropertyGroupsForms: {}", pGroups);
    return clonedPropertyGroups();
}

From source file:org.synyx.hades.dao.config.TypeFilterParserUnitTest.java

@Before
public void setUp() throws SAXException, IOException, ParserConfigurationException {

    parser = new TypeFilterParser(classLoader, context);

    Resource sampleXmlFile = new ClassPathResource("config/type-filter-test.xml");

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

    documentElement = factory.newDocumentBuilder().parse(sampleXmlFile.getInputStream()).getDocumentElement();
}

From source file:org.openmrs.module.metadatasharing.converter.ConverterEngine.java

private Document fromXML(String xml) throws SerializationException {
    Document doc;/*from   w  ww.  j av a2s .  c o  m*/
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(new InputSource(new StringReader(xml)));
    } catch (Exception e) {
        throw new SerializationException(e);
    }
    return doc;
}

From source file:nl.surfnet.coin.selfservice.provisioner.SAMLProvisionerTest.java

private Assertion readAssertionFromFile(String filename) throws ConfigurationException, IOException,
        UnmarshallingException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    f.setNamespaceAware(true);
    DefaultBootstrap.bootstrap();//ww w . java 2 s  .  co  m
    return (Assertion) readFromFile(f.newDocumentBuilder(), new ClassPathResource(filename).getFile());
}

From source file:DOMImport.java

public void inandout(String infile1, String infile2, String outfile) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);/* w ww. ja  va2s .  c  o  m*/
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc1 = null;
    Document doc2 = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is1 = new InputSource(infile1);
        doc1 = builder.parse(is1);
        InputSource is2 = new InputSource(infile2);
        doc2 = builder.parse(is2);
        importName(doc1, doc2);
        FileOutputStream fos = new FileOutputStream(outfile);
        TreeToXML ttxml = new TreeToXML();
        ttxml.write(fos, doc2);
        fos.close();
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:edu.utah.further.core.xml.parser.AbstractXmlResultParser.java

/**
 * Parse an XML input source using JAXP.
 *
 * @param xmlSource//w w w . j  a  v a 2 s.c  o  m
 *            XML input source
 * @return DOM document node
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 */
protected Document parseDocument(final InputStream xmlSource)
        throws ParserConfigurationException, IOException, SAXException {
    final DocumentBuilderFactory factory = XmlUtil.getDocumentBuilderFactory();
    factory.setNamespaceAware(true); // never forget this!
    final DocumentBuilder builder = factory.newDocumentBuilder();

    // Force re-encoding of the input source and parse
    return builder.parse(StringUtil.toUtf8(xmlSource));
}

From source file:org.springmodules.validation.bean.conf.loader.xml.AbstractXmlBeanValidationConfigurationLoader.java

/**
 * todo: document/*from  w w  w  .j  a  va  2 s.co m*/
 *
 * @see org.springmodules.validation.bean.conf.loader.xml.AbstractResourceBasedBeanValidationConfigurationLoader#loadConfigurations(org.springframework.core.io.Resource)
 */
protected final Map loadConfigurations(Resource resource) {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document document = builder.parse(resource.getInputStream());
        return loadConfigurations(document, resource.getDescription());
    } catch (IOException ioe) {
        logger.error("Could not read resource '" + resource.getDescription() + "'", ioe);
        throw new ResourceConfigurationLoadingException(resource, ioe);
    } catch (ParserConfigurationException pce) {
        logger.error("Could not parse xml resource '" + resource.getDescription() + "'", pce);
        throw new ResourceConfigurationLoadingException(resource, pce);
    } catch (SAXException se) {
        logger.error("Could not parse xml resource '" + resource.getDescription() + "'", se);
        throw new ResourceConfigurationLoadingException(resource, se);
    } catch (Throwable t) {
        logger.error("Could not parse xml resource '" + resource.getDescription() + "'", t);
        throw new ResourceConfigurationLoadingException(resource, t);
    }
}