List of usage examples for javax.xml.parsers SAXParserFactory setValidating
public void setValidating(boolean validating)
From source file:Main.java
public static final XMLReader newXMLReader() throws SAXException, ParserConfigurationException { final SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false); spf.setNamespaceAware(true);// w w w. j a v a 2 s . c o m return spf.newSAXParser().getXMLReader(); }
From source file:Main.java
/** * To get a SAXParserFactory instance which is then used to obtain an {@link SAXParser} instance. * /*from w ww .j a v a 2 s. c o m*/ * @param namespaces Whether to provide namespace support. * @param validation Whether to produce a validating SAX parser. * @return the SAXParserFactory for further use */ public static SAXParserFactory getSAXParserFactory(final boolean validation, final boolean namespaces) { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(namespaces); factory.setValidating(validation); return factory; }
From source file:Main.java
/** Create a SAX parser from the JAXP factory. * The result can be used to parse XML files. * /*from www .ja v a2s. c om*/ * <p>See class Javadoc for hints on setting an entity resolver. * This parser has its entity resolver set to the system entity resolver chain. * * @param validate if true, a validating parser is returned * @param namespaceAware if true, a namespace aware parser is returned * * @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type. * @throws SAXException if a parser fulfilling given parameters can not be created * * @return XMLReader configured according to passed parameters */ public static XMLReader createXMLReader(boolean validate, boolean namespaceAware) throws SAXException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); try { return factory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException ex) { throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N } }
From source file:Main.java
/** * Creates a non-validating, non-namespace-aware {@link XMLReader} using the specified * {@link ContentHandler}./* w w w .jav a 2 s . com*/ * * <p>If the given {@link ContentHandler} is <code>null</code>, the {@link XMLReader} is * not initialised. * * @param handler The content handler to use. * * @return The requested {@link XMLReader} * * @throws SAXException Should a SAX exception occur * @throws ParserConfigurationException Should a parser config exception occur */ public static XMLReader makeXMLReader(ContentHandler handler) throws SAXException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); XMLReader reader = factory.newSAXParser().getXMLReader(); if (handler != null) { reader.setContentHandler(handler); } return reader; }
From source file:Main.java
public static SAXParserFactory createParserFactory() { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);// ww w . j a va 2 s . c o m factory.setValidating(false); try { factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); } catch (ParserConfigurationException ex) { } catch (SAXNotRecognizedException ex) { } catch (SAXNotSupportedException ex) { } try { factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (ParserConfigurationException ex) { } catch (SAXNotRecognizedException ex) { } catch (SAXNotSupportedException ex) { } return factory; }
From source file:Main.java
/** * Convenience method: creates and returns a SAXParser. * //from www . ja v a 2 s . c o m * @param namespaceAware specifies whether the parser produced * by this code will provide support for XML namespaces * @param validating specifies whether the parser produced by * this code will validate documents against their DTD * @param xIncludeAware specifies whether the parser produced by * this code will process XIncludes * @return newly created SAXParser * @exception ParserConfigurationException if a parser cannot be created * which satisfies the requested configuration * @exception SAXException for SAX errors */ public static SAXParser newSAXParser(boolean namespaceAware, boolean validating, boolean xIncludeAware) throws ParserConfigurationException, SAXException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(namespaceAware); factory.setValidating(validating); factory.setXIncludeAware(xIncludeAware); // For Xerces which otherwise, does not support "x-MacRoman". try { factory.setFeature("http://apache.org/xml/features/allow-java-encodings", true); } catch (Exception ignored) { } return factory.newSAXParser(); }
From source file:Main.java
public static void saxParserValidation(InputStream streamDaValidare, InputStream inputSchema, DefaultHandler handler) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);/*from w w w .j a v a 2 s. co m*/ if (inputSchema != null) { factory.setValidating(true); } else { factory.setValidating(false); } SAXParser parser = factory.newSAXParser(); if (inputSchema != null) { parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); parser.setProperty(JAXP_SCHEMA_SOURCE, inputSchema); } parser.parse(streamDaValidare, handler); }
From source file:com.lucidworks.hadoop.ingest.util.EmptyEntityResolver.java
/** * Configures the given {@link SAXParserFactory} to do secure XML processing of untrusted sources. * It is required to also set {@link #SAX_INSTANCE} on the created {@link org.xml.sax.XMLReader}. * * @see #SAX_INSTANCE//from w w w . j a va2s .c o m */ public static void configureSAXParserFactory(SAXParserFactory saxFactory) { // don't enable validation of DTDs: saxFactory.setValidating(false); // enable secure processing: trySetSAXFeature(saxFactory, XMLConstants.FEATURE_SECURE_PROCESSING, true); }
From source file:Main.java
/** * Constructs a validating secure SAX Parser. * * @param schemaStream the schema to validate the XML against * @return a SAX Parser/*from ww w . j a v a 2 s.co m*/ * @throws ParserConfigurationException is 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(InputStream schemaStream) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException, SAXException { final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); //setting the following unfortunately breaks reading the old suppression files (version 1). //factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); final SAXParser saxParser = factory.newSAXParser(); saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaStream); return saxParser; }
From source file:com.mindquarry.desktop.I18N.java
protected static Map<String, String> initTranslationMap(String fileBase, String fileSuffix) { try {/* w ww. j ava 2s . c om*/ SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setValidating(false); SAXParser parser = parserFactory.newSAXParser(); XMLReader reader = parser.getXMLReader(); TranslationMessageParser translationParser = new TranslationMessageParser(); reader.setContentHandler(translationParser); reader.setErrorHandler(translationParser); // TODO: use "xx_YY" if available, use "xx" otherwise: String transFile = fileBase + Locale.getDefault().getLanguage() + fileSuffix; InputStream is = I18N.class.getResourceAsStream(transFile); if (is == null) { // no translation available for this language log.debug("No translation file available for language: " + Locale.getDefault().getLanguage()); return new HashMap<String, String>(); } log.debug("Loading translation file " + transFile + " from JAR"); reader.parse(new InputSource(is)); return translationParser.getMap(); } catch (Exception e) { throw new RuntimeException(e.toString(), e); } }