/** * 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; }
/** * Create a {@code DocumentBuilder} that this marshaller will use for creating * DOM documents when passed an empty {@code DOMSource}. * <p>Can be overridden in subclasses, adding further initialization of the builder. * @param factory the {@code DocumentBuilderFactory} that the DocumentBuilder should be created with * @return the {@code DocumentBuilder} * @throws ParserConfigurationException if thrown by JAXP methods */ protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory) throws ParserConfigurationException { DocumentBuilder documentBuilder = factory.newDocumentBuilder(); if (!isProcessExternalEntities()) { documentBuilder.setEntityResolver(NO_OP_ENTITY_RESOLVER); } return documentBuilder; }
/** * Parse the supplied XML stream data to a {@link Document}. * <p> * This function does not close the stream. * * @param stream The XML stream. * @return The XML {@link Document}. * @throws SAXException Error parsing the XML stream data e.g. badly formed XML. * @throws IOException Error reading from the steam. * @since 2.0 */ public static @Nonnull Document parse(@Nonnull Reader stream) throws SAXException, IOException { DocumentBuilder docBuilder; try { docBuilder = newDocumentBuilderFactory().newDocumentBuilder(); docBuilder.setEntityResolver(RestrictiveEntityResolver.INSTANCE); } catch (ParserConfigurationException e) { throw new IllegalStateException("Unexpected error creating DocumentBuilder.", e); } return docBuilder.parse(new InputSource(stream)); }
public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(false); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) { return new InputSource(dtd); } }); builder.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException e) { } @Override public void error(SAXParseException e) throws SAXException { throw e; } @Override public void fatalError(SAXParseException e) throws SAXException { throw e; } }); return builder.parse(xml); }
/** * get document builder * * @return DocumentBuilder */ private static DocumentBuilder getDocumentBuilder() { DocumentBuilder documentBuilder = null; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); try { documentBuilder = documentBuilderFactory.newDocumentBuilder(); // Block any external content resolving actions since we don't need them and a report // says these actions may cause security problems. documentBuilder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(); } }); } catch (Exception e) { throw new JavaXmlUtilException(e); } return documentBuilder; }
/** * Parse XML data into a DOM representation, taking local resources and Schemas into account. * * @param inputData * a reader from which the XML data is to be read. * @param validating * whether to Schema-validate the XML data * @return the DOM document resulting from the parse * @throws ParserConfigurationException * if no parser could be created * @throws SAXException * if there was a parse error * @throws IOException * if there was a problem reading from the reader */ public static Document parseDocument(Reader inputData, boolean validating) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder builder = createDocumentBuilder(validating); /* * Set Entity resolver for APML and SABLE */ builder.setEntityResolver(new MaryEntityResolver()); return builder.parse(new InputSource(inputData)); }
public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); // the actual builder or parser DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(new CacheXmlParser()); return builder; }
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = fac.newDocumentBuilder(); builder.setEntityResolver(FSEntityResolver.instance());
/** * 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; }
/** * @deprecated since 2.2.0 migrate the method to your code base if needed */ @Deprecated public static Document parseXml(String xml) throws ParserConfigurationException { DocumentBuilder builder = factory.newDocumentBuilder(); try { // Disable resolution of external entities. See TRUNK-3942 builder.setEntityResolver((publicId, systemId) -> new InputSource(new StringReader(""))); return builder.parse(new InputSource(new StringReader(xml))); } catch (IOException | SAXException ex) { return null; } }
/** * Creates a new document builder, with sensible defaults */ public static synchronized DocumentBuilder newDocumentBuilder() { try { DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); documentBuilder.setEntityResolver(SAXHelper.IGNORING_ENTITY_RESOLVER); return documentBuilder; } catch (ParserConfigurationException e) { throw new IllegalStateException("cannot create a DocumentBuilder", e); } }
/** * Parse XML data into a DOM representation, taking local resources and Schemas into account. * * @param inputData * a reader from which the XML data is to be read. * @param validating * whether to Schema-validate the XML data * @return the DOM document resulting from the parse * @throws ParserConfigurationException * if no parser could be created * @throws SAXException * if there was a parse error * @throws IOException * if there was a problem reading from the reader */ public static Document parseDocument(Reader inputData, boolean validating) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder builder = createDocumentBuilder(validating); /* * Set Entity resolver for APML and SABLE */ builder.setEntityResolver(new MaryEntityResolver()); return builder.parse(new InputSource(inputData)); }
private DOMSource readDOMSource(InputStream body, HttpInputMessage inputMessage) throws IOException { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setFeature( "http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); documentBuilderFactory.setFeature( "http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); if (!isProcessExternalEntities()) { documentBuilder.setEntityResolver(NO_OP_ENTITY_RESOLVER); Document document = documentBuilder.parse(body); return new DOMSource(document);
private DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver((publicId, systemId) -> { // When asked to resolve external entities (such as a // DTD) we return an InputSource // with no data at the end, causing the parser to ignore // the DTD. return new InputSource(new StringReader("")); }); return db; }
/** * 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); } }
BufferedInputStream input = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); input = new BufferedInputStream(new FileInputStream(xmlConfigFile)); InputSource source = new InputSource(input); factory.setNamespaceAware(false); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { Document document = builder.parse(source); NodeList issues = document.getElementsByTagName(TAG_ISSUE); for (int i = 0, count = issues.getLength(); i < count; i++) {
/** * 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; }
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); docBuilder.setEntityResolver((publicId, systemId) -> { Document doc = docBuilder.parse(svgFontFile); doc.getDocumentElement().normalize();
/** * 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; }
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); docBuilder.setEntityResolver((publicId, systemId) -> { Document doc = docBuilder.parse(stream); doc.getDocumentElement().normalize();