Search in sources :

Example 46 with XmlSchema

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

the class JAXBDataBinding method addBindingFiles.

private void addBindingFiles(Options opts, List<InputSource> jaxbBindings, SchemaCollection schemas) {
    for (InputSource binding : jaxbBindings) {
        XMLStreamReader r = StaxUtils.createXMLStreamReader(binding);
        try {
            StaxUtils.toNextTag(r);
            String s = r.getAttributeValue(null, "schemaLocation");
            if (StringUtils.isEmpty(s)) {
                Document d = StaxUtils.read(r);
                XPath p = XPathFactory.newInstance().newXPath();
                p.setNamespaceContext(new W3CNamespaceContext(d.getDocumentElement()));
                XPathExpression xpe = p.compile(d.getDocumentElement().getAttribute("node"));
                for (XmlSchema schema : schemas.getXmlSchemas()) {
                    if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
                        continue;
                    }
                    Object src = getSchemaNode(schema, schemas);
                    NodeList nodes = (NodeList) xpe.evaluate(src, XPathConstants.NODESET);
                    if (nodes.getLength() > 0) {
                        String key = schema.getSourceURI();
                        binding = convertToTmpInputSource(d.getDocumentElement(), key);
                        opts.addBindFile(binding);
                        binding = null;
                    }
                }
            }
        } catch (Exception ex) {
        // ignore, just pass to jaxb
        } finally {
            try {
                r.close();
            } catch (Exception ex) {
            // ignore
            }
        }
        if (binding != null) {
            opts.addBindFile(binding);
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) W3CNamespaceContext(org.apache.cxf.staxutils.W3CNamespaceContext) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) SAXException(org.xml.sax.SAXException) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) DOMException(org.w3c.dom.DOMException) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) SAXParseException(org.xml.sax.SAXParseException)

Example 47 with XmlSchema

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

the class SourceGenerator method createSchemaInfo.

private SchemaInfo createSchemaInfo(Element schemaEl, String systemId) {
    SchemaInfo info = new SchemaInfo(schemaEl.getAttribute("targetNamespace"));
    info.setElement(schemaEl);
    info.setSystemId(systemId);
    // eviction of the DOM element from the memory
    try {
        XmlSchema xmlSchema = schemaCollection.read(schemaEl, systemId);
        info.setSchema(xmlSchema);
    } catch (Exception ex) {
    // may be due to unsupported resolvers for protocols like
    // classpath: or not the valid schema definition, may not be critical
    // for the purpose of the schema compilation.
    }
    return info;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 48 with XmlSchema

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

the class JAXBDataBinding method addSchemas.

private void addSchemas(Options opts, SchemaCompiler schemaCompiler, SchemaCollection schemaCollection) {
    Set<String> ids = new HashSet<>();
    @SuppressWarnings("unchecked") List<ServiceInfo> serviceList = (List<ServiceInfo>) context.get(ToolConstants.SERVICE_LIST);
    for (ServiceInfo si : serviceList) {
        for (SchemaInfo sci : si.getSchemas()) {
            String key = sci.getSystemId();
            if (ids.contains(key)) {
                continue;
            }
            ids.add(key);
        }
    }
    Bus bus = context.get(Bus.class);
    OASISCatalogManager catalog = bus.getExtension(OASISCatalogManager.class);
    for (XmlSchema schema : schemaCollection.getXmlSchemas()) {
        if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
            continue;
        }
        String key = schema.getSourceURI();
        if (ids.contains(key)) {
            continue;
        }
        if (!key.startsWith("file:") && !key.startsWith("jar:")) {
            XmlSchemaSerializer xser = new XmlSchemaSerializer();
            xser.setExtReg(schemaCollection.getExtReg());
            Document[] docs;
            try {
                docs = xser.serializeSchema(schema, false);
            } catch (XmlSchemaSerializerException e) {
                throw new RuntimeException(e);
            }
            Element ele = docs[0].getDocumentElement();
            if (context.fullValidateWSDL()) {
                String uri = null;
                try {
                    uri = docs[0].getDocumentURI();
                } catch (Throwable ex) {
                // ignore - DOM level 3
                }
                validateSchema(ele, uri, catalog, schemaCollection);
            }
            ele = removeImportElement(ele, key, catalog);
            try {
                docs[0].setDocumentURI(key);
            } catch (Throwable t) {
            // ignore - DOM level 3
            }
            InputSource is = new InputSource((InputStream) null);
            // key = key.replaceFirst("#types[0-9]+$", "");
            is.setSystemId(key);
            is.setPublicId(key);
            opts.addGrammar(is);
            try {
                schemaCompiler.parseSchema(key, createNoCDATAReader(StaxUtils.createXMLStreamReader(ele, key)));
            } catch (XMLStreamException e) {
                throw new ToolException(e);
            }
        }
    }
    for (XmlSchema schema : schemaCollection.getXmlSchemas()) {
        if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
            continue;
        }
        String key = schema.getSourceURI();
        String tns = schema.getTargetNamespace();
        String ltns = schema.getLogicalTargetNamespace();
        // accepting also a null tns (e.g., reported by apache.ws.xmlschema for no-namespace)
        if (ids.contains(key) || (tns == null && !StringUtils.isEmpty(ltns))) {
            continue;
        }
        if (key.startsWith("file:") || key.startsWith("jar:")) {
            InputStream in = null;
            try {
                if (key.startsWith("file:")) {
                    in = Files.newInputStream(new File(new URI(key)).toPath());
                } else {
                    in = new URL(key).openStream();
                }
                XMLStreamReader reader = StaxUtils.createXMLStreamReader(key, in);
                reader = createNoCDATAReader(new LocationFilterReader(reader, catalog));
                InputSource is = new InputSource(key);
                opts.addGrammar(is);
                schemaCompiler.parseSchema(key, reader);
                reader.close();
            } catch (RuntimeException ex) {
                throw ex;
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                    // ignore
                    }
                }
            }
        }
    }
    addSchemasForServiceInfos(catalog, serviceList, opts, schemaCompiler, schemaCollection);
}
Also used : InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) URI(java.net.URI) URL(java.net.URL) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) OASISCatalogManager(org.apache.cxf.catalog.OASISCatalogManager) ArrayList(java.util.ArrayList) List(java.util.List) NodeList(org.w3c.dom.NodeList) ToolException(org.apache.cxf.tools.common.ToolException) HashSet(java.util.HashSet) SchemaInfo(org.apache.cxf.service.model.SchemaInfo) Bus(org.apache.cxf.Bus) InputStream(java.io.InputStream) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) SAXException(org.xml.sax.SAXException) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) DOMException(org.w3c.dom.DOMException) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) SAXParseException(org.xml.sax.SAXParseException) XMLStreamException(javax.xml.stream.XMLStreamException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) File(java.io.File)

Example 49 with XmlSchema

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

the class WSDLSchemaManager method attachSchemaToWSDL.

public void attachSchemaToWSDL(Definition definition, XmlSchema schema, boolean isSchemaGenerated) throws Exception {
    Types types = definition.getTypes();
    if (types == null) {
        types = definition.createTypes();
        definition.setTypes(types);
    }
    Schema wsdlSchema = (Schema) definition.getExtensionRegistry().createExtension(Types.class, new QName(Constants.URI_2001_SCHEMA_XSD, "schema"));
    // See if a NamespaceMap has already been added to the schema (this can be the case with object
    // references. If so, simply add the XSD URI to the map. Otherwise, create a new one.
    NamespaceMap nsMap = null;
    try {
        nsMap = (NamespaceMap) schema.getNamespaceContext();
    } catch (ClassCastException ex) {
    // Consume. This will mean that the context has not been set.
    }
    if (nsMap == null) {
        nsMap = new NamespaceMap();
        nsMap.add("xs", Constants.URI_2001_SCHEMA_XSD);
        schema.setNamespaceContext(nsMap);
    } else {
        nsMap.add("xs", Constants.URI_2001_SCHEMA_XSD);
    }
    if (isSchemaGenerated) {
        nsMap.add("tns", schema.getTargetNamespace());
    }
    org.w3c.dom.Element el = schema.getAllSchemas()[0].getDocumentElement();
    wsdlSchema.setElement(el);
    for (XmlSchemaExternal ext : schema.getExternals()) {
        if (ext instanceof XmlSchemaImport) {
            XmlSchemaImport xmlSchemaImport = (XmlSchemaImport) ext;
            SchemaImport schemaimport = wsdlSchema.createImport();
            schemaimport.setNamespaceURI(xmlSchemaImport.getNamespace());
            if (xmlSchemaImport.getSchemaLocation() != null && !ignoreImports) {
                schemaimport.setSchemaLocationURI(xmlSchemaImport.getSchemaLocation());
            }
            wsdlSchema.addImport(schemaimport);
        }
    }
    types.addExtensibilityElement(wsdlSchema);
}
Also used : Types(javax.wsdl.Types) XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) SchemaImport(javax.wsdl.extensions.schema.SchemaImport) QName(javax.xml.namespace.QName) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(javax.wsdl.extensions.schema.Schema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport)

Example 50 with XmlSchema

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

the class WSDLToCorbaHelper method getSchemaType.

public XmlSchemaType getSchemaType(QName name) throws Exception {
    XmlSchemaType type = null;
    for (XmlSchema xmlSchema : xmlSchemaList.getXmlSchemas()) {
        String nspace = name.getNamespaceURI();
        if (nspace == null) {
            nspace = xmlSchema.getTargetNamespace();
        }
        // QName tname = createQName(nspace, name.getLocalPart(), "xsd");
        QName tname = createQName(nspace, name.getLocalPart(), "");
        type = findSchemaType(tname);
        if (type != null) {
            break;
        }
    }
    return type;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) QName(javax.xml.namespace.QName) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType)

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