Search in sources :

Example 1 with LSInputSAXWrapper

use of com.sun.tools.xjc.reader.xmlschema.parser.LSInputSAXWrapper in project cxf by apache.

the class JAXBDataBinding method validateSchema.

public void validateSchema(Element ele, String uri, final OASISCatalogManager catalog, final SchemaCollection schemaCollection) throws ToolException {
    SchemaFactory schemaFact = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFact.setResourceResolver(new LSResourceResolver() {

        public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
            String s = JAXBDataBinding.mapSchemaLocation(systemId, baseURI, catalog);
            LOG.fine("validating: " + namespaceURI + " " + systemId + " " + baseURI + " " + s);
            if (s == null) {
                XmlSchema sc = schemaCollection.getSchemaByTargetNamespace(namespaceURI);
                if (sc != null) {
                    StringWriter writer = new StringWriter();
                    sc.write(writer);
                    InputSource src = new InputSource(new StringReader(writer.toString()));
                    src.setSystemId(sc.getSourceURI());
                    return new LSInputSAXWrapper(src);
                }
                throw new ToolException("Schema not found for namespace: " + namespaceURI);
            }
            return new LSInputSAXWrapper(new InputSource(s));
        }
    });
    DOMSource domSrc = new DOMSource(ele, uri);
    try {
        schemaFact.newSchema(domSrc);
    } catch (SAXException e) {
        if (e.getLocalizedMessage().indexOf("src-resolve.4.2") > -1) {
        // Ignore schema resolve error and do nothing
        } else {
            // e.printStackTrace();
            throw new ToolException("Schema Error : " + e.getLocalizedMessage(), e);
        }
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) StringWriter(java.io.StringWriter) LSResourceResolver(org.w3c.dom.ls.LSResourceResolver) XmlSchema(org.apache.ws.commons.schema.XmlSchema) LSInput(org.w3c.dom.ls.LSInput) StringReader(java.io.StringReader) LSInputSAXWrapper(com.sun.tools.xjc.reader.xmlschema.parser.LSInputSAXWrapper) ToolException(org.apache.cxf.tools.common.ToolException) SAXException(org.xml.sax.SAXException)

Aggregations

LSInputSAXWrapper (com.sun.tools.xjc.reader.xmlschema.parser.LSInputSAXWrapper)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 DOMSource (javax.xml.transform.dom.DOMSource)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 ToolException (org.apache.cxf.tools.common.ToolException)1 XmlSchema (org.apache.ws.commons.schema.XmlSchema)1 LSInput (org.w3c.dom.ls.LSInput)1 LSResourceResolver (org.w3c.dom.ls.LSResourceResolver)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1