List of usage examples for javax.xml.parsers DocumentBuilderFactory setValidating
public void setValidating(boolean validating)
From source file:com.bluexml.side.form.utils.DOMUtil.java
/** * Gets a namespace aware non-validating DocumentBuilder object. If non existent, the object is * created.//from ww w . j a v a2 s.c o m * * @return */ public static DocumentBuilder getDocumentBuilder() { if (documentBuilder == null) { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); // @since 1.0.2 docBuilderFactory.setValidating(false); // @since 1.0.2 documentBuilder = docBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); return null; } } return documentBuilder; }
From source file:com.github.anba.es6draft.util.Resources.java
/** * Reads the xml-structure from {@link Reader} and returns the corresponding {@link Document}. *//*ww w. j a v a 2 s . c om*/ public static Document xml(Reader xml) throws IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // turn off any validation or namespace features factory.setNamespaceAware(false); factory.setValidating(false); List<String> features = Arrays.asList("http://xml.org/sax/features/namespaces", "http://xml.org/sax/features/validation", "http://apache.org/xml/features/nonvalidating/load-dtd-grammar", "http://apache.org/xml/features/nonvalidating/load-external-dtd"); for (String feature : features) { try { factory.setFeature(feature, false); } catch (ParserConfigurationException e) { // ignore invalid feature names } } try { return factory.newDocumentBuilder().parse(new InputSource(xml)); } catch (ParserConfigurationException | SAXException e) { throw new IOException(e); } }
From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java
/** * Validates an XML file with its schema. * /*from w w w .ja v a2 s.c o m*/ * @param xmlFileStream - the inputStream of the xml content * @param xsdFileStream - the inputStream of the xsd content * @throws Exception */ public static void validateXML(InputStream xmlFileStream, InputStream xsdFileStream) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setIgnoringElementContentWhitespace(true); documentBuilderFactory.setValidating(false); DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); Document document = parser.parse(xmlFileStream); validateDocument(document, xsdFileStream); }
From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java
/** * Creates the parser instance based on dom parser factory Validates the xml file against the * XSD schema Return the documentElement of the xml document if validations succeeded * //from ww w .ja v a 2 s . c o m * @param xmlFile - XML file to be parsed * @param xmlSchema - XSD that XML file should be validated against * @return documentElement of the XML file */ public static Document getXmlDocumentElement(String xmlFile, String xmlSchema) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setCoalescing(true); factory.setNamespaceAware(false); factory.setIgnoringElementContentWhitespace(true); factory.setValidating(true); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", XMLUtil.class.getResource(xmlSchema).getFile()); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new org.xml.sax.helpers.DefaultHandler()); // Parse the document from the classpath. URL xmlFileUri = XMLUtil.class.getResource(xmlFile); if (null == xmlFileUri) { log.error("Unable to find file on classpath: " + xmlFile); return null; } return builder.parse(xmlFileUri.getFile()); }
From source file:com.vmware.identity.SharedUtils.java
/** * Read XML as DOM.//from w w w . ja v a 2s .co 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 {// ww w . j a va 2 s . 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 {//w w w .ja v a 2s . co m 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: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 * // w ww .jav a 2s. com * @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: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 w ww .ja v a2 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:com.amalto.webapp.core.util.Util.java
public static Document parse(String xmlString, String schema) throws Exception { // parse//from www.ja v a 2s . c om 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; }