List of usage examples for javax.xml.parsers SAXParserFactory setFeature
public abstract void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException;
From source file:MainClass.java
public static void main(String args[]) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = null;/*from ww w . j a va2 s . c om*/ spf.setNamespaceAware(true); spf.setValidating(true); try { spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true); parser = spf.newSAXParser(); System.out.println("Parser object is: " + parser); } catch (Exception e) { e.printStackTrace(System.err); } MySAXHandler handler = new MySAXHandler(); parser.parse(new InputSource(new StringReader(xmlString)), handler); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = null;/*w w w.j av a2 s. co m*/ spf.setNamespaceAware(true); spf.setValidating(true); try { spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true); parser = spf.newSAXParser(); System.out.println("Parser object is: " + parser); } catch (SAXException e) { e.printStackTrace(System.err); System.exit(1); } catch (ParserConfigurationException e) { e.printStackTrace(System.err); System.exit(1); } MySAXHandler handler = new MySAXHandler(); parser.parse(new InputSource(new StringReader(xmlString)), handler); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T readXMLFromString(Class<?> class1, String content) throws JAXBException, FileNotFoundException, SAXException, ParserConfigurationException { JAXBContext context = JAXBContext.newInstance(class1); Unmarshaller um = context.createUnmarshaller(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setFeature("http://xml.org/sax/features/validation", false); XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader(); try (StringReader reader = new StringReader(content)) { SAXSource source = new SAXSource(xr, new InputSource(reader)); T obj = (T) um.unmarshal(source); return obj; }/*from w w w. ja va 2s. co m*/ }
From source file:Main.java
/** * Returns a {@link SAXSource} for the provided {@link InputStream} that explicitly forfeit external DTD validation * * @param in the {@link InputStream} for the {@link SAXSource} * @return a {@link SAXSource} for the provided {@link InputStream} that explicitly forfeit external DTD validation * @throws SAXException if a SAX error occurs * @throws ParserConfigurationException if a configuration error occurs *//*from ww w .j av a 2s .c om*/ public static Source toNonValidatingSAXSource(InputStream in) throws SAXException, ParserConfigurationException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); InputSource inputSource = new InputSource(in); return new SAXSource(xmlReader, inputSource); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T readXML(Class<?> class1, File file) throws JAXBException, IOException, SAXException, ParserConfigurationException { JAXBContext context = JAXBContext.newInstance(class1); Unmarshaller um = context.createUnmarshaller(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setFeature("http://xml.org/sax/features/validation", false); XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader(); try (FileReader reader = new FileReader(file)) { SAXSource source = new SAXSource(xr, new InputSource(reader)); T obj = (T) um.unmarshal(source); return obj; }//from w w w.j a v a2 s . c om }
From source file:Main.java
/** * Create a new SAXParser which processes XML securely. * * @return a SAXParser/*from www . j a v a 2 s .c o m*/ */ public static SAXParser createSaxParser() { SAXParserFactory spf = SAXParserFactory.newInstance(); try { spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); return spf.newSAXParser(); } catch (ParserConfigurationException | SAXException e) { throw new IllegalStateException(e); } }
From source file:Main.java
/** * Helper method to unmarshall a xml doc * @see http://docs.oracle.com/javase/tutorial/jaxb/intro/basic.html * http://jaxb.java.net/nonav/2.2.6/docs/ch03.html#unmarshalling * this uses <T> JAXBElement<T> unmarshal(Source source, Class<T> declaredType) throws JAXBException/*from ww w . ja v a 2 s . co m*/ * */ /* public static <T> T unmarshall(Class<T> docClass, InputStream inputStream) throws JAXBException{ String packageName = docClass.getPackage().getName(); JAXBContext jc = JAXBContext.newInstance( packageName ); Unmarshaller u = jc.createUnmarshaller(); JAXBElement<T> root = u.unmarshal(new StreamSource(inputStream),docClass); return root.getValue(); }*/ public static <T> T unmarshall(Class<T> docClass, InputStream inputStream) throws JAXBException, ParserConfigurationException, SAXException { String packageName = docClass.getPackage().getName(); JAXBContext jc = JAXBContext.newInstance(packageName); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/validation/schema", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); InputSource inputSource = new InputSource(inputStream); SAXSource source = new SAXSource(xmlReader, inputSource); Unmarshaller u = jc.createUnmarshaller(); JAXBElement<T> root = u.unmarshal(source, docClass); return root.getValue(); }
From source file:Main.java
public static SAXParser getParser() throws ParserConfigurationException, SAXException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);//from w w w . j a v a2 s .co m factory.setValidating(false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); return factory.newSAXParser(); }
From source file:mondrian.util.XmlParserFactoryProducer.java
/** * Creates an instance of {@link SAXParserFactory} class with enabled {@link XMLConstants#FEATURE_SECURE_PROCESSING} property. * Enabling this feature prevents from some XXE attacks (e.g. XML bomb) * * @throws ParserConfigurationException if a parser cannot * be created which satisfies the requested configuration. * * @throws SAXNotRecognizedException When the underlying XMLReader does * not recognize the property name. * * @throws SAXNotSupportedException When the underlying XMLReader * recognizes the property name but doesn't support the * property./*from w ww . j a v a 2s .c o m*/ */ public static SAXParserFactory createSecureSAXParserFactory() throws SAXNotSupportedException, SAXNotRecognizedException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); return factory; }
From source file:Main.java
/** * Constructs a secure SAX Parser./*from w w w. j a va 2s . com*/ * * @return a SAX Parser * @throws ParserConfigurationException thrown if there is a parser * configuration exception * @throws SAXNotRecognizedException thrown if there is an unrecognized * feature * @throws SAXNotSupportedException thrown if there is a non-supported * feature * @throws SAXException is thrown if there is a SAXException */ public static SAXParser buildSecureSaxParser() throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException, SAXException { final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); return factory.newSAXParser(); }