List of usage examples for javax.xml.parsers DocumentBuilder setErrorHandler
public abstract void setErrorHandler(ErrorHandler eh);
From source file:Main.java
private static DocumentBuilder documentBuilder() throws Exception { DocumentBuilder b = null; try {//from w ww . j a v a2 s .co m DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); b = f.newDocumentBuilder(); b.setErrorHandler(fErrorHandler); } catch (ParserConfigurationException e) { throw e; } return b; }
From source file:XMLUtil.java
public static Document parse(InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate);/*w w w .j ava 2 s . co m*/ factory.setNamespaceAware(namespaceAware); DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { throw new SAXException(ex); } if (errorHandler != null) { builder.setErrorHandler(errorHandler); } if (entityResolver != null) { builder.setEntityResolver(entityResolver); } assert input != null : "InputSource cannot be null"; return builder.parse(input); }
From source file:Main.java
/** * Create from factory a DocumentBuilder and let it create a org.w3c.dom.Document. * This method takes InputSource. After successful finish the document tree is returned. * * @param input a parser input (for URL users use: <code>new InputSource(url.toExternalForm())</code> * @param validate if true validating parser is used * @param namespaceAware if true DOM is created by namespace aware parser * @param errorHandler a error handler to notify about exception or <code>null</code> * @param entityResolver SAX entity resolver or <code>null</code>; see class Javadoc for hints * * @throws IOException if an I/O problem during parsing occurs * @throws SAXException is thrown if a parser error occurs * @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type. * * @return document representing given input, or null if a parsing error occurs *///from w w w .j a v a2 s.c o m public static Document parse(InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N } if (errorHandler != null) { builder.setErrorHandler(errorHandler); } if (entityResolver != null) { builder.setEntityResolver(entityResolver); } return builder.parse(input); }
From source file:edu.lternet.pasta.common.XmlUtility.java
/** * Parses the provided XML string as a (DOM) document. * * @param xmlString the XML string to be parsed. * @param schema the schema used to validate the provided XML. * * @return a document derived from the provided XML string. *///ww w . j av a2s . c o m public static Document xmlStringToDoc(String xmlString, Schema schema) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setSchema(schema); InputSource source = new InputSource(new StringReader(xmlString)); try { DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new XmlParsingErrorHandler(xmlString)); return builder.parse(source); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); // shouldn't be reached } catch (SAXException e) { throw new IllegalStateException(e); // shouldn't be reached } catch (IOException e) { throw new IllegalStateException(e); // shouldn't be reached } }
From source file:com.gargoylesoftware.htmlunit.xml.XmlUtil.java
/** * Builds a document from the content of the web response. * A warning is logged if an exception is thrown while parsing the XML content * (for instance when the content is not a valid XML and can't be parsed). * * @param webResponse the response from the server * @throws IOException if the page could not be created * @return the parse result/*from ww w. j a va 2 s.c om*/ * @throws SAXException if the parsing fails * @throws ParserConfigurationException if a DocumentBuilder cannot be created */ public static Document buildDocument(final WebResponse webResponse) throws IOException, SAXException, ParserConfigurationException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); if (webResponse == null) { return factory.newDocumentBuilder().newDocument(); } factory.setNamespaceAware(true); final InputStreamReader reader = new InputStreamReader(webResponse.getContentAsStream(), webResponse.getContentCharset()); // we have to do the blank input check and the parsing in one step final TrackBlankContentReader tracker = new TrackBlankContentReader(reader); final InputSource source = new InputSource(tracker); final DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(DISCARD_MESSAGES_HANDLER); builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException, IOException { return new InputSource(new StringReader("")); } }); try { return builder.parse(source); } catch (final SAXException e) { if (tracker.wasBlank()) { return factory.newDocumentBuilder().newDocument(); } throw e; } }
From source file:com.google.code.joliratools.bind.apt.JAXROProcessorJSONTest.java
/** * @param xmlContent/*from ww w . j a v a 2s. c o m*/ * @return * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ private static XMLNode parse(final String xmlContent) throws ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); final DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override public void error(final SAXParseException exception) throws SAXException { throw exception; } @Override public void fatalError(final SAXParseException exception) throws SAXException { throw exception; } @Override public void warning(final SAXParseException exception) throws SAXException { throw exception; } }); final Document document = builder.parse(new InputSource(new StringReader(xmlContent))); final XMLNode root = new XMLNode(document); return root; }
From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java
/** * Gets the document.//ww w . ja v a 2 s . c o m * * @param configURL * the config url * @return the document * @throws Exception * the exception */ private static Document getDocument(URL configURL) throws Exception { InputStream is = null; if (configURL != null) { URLConnection conn = configURL.openConnection(); conn.setUseCaches(false); // avoid JAR locking on Windows and Tomcat is = conn.getInputStream(); } if (is == null) { throw new IOException("Failed to obtain InputStream from url: " + configURL); } DocumentBuilderFactory docBuilderFactory = null; docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); try { // otherwise Xerces fails in validation docBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", true); } catch (IllegalArgumentException e) { docBuilderFactory.setValidating(false); docBuilderFactory.setNamespaceAware(false); } InputSource source = new InputSource(is); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); // docBuilder.setEntityResolver( resolver ); List errors = new ArrayList(); docBuilder.setErrorHandler(new ErrorLogger("XML InputStream", errors)); Document doc = docBuilder.parse(source); if (errors.size() != 0) { throw new PersistenceException("invalid persistence.xml", (Throwable) errors.get(0)); } is.close(); //Close input Stream return doc; }
From source file:com.adaptris.core.util.XmlHelper.java
private static DocumentBuilder newDocumentBuilder(DocumentBuilderFactoryBuilder cfg) throws ParserConfigurationException { DocumentBuilder builder = DocumentBuilderFactoryBuilder.newInstance(cfg).build().newDocumentBuilder(); builder.setErrorHandler(new DefaultErrorHandler()); return builder; }
From source file:com.moviejukebox.tools.DOMHelper.java
/** * Get a DOM document from the supplied file * * @param xmlFile/*from www.j a v a 2s . co m*/ * @return * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public static Document getDocFromFile(File xmlFile) throws ParserConfigurationException, SAXException, IOException { URL url = xmlFile.toURI().toURL(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc; // Custom error handler db.setErrorHandler(new SaxErrorHandler()); try (InputStream in = url.openStream()) { doc = db.parse(in); } catch (SAXParseException ex) { if (FilenameUtils.isExtension(xmlFile.getName().toLowerCase(), "xml")) { throw new SAXParseException("Failed to process file as XML", null, ex); } // Try processing the file a different way doc = null; } if (doc == null) { try ( // try wrapping the file in a root StringReader sr = new StringReader(wrapInXml(FileTools.readFileToString(xmlFile)))) { doc = db.parse(new InputSource(sr)); } } if (doc != null) { doc.getDocumentElement().normalize(); } return doc; }
From source file:Main.java
private static void validateDTD0(String xmlFile, final String dtdPath) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); if (dtdPath != null && !dtdPath.isEmpty()) { documentBuilder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new FileInputStream(dtdPath)); }/*from w w w. ja v a 2 s . c o m*/ }); } documentBuilder.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException exception) throws SAXException { } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } public void error(SAXParseException exception) throws SAXException { throw exception; } }); documentBuilder.parse(new FileInputStream(xmlFile)); }