Search in sources :

Example 96 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 97 with SchemaFactory

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;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XmlSchema(jakarta.xml.bind.annotation.XmlSchema) Schema(javax.xml.validation.Schema) URL(java.net.URL) ConfigurationException(org.jooq.exception.ConfigurationException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ReflectException(org.jooq.tools.reflect.ReflectException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 98 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-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);
}
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 99 with SchemaFactory

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;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLException(org.activiti.bpmn.exceptions.XMLException) Schema(javax.xml.validation.Schema)

Example 100 with SchemaFactory

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");
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SchemaFactory (javax.xml.validation.SchemaFactory)268 Schema (javax.xml.validation.Schema)196 StreamSource (javax.xml.transform.stream.StreamSource)130 SAXException (org.xml.sax.SAXException)99 Validator (javax.xml.validation.Validator)94 IOException (java.io.IOException)73 Source (javax.xml.transform.Source)73 URL (java.net.URL)72 File (java.io.File)51 JAXBContext (javax.xml.bind.JAXBContext)50 InputStream (java.io.InputStream)40 Unmarshaller (javax.xml.bind.Unmarshaller)38 JAXBException (javax.xml.bind.JAXBException)34 DOMSource (javax.xml.transform.dom.DOMSource)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 Test (org.junit.Test)29 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)23 InputSource (org.xml.sax.InputSource)23 SAXParseException (org.xml.sax.SAXParseException)21 StringReader (java.io.StringReader)20