Search in sources :

Example 26 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema in project ddf by codice.

the class TestXmlSchemaMessageBodyReader method testReadFromServiceExceptionReport.

@Test
public void testReadFromServiceExceptionReport() throws WebApplicationException, IOException {
    XmlSchemaMessageBodyReaderWfs10 reader = new XmlSchemaMessageBodyReaderWfs10();
    XmlSchema schema = reader.readFrom(null, null, null, null, null, ser);
    assertNull(schema);
}
Also used : XmlSchemaMessageBodyReaderWfs10(org.codice.ddf.spatial.ogc.wfs.v1_0_0.catalog.source.reader.XmlSchemaMessageBodyReaderWfs10) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Test(org.junit.Test)

Example 27 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.

the class AbstractDataBinding method addSchemaDocument.

public XmlSchema addSchemaDocument(ServiceInfo serviceInfo, SchemaCollection col, Document d, String systemId, Collection<String> ids) {
    /*
         * Sanity check. The document has to remotely resemble a schema.
         */
    if (!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(d.getDocumentElement().getNamespaceURI())) {
        QName qn = DOMUtils.getElementQName(d.getDocumentElement());
        throw new RuntimeException("Invalid schema document passed to " + "AbstractDataBinding.addSchemaDocument, " + "not in W3C schema namespace: " + qn);
    }
    if (!"schema".equals(d.getDocumentElement().getLocalName())) {
        QName qn = DOMUtils.getElementQName(d.getDocumentElement());
        throw new RuntimeException("Invalid schema document passed to " + "AbstractDataBinding.addSchemaDocument, " + "document element isn't 'schema': " + qn);
    }
    String ns = d.getDocumentElement().getAttribute("targetNamespace");
    boolean copied = false;
    if (StringUtils.isEmpty(ns)) {
        if (DOMUtils.getFirstElement(d.getDocumentElement()) == null) {
            hackAroundEmptyNamespaceIssue = true;
            return null;
        }
        // create a copy of the dom so we
        // can modify it.
        d = copy(d);
        copied = true;
        ns = serviceInfo.getInterface().getName().getNamespaceURI();
        d.getDocumentElement().setAttribute("targetNamespace", ns);
    }
    SchemaInfo schemaInfo = serviceInfo.getSchema(ns);
    if (schemaInfo != null && (systemId == null && schemaInfo.getSystemId() == null || systemId != null && systemId.equalsIgnoreCase(schemaInfo.getSystemId()))) {
        return schemaInfo.getSchema();
    }
    if (hackAroundEmptyNamespaceIssue) {
        d = doEmptyNamespaceHack(d, copied);
    }
    Node n = d.getDocumentElement().getFirstChild();
    boolean patchRequired = false;
    while (n != null) {
        if (n instanceof Element) {
            Element e = (Element) n;
            if (e.getLocalName().equals("import")) {
                patchRequired = true;
                break;
            }
        }
        n = n.getNextSibling();
    }
    if (patchRequired) {
        if (!copied) {
            d = copy(d);
        }
        n = d.getDocumentElement().getFirstChild();
        while (n != null) {
            if (n instanceof Element) {
                Element e = (Element) n;
                if (e.getLocalName().equals("import")) {
                    e = (Element) n;
                    String loc = e.getAttribute("schemaLocation");
                    if (ids == null || ids.contains(loc)) {
                        e.removeAttribute("schemaLocation");
                    }
                    updateSchemaLocation(e);
                    if (StringUtils.isEmpty(e.getAttribute("namespace"))) {
                        e.setAttribute("namespace", serviceInfo.getInterface().getName().getNamespaceURI());
                    }
                }
            }
            n = n.getNextSibling();
        }
    }
    SchemaInfo schema = new SchemaInfo(ns);
    schema.setSystemId(systemId);
    XmlSchema xmlSchema;
    synchronized (d) {
        xmlSchema = col.read(d, systemId);
        schema.setSchema(xmlSchema);
        schema.setElement(d.getDocumentElement());
    }
    serviceInfo.addSchema(schema);
    return xmlSchema;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 28 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.

the class ImportRepairTest method tryToParseSchemas.

private void tryToParseSchemas() throws Exception {
    // Get DOM Implementation using DOM Registry
    final List<DOMLSInput> inputs = new ArrayList<>();
    final Map<String, LSInput> resolverMap = new HashMap<>();
    for (XmlSchema schema : collection.getXmlSchemas()) {
        if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
            continue;
        }
        Document document = new XmlSchemaSerializer().serializeSchema(schema, false)[0];
        DOMLSInput input = new DOMLSInput(document, schema.getTargetNamespace());
        dumpSchema(document);
        resolverMap.put(schema.getTargetNamespace(), input);
        inputs.add(input);
    }
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementation impl = registry.getDOMImplementation("XS-Loader");
    try {
        Object schemaLoader = findMethod(impl, "createXSLoader").invoke(impl, new Object[1]);
        DOMConfiguration config = (DOMConfiguration) findMethod(schemaLoader, "getConfig").invoke(schemaLoader);
        config.setParameter("validate", Boolean.TRUE);
        try {
            // bug in the JDK doesn't set this, but accesses it
            config.setParameter("http://www.oracle.com/xml/jaxp/properties/xmlSecurityPropertyManager", Class.forName("com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager").newInstance());
            config.setParameter("http://apache.org/xml/properties/security-manager", Class.forName("com.sun.org.apache.xerces.internal.utils.XMLSecurityManager").newInstance());
        } catch (Throwable t) {
        // ignore
        }
        config.setParameter("error-handler", new DOMErrorHandler() {

            public boolean handleError(DOMError error) {
                LOG.info("Schema parsing error: " + error.getMessage() + " " + error.getType() + " " + error.getLocation().getUri() + " " + error.getLocation().getLineNumber() + ":" + error.getLocation().getColumnNumber());
                throw new DOMErrorException(error);
            }
        });
        config.setParameter("resource-resolver", new LSResourceResolver() {

            public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
                return resolverMap.get(namespaceURI);
            }
        });
        Method m = findMethod(schemaLoader, "loadInputList");
        String name = m.getParameterTypes()[0].getName() + "Impl";
        name = name.replace("xs.LS", "impl.xs.util.LS");
        Class<?> c = Class.forName(name);
        Object inputList = c.getConstructor(LSInput[].class, Integer.TYPE).newInstance(inputs.toArray(new LSInput[inputs.size()]), inputs.size());
        findMethod(schemaLoader, "loadInputList").invoke(schemaLoader, inputList);
    } catch (InvocationTargetException ite) {
        throw (Exception) ite.getTargetException();
    }
}
Also used : DOMErrorHandler(org.w3c.dom.DOMErrorHandler) HashMap(java.util.HashMap) DOMError(org.w3c.dom.DOMError) ArrayList(java.util.ArrayList) DOMImplementation(org.w3c.dom.DOMImplementation) DOMConfiguration(org.w3c.dom.DOMConfiguration) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) Method(java.lang.reflect.Method) Document(org.w3c.dom.Document) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) LSResourceResolver(org.w3c.dom.ls.LSResourceResolver) LSInput(org.w3c.dom.ls.LSInput) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry)

Example 29 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema 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 30 with XmlSchema

use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.

the class SchemaInfo method getElement.

/**
 * Build and return a DOM tree for this schema.
 * @return a DOM Element representation of the schema
 */
public synchronized Element getElement() {
    // if someone recently used this DOM tree, take advantage.
    Element element = cachedElement == null ? null : cachedElement.get();
    if (element != null) {
        return element;
    }
    if (getSchema() == null) {
        throw new RuntimeException("No XmlSchema in SchemaInfo");
    }
    XmlSchema sch = getSchema();
    synchronized (sch) {
        XmlSchema schAgain = getSchema();
        // Some unit tests really want to see 'tns:'.
        if (schAgain.getNamespaceContext() == null) {
            NamespaceMap nsMap = new NamespaceMap();
            nsMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
            nsMap.add("tns", schAgain.getTargetNamespace());
            schAgain.setNamespaceContext(nsMap);
        }
        Document serializedSchema;
        try {
            serializedSchema = schAgain.getSchemaDocument();
        } catch (XmlSchemaSerializerException e) {
            throw new RuntimeException("Error serializing Xml Schema", e);
        }
        element = serializedSchema.getDocumentElement();
        cachedElement = new SoftReference<Element>(element);
    }
    // The aegis databinding tests demonstrate this particularly.
    if (element.getPrefix() == null && !Constants.URI_2001_SCHEMA_XSD.equals(element.getAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE))) {
        Attr attr = element.getOwnerDocument().createAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE);
        attr.setValue(Constants.URI_2001_SCHEMA_XSD);
        element.setAttributeNodeNS(attr);
    }
    return element;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) Element(org.w3c.dom.Element) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr)

Aggregations

XmlSchema (org.apache.ws.commons.schema.XmlSchema)116 QName (javax.xml.namespace.QName)61 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)52 Test (org.junit.Test)49 FeatureMetacardType (org.codice.ddf.spatial.ogc.wfs.catalog.common.FeatureMetacardType)37 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)32 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)23 XmlSchemaType (org.apache.ws.commons.schema.XmlSchemaType)18 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)17 ArrayList (java.util.ArrayList)14 XmlSchemaSequence (org.apache.ws.commons.schema.XmlSchemaSequence)13 Element (org.w3c.dom.Element)12 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)10 XmlSchemaCollection (org.apache.ws.commons.schema.XmlSchemaCollection)10 Document (org.w3c.dom.Document)10 HashMap (java.util.HashMap)9 Map (java.util.Map)8 NamespaceMap (org.apache.ws.commons.schema.utils.NamespaceMap)8 List (java.util.List)7 XmlSchemaSequenceMember (org.apache.ws.commons.schema.XmlSchemaSequenceMember)7