Search in sources :

Example 41 with SchemaFactory

use of javax.xml.validation.SchemaFactory 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 42 with SchemaFactory

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

the class BaseXsltDataUpgraderTest method loadSchema.

private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
    Assert.notEmpty(resources, "No resources given");
    Assert.hasLength(schemaLanguage, "No schema language provided");
    Source[] schemaSources = new Source[resources.length];
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    for (int i = 0; i < resources.length; i++) {
        Assert.notNull(resources[i], "Resource is null");
        Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
        InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
        schemaSources[i] = new SAXSource(xmlReader, inputSource);
    }
    SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
    return schemaFactory.newSchema(schemaSources);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader)

Example 43 with SchemaFactory

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

Example 44 with SchemaFactory

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

the class JaxbJavaee method validateJavaee.

/**
     * validate the inputStream, which should be a Java EE standard deployment descriptor against its schema type
     * Note, this method will use the new Java EE 6 schema to validate the old descriptors after changing their namespace and version.
     *
     * @param type
     * @param in
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
public static void validateJavaee(final JavaeeSchema type, final InputStream in) throws ParserConfigurationException, SAXException, IOException {
    final URL javaeeSchemaURL = resolveJavaeeSchemaURL(type);
    if (javaeeSchemaURL == null) {
        throw new IllegalArgumentException("Can not find the xsd file against type:" + type);
    }
    final URL xmlSchemaURL = JaxbJavaee.getSchemaURL("xml.xsd");
    if (xmlSchemaURL == null) {
        throw new IllegalArgumentException("Can not find the xml.xsd file");
    }
    // get the parser
    final SAXParserFactory parserfactory = SAXParserFactory.newInstance();
    parserfactory.setNamespaceAware(true);
    parserfactory.setValidating(false);
    final SAXParser parser = parserfactory.newSAXParser();
    // get the xml filter
    final Javaee6SchemaFilter xmlFilter = new Javaee6SchemaFilter(parser.getXMLReader());
    // get the source
    final SAXSource sourceForValidate = new SAXSource(xmlFilter, new InputSource(in));
    // get the schema
    final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final JaxbJavaeeSchemaResourceResolver resourceResolver = new JaxbJavaeeSchemaResourceResolver();
    schemaFactory.setResourceResolver(resourceResolver);
    final Schema schema = schemaFactory.newSchema(new Source[] { new StreamSource(xmlSchemaURL.openStream()), new StreamSource(javaeeSchemaURL.openStream()) });
    // validate
    schema.newValidator().validate(sourceForValidate);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) SAXParser(javax.xml.parsers.SAXParser) URL(java.net.URL) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 45 with SchemaFactory

use of javax.xml.validation.SchemaFactory in project jqa-core-framework by buschmais.

the class XmlReportTest method readReport.

private JqassistantReport readReport(String xmlReport) throws SAXException, JAXBException {
    SchemaFactory xsdFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = xsdFactory.newSchema(new StreamSource(XmlReportTest.class.getResourceAsStream("/META-INF/xsd/jqassistant-report-1.3.xsd")));
    JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
    StreamSource streamSource = new StreamSource(new StringReader(xmlReport));
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    unmarshaller.setSchema(schema);
    return unmarshaller.unmarshal(streamSource, JqassistantReport.class).getValue();
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

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