/** * 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; }
/** * 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; }
private void setFeature(Validator validator, String feature, boolean value) { try { validator.setFeature(feature, value); } catch (SAXNotRecognizedException e) { throw new SonarException(e); } catch (SAXNotSupportedException e) { throw new SonarException(e); } }
/** * Validate the XML content against the XSD. * * The whole XML content must be valid. */ static void validateXML(String xmlContent, String xsdPath, Properties resolvedProperties) throws Exception { String resolvedXml = resolveAllExpressions(xmlContent, resolvedProperties); InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(xsdPath); final Source source = new StreamSource(stream); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setErrorHandler(ERROR_HANDLER); schemaFactory.setResourceResolver(new JBossEntityResolver()); Schema schema = schemaFactory.newSchema(source); javax.xml.validation.Validator validator = schema.newValidator(); validator.setErrorHandler(ERROR_HANDLER); validator.setFeature("http://apache.org/xml/features/validation/schema", true); validator.validate(new StreamSource(new StringReader(resolvedXml))); }
/** * Validate the XML content against the XSD. * * The whole XML content must be valid. */ static void validateXML(String xmlContent, String xsdPath, Properties resolvedProperties) throws Exception { String resolvedXml = resolveAllExpressions(xmlContent, resolvedProperties); InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(xsdPath); final Source source = new StreamSource(stream); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setErrorHandler(ERROR_HANDLER); schemaFactory.setResourceResolver(new JBossEntityResolver()); Schema schema = schemaFactory.newSchema(source); javax.xml.validation.Validator validator = schema.newValidator(); validator.setErrorHandler(ERROR_HANDLER); validator.setFeature("http://apache.org/xml/features/validation/schema", true); validator.validate(new StreamSource(new StringReader(resolvedXml))); }
/** * 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; }
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());