/** * Validate the given stream and return a valid DOM document for parsing. */ protected Document buildDocument(ErrorHandler handler, InputStream stream) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder parser = dbf.newDocumentBuilder(); parser.setErrorHandler(handler); return parser.parse(stream); }
/** * Create a JAXP DocumentBuilder that this bean definition reader * will use for parsing XML documents. Can be overridden in subclasses, * adding further initialization of the builder. * @param factory the JAXP DocumentBuilderFactory that the DocumentBuilder * should be created with * @param entityResolver the SAX EntityResolver to use * @param errorHandler the SAX ErrorHandler to use * @return the JAXP DocumentBuilder * @throws ParserConfigurationException if thrown by JAXP methods */ protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory, @Nullable EntityResolver entityResolver, @Nullable ErrorHandler errorHandler) throws ParserConfigurationException { DocumentBuilder docBuilder = factory.newDocumentBuilder(); if (entityResolver != null) { docBuilder.setEntityResolver(entityResolver); } if (errorHandler != null) { docBuilder.setErrorHandler(errorHandler); } return docBuilder; }
public static DocumentBuilder createAndGetDocumentBuilder(Collection<Source> schemaSources) throws SAXException, ParserConfigurationException { DocumentBuilderFactory factory = createAndGetFactory(schemaSources); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); documentBuilder.setErrorHandler(new TransformationErrorHandler()); return documentBuilder; }
DocumentBuilder builder = getDocumentBuilderFactory().newDocumentBuilder(); ErrorHandler errorHandler = getErrorHandler(); if (errorHandler != null) builder.setErrorHandler(errorHandler); final Document doc; try { doc = builder.parse(is); } catch (MalformedURLException e) {
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); builderFactory = DocumentBuilderFactory.newInstance(); builder = builderFactory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException {
void convertToNodes() throws CanonicalizationException, ParserConfigurationException, IOException, SAXException { DocumentBuilder db = XMLUtils.createDocumentBuilder(false, secureValidation); // select all nodes, also the comments. try { db.setErrorHandler(new org.docx4j.org.apache.xml.security.utils.IgnoreAllErrorHandler()); Document doc = db.parse(this.getOctetStream()); this.subNode = doc; } catch (SAXException ex) { // if a not-wellformed nodeset exists, put a container around it... ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write("<container>".getBytes("UTF-8")); baos.write(this.getBytes()); baos.write("</container>".getBytes("UTF-8")); byte result[] = baos.toByteArray(); Document document = db.parse(new ByteArrayInputStream(result)); this.subNode = document.getDocumentElement().getFirstChild().getFirstChild(); } finally { XMLUtils.repoolDocumentBuilder(db); if (this.inputOctetStreamProxy != null) { this.inputOctetStreamProxy.close(); } this.inputOctetStreamProxy = null; this.bytes = null; } }
public static Document readDocumentFromFile(String filename) throws Exception { InputSource in = new InputSource(new FileReader(filename)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false); DocumentBuilder db = factory.newDocumentBuilder(); db.setErrorHandler(new SAXErrorHandler()); return db.parse(in); }
/** * Same as {@link #parse(InputSource, boolean, boolean)}, but loads from a {@link File}; don't miss the security * warnings documented there. */ static public NodeModel parse(File f, boolean removeComments, boolean removePIs) throws SAXException, IOException, ParserConfigurationException { DocumentBuilder builder = getDocumentBuilderFactory().newDocumentBuilder(); ErrorHandler errorHandler = getErrorHandler(); if (errorHandler != null) builder.setErrorHandler(errorHandler); Document doc = builder.parse(f); if (removeComments && removePIs) { simplify(doc); } else { if (removeComments) { removeComments(doc); } if (removePIs) { removePIs(doc); } mergeAdjacentText(doc); } return wrap(doc); }
/** * Create a JAXP DocumentBuilder that this bean definition reader * will use for parsing XML documents. Can be overridden in subclasses, * adding further initialization of the builder. * @param factory the JAXP DocumentBuilderFactory that the DocumentBuilder * should be created with * @param entityResolver the SAX EntityResolver to use * @param errorHandler the SAX ErrorHandler to use * @return the JAXP DocumentBuilder * @throws ParserConfigurationException if thrown by JAXP methods */ protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory, @Nullable EntityResolver entityResolver, @Nullable ErrorHandler errorHandler) throws ParserConfigurationException { DocumentBuilder docBuilder = factory.newDocumentBuilder(); if (entityResolver != null) { docBuilder.setEntityResolver(entityResolver); } if (errorHandler != null) { docBuilder.setErrorHandler(errorHandler); } return docBuilder; }
/** * Returns a validating XML parser given an XSD (not DTD!). * * @param schemaFile File wit hXML schema * @return An XML parser in the form of a DocumentBuilder */ public static DocumentBuilder getValidatingXmlParser(File schemaFile) { DocumentBuilder db = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(schemaFile); dbf.setSchema(schema); db = dbf.newDocumentBuilder(); db.setErrorHandler(new SAXErrorHandler()); } catch (ParserConfigurationException e) { log.warnf("%s: Unable to create XML parser\n", XMLUtils.class.getName()); log.warn(e); } catch (SAXException e) { log.warnf("%s: XML parsing exception while loading schema %s\n", XMLUtils.class.getName(),schemaFile.getPath()); log.warn(e); } catch(UnsupportedOperationException e) { log.warnf("%s: API error while setting up XML parser. Check your JAXP version\n", XMLUtils.class.getName()); log.warn(e); } return db; }
throw new ParserConfigurationException("factory should be validating but isn't"); builder = validatingFactory.newDocumentBuilder(); assert builder.isValidating(); builder.setErrorHandler(new ErrorHandler() { public void error(SAXParseException e) throws SAXParseException { throw e; builder = factory.newDocumentBuilder();
db.setErrorHandler(new org.docx4j.org.apache.xml.security.utils.IgnoreAllErrorHandler()); document = db.parse(in); } finally { XMLUtils.repoolDocumentBuilder(db);
/** * Validate the given stream and return a valid DOM document for parsing. */ protected Document buildDocument(ErrorHandler handler, InputStream stream) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder parser = dbf.newDocumentBuilder(); parser.setErrorHandler(handler); return parser.parse(stream); }
/** * Create a new DOM document from the input stream * * @param documentBuilderFactory the factory to build to DOM document * @param inputStream the input stream to parse * @return the new DOM document * @throws ModelParseException if a parsing or IO error is triggered */ public static DomDocument parseInputStream(DocumentBuilderFactory documentBuilderFactory, InputStream inputStream) { try { DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); documentBuilder.setErrorHandler(new DomErrorHandler()); return new DomDocumentImpl(documentBuilder.parse(inputStream)); } catch (ParserConfigurationException e) { throw new ModelParseException("ParserConfigurationException while parsing input stream", e); } catch (SAXException e) { throw new ModelParseException("SAXException while parsing input stream", e); } catch (IOException e) { throw new ModelParseException("IOException while parsing input stream", e); } }
/** * Creates a new document builder, with sensible defaults * * @throws IllegalStateException If creating the DocumentBuilder fails, e.g. * due to {@link ParserConfigurationException}. */ public static synchronized DocumentBuilder newDocumentBuilder() { try { DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); documentBuilder.setEntityResolver(SAXHelper.IGNORING_ENTITY_RESOLVER); documentBuilder.setErrorHandler(new DocHelperErrorHandler()); return documentBuilder; } catch (ParserConfigurationException e) { throw new IllegalStateException("cannot create a DocumentBuilder", e); } }
throw new ParserConfigurationException("factory should be validating but isn't"); builder = validatingFactory.newDocumentBuilder(); assert builder.isValidating(); builder.setErrorHandler(new ErrorHandler() { public void error(SAXParseException e) throws SAXParseException { throw e; builder = factory.newDocumentBuilder();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new SimpleErrorHandler()); // the "parse" method also validates XML, will throw an exception if misformatted Document document = builder.parse(new InputSource("document.xml"));
/** * Create a JAXP DocumentBuilder that this bean definition reader will use for parsing XML documents. Can be * overridden in subclasses, adding further initialization of the builder. * * @param factory the JAXP DocumentBuilderFactory that the DocumentBuilder should be created with * @return the JAXP DocumentBuilder * @throws javax.xml.parsers.ParserConfigurationException if thrown by JAXP methods */ private DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory) throws ParserConfigurationException { DocumentBuilder docBuilder = factory.newDocumentBuilder(); docBuilder.setErrorHandler(new DozerDefaultHandler()); docBuilder.setEntityResolver(new DozerResolver(beanContainer)); return docBuilder; }
public XMLSanitizer(String pattern, VirtualFileFilter filter) throws Exception { this.filter = filter; XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); expression = xpath.compile(pattern); DocumentBuilderFactory DBfactory = DocumentBuilderFactory.newInstance(); DBfactory.setNamespaceAware(true); builder = DBfactory.newDocumentBuilder(); builder.setErrorHandler(null); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformer = transformerFactory.newTransformer(); }
/** * Create a JAXP DocumentBuilder that this bean definition reader * will use for parsing XML documents. Can be overridden in subclasses, * adding further initialization of the builder. * @param factory the JAXP DocumentBuilderFactory that the DocumentBuilder * should be created with * @param entityResolver the SAX EntityResolver to use * @param errorHandler the SAX ErrorHandler to use * @return the JAXP DocumentBuilder * @throws ParserConfigurationException if thrown by JAXP methods */ protected DocumentBuilder createDocumentBuilder( DocumentBuilderFactory factory, EntityResolver entityResolver, ErrorHandler errorHandler) throws ParserConfigurationException { DocumentBuilder docBuilder = factory.newDocumentBuilder(); if (entityResolver != null) { docBuilder.setEntityResolver(entityResolver); } if (errorHandler != null) { docBuilder.setErrorHandler(errorHandler); } return docBuilder; }