Search in sources :

Example 86 with Schema

use of javax.xml.validation.Schema 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 87 with Schema

use of javax.xml.validation.Schema 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 88 with Schema

use of javax.xml.validation.Schema in project midpoint by Evolveum.

the class TestPrismObjectConstruction method serializeAndValidate.

private void serializeAndValidate(PrismObject<UserType> user, PrismContext prismContext) throws SchemaException, SAXException, IOException {
    String xmlString = prismContext.serializeObjectToString(user, PrismContext.LANG_XML);
    System.out.println("Serialized XML");
    System.out.println(xmlString);
    Document xmlDocument = DOMUtil.parseDocument(xmlString);
    Schema javaxSchema = prismContext.getSchemaRegistry().getJavaxSchema();
    Validator validator = javaxSchema.newValidator();
    validator.setResourceResolver(prismContext.getEntityResolver());
    validator.validate(new DOMSource(xmlDocument));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Schema(javax.xml.validation.Schema) Document(org.w3c.dom.Document) Validator(javax.xml.validation.Validator)

Example 89 with Schema

use of javax.xml.validation.Schema in project midpoint by Evolveum.

the class TestExtraSchema method testExtraSchema.

/**
	 * Test is extra schema can be loaded to the schema registry and whether the file compliant to that
	 * schema can be validated.
	 */
@Test
public void testExtraSchema() throws SAXException, IOException, SchemaException {
    System.out.println("===[ testExtraSchema ]===");
    Document dataDoc = DOMUtil.parseFile(new File(COMMON_DIR_PATH, "root-foo.xml"));
    PrismContext context = constructPrismContext();
    SchemaRegistryImpl reg = (SchemaRegistryImpl) context.getSchemaRegistry();
    Document extraSchemaDoc = DOMUtil.parseFile(new File(EXTRA_SCHEMA_DIR, "root.xsd"));
    reg.registerSchema(extraSchemaDoc, "file root.xsd");
    reg.initialize();
    Schema javaxSchema = reg.getJavaxSchema();
    assertNotNull(javaxSchema);
    Validator validator = javaxSchema.newValidator();
    DOMResult validationResult = new DOMResult();
    validator.validate(new DOMSource(dataDoc), validationResult);
//		System.out.println("Validation result:");
//		System.out.println(DOMUtil.serializeDOMToString(validationResult.getNode()));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) Schema(javax.xml.validation.Schema) PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) Document(org.w3c.dom.Document) SchemaRegistryImpl(com.evolveum.midpoint.prism.schema.SchemaRegistryImpl) File(java.io.File) Validator(javax.xml.validation.Validator) Test(org.testng.annotations.Test)

Example 90 with Schema

use of javax.xml.validation.Schema 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)

Aggregations

Schema (javax.xml.validation.Schema)102 SchemaFactory (javax.xml.validation.SchemaFactory)72 Validator (javax.xml.validation.Validator)51 StreamSource (javax.xml.transform.stream.StreamSource)45 SAXException (org.xml.sax.SAXException)35 Source (javax.xml.transform.Source)29 DOMSource (javax.xml.transform.dom.DOMSource)25 IOException (java.io.IOException)22 JAXBContext (javax.xml.bind.JAXBContext)18 Document (org.w3c.dom.Document)18 InputStream (java.io.InputStream)17 File (java.io.File)15 URL (java.net.URL)15 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 JAXBException (javax.xml.bind.JAXBException)13 Unmarshaller (javax.xml.bind.Unmarshaller)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13 InputSource (org.xml.sax.InputSource)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 SAXParseException (org.xml.sax.SAXParseException)9