class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> { private Exception exception; protected RSSFeed doInBackground(String... urls) { try { URL url = new URL(urls[0]); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader xmlreader = parser.getXMLReader(); RssHandler theRSSHandler = new RssHandler(); xmlreader.setContentHandler(theRSSHandler); InputSource is = new InputSource(url.openStream()); xmlreader.parse(is); return theRSSHandler.getFeed(); } catch (Exception e) { this.exception = e; return null; } } protected void onPostExecute(RSSFeed feed) { // TODO: check this.exception // TODO: do something with the feed } }
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); XMLReader xr = factory.newSAXParser().getXMLReader(); xr.setContentHandler(this); xr.setErrorHandler(this); xr.parse(new InputSource(r)); } finally { if (r != null){
private static void parseXml(InputStream in, String systemId, DefaultHandler handler) throws SAXException, IOException { InputSource inSrc = new InputSource(); inSrc.setSystemId(systemId); inSrc.setByteStream(toCloseIgnoring(in)); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); // Especially as we use dummy empty DTD-s XMLReader reader; try { reader = factory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException e) { // Not expected throw new RuntimeException("XML parser setup failed", e); } reader.setEntityResolver(new EmptyContentEntityResolver()); // To deal with referred DTD-s reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.parse(inSrc); }
public static XMLReader parse(InputStream in, ContentHandler handler) throws SAXException, IOException { XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setContentHandler(handler); reader.parse(new InputSource(in)); in.close(); return reader; }
/** * Set up before a parse. * * <p>Before every parse, check whether the parent is * non-null, and re-register the filter for all of the * events.</p> */ private void setupParse () { if (parent == null) { throw new NullPointerException("No parent for filter"); } parent.setEntityResolver(this); parent.setDTDHandler(this); parent.setContentHandler(this); parent.setErrorHandler(this); }
// the SAX way: XMLReader myReader = XMLReaderFactory.createXMLReader(); myReader.setContentHandler(handler); myReader.parse(new InputSource(new URL(url).openStream())); // or if you prefer DOM: DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new URL(url).openStream());
throw new BuildException(ex); buildFileName = url.toString(); } else { throw new BuildException("Source " + source.getClass().getName() inputSource = new InputSource(inputStream); if (uri != null) { inputSource.setSystemId(uri); Project.MSG_VERBOSE); parser.setContentHandler(handler); parser.setEntityResolver(handler); parser.setErrorHandler(handler); parser.setDTDHandler(handler); parser.parse(inputSource); } catch (SAXParseException exc) { Location location = new Location(exc.getSystemId(), exc.getLineNumber(), exc Throwable t = exc.getException(); if (t instanceof BuildException) { throw (BuildException) t; throw new BuildException(exc.getMessage(), t == null ? exc : t); } catch (FileNotFoundException exc) { throw new BuildException(exc);
protected void loadXML() throws IOException { try { SAXParserFactory factory = SAXParserFactory.newInstance(); XMLReader xr = factory.newSAXParser().getXMLReader(); rules = new HashMap<String, JSGFRule>(); GrXMLHandler handler = new GrXMLHandler(baseURL, rules, logger); xr.setContentHandler(handler); xr.setErrorHandler(handler); InputStream is = baseURL.openStream(); xr.parse(new InputSource(is)); is.close(); } catch (SAXParseException e) { String msg = "Error while parsing line " + e.getLineNumber() + " of " + baseURL + ": " + e.getMessage(); throw new IOException(msg); } catch (SAXException e) { throw new IOException("Problem with XML: " + e); } catch (ParserConfigurationException e) { throw new IOException(e.getMessage()); } return; }
javax.xml.parsers.SAXParserFactory.newInstance(); factory.setNamespaceAware( true ); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); factory.newSAXParser(); reader=jaxpParser.getXMLReader(); throw new org.xml.sax.SAXException( ex ); } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { throw new org.xml.sax.SAXException( ex1.toString() ); } catch( NoSuchMethodError ex2 ) { parent= XMLReaderFactory.createXMLReader(); else parent=reader; try parent.setFeature("http://xml.org/sax/features/namespace-prefixes", true); throw new org.xml.sax.SAXException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_CALL_PARSE, null)); //"parse can not be called if the ContentHandler has not been set!"); getParent().parse(input); Exception e = m_transformer.getExceptionThrown(); if(null != e)
reader = XMLReaderFactory.createXMLReader(); } catch (Exception e) { try { m_parserFactory = SAXParserFactory.newInstance(); m_parserFactory.setNamespaceAware(true); reader = m_parserFactory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException pce) { throw pce; // pass along pce reader.setFeature(NAMESPACES_FEATURE, true); reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false); } catch (SAXException se) { throw new SAXException(ex); } catch (FactoryConfigurationError ex1) { throw new SAXException(ex1.toString()); } catch (NoSuchMethodError ex2) { } catch (AbstractMethodError ame) {
@Test public void contentHandlerDocumentNamespacePrefixes() throws Exception { xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); handler = new DomContentHandler(result); expected = documentBuilder.parse(new InputSource(new StringReader(XML_1))); xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(new StringReader(XML_1))); assertThat("Invalid result", result, isSimilarTo(expected)); }
isource.setSystemId(baseID); XMLReader reader = null; javax.xml.parsers.SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser(); reader = jaxpParser.getXMLReader(); throw new org.xml.sax.SAXException(ex); throw new org.xml.sax.SAXException(ex1.toString()); reader = XMLReaderFactory.createXMLReader(); reader.setContentHandler(builder); reader.parse(isource); throw new TransformerConfigurationException(se.getMessage(), se);
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); reader.parse(new InputSource(new FileReader ("document.xml")));
/** * Indents the given XML String. * * @param r A reader on XML data * @param w A writer for the indented XML * * @throws IOException If an IOException occurs. * @throws SAXException If the XML is not well-formed. * @throws ParserConfigurationException If the parser could not be configured */ public static void indent(Reader r, Writer w) throws SAXException, IOException, ParserConfigurationException { // create the indenter XMLIndenter indenter = new XMLIndenter(w); // initialise the SAX framework SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); InputSource source = new InputSource(r); // parse the XML SAXParser parser = factory.newSAXParser(); parser.setProperty("http://xml.org/sax/features/external-general-entities", false); parser.setProperty("http://xml.org/sax/features/external-parameter-entities", false); parser.setProperty("http://apache.org/xml/features/disallow-doctype-decl", true); XMLReader xmlreader = parser.getXMLReader(); xmlreader.setContentHandler(indenter); xmlreader.parse(source); }
@SuppressWarnings("deprecation") // on JDK 9 private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException { try { XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); if (!isProcessExternalEntities()) { xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER); } byte[] bytes = StreamUtils.copyToByteArray(body); return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes))); } catch (SAXException ex) { throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage); } }
/** * Parse the content of the specified file using this Digester. Returns * the root element from the object stack (if any). * * @param file File containing the XML data to be parsed * * @exception IOException if an input/output error occurs * @exception SAXException if a parsing exception occurs */ public Object parse(File file) throws IOException, SAXException { if (file == null) { throw new IllegalArgumentException("File to parse is null"); } configure(); InputSource input = new InputSource(new FileInputStream(file)); input.setSystemId(file.toURI().toURL().toString()); getXMLReader().parse(input); cleanup(); return (root); } /**
private Source getSource(final InputStream is, final Resource resource) throws ParserConfigurationException, SAXException { // todo: is this comment still relevant ?? // FIXME: need to use a SAXSource as the source for the transform // so we can plug in our own entity resolver Source src = null; if (entityResolver != null) { if (getFactory().getFeature(SAXSource.FEATURE)) { final SAXParserFactory spFactory = SAXParserFactory.newInstance(); spFactory.setNamespaceAware(true); final XMLReader reader = spFactory.newSAXParser().getXMLReader(); reader.setEntityResolver(entityResolver); src = new SAXSource(reader, new InputSource(is)); } else { throw new IllegalStateException("xcatalog specified, but " + "parser doesn't support SAX"); } } else { // WARN: Don't use the StreamSource(File) ctor. It won't work with // xalan prior to 2.2 because of systemid bugs. src = new StreamSource(is); } // The line below is a hack: the system id must an URI, but it is not // cleat to get the URI of an resource, so just set the name of the // resource as a system id src.setSystemId(resourceToURI(resource)); return src; }
try { XMLReader parser = createXMLReader(); parser.setEntityResolver(this); parser.setErrorHandler(this); parser.setContentHandler(this); parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true); //NOI18N } catch (SAXException ignore) { InputSource in = new InputSource(); is = fo.getInputStream(); in.setByteStream(is); in.setSystemId(fo.toURL().toExternalForm()); customizeInputSource(in); parser.parse(in);
final SuppressionHandler handler = new SuppressionHandler(); final SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream12, schemaStream11, schemaStream10); final XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setErrorHandler(new SuppressionErrorHandler()); xmlReader.setContentHandler(handler); try (Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { final InputSource in = new InputSource(reader); xmlReader.parse(in); return handler.getSuppressionRules(); throw new SuppressionParseException(ex); } catch (SAXException ex) { if (ex.getMessage().contains("Cannot find the declaration of element 'suppressions'.")) { throw ex; } else {
/** * Creates a new instance. * @param publicIdToResourceNameMap maps public IDs to DTD resource names * @throws SAXException if an error occurs * @throws ParserConfigurationException if an error occurs */ protected XmlLoader(Map<String, String> publicIdToResourceNameMap) throws SAXException, ParserConfigurationException { this.publicIdToResourceNameMap = new HashMap<>(publicIdToResourceNameMap); final SAXParserFactory factory = SAXParserFactory.newInstance(); FeaturesForVerySecureJavaInstallations.addFeaturesForVerySecureJavaInstallations(factory); factory.setValidating(true); parser = factory.newSAXParser().getXMLReader(); parser.setContentHandler(this); parser.setEntityResolver(this); parser.setErrorHandler(this); }