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 } }
public static XMLReader createSafeSaxReader(SAXParserFactory saxParserFactory, ContentHandler contentHandler) throws SAXException, ParserConfigurationException { if (saxParserFactory == null) { throw new IllegalArgumentException("The provided SAX parser factory cannot be null"); } if (contentHandler == null) { throw new IllegalArgumentException("The provided SAX content handler cannot be null"); } saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); SAXParser saxParser = saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(contentHandler); return xmlReader; } }
/** * Create new {@link javax.xml.transform.sax.SAXSource} for a given entity input stream. * * @param spf SAX parser factory to be used to create the SAX source. * @param entityStream entity input stream. * @return new {@link javax.xml.transform.sax.SAXSource} representing the entity input stream. * @throws JAXBException in case SAX source creation fails. */ protected static SAXSource getSAXSource(SAXParserFactory spf, InputStream entityStream) throws JAXBException { try { return new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(entityStream)); } catch (Exception ex) { throw new JAXBException("Error creating SAXSource", ex); } }
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();
public final List<BitextPatternRule> getRules(InputStream is, String filename) throws IOException { List<BitextPatternRule> rules; try { BitextPatternRuleHandler handler = new BitextPatternRuleHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); saxParser.parse(is, handler); rules = handler.getBitextRules(); return rules; } catch (Exception e) { throw new IOException("Cannot load or parse '" + filename + "'", e); } }
protected final SAXParser createParser(SAXParserFactory parserFactory) throws ParserConfigurationException, SAXException { parserFactory.setNamespaceAware(true); final SAXParser parser = parserFactory.newSAXParser(); final XMLReader reader = parser.getXMLReader(); //reader.setProperty("http://xml.org/sax/properties/lexical-handler", this); //$NON-NLS-1$ // disable DTD validation (bug 63625) try { // be sure validation is "off" or the feature to ignore DTD's will not apply reader.setFeature("http://xml.org/sax/features/validation", false); //$NON-NLS-1$ reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$ } catch (SAXNotRecognizedException e) { // not a big deal if the parser does not recognize the features } catch (SAXNotSupportedException e) { // not a big deal if the parser does not support the features } return parser; }
/** * Parse the content given {@link org.xml.sax.InputSource} * as XML using the specified * {@link org.xml.sax.helpers.DefaultHandler}. * * @param is The InputSource containing the content to be parsed. * @param dh The SAX DefaultHandler to use. * * @throws IllegalArgumentException If the <code>InputSource</code> object * is <code>null</code>. * @throws IOException If any IO errors occur. * @throws SAXException If any SAX errors occur during processing. * * @see org.xml.sax.DocumentHandler */ public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("InputSource cannot be null"); } XMLReader reader = this.getXMLReader(); if (dh != null) { reader.setContentHandler(dh); reader.setEntityResolver(dh); reader.setErrorHandler(dh); reader.setDTDHandler(dh); } reader.parse(is); }
/** * Creates a new SAX XMLReader, with sensible defaults */ public static synchronized XMLReader newXMLReader() throws SAXException, ParserConfigurationException { XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader(); xmlReader.setEntityResolver(IGNORING_ENTITY_RESOLVER); trySetSAXFeature(xmlReader, XMLConstants.FEATURE_SECURE_PROCESSING, true); trySetXercesSecurityManager(xmlReader); return xmlReader; }
/** * Creates a new <code>LoadXMLAction</code> instance. * * @param aParent the parent frame * @param aModel the model to add events to * @exception SAXException if an error occurs * @throws ParserConfigurationException if an error occurs */ LoadXMLAction(JFrame aParent, MyTableModel aModel) throws SAXException, ParserConfigurationException { mParent = aParent; mHandler = new XMLFileHandler(aModel); mParser = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); mParser.setContentHandler(mHandler); }
/** * Unmarshal a jaxb bean into a type of {@code resultClass} from the given {@code inputStream}. * * @param inputStream Input stream that contains input xml that should be processed. * @param saxParserFactory Sax parser factory for unmarshalling xml. * @param resultClass Class of the result bean into which the content of {@code inputStream} should be unmarshalled. * @param <T> Type of the result jaxb bean. * @return Unmarshalled jaxb bean. * * @throws JAXBException In case of jaxb problem. * @throws ParserConfigurationException In case of problem with parsing xml. * @throws SAXException In case of problem with parsing xml. */ public static <T> T unmarshall(InputStream inputStream, SAXParserFactory saxParserFactory, Class<T> resultClass) throws JAXBException, ParserConfigurationException, SAXException { JAXBContext jaxbContext = null; try { jaxbContext = JAXBContext.newInstance(resultClass); } catch (JAXBException ex) { throw new ProcessingException(LocalizationMessages.ERROR_WADL_JAXB_CONTEXT(), ex); } final SAXParser saxParser = saxParserFactory.newSAXParser(); SAXSource source = new SAXSource(saxParser.getXMLReader(), new InputSource(inputStream)); final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); final Object result = unmarshaller.unmarshal(source); return resultClass.cast(result); }
final HintHandler handler = new HintHandler(); final SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream13, schemaStream12, schemaStream11); final XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setErrorHandler(new HintErrorHandler()); xmlReader.setContentHandler(handler); try (Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { final InputSource in = new InputSource(reader); xmlReader.parse(in); this.hintRules = handler.getHintRules(); this.vendorDuplicatingHintRules = handler.getVendorDuplicatingHintRules();
/** * @param is stream with the XML rules * @param filename used only for verbose exception message - should refer to where the stream comes from */ public final List<AbstractPatternRule> getRules(InputStream is, String filename) throws IOException { try { PatternRuleHandler handler = new PatternRuleHandler(filename); handler.setRelaxedMode(relaxedMode); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); Tools.setPasswordAuthenticator(); saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); saxParser.parse(is, handler); return handler.getRules(); } catch (Exception e) { throw new IOException("Cannot load or parse input stream of '" + filename + "'", e); } }
SAXParserFactory spf = SAXParserFactoryImpl.newInstance(); spf.setNamespaceAware(true); spf.setValidating(false); spf.setFeature("http://xml.org/sax/features/validation", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); SAXParser sp = spf.newSAXParser() ; Source src = new SAXSource ( sp.getXMLReader(), new InputSource( input.getAbsolutePath() ) ) ; String resultFileName = input.getAbsolutePath().replaceAll(".xml$", ".cooked.xml" ) ; Result result = new StreamResult( new File (resultFileName) ) ; TransformerFactory tf = TransformerFactory.newInstance(); Source xsltSource = new StreamSource( new File ( COOKER_XSL ) ); xsl = tf.newTransformer( xsltSource ) ; xsl.setParameter( "srcDocumentName", input.getName() ) ; xsl.setParameter( "srcDocumentPath", input.getAbsolutePath() ) ; xsl.transform(src, result );
/** * Creates a new SAX XMLReader, with sensible defaults */ public static synchronized XMLReader newXMLReader() throws SAXException, ParserConfigurationException { XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader(); xmlReader.setEntityResolver(IGNORING_ENTITY_RESOLVER); trySetSAXFeature(xmlReader, XMLConstants.FEATURE_SECURE_PROCESSING); trySetXercesSecurityManager(xmlReader); return xmlReader; }
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")));
/** * Unmarshal a jaxb bean into a type of {@code resultClass} from the given {@code inputStream}. * * @param inputStream Input stream that contains input xml that should be processed. * @param saxParserFactory Sax parser factory for unmarshalling xml. * @param resultClass Class of the result bean into which the content of {@code inputStream} should be unmarshalled. * @param <T> Type of the result jaxb bean. * @return Unmarshalled jaxb bean. * * @throws JAXBException In case of jaxb problem. * @throws ParserConfigurationException In case of problem with parsing xml. * @throws SAXException In case of problem with parsing xml. */ public static <T> T unmarshall(InputStream inputStream, SAXParserFactory saxParserFactory, Class<T> resultClass) throws JAXBException, ParserConfigurationException, SAXException { JAXBContext jaxbContext = null; try { jaxbContext = JAXBContext.newInstance(resultClass); } catch (JAXBException ex) { throw new ProcessingException(LocalizationMessages.ERROR_WADL_JAXB_CONTEXT(), ex); } final SAXParser saxParser = saxParserFactory.newSAXParser(); SAXSource source = new SAXSource(saxParser.getXMLReader(), new InputSource(inputStream)); final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); final Object result = unmarshaller.unmarshal(source); return resultClass.cast(result); }
final PomHandler handler = new PomHandler(); final SAXParser saxParser = XmlUtils.buildSecureSaxParser(); final XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(handler); final String charsetName = bom == null ? defaultEncoding : bom.getCharsetName(); final Reader reader = new InputStreamReader(bomStream, charsetName); final InputSource in = new InputSource(reader); xmlReader.parse(in); return handler.getModel(); } catch (ParserConfigurationException | SAXException | FileNotFoundException ex) {
public final List<AbstractPatternRule> getRules(InputStream stream, Language textLanguage, Language motherTongue) throws ParserConfigurationException, SAXException, IOException { FalseFriendRuleHandler handler = new FalseFriendRuleHandler( textLanguage, motherTongue); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.getXMLReader().setFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd", false); saxParser.parse(stream, handler); List<AbstractPatternRule> rules = handler.getRules(); // Add suggestions to each rule: ResourceBundle messages = ResourceBundle.getBundle( JLanguageTool.MESSAGE_BUNDLE, motherTongue.getLocale()); MessageFormat msgFormat = new MessageFormat(messages.getString("false_friend_suggestion")); for (AbstractPatternRule rule : rules) { List<String> suggestions = handler.getSuggestionMap().get(rule.getId()); if (suggestions != null) { String[] msg = { formatSuggestions(suggestions) }; rule.setMessage(rule.getMessage() + " " + msgFormat.format(msg)); } } return rules; }
/** * Creates a <code>XmlParser</code>. * * @param validating <code>true</code> if the parser should validate documents as they are parsed; false otherwise. * @param namespaceAware <code>true</code> if the parser should provide support for XML namespaces; <code>false</code> otherwise. * @param allowDocTypeDeclaration <code>true</code> if the parser should provide support for DOCTYPE declarations; <code>false</code> otherwise. * @throws ParserConfigurationException if no parser which satisfies the requested configuration can be created. * @throws SAXException for SAX errors. */ public XmlParser(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration) throws ParserConfigurationException, SAXException { SAXParserFactory factory = FactorySupport.createSaxParserFactory(); factory.setNamespaceAware(namespaceAware); this.namespaceAware = namespaceAware; factory.setValidating(validating); setQuietly(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true); setQuietly(factory, "http://apache.org/xml/features/disallow-doctype-decl", !allowDocTypeDeclaration); reader = factory.newSAXParser().getXMLReader(); }
/** * 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); }