use of javax.xml.validation.SchemaFactory in project groovy-core by groovy.
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 jOOQ by jOOQ.
the class MiniJAXB method getSchema.
private static Schema getSchema(Class<?> type, String namespace) {
try {
URL url;
if (PROVIDED_SCHEMAS.containsKey(namespace))
url = type.getResource(PROVIDED_SCHEMAS.get(namespace));
else
url = new URL(namespace);
if (url != null) {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(url);
return schema;
}
} catch (Exception e) {
log.warn("Failed to load schema for namespace " + namespace, e);
}
return null;
}
use of javax.xml.validation.SchemaFactory in project hazelcast by hazelcast.
the class DiscoverySpiTest method testSchema.
@Test
public void testSchema() throws Exception {
String xmlFileName = "test-hazelcast-discovery-spi.xml";
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaResource = DiscoverySpiTest.class.getClassLoader().getResource("hazelcast-config-4.0.xsd");
assertNotNull(schemaResource);
InputStream xmlResource = DiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
Source source = new StreamSource(xmlResource);
Schema schema = factory.newSchema(schemaResource);
Validator validator = schema.newValidator();
validator.validate(source);
}
use of javax.xml.validation.SchemaFactory in project Activiti by Activiti.
the class BpmnXMLConverter method createSchema.
protected Schema createSchema() throws SAXException {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = null;
if (classloader != null) {
schema = factory.newSchema(classloader.getResource(BPMN_XSD));
}
if (schema == null) {
schema = factory.newSchema(BpmnXMLConverter.class.getClassLoader().getResource(BPMN_XSD));
}
if (schema == null) {
throw new XMLException("BPMN XSD could not be found");
}
return schema;
}
use of javax.xml.validation.SchemaFactory in project hazelcast by hazelcast.
the class XmlUtilTest method testGetSchemaFactory.
@Test
public void testGetSchemaFactory() throws Exception {
SchemaFactory schemaFactory = XmlUtil.getSchemaFactory();
assertNotNull(schemaFactory);
assertThrows(SAXException.class, () -> XmlUtil.setProperty(schemaFactory, "test://no-such-property"));
ignoreXxeFailureProp.setOrClearProperty("false");
assertThrows(SAXException.class, () -> XmlUtil.setProperty(schemaFactory, "test://no-such-property"));
ignoreXxeFailureProp.setOrClearProperty("true");
XmlUtil.setProperty(schemaFactory, "test://no-such-property");
}
Aggregations