/** * Configure schema validation as recommended by the JAXP 1.2 spec. * The <code>properties</code> object may contains information about * the schema local and language. * @param properties parser optional info */ private static void configureOldXerces(SAXParser parser, Properties properties) throws ParserConfigurationException, SAXNotSupportedException { String schemaLocation = (String)properties.get("schemaLocation"); String schemaLanguage = (String)properties.get("schemaLanguage"); try{ if (schemaLocation != null) { parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage); parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation); } } catch (SAXNotRecognizedException e){ log.info(parser.getClass().getName() + ": " + e.getMessage() + " not supported."); } }
private void setSchemaValidatorFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { try { fSAXParser.fSchemaValidator.setFeature(name, value); } // This should never be thrown from the schema validator. catch (XMLConfigurationException e) { String identifier = e.getIdentifier(); if (e.getType() == Status.NOT_RECOGNIZED) { throw new SAXNotRecognizedException( SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "feature-not-recognized", new Object [] {identifier})); } else { throw new SAXNotSupportedException( SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "feature-not-supported", new Object [] {identifier})); } } }
public static SAXParserFactory newSAXParserFactory(boolean disableSecurity) { SAXParserFactory factory = SAXParserFactory.newInstance(); String featureToSet = XMLConstants.FEATURE_SECURE_PROCESSING; try { boolean securityOn = !xmlSecurityDisabled(disableSecurity); factory.setFeature(featureToSet, securityOn); factory.setNamespaceAware(true); if (securityOn) { featureToSet = DISALLOW_DOCTYPE_DECL; factory.setFeature(featureToSet, true); featureToSet = EXTERNAL_GE; factory.setFeature(featureToSet, false); featureToSet = EXTERNAL_PE; factory.setFeature(featureToSet, false); featureToSet = LOAD_EXTERNAL_DTD; factory.setFeature(featureToSet, false); } } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) { LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support "+featureToSet+" feature!", new Object[]{factory.getClass().getName()}); } return factory; }
/** * Create a <code>SAXParser</code> configured to support XML Scheman and DTD * @param properties parser specific properties/features * @return an XML Schema/DTD enabled <code>SAXParser</code> */ public static SAXParser newSAXParser(Properties properties) throws ParserConfigurationException, SAXException, SAXNotRecognizedException{ SAXParserFactory factory = (SAXParserFactory)properties.get("SAXParserFactory"); SAXParser parser = factory.newSAXParser(); String schemaLocation = (String)properties.get("schemaLocation"); String schemaLanguage = (String)properties.get("schemaLanguage"); try{ if (schemaLocation != null) { parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage); parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation); } } catch (SAXNotRecognizedException e){ log.info(parser.getClass().getName() + ": " + e.getMessage() + " not supported."); } return parser; }
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { if (name.equals(Properties.LEXICAL_HANDLER_PROPERTY)) { return getLexicalHandler(); } else if (name.equals(Properties.DTD_DECLARATION_HANDLER_PROPERTY)) { return getDeclHandler(); } else if (name.equals(FastInfosetReader.EXTERNAL_VOCABULARIES_PROPERTY)) { return getExternalVocabularies(); } else if (name.equals(FastInfosetReader.REGISTERED_ENCODING_ALGORITHMS_PROPERTY)) { return getRegisteredEncodingAlgorithms(); } else if (name.equals(FastInfosetReader.ENCODING_ALGORITHM_CONTENT_HANDLER_PROPERTY)) { return getEncodingAlgorithmContentHandler(); } else if (name.equals(FastInfosetReader.PRIMITIVE_TYPE_CONTENT_HANDLER_PROPERTY)) { return getPrimitiveTypeContentHandler(); } else { throw new SAXNotRecognizedException(CommonResourceBundle.getInstance(). getString("message.propertyNotRecognized", new Object[]{name})); } }
/** * Return the SAXParserFactory we will use, creating one if necessary. * * @throws ParserConfigurationException * @throws SAXNotSupportedException * @throws SAXNotRecognizedException */ public SAXParserFactory getFactory() throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException { if (factory == null) { factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(namespaceAware); // Preserve xmlns attributes if (namespaceAware) { factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true); } factory.setValidating(validating); if (validating) { // Enable DTD validation factory.setFeature("http://xml.org/sax/features/validation", true); // Enable schema validation factory.setFeature("http://apache.org/xml/features/validation/schema", true); } } return (factory); }
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { if (name.equals(Features.NAMESPACES_FEATURE)) { if (!value) { throw new SAXNotSupportedException(name + ":" + value); } } else if (name.equals(Features.NAMESPACE_PREFIXES_FEATURE)) { _namespacePrefixesFeature = value; } else if (name.equals(Features.EXTERNAL_GENERAL_ENTITIES)) { // ignore } else if (name.equals(Features.EXTERNAL_PARAMETER_ENTITIES)) { // ignore } else if (name.equals(Features.STRING_INTERNING_FEATURE)) { if (value != _stringInterningFeature) { throw new SAXNotSupportedException(name + ":" + value); } } else { throw new SAXNotRecognizedException( "Feature not supported: " + name); } }
/** * Set a feature flag for the parser. * * <p>The only features recognized are namespaces and * namespace-prefixes.</p> * * @param name The feature name, as a complete URI. * @param value The requested feature value. * @exception SAXNotRecognizedException If the feature * can't be assigned or retrieved. * @exception SAXNotSupportedException If the feature * can't be assigned that value. * @see org.xml.sax.XMLReader#setFeature */ public void setFeature (String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { if (name.equals(NAMESPACES)) { checkNotParsing("feature", name); namespaces = value; if (!namespaces && !prefixes) { prefixes = true; } } else if (name.equals(NAMESPACE_PREFIXES)) { checkNotParsing("feature", name); prefixes = value; if (!prefixes && !namespaces) { namespaces = true; } } else if (name.equals(XMLNS_URIs)) { checkNotParsing("feature", name); uris = value; } else { throw new SAXNotRecognizedException("Feature: " + name); } }
@Override public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { if (name == null) { throw new NullPointerException(); } try { return fComponentManager.getFeature(name); } catch (XMLConfigurationException e) { final String identifier = e.getIdentifier(); final String key = e.getType() == Status.NOT_RECOGNIZED ? "feature-not-recognized" : "feature-not-supported"; throw new SAXNotRecognizedException( SAXMessageFormatter.formatMessage(fComponentManager.getLocale(), key, new Object [] {identifier})); } }
private void setFeature(SAXParserFactory factory, String feature, boolean enable) { try { if (ADDITIONAL_FEATURE_X_INCLUDE_AWARE.equals(feature)) { factory.setXIncludeAware(enable); } else if (!ADDITIONAL_FEATURE_EXPAND_ENTITY_REFERENCES.equals(feature)) // Does not exist on SAXParserFactory { factory.setFeature(feature, enable); } debug(debugCounter+" SAXParserFactory "+feature+" "+enable); } catch (ParserConfigurationException | SAXNotSupportedException | SAXNotRecognizedException e) { logConfigurationFailure(factory.getClass().getName(), feature, e); } }
/** * Configure the parser to allow access to DTDs on the file system. */ private static void enableFileAccess(SAXParser parser) throws SAXNotSupportedException { try { parser.setProperty("http://javax.xml.XMLConstants/property/accessExternalDTD", "file"); } catch (SAXNotRecognizedException ignore) { // property requires >= JAXP 1.5 } }
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { if (name == null) { throw new NullPointerException(); } //Support current-element-node; return current node if DOMSource is used. if (CURRENT_ELEMENT_NODE.equals(name)) { return (fDOMValidatorHelper != null) ? fDOMValidatorHelper.getCurrentElement() : null; } try { return fComponentManager.getProperty(name); } catch (XMLConfigurationException e) { final String identifier = e.getIdentifier(); final String key = e.getType() == Status.NOT_RECOGNIZED ? "property-not-recognized" : "property-not-supported"; throw new SAXNotRecognizedException( SAXMessageFormatter.formatMessage(fComponentManager.getLocale(), key, new Object [] {identifier})); } }
public void setProperty(String name, Object object) throws SAXNotRecognizedException, SAXNotSupportedException { if (name == null) { throw new NullPointerException(); } try { fComponentManager.setProperty(name, object); } catch (XMLConfigurationException e) { final String identifier = e.getIdentifier(); final String key = e.getType() == Status.NOT_RECOGNIZED ? "property-not-recognized" : "property-not-supported"; throw new SAXNotRecognizedException( SAXMessageFormatter.formatMessage(fComponentManager.getLocale(), key, new Object [] {identifier})); } }
private static XmlParser createParser() { try { XmlParser parser = new XmlParser(false, true, true); try { // If not set for >= JAXP 1.5 / Java8 won't allow referencing DTDs, e.g. // using http URLs, because Groovy's XmlParser requests FEATURE_SECURE_PROCESSING parser.setProperty(ACCESS_EXTERNAL_DTD, ALLOW_ANY_EXTERNAL_DTD); } catch (SAXNotRecognizedException ignore) { // property requires >= JAXP 1.5 / Java8 } return parser; } catch (Exception ex) { throw new UncheckedException(ex); } }
@Test public void test1() throws SAXException { try { ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler(); validatorHandler.getProperty("unknown1234"); Assert.fail("SAXNotRecognizedException was not thrown."); } catch (SAXNotRecognizedException e) { } }
/** * Configure schema validation as recommended by the Xerces spec. * Both DTD and Schema validation will be enabled simultaneously. * @param factory SAXParserFactory to be configured */ private static void configureXerces(SAXParserFactory factory) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException { factory.setFeature(XERCES_DYNAMIC, true); factory.setFeature(XERCES_SCHEMA, true); }
public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { if (name.equals(NAMESPACES)) { namespaces = value; } else if (name.equals(NAMESPACE_PREFIXES)) { namespacePrefixes = value; } else { throw new SAXNotRecognizedException(); } }
/** * Create a new <code>SAXParser</code> * @param properties (logical) properties to be set on parser * @return a <code>SAXParser</code> configured based on the underlying * parser implementation. */ public static SAXParser newSAXParser(Properties properties) throws ParserConfigurationException, SAXException, SAXNotRecognizedException, SAXNotSupportedException { if (isXercesUsed){ return XercesParser.newSAXParser(properties); } else { return GenericParser.newSAXParser(properties); } }
/** * getFeature with a unknown feature name throws SAXNotRecognizedException. * * @throws Exception If any errors occur. */ @Test(expectedExceptions = SAXNotRecognizedException.class) public void featureNE01() throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.newSAXParser().getXMLReader().getFeature("no-meaning-feature"); }
private void setFeatures( Map<String, Boolean> features) throws SAXNotSupportedException, SAXNotRecognizedException { if (features != null) { for (Map.Entry<String, Boolean> entry : features.entrySet()) { domParser.setFeature(entry.getKey(), entry.getValue()); } } }
public static void main(String[] args) throws Throwable { System.setSecurityManager(new SecurityManager()); try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.setValidating(true); SAXParser sp = spf.newSAXParser(); // The following line shouldn't throw a // java.security.AccessControlException. sp.setProperty("foo", "bar"); } catch (SAXNotRecognizedException e) { // Ignore this expected exception. } }
@Override public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { if(name.equals(PREFIX_FEATURE)) { this.namespacePrefixes = value; return; } if(name.equals(NAMESPACE_FEATURE) && value) return; super.setFeature(name, value); }
/** * Query the state of a feature. * * Query the current state of any feature in a SAX2 parser. The * parser might not recognize the feature. * * @param featureId The unique identifier (URI) of the feature * being set. * @return The current state of the feature. * @exception org.xml.sax.SAXNotRecognizedException If the * requested feature is not known. * @exception SAXNotSupportedException If the * requested feature is known but not supported. */ public boolean getFeature(String featureId) throws SAXNotRecognizedException, SAXNotSupportedException { try { // http://xml.org/sax/features/use-entity-resolver2 // controls whether the methods of an object implementing // org.xml.sax.ext.EntityResolver2 will be used by the parser. // if (featureId.equals(USE_ENTITY_RESOLVER2)) { return fUseEntityResolver2; } // // Default handling // return fConfiguration.getFeature(featureId); } catch (XMLConfigurationException e) { String identifier = e.getIdentifier(); if (e.getType() == Status.NOT_RECOGNIZED) { throw new SAXNotRecognizedException( SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "feature-not-recognized", new Object [] {identifier})); } else { throw new SAXNotSupportedException( SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "feature-not-supported", new Object [] {identifier})); } } }
@Test(expectedExceptions = NullPointerException.class) public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { SchemaFactory sf = newSchemaFactory(); assertNotNull(sf); sf.getFeature(null); }
public void setProperty(String name, Object value) throws SAXNotRecognizedException { if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { this.lexicalHandler = (LexicalHandler)value; return; } throw new SAXNotRecognizedException(name); }
public boolean getFeature (String name) throws SAXNotRecognizedException, SAXNotSupportedException { Boolean b = (Boolean)theFeatures.get(name); if (b == null) { throw new SAXNotRecognizedException("Unknown feature " + name); } return b.booleanValue(); }
@Test(expectedExceptions = NullPointerException.class) public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { ValidatorHandler validatorHandler = getValidatorHandler(); assertNotNull(validatorHandler); validatorHandler.getFeature(null); }
private void setFeatures(Hashtable features) throws SAXNotSupportedException, SAXNotRecognizedException { if (features != null) { Iterator entries = features.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); String feature = (String) entry.getKey(); boolean value = ((Boolean) entry.getValue()).booleanValue(); domParser.setFeature(feature, value); } } }
/** * Sets the XMLReader instance with the specified property if the the * property is supported, ignores error if not, issues a warning if so * requested. * * @param reader an XMLReader instance * @param property the name of the property * @param value the value of the property * @param warn a flag indicating whether a warning should be issued */ public static void setXMLReaderPropertyIfSupport(XMLReader reader, String property, Object value, boolean warn) { try { reader.setProperty(property, value); } catch (SAXNotRecognizedException | SAXNotSupportedException e) { if (warn) { XMLSecurityManager.printWarning(reader.getClass().getName(), property, e); } } }
/** * Set any features of our XMLReader based on any features set on the * SAXParserFactory. * * XXX Does not handle possible conflicts between SAX feature names and * JAXP specific feature names, eg. SAXParserFactory.isValidating() */ private void setFeatures(Map<String, Boolean> features) throws SAXNotSupportedException, SAXNotRecognizedException { if (features != null) { for (Map.Entry<String, Boolean> entry : features.entrySet()) { xmlReader.setFeature0(entry.getKey(), entry.getValue()); } } }
@Test(expectedExceptions = NullPointerException.class) public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { ValidatorHandler validatorHandler = getValidatorHandler(); assertNotNull(validatorHandler); validatorHandler.getProperty(null); }
@Override public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException { }
/** * This class is only used internally so this method should never * be called. */ public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { }
@Test(expectedExceptions = SAXNotRecognizedException.class) public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { ValidatorHandler validatorHandler = getValidatorHandler(); validatorHandler.getProperty(FEATURE_NAME); }