Search in sources :

Example 46 with Schema

use of javax.xml.validation.Schema in project maven-plugins by apache.

the class DefaultChangesSchemaValidator method compileJAXPSchema.

/**
     * @param uriSchema
     * @return Schema
     * @throws Exception
     */
private Schema compileJAXPSchema(String uriSchema) throws IOException, SAXException, NullPointerException {
    InputStream in = null;
    try {
        in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uriSchema);
        if (in == null) {
            throw new NullPointerException(" impossible to load schema with path " + uriSchema);
        }
        //newInstance de SchemaFactory not ThreadSafe
        final Schema schema = SchemaFactory.newInstance(W3C_XML_SCHEMA).newSchema(new StreamSource(in));
        in.close();
        in = null;
        return schema;
    } finally {
        IOUtil.close(in);
    }
}
Also used : InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource)

Example 47 with Schema

use of javax.xml.validation.Schema in project oxCore by GluuFederation.

the class FacesConfigPopulator method isValidFacesConfig.

/**
     * Validates *.faces-config.xml file
     *
     * @param xml
     * @return
     */
private boolean isValidFacesConfig(InputStream xml) {
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        factory.setResourceResolver((LSResourceResolver) DbfFactory.FACES_ENTITY_RESOLVER);
        InputStream xsd = this.getClass().getResourceAsStream(FACES_2_2_XSD);
        Schema schema = factory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(xml));
        return true;
    } catch (Exception ex) {
        return false;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Validator(javax.xml.validation.Validator)

Example 48 with Schema

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

the class TestMisc method test200ExportUsers.

@Test
public void test200ExportUsers() throws Exception {
    final String TEST_NAME = "test200ExportUsers";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = taskManager.createTaskInstance(TestMisc.class.getName() + "." + TEST_NAME);
    OperationResult result = task.getResult();
    // WHEN
    List<PrismObject<UserType>> users = modelService.searchObjects(UserType.class, null, SelectorOptions.createCollection(new ItemPath(), GetOperationOptions.createRaw()), task, result);
    // THEN
    result.computeStatus();
    display("Search users result", result);
    TestUtil.assertSuccess(result);
    assertEquals("Unexpected number of users", 5, users.size());
    for (PrismObject<UserType> user : users) {
        display("Exporting user", user);
        assertNotNull("Null definition in " + user, user.getDefinition());
        display("Definition", user.getDefinition());
        String xmlString = prismContext.serializeObjectToString(user, PrismContext.LANG_XML);
        display("Exported user", xmlString);
        Document xmlDocument = DOMUtil.parseDocument(xmlString);
        Schema javaxSchema = prismContext.getSchemaRegistry().getJavaxSchema();
        Validator validator = javaxSchema.newValidator();
        validator.setResourceResolver(prismContext.getEntityResolver());
        validator.validate(new DOMSource(xmlDocument));
        PrismObject<Objectable> parsedUser = prismContext.parseObject(xmlString);
        assertTrue("Re-parsed user is not equal to original: " + user, user.equals(parsedUser));
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) DOMSource(javax.xml.transform.dom.DOMSource) Schema(javax.xml.validation.Schema) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Document(org.w3c.dom.Document) PrismObject(com.evolveum.midpoint.prism.PrismObject) Objectable(com.evolveum.midpoint.prism.Objectable) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) Validator(javax.xml.validation.Validator) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Test(org.testng.annotations.Test)

Example 49 with Schema

use of javax.xml.validation.Schema in project uPortal by Jasig.

the class BaseXsltDataUpgraderTest method testXsltUpgrade.

protected void testXsltUpgrade(final Resource xslResource, final PortalDataKey dataKey, final Resource inputResource, final Resource expectedResultResource, final Resource xsdResource) throws Exception {
    final XmlUtilities xmlUtilities = new XmlUtilitiesImpl() {

        @Override
        public Templates getTemplates(Resource stylesheet) throws TransformerConfigurationException, IOException {
            final TransformerFactory transformerFactory = TransformerFactory.newInstance();
            return transformerFactory.newTemplates(new StreamSource(stylesheet.getInputStream()));
        }
    };
    final XsltDataUpgrader xsltDataUpgrader = new XsltDataUpgrader();
    xsltDataUpgrader.setPortalDataKey(dataKey);
    xsltDataUpgrader.setXslResource(xslResource);
    xsltDataUpgrader.setXmlUtilities(xmlUtilities);
    xsltDataUpgrader.afterPropertiesSet();
    //Create XmlEventReader (what the JaxbPortalDataHandlerService has)
    final XMLInputFactory xmlInputFactory = xmlUtilities.getXmlInputFactory();
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputResource.getInputStream());
    final Node sourceNode = xmlUtilities.convertToDom(xmlEventReader);
    final DOMSource source = new DOMSource(sourceNode);
    final DOMResult result = new DOMResult();
    xsltDataUpgrader.upgradeData(source, result);
    //XSD Validation
    final String resultString = XmlUtilitiesImpl.toString(result.getNode());
    if (xsdResource != null) {
        final Schema schema = this.loadSchema(new Resource[] { xsdResource }, XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final Validator validator = schema.newValidator();
        try {
            validator.validate(new StreamSource(new StringReader(resultString)));
        } catch (Exception e) {
            throw new XmlTestException("Failed to validate XSLT output against provided XSD", resultString, e);
        }
    }
    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalizeWhitespace(true);
    try {
        Diff d = new Diff(new InputStreamReader(expectedResultResource.getInputStream()), new StringReader(resultString));
        assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
    } catch (Exception e) {
        throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
    } catch (Error e) {
        throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) DOMResult(javax.xml.transform.dom.DOMResult) InputStreamReader(java.io.InputStreamReader) Diff(org.custommonkey.xmlunit.Diff) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) Schema(javax.xml.validation.Schema) Resource(org.springframework.core.io.Resource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) XmlUtilitiesImpl(org.apereo.portal.xml.XmlUtilitiesImpl) XmlUtilities(org.apereo.portal.xml.XmlUtilities) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) Validator(javax.xml.validation.Validator)

Example 50 with Schema

use of javax.xml.validation.Schema in project webservices-axiom by apache.

the class ValidateSample method validate.

// START SNIPPET: sax
public void validate(InputStream in, URL schemaUrl) throws Exception {
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(in, "UTF-8");
    SOAPEnvelope envelope = builder.getSOAPEnvelope();
    OMElement bodyContent = envelope.getBody().getFirstElement();
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemaUrl);
    Validator validator = schema.newValidator();
    validator.validate(bodyContent.getSAXSource(true));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) OMElement(org.apache.axiom.om.OMElement) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) Validator(javax.xml.validation.Validator)

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