@Override public Void answer(InvocationOnMock invocation) { String name = (String) invocation.getArguments()[0]; Object value = invocation.getArguments()[1]; try { if (propertySetter instanceof SchemaFactory) { ((SchemaFactory) propertySetter).setProperty(name, value); } else if (propertySetter instanceof Validator) { ((Validator) propertySetter).setProperty(name, value); } else if (propertySetter instanceof TransformerFactory) { ((TransformerFactory) propertySetter).setAttribute(name, value); } else { throw new IllegalArgumentException("Invalid property setter."); } } catch (Exception e) { exception = e; } return null; } }
@Test public void validatorProperties() throws Exception { SetPropertyAnswer setPropertyAnswer = new SetPropertyAnswer(schemaFactory.newSchema().newValidator()); doAnswer(setPropertyAnswer).when(validatorWrapper).setProperty(anyString(), anyObject()); defaultXMLSecureFactories.configureValidator(validatorWrapper); assertThat(setPropertyAnswer.exception, is(nullValue())); for (String property : VALIDATOR_PROPERTIES) { verify(validatorWrapper).setProperty(property, ""); } }
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); }catch (SAXNotRecognizedException ex){ ourLog.warn("Jaxp 1.5 Support not found.",ex);
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema myschema = factory.newSchema(); Validator validator = myschema.newValidator(); try { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); validator.validate(new StreamSource(new StringReader(xmlString))); } catch ...
public void configureValidator(Validator validator) { if (!externalEntities && !expandEntities) { try { validator.setProperty(ACCESS_EXTERNAL_STYLESHEET, ""); validator.setProperty(ACCESS_EXTERNAL_DTD, ""); } catch (Exception e) { logConfigurationWarning("Validator", validator.getClass().getName(), e); } } }
/** * Disables {@link XMLConstants#ACCESS_EXTERNAL_DTD} and {@link XMLConstants#ACCESS_EXTERNAL_DTD} features if * {@link DefaultXMLSecureFactories#externalEntities} and {@link DefaultXMLSecureFactories#expandEntities} are false. * * @see Validator#setProperty(String, Object) for more information about supported properties. * * @param validator the {@link Validator} to configure. */ public void configureValidator(Validator validator) { if (!externalEntities && !expandEntities) { try { validator.setProperty(ACCESS_EXTERNAL_SCHEMA, ""); validator.setProperty(ACCESS_EXTERNAL_DTD, ""); } catch (Exception e) { logConfigurationWarning(Validator.class.getName(), validator.getClass().getName(), e); } } }
@Override public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException { if (errorHandler == null) { errorHandler = new DefaultValidationErrorHandler(); } Validator validator = schema.newValidator(); try { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); } catch (SAXNotRecognizedException | SAXNotSupportedException e) { if (log.isWarnEnabled()) { log.warn(XMLConstants.ACCESS_EXTERNAL_DTD + " property not supported by " + validator.getClass().getCanonicalName()); } } try { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); } catch (SAXNotRecognizedException | SAXNotSupportedException e) { if (log.isWarnEnabled()) { log.warn(XMLConstants.ACCESS_EXTERNAL_SCHEMA + " property not supported by " + validator.getClass().getCanonicalName()); } } validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }
@Override public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException { if (errorHandler == null) { errorHandler = new DefaultValidationErrorHandler(); } Validator validator = schema.newValidator(); try { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); } catch (SAXNotRecognizedException | SAXNotSupportedException e) { if (log.isWarnEnabled()) { log.warn(XMLConstants.ACCESS_EXTERNAL_DTD + " property not supported by " + validator.getClass().getCanonicalName()); } } try { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); } catch (SAXNotRecognizedException | SAXNotSupportedException e) { if (log.isWarnEnabled()) { log.warn(XMLConstants.ACCESS_EXTERNAL_SCHEMA + " property not supported by " + validator.getClass().getCanonicalName()); } } validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }
/** * Returns validator for schema * @param schema * @return * @throws SAXException */ public static Validator getValidator(Schema schema) throws SAXException { Validator validator = schema.newValidator(); validator.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); validator.setFeature("http://xml.org/sax/features/external-general-entities", false); validator.setFeature("http://xml.org/sax/features/external-parameter-entities", false); validator.setProperty("http://apache.org/xml/properties/internal/entity-resolver", xmlEntityResolver); return validator; }
/** * Create a validator. * * @return The validator. */ public Validator createValidator() throws SAXException { Validator validator = getSchemaObject().newValidator(); XMLSecureFactories.createDefault().configureValidator(validator); if (this.validatorFeatures != null) { for (Map.Entry<String, Boolean> feature : this.validatorFeatures.entrySet()) { validator.setFeature(feature.getKey(), feature.getValue()); } } if (this.validatorProperties != null) { for (Map.Entry<String, Object> validatorProperty : this.validatorProperties.entrySet()) { validator.setProperty(validatorProperty.getKey(), validatorProperty.getValue()); } } return validator; }
/** * Returns validator for schema * @param schema * @return * @throws SAXException */ public static Validator getValidator(Schema schema) throws SAXException { Validator validator = schema.newValidator(); validator.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); validator.setFeature("http://xml.org/sax/features/external-general-entities", false); validator.setFeature("http://xml.org/sax/features/external-parameter-entities", false); validator.setProperty("http://apache.org/xml/properties/internal/entity-resolver", xmlEntityResolver); return validator; }
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
aValidator.setProperty (m_sName, aRealValue);
@Override public Void answer(InvocationOnMock invocation) { String name = (String) invocation.getArguments()[0]; Object value = invocation.getArguments()[1]; try { if (propertySetter instanceof SchemaFactory) { ((SchemaFactory) propertySetter).setProperty(name, value); } else if (propertySetter instanceof Validator) { ((Validator) propertySetter).setProperty(name, value); } else if (propertySetter instanceof TransformerFactory) { ((TransformerFactory) propertySetter).setAttribute(name, value); } else { throw new IllegalArgumentException("Invalid property setter."); } } catch (Exception e) { exception = e; } return null; } }
@Test public void validatorProperties() throws Exception { SetPropertyAnswer setPropertyAnswer = new SetPropertyAnswer(schemaFactory.newSchema().newValidator()); doAnswer(setPropertyAnswer).when(validatorWrapper).setProperty(anyString(), anyObject()); defaultXMLSecureFactories.configureValidator(validatorWrapper); assertThat(setPropertyAnswer.exception, is(nullValue())); for (String property : VALIDATOR_PROPERTIES) { verify(validatorWrapper).setProperty(property, ""); } }
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); }catch (SAXNotRecognizedException ex){ ourLog.warn("Jaxp 1.5 Support not found.",ex);
validator.setFeature("http://xml.org/sax/features/validation", true); validator.setFeature("http://apache.org/xml/features/validation/schema", true); validator.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", noNamespaceSchemaResource.getURL().toExternalForm());