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 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-3.9.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 jdk8u_jdk by JetBrains.
the class SchemaTest method testValidation.
/*
* @bug 8149915
* Verifies that the annotation validator is initialized with the security manager for schema
* creation with http://apache.org/xml/features/validate-annotations=true.
*/
@Test
public void testValidation() throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setFeature("http://apache.org/xml/features/validate-annotations", true);
factory.newSchema(new File(getClass().getResource("Bug8149915.xsd").getFile()));
}
use of javax.xml.validation.SchemaFactory in project jdk8u_jdk by JetBrains.
the class AnyURITest method main.
public static void main(String[] args) throws Exception {
try {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File(System.getProperty("test.src", "."), XSDFILE));
throw new RuntimeException("Illegal URI // should be rejected.");
} catch (SAXException e) {
//expected:
//Enumeration value '//' is not in the value space of the base type, anyURI.
}
}
use of javax.xml.validation.SchemaFactory in project ORCID-Source by ORCID.
the class ValidateV2_1SamplesTest method unmarshall.
private Object unmarshall(Reader reader, Class<?> type, String schemaPath) throws SAXException, URISyntaxException {
try {
JAXBContext context = JAXBContext.newInstance(type);
Unmarshaller unmarshaller = context.createUnmarshaller();
if (schemaPath != null) {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File(getClass().getResource(schemaPath).toURI()));
unmarshaller.setSchema(schema);
}
return unmarshaller.unmarshal(reader);
} catch (JAXBException e) {
throw new RuntimeException("Unable to unmarshall orcid message" + e);
}
}
Aggregations