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.amalto.webapp.core.util.Util.java

public static Document parse(String xmlString, String schema) throws Exception {
    // parse// ww w . jav a  2  s.co m
    Document d;
    SAXErrorHandler seh = new SAXErrorHandler();
    try {
        // initialize the sax parser which uses Xerces
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Schema validation based on schemaURL
        factory.setNamespaceAware(true);
        factory.setValidating((schema != null));
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", //$NON-NLS-1$
                "http://www.w3.org/2001/XMLSchema"); //$NON-NLS-1$
        if (schema != null) {
            factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", //$NON-NLS-1$
                    new InputSource(new StringReader(schema)));
        }
        DocumentBuilder builder;
        builder = factory.newDocumentBuilder();
        builder.setErrorHandler(seh);
        d = builder.parse(new InputSource(new StringReader(xmlString)));
    } catch (Exception e) {
        String err = "Unable to parse the document" + ": " + e.getClass().getName() + ": " //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
                + e.getLocalizedMessage() + "\n " //$NON-NLS-1$
                + xmlString;
        throw new Exception(err);
    }
    // check if document parsed correctly against the schema
    if (schema != null) {
        String errors = seh.getErrors();
        if (!errors.equals("")) { //$NON-NLS-1$
            String err = "Document  did not parse against schema: \n" + errors + "\n" + xmlString; //$NON-NLS-1$//$NON-NLS-2$
            throw new Exception(err);
        }
    }
    return d;
}

From source file:com.nickandjerry.dynamiclayoutinflator.DynamicLayoutInflator.java

public static View inflate(Context context, InputStream inputStream, ViewGroup parent) {
    try {/* www.jav a2  s. c  o  m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(inputStream);
        try {
            return inflate(context, document.getDocumentElement(), parent);
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
    return null;
}

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

/**
 * Parse params.xml definitions/*from   w  w  w. j av a  2s . c  o  m*/
 * 
 * @return A List parameter elements.
 */
public static List<FormElement> parseReportParameters(InputStream is) throws ParseException {
    log.debug("parseReportParameters({})", is);
    List<FormElement> params = new ArrayList<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("report-parameters");

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

                if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                    NodeList nlField = nForm.getChildNodes();
                    params = parseField(nlField);
                }
            }
        }
    } 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.debug("parseReportParameters: {}", params);
    return params;
}

From source file:org.geowebcache.config.XMLConfiguration.java

/**
 * Method responsible for loading xml configuration file and parsing it into a W3C DOM Document
 * /*from  w  ww  .  j a v  a2  s. c  o  m*/
 * @param file
 *            the file contaning the layer configurations
 * @return W3C DOM Document
 */
static Node loadDocument(InputStream xmlFile) throws ConfigurationException, IOException {
    Node topNode = null;
    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        topNode = checkAndTransform(docBuilder.parse(xmlFile));
    } catch (Exception e) {
        throw (IOException) new IOException(e.getMessage()).initCause(e);
    }

    return topNode;
}

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

/**
 * Parse form.xml definitions//from w  w w. ja v  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);
    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.debug("parseWorkflowForms: {}", forms);
    return forms;
}

From source file:com.tremolosecurity.openunison.util.OpenUnisonUtils.java

private static EntityDescriptor loadIdPMetaData(String pathToMetaData, KeyStore ks, TremoloType tt)
        throws Exception {

    InitializationService.initialize();//from w  w w .j  a v a  2s. co  m

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

    DocumentBuilder builder = factory.newDocumentBuilder();

    Element root = builder.parse(new InputSource(new FileInputStream(pathToMetaData))).getDocumentElement();

    EntityDescriptor ed = (EntityDescriptor) XMLObjectSupport.getUnmarshaller(root).unmarshall(root);

    return ed;
}

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

/**
 * Parse PropertyGroups.xml definitions/*  w w w. j  av  a 2 s .c  om*/
 * 
 * @return A Map with all the forms and its form elements.
 */
public static synchronized Map<PropertyGroup, List<FormElement>> parsePropertyGroupsForms(String pgForm)
        throws IOException, ParseException {
    log.debug("parsePropertyGroupsForms({})", pgForm);

    if (pGroups == null) {
        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(pgForm);

            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.debug("parsePropertyGroupsForms: {}", pGroups);
    return clonedPropertyGroups();
}

From source file:com.yahoo.xpathproto.horoscope.TransformTestHoroscope.java

private Document loadXml(String body) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(body.getBytes()));
}

From source file:com.wavemaker.tools.ws.WebServiceToolsManager.java

/**
 * Returns the XML qualified name of the root element type for the given XML.
 *//*  ww  w  .  j  a  v  a 2s.  c  om*/
public static String getXmlRootElementType(String xml)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Reader reader = new StringReader(xml);
    Document doc = db.parse(new InputSource(reader));
    Node node = doc.getFirstChild();
    if (node != null) {
        String namespaceURI = node.getNamespaceURI();
        String localName = node.getLocalName();
        if (localName != null) {
            return namespaceURI == null ? localName : namespaceURI + ":" + localName;
        }
    }
    return null;
}

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

/**
 * Parse params.xml definitions//w w w .  j av  a2s  . c o  m
 * 
 * @return A List parameter elements.
 */
public static List<FormElement> parseReportParameters(InputStream is) throws ParseException {
    log.debug("parseReportParameters({})", is);
    long begin = System.currentTimeMillis();
    List<FormElement> params = new ArrayList<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("report-parameters");

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

                if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                    NodeList nlField = nForm.getChildNodes();
                    params = parseField(nlField);
                }
            }
        }
    } 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("parseReportParameters.Time: {}", System.currentTimeMillis() - begin);
    log.debug("parseReportParameters: {}", params);
    return params;
}