Search in sources :

Example 6 with Validator

use of javax.xml.validation.Validator in project graphhopper by graphhopper.

the class InstructionListTest method verifyGPX.

public void verifyGPX(String gpx) {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = null;
    try {
        Source schemaFile = new StreamSource(getClass().getResourceAsStream("gpx-schema.xsd"));
        schema = schemaFactory.newSchema(schemaFile);
    // using more schemas: http://stackoverflow.com/q/1094893/194609
    } catch (SAXException e1) {
        throw new IllegalStateException("There was a problem with the schema supplied for validation. Message:" + e1.getMessage());
    }
    Validator validator = schema.newValidator();
    try {
        validator.validate(new StreamSource(new StringReader(gpx)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 7 with Validator

use of javax.xml.validation.Validator 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 8 with Validator

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

the class TestExtraSchema method testUserExtensionSchemaParseUser.

@Test
public void testUserExtensionSchemaParseUser() throws SAXException, IOException, SchemaException {
    System.out.println("===[ testUserExtensionSchemaParseUser ]===");
    Document dataDoc = DOMUtil.parseFile(USER_JACK_FILE_XML);
    PrismContext context = constructPrismContext();
    SchemaRegistryImpl reg = (SchemaRegistryImpl) context.getSchemaRegistry();
    reg.registerPrismSchemasFromDirectory(EXTRA_SCHEMA_DIR);
    context.initialize();
    // Parsing user
    // items from user extension are not correctly defined in the schema
    PrismObject<UserType> user = context.parserFor(DOMUtil.getFirstChildElement(dataDoc)).compat().parse();
    assertNotNull("No definition for user", user.getDefinition());
    System.out.println("Parsed root object:");
    System.out.println(user.debugDump());
    // TODO: assert user
    // Try javax schemas by validating a XML file
    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) UserType(com.evolveum.midpoint.prism.foo.UserType) Validator(javax.xml.validation.Validator) Test(org.testng.annotations.Test)

Example 9 with Validator

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

the class TestSchemaRegistry method testBasic.

/**
	 * Test whether the midpoint prism context was constructed OK and if it can validate
	 * ordinary user object.
	 */
@Test
public void testBasic() throws SAXException, IOException, SchemaException {
    MidPointPrismContextFactory factory = getContextFactory();
    PrismContext context = factory.createInitializedPrismContext();
    SchemaRegistry reg = context.getSchemaRegistry();
    Schema javaxSchema = reg.getJavaxSchema();
    assertNotNull(javaxSchema);
    // Try to use the schema to validate Jack
    //		PrismObject<UserType> user = context.parseObject(new File("src/test/resources/common/user-jack.xml"));
    //		Element document = context.serializeToDom(user);
    Document document = DOMUtil.parseFile("src/test/resources/common/user-jack.xml");
    Validator validator = javaxSchema.newValidator();
    DOMResult validationResult = new DOMResult();
    validator.validate(new DOMSource(document), 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) SchemaRegistry(com.evolveum.midpoint.prism.schema.SchemaRegistry) Validator(javax.xml.validation.Validator) Test(org.testng.annotations.Test)

Example 10 with Validator

use of javax.xml.validation.Validator in project ORCID-Source by ORCID.

the class ValidateV2_1IdentifiersTest method testFunding.

@Test
public void testFunding() throws SAXException, IOException, JAXBException, ParserConfigurationException {
    Funding funding = unmarshallFromPath("/record_2.1/samples/read_samples/funding-2.1.xml", Funding.class);
    assertEquals("funding:organization-defined-type", funding.getOrganizationDefinedType().getContent());
    assertNotNull(funding.getExternalIdentifiers());
    assertNotNull(funding.getExternalIdentifiers().getExternalIdentifier());
    Assert.notEmpty(funding.getExternalIdentifiers().getExternalIdentifier());
    assertEquals("grant_number", funding.getExternalIdentifiers().getExternalIdentifier().get(0).getType());
    assertEquals("funding:external-identifier-value", funding.getExternalIdentifiers().getExternalIdentifier().get(0).getValue());
    assertEquals(new Url("http://tempuri.org"), funding.getExternalIdentifiers().getExternalIdentifier().get(0).getUrl());
    assertEquals(Relationship.SELF, funding.getExternalIdentifiers().getExternalIdentifier().get(0).getRelationship());
    Validator validator = getValidator("funding");
    validator.validate(marshall(Funding.class, funding));
    validator.validate(marshallToDOM(Funding.class, funding));
}
Also used : Funding(org.orcid.jaxb.model.record_v2.Funding) Url(org.orcid.jaxb.model.common_v2.Url) Validator(javax.xml.validation.Validator) MarshallingTest(org.orcid.jaxb.model.notification.custom.MarshallingTest) Test(org.junit.Test)

Aggregations

Validator (javax.xml.validation.Validator)80 Schema (javax.xml.validation.Schema)51 SchemaFactory (javax.xml.validation.SchemaFactory)39 StreamSource (javax.xml.transform.stream.StreamSource)38 DOMSource (javax.xml.transform.dom.DOMSource)30 Source (javax.xml.transform.Source)29 Test (org.junit.Test)21 SAXException (org.xml.sax.SAXException)21 IOException (java.io.IOException)17 MarshallingTest (org.orcid.jaxb.model.notification.custom.MarshallingTest)17 Document (org.w3c.dom.Document)13 InputStream (java.io.InputStream)9 URL (java.net.URL)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 Url (org.orcid.jaxb.model.common_v2.Url)8 InputSource (org.xml.sax.InputSource)8 StringReader (java.io.StringReader)7 JAXBSource (javax.xml.bind.util.JAXBSource)7 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)7 SAXParseException (org.xml.sax.SAXParseException)7