Search in sources :

Example 81 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project ACS by ACS-Community.

the class XMLValidator method run.

public void run(String[] args) {
    try {
        if (args.length != 2) {
            System.out.println("Incorrect arguments. You need to provide the XML and XSD files.");
            System.exit(3);
        }
        XMLValidator.error = false;
        // define the type of schema - we use W3C:
        String schemaLang = "http://www.w3.org/2001/XMLSchema";
        // get validation driver:
        SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
        // create schema by reading it from an XSD file:
        Schema schema = factory.newSchema(new StreamSource(args[1]));
        Validator validator = schema.newValidator();
        ErrorHandler eh = new XMLErrorHandler(args[0]);
        validator.setErrorHandler(eh);
        // at last perform validation:
        XMLInputFactory xFact = XMLInputFactory.newInstance();
        XMLStreamReader xRead = xFact.createXMLStreamReader(new FileReader(args[0]));
        if (xRead.getVersion() == null) {
            System.err.println(args[0] + ": There is no XML Definition.");
            XMLValidator.error = true;
        } else if (xRead.getCharacterEncodingScheme() == null) {
            System.err.println(args[0] + ": The encoding attribute is not defined in the XML Definition.");
            XMLValidator.error = true;
        } else if (xRead.getCharacterEncodingScheme().compareTo("ISO-8859-1") != 0) {
            System.err.println(args[0] + ": Incorrect encoding type in the XML Definition.");
            XMLValidator.error = true;
        }
        validator.validate(new StreamSource(args[0]));
    } catch (SAXException ex) {
        System.err.println("Fatal Error");
        System.err.println(args[0] + ": " + ex.getMessage());
        XMLValidator.error = true;
    } catch (Exception ex) {
        ex.printStackTrace();
        XMLValidator.error = true;
    }
    if (XMLValidator.error) {
        //System.exit(1); //Error
        //Warning
        System.exit(0);
    } else {
        System.exit(0);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ErrorHandler(org.xml.sax.ErrorHandler) XMLStreamReader(javax.xml.stream.XMLStreamReader) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) FileReader(java.io.FileReader) Validator(javax.xml.validation.Validator) XMLInputFactory(javax.xml.stream.XMLInputFactory) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 82 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project jdk8u_jdk by JetBrains.

the class ValidationWarningsTest method doOneTestIteration.

//One iteration of xml validation test case. It will be called from each
//TestWorker task defined in WarningsTestBase class.
void doOneTestIteration() throws Exception {
    Source src = new StreamSource(new StringReader(xml));
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    SAXSource xsdSource = new SAXSource(new InputSource(new ByteArrayInputStream(xsd.getBytes())));
    Schema schema = schemaFactory.newSchema(xsdSource);
    Validator v = schema.newValidator();
    v.validate(src);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) StringReader(java.io.StringReader) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 83 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project jdk8u_jdk by JetBrains.

the class XPathWhiteSpaceTest 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));
    } catch (SAXException e) {
        throw new RuntimeException(e.getMessage());
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) SAXException(org.xml.sax.SAXException)

Example 84 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project sukija by ahomansikka.

the class JAXBUtil method getUnmarshaller.

//  private static final Logger LOG = LoggerFactory.getLogger (JAXBUtil.class);
private static final Unmarshaller getUnmarshaller(String schemaFile, String schemaLocation, String contextPath, ClassLoader classLoader) throws JAXBException, SAXException {
    // System.out.println ("Path " + "/" + schemaLocation + "/" + schemaFile);
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    //
    // Miten schemaFile löytyy näyttää muutuvan jokaisessa Java-versiossa. (-:
    //    Schema schema = sf.newSchema (classLoader.getResource (schemaFile));
    //    Schema schema = sf.newSchema (JAXBUtil.class.getResource ("/" + schemaLocation + "/" + schemaFile));
    Schema schema = sf.newSchema(JAXBUtil.class.getResource("/" + schemaFile));
    JAXBContext jc = JAXBContext.newInstance(contextPath, classLoader);
    Unmarshaller u = jc.createUnmarshaller();
    u.setSchema(schema);
    return u;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 85 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project aries by apache.

the class BlueprintFileWriterTest method generatedXmlIsValid.

@Test
public void generatedXmlIsValid() throws Exception {
    Document document = readToDocument(xmlAsBytes, true);
    Source[] schemas = new StreamSource[] { new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/example.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/blueprint.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.1.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.2.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.3.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.4.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/ext/impl/blueprint-ext-1.5.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/transaction/parsing/transactionv12.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/jpa/blueprint/namespace/jpa_110.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.0.0.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.1.0.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.3.0.xsd")), new StreamSource(BlueprintFileWriterTest.class.getResourceAsStream("/schema/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.2.0.xsd")) };
    Source xmlFile = new DOMSource(document);
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemas);
    Validator validator = schema.newValidator();
    validator.validate(xmlFile);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) Test(org.junit.Test)

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