use of javax.xml.validation.SchemaFactory in project groovy by apache.
the class XmlUtil method newSAXParser.
/**
* Factory method to create a SAXParser configured to validate according to a particular schema language and
* optionally providing the schema sources to validate with.
*
* @param schemaLanguage the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)
* @param namespaceAware will the parser be namespace aware
* @param validating will the parser also validate against DTDs
* @param schemas the schemas to validate against
* @return the created SAXParser
* @throws SAXException
* @throws ParserConfigurationException
* @since 1.8.7
*/
public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, Source... schemas) throws SAXException, ParserConfigurationException {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(validating);
factory.setNamespaceAware(namespaceAware);
if (schemas.length != 0) {
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
factory.setSchema(schemaFactory.newSchema(schemas));
}
SAXParser saxParser = factory.newSAXParser();
if (schemas.length == 0) {
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", schemaLanguage);
}
return saxParser;
}
use of javax.xml.validation.SchemaFactory in project camel by apache.
the class SchemaReader method createSchema.
protected Schema createSchema() throws SAXException, IOException {
SchemaFactory factory = getSchemaFactory();
URL url = getSchemaUrl();
if (url != null) {
synchronized (this) {
return factory.newSchema(url);
}
}
File file = getSchemaFile();
if (file != null) {
synchronized (this) {
return factory.newSchema(file);
}
}
byte[] bytes = getSchemaAsByteArray();
if (bytes != null) {
synchronized (this) {
return factory.newSchema(new StreamSource(new ByteArrayInputStream(schemaAsByteArray)));
}
}
if (schemaResourceUri != null) {
synchronized (this) {
bytes = readSchemaResource();
return factory.newSchema(new StreamSource(new ByteArrayInputStream(bytes)));
}
}
Source source = getSchemaSource();
synchronized (this) {
return factory.newSchema(source);
}
}
use of javax.xml.validation.SchemaFactory in project camel by apache.
the class CustomSchemaFactoryFeatureTest method createRegistry.
// Need to bind the CustomerSchemaFactory
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
SchemaFactory mySchemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
mySchemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
registry.bind("MySchemaFactory", mySchemaFactory);
return registry;
}
use of javax.xml.validation.SchemaFactory in project OpenAttestation by OpenAttestation.
the class XML method parseDocumentElement.
/**
* Example schema locations:
* http://docs.oasis-open.org/security/saml/v2.0/saml-schema-protocol-2.0.xsd
* http://docs.oasis-open.org/security/saml/v2.0/saml-schema-assertion-2.0.xsd
* http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd
* http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd
*
* @param xml
* @return
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public Element parseDocumentElement(String xml) throws ParserConfigurationException, SAXException, IOException {
ClasspathResourceResolver resolver = new ClasspathResourceResolver();
resolver.setResourcePackage(schemaPackageName);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setResourceResolver(resolver);
Source[] schemaSources = new Source[schemaLocations.size()];
int i = 0;
InputStream inStream = null;
Schema schema;
try {
for (String schemaLocation : schemaLocations) {
inStream = resolver.findResource(schemaLocation);
// if(in == null) log.debug("parseDocumentElement - InputStream is NULL");
// else log.debug("parseDocumentElement - InputStream OK");
schemaSources[i] = new StreamSource(inStream);
log.debug("parseDocumentElement - schemaSources[" + i + "] :" + schemaSources[i].getSystemId());
i++;
}
// if(schemaSources!=null) log.debug("parseDocumentElement - schemaSources.length: " + schemaSources.length);
// else log.debug("parseDocumentElement - schemaSources is null");
schema = schemaFactory.newSchema(schemaSources);
} catch (Exception e) {
schema = null;
log.error(e.getMessage());
} finally {
if (inStream != null)
inStream.close();
}
// Validator validator = schema.newValidator();
// validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes())));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
// bug #1038 prevent XXE
factory.setExpandEntityReferences(false);
// bug #1038 prevent XXE
factory.setXIncludeAware(false);
// fix for javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID HostTrustAssertion
factory.setSchema(schema);
// ParserConfigurationException
DocumentBuilder builder = factory.newDocumentBuilder();
try (ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes())) {
// SAXException, IOException
Element document = builder.parse(in).getDocumentElement();
return document;
}
}
use of javax.xml.validation.SchemaFactory in project honeycomb by altamiracorp.
the class ConfigurationParser method checkValidConfig.
/**
* Performs validation on the configuration content supplied by the
* configuration supplier against the schema document provided by the
* validation supplier. Throws Runtime exception if validation fails.
*
* @param configSupplier The supplier that provides the configuration to inspect, not null
* @param schemaSupplier The supplier that provides the schema used to inspect the configuration, not null
*/
private static void checkValidConfig(final InputSupplier<? extends InputStream> configSupplier, final InputSupplier<? extends InputStream> schemaSupplier) {
try {
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Schema schema = schemaFactory.newSchema(new StreamSource(schemaSupplier.getInput()));
final Validator validator = schema.newValidator();
validator.validate(new StreamSource(configSupplier.getInput()));
} catch (Exception e) {
logger.error("Unable to validate honeycomb configuration.", e);
throw new RuntimeException("Exception while validating honeycomb configuration.", e);
}
}
Aggregations