Search in sources :

Example 16 with SchemaFactory

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;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 17 with SchemaFactory

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);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 18 with SchemaFactory

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()));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) File(java.io.File) Test(org.testng.annotations.Test)

Example 19 with SchemaFactory

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.
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) SAXException(org.xml.sax.SAXException)

Example 20 with SchemaFactory

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);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Aggregations

SchemaFactory (javax.xml.validation.SchemaFactory)92 Schema (javax.xml.validation.Schema)72 StreamSource (javax.xml.transform.stream.StreamSource)47 Validator (javax.xml.validation.Validator)39 SAXException (org.xml.sax.SAXException)29 Source (javax.xml.transform.Source)28 IOException (java.io.IOException)20 URL (java.net.URL)18 DOMSource (javax.xml.transform.dom.DOMSource)18 JAXBContext (javax.xml.bind.JAXBContext)17 File (java.io.File)16 InputStream (java.io.InputStream)16 InputSource (org.xml.sax.InputSource)14 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Unmarshaller (javax.xml.bind.Unmarshaller)11 Test (org.junit.Test)10 Document (org.w3c.dom.Document)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8