Search in sources :

Example 1 with XMLValidationSchema

use of org.codehaus.stax2.validation.XMLValidationSchema in project cxf by apache.

the class Stax2ValidationUtils method getValidator.

/**
 * Create woodstox validator for a schema set.
 *
 * @throws XMLStreamException
 */
private XMLValidationSchema getValidator(Endpoint endpoint, ServiceInfo serviceInfo) throws XMLStreamException {
    synchronized (endpoint) {
        XMLValidationSchema ret = (XMLValidationSchema) endpoint.get(KEY);
        if (ret == null) {
            if (endpoint.containsKey(KEY)) {
                return null;
            }
            Map<String, EmbeddedSchema> sources = new TreeMap<>();
            for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
                XmlSchema sch = schemaInfo.getSchema();
                String uri = sch.getTargetNamespace();
                if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri)) {
                    continue;
                }
                if (sch.getTargetNamespace() == null && sch.getExternals().size() > 0) {
                    for (XmlSchemaExternal xmlSchemaExternal : sch.getExternals()) {
                        addSchema(sources, xmlSchemaExternal.getSchema(), getElement(xmlSchemaExternal.getSchema().getSourceURI()));
                    }
                    continue;
                } else if (sch.getTargetNamespace() == null) {
                    throw new IllegalStateException("An Schema without imports must have a targetNamespace");
                }
                addSchema(sources, sch, schemaInfo.getElement());
            }
            W3CMultiSchemaFactory factory = new W3CMultiSchemaFactory();
            // I don't think that we need the baseURI.
            try {
                ret = factory.loadSchemas(null, sources);
                endpoint.put(KEY, ret);
            } catch (XMLStreamException ex) {
                LOG.log(Level.INFO, "Problem loading schemas. Falling back to slower method.", ret);
                endpoint.put(KEY, null);
            }
        }
        return ret;
    }
}
Also used : XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XMLStreamException(javax.xml.stream.XMLStreamException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) TreeMap(java.util.TreeMap) XMLValidationSchema(org.codehaus.stax2.validation.XMLValidationSchema) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 2 with XMLValidationSchema

use of org.codehaus.stax2.validation.XMLValidationSchema in project cxf by apache.

the class Stax2ValidationUtils method setupValidation.

/**
 * {@inheritDoc}
 *
 * @throws XMLStreamException
 */
public boolean setupValidation(XMLStreamReader reader, Endpoint endpoint, ServiceInfo serviceInfo) throws XMLStreamException {
    // Gosh, this is bad, but I don't know a better solution, unless we're willing
    // to require the stax2 API no matter what.
    XMLStreamReader effectiveReader = reader;
    if (effectiveReader instanceof DepthXMLStreamReader) {
        effectiveReader = ((DepthXMLStreamReader) reader).getReader();
    }
    final XMLStreamReader2 reader2 = (XMLStreamReader2) effectiveReader;
    XMLValidationSchema vs = getValidator(endpoint, serviceInfo);
    if (vs == null) {
        return false;
    }
    reader2.setValidationProblemHandler(new ValidationProblemHandler() {

        public void reportProblem(XMLValidationProblem problem) throws XMLValidationException {
            throw new Fault(new Message("READ_VALIDATION_ERROR", LOG, problem.getMessage()), Fault.FAULT_CODE_CLIENT);
        }
    });
    reader2.validateAgainst(vs);
    return true;
}
Also used : XMLValidationProblem(org.codehaus.stax2.validation.XMLValidationProblem) XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) ValidationProblemHandler(org.codehaus.stax2.validation.ValidationProblemHandler) XMLValidationException(org.codehaus.stax2.validation.XMLValidationException) Message(org.apache.cxf.common.i18n.Message) XMLStreamReader2(org.codehaus.stax2.XMLStreamReader2) Fault(org.apache.cxf.interceptor.Fault) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) XMLValidationSchema(org.codehaus.stax2.validation.XMLValidationSchema)

Example 3 with XMLValidationSchema

use of org.codehaus.stax2.validation.XMLValidationSchema in project cxf by apache.

the class Stax2ValidationUtils method setupValidation.

public boolean setupValidation(XMLStreamWriter writer, Endpoint endpoint, ServiceInfo serviceInfo) throws XMLStreamException {
    XMLStreamWriter2 writer2 = (XMLStreamWriter2) writer;
    XMLValidationSchema vs = getValidator(endpoint, serviceInfo);
    if (vs == null) {
        return false;
    }
    writer2.setValidationProblemHandler(new ValidationProblemHandler() {

        public void reportProblem(XMLValidationProblem problem) throws XMLValidationException {
            throw new Fault(problem.getMessage(), LOG);
        }
    });
    writer2.validateAgainst(vs);
    return true;
}
Also used : XMLValidationProblem(org.codehaus.stax2.validation.XMLValidationProblem) XMLStreamWriter2(org.codehaus.stax2.XMLStreamWriter2) ValidationProblemHandler(org.codehaus.stax2.validation.ValidationProblemHandler) XMLValidationException(org.codehaus.stax2.validation.XMLValidationException) Fault(org.apache.cxf.interceptor.Fault) XMLValidationSchema(org.codehaus.stax2.validation.XMLValidationSchema)

Aggregations

XMLValidationSchema (org.codehaus.stax2.validation.XMLValidationSchema)3 Fault (org.apache.cxf.interceptor.Fault)2 ValidationProblemHandler (org.codehaus.stax2.validation.ValidationProblemHandler)2 XMLValidationException (org.codehaus.stax2.validation.XMLValidationException)2 XMLValidationProblem (org.codehaus.stax2.validation.XMLValidationProblem)2 TreeMap (java.util.TreeMap)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Message (org.apache.cxf.common.i18n.Message)1 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)1 DepthXMLStreamReader (org.apache.cxf.staxutils.DepthXMLStreamReader)1 XmlSchema (org.apache.ws.commons.schema.XmlSchema)1 XmlSchemaExternal (org.apache.ws.commons.schema.XmlSchemaExternal)1 XMLStreamReader2 (org.codehaus.stax2.XMLStreamReader2)1 XMLStreamWriter2 (org.codehaus.stax2.XMLStreamWriter2)1