Search in sources :

Example 1 with XmlSchemaSerializerException

use of org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException 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)

Example 2 with XmlSchemaSerializerException

use of org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException 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 3 with XmlSchemaSerializerException

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

the class JAXBDataBinding method getSchemaNode.

private Object getSchemaNode(XmlSchema schema, SchemaCollection schemaCollection) {
    XmlSchemaSerializer xser = new XmlSchemaSerializer();
    xser.setExtReg(schemaCollection.getExtReg());
    Document[] docs;
    try {
        docs = xser.serializeSchema(schema, false);
    } catch (XmlSchemaSerializerException e) {
        throw new RuntimeException(e);
    }
    return docs[0].getDocumentElement();
}
Also used : XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) Document(org.w3c.dom.Document)

Example 4 with XmlSchemaSerializerException

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

the class DynamicClientFactory method addSchemas.

private void addSchemas(Options opts, SchemaCompiler schemaCompiler, List<ServiceInfo> serviceList, SchemaCollection schemaCollection) {
    Map<String, Element> done = new HashMap<>();
    Map<String, Element> notDone = new HashMap<>();
    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 (done.containsKey(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();
            ele = removeImportElement(ele, key, catalog, done, notDone);
            try {
                docs[0].setDocumentURI(key);
            } catch (Throwable t) {
            // ignore - DOM level 3
            }
            if (ele != null) {
                InputSource is = new InputSource((InputStream) null);
                // key = key.replaceFirst("#types[0-9]+$", "");
                is.setSystemId(key);
                is.setPublicId(key);
                opts.addGrammar(is);
                schemaCompiler.parseSchema(key, ele);
            }
        }
    }
    for (XmlSchema schema : schemaCollection.getXmlSchemas()) {
        if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
            continue;
        }
        String key = schema.getSourceURI();
        if (done.containsKey(key)) {
            continue;
        }
        if (key.startsWith("file:") || key.startsWith("jar:")) {
            InputStream in = null;
            try {
                if (key.contains("#")) {
                    for (ServiceInfo si : serviceList) {
                        for (SchemaInfo sci : si.getSchemas()) {
                            if (key != null && key.equals(sci.getSystemId())) {
                                key = null;
                            }
                        }
                    }
                }
                if (key == null) {
                    continue;
                }
                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 = 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
                    }
                }
            }
        }
    }
    for (ServiceInfo si : serviceList) {
        for (SchemaInfo sci : si.getSchemas()) {
            String key = sci.getSystemId();
            if (done.containsKey(key)) {
                continue;
            }
            Element ele = sci.getElement();
            ele = removeImportElement(ele, key, catalog, done, notDone);
            if (ele != null) {
                InputSource is = new InputSource((InputStream) null);
                // key = key.replaceFirst("#types[0-9]+$", "");
                is.setSystemId(key);
                is.setPublicId(key);
                opts.addGrammar(is);
                schemaCompiler.parseSchema(key, StaxUtils.createXMLStreamReader(ele, key));
            }
        }
    }
    for (Map.Entry<String, Element> el : notDone.entrySet()) {
        InputSource is = new InputSource((InputStream) null);
        // key = key.replaceFirst("#types[0-9]+$", "");
        is.setSystemId(el.getKey());
        is.setPublicId(el.getKey());
        opts.addGrammar(is);
        schemaCompiler.parseSchema(el.getKey(), StaxUtils.createXMLStreamReader(el.getValue(), el.getKey()));
    }
}
Also used : InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) 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) SchemaInfo(org.apache.cxf.service.model.SchemaInfo) InputStream(java.io.InputStream) XmlSchemaSerializer(org.apache.ws.commons.schema.XmlSchemaSerializer) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) DOMException(org.w3c.dom.DOMException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) JarFile(java.util.jar.JarFile) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 5 with XmlSchemaSerializerException

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

the class ServiceWSDLBuilder method buildTypes.

protected void buildTypes(final Collection<SchemaInfo> schemas, final Map<String, SchemaInfo> imports, final Definition def) {
    Types types = def.createTypes();
    for (SchemaInfo schemaInfo : schemas) {
        Schema schemaImpl = getSchemaImplementation(def);
        schemaImpl.setRequired(true);
        schemaImpl.setElementType(WSDLConstants.QNAME_SCHEMA);
        schemaImpl.setElement(schemaInfo.getElement());
        for (XmlSchemaExternal ext : schemaInfo.getSchema().getExternals()) {
            if (ext.getSchema() == null) {
                continue;
            }
            if (ext instanceof XmlSchemaImport) {
                SchemaImport imp = schemaImpl.createImport();
                imp.setNamespaceURI(((XmlSchemaImport) ext).getNamespace());
                imp.setSchemaLocationURI(((XmlSchemaImport) ext).getSchemaLocation());
                Schema schemaImpl2 = getSchemaImplementation(def);
                schemaImpl2.setRequired(true);
                schemaImpl2.setElementType(WSDLConstants.QNAME_SCHEMA);
                schemaImpl2.setDocumentBaseURI(ext.getSchema().getSourceURI());
                try {
                    schemaImpl2.setElement(ext.getSchema().getSchemaDocument().getDocumentElement());
                } catch (XmlSchemaSerializerException e) {
                // ignore
                }
                imp.setReferencedSchema(schemaImpl2);
                schemaImpl.addImport(imp);
            } else if (ext instanceof XmlSchemaInclude) {
                SchemaReference imp = schemaImpl.createInclude();
                imp.setSchemaLocationURI(((XmlSchemaInclude) ext).getSchemaLocation());
                Schema schemaImpl2 = getSchemaImplementation(def);
                schemaImpl2.setRequired(true);
                schemaImpl2.setElementType(WSDLConstants.QNAME_SCHEMA);
                schemaImpl2.setDocumentBaseURI(ext.getSchema().getSourceURI());
                try {
                    schemaImpl2.setElement(ext.getSchema().getSchemaDocument().getDocumentElement());
                } catch (XmlSchemaSerializerException e) {
                // ignore
                }
                imp.setReferencedSchema(schemaImpl2);
                schemaImpl.addInclude(imp);
            } else if (ext instanceof XmlSchemaRedefine) {
                SchemaReference imp = schemaImpl.createRedefine();
                imp.setSchemaLocationURI(((XmlSchemaRedefine) ext).getSchemaLocation());
                Schema schemaImpl2 = getSchemaImplementation(def);
                schemaImpl2.setRequired(true);
                schemaImpl2.setElementType(WSDLConstants.QNAME_SCHEMA);
                schemaImpl2.setDocumentBaseURI(ext.getSchema().getSourceURI());
                try {
                    schemaImpl2.setElement(ext.getSchema().getSchemaDocument().getDocumentElement());
                } catch (XmlSchemaSerializerException e) {
                // ignore
                }
                imp.setReferencedSchema(schemaImpl2);
                schemaImpl.addRedefine(imp);
            }
        }
        types.addExtensibilityElement(schemaImpl);
    }
    def.setTypes(types);
}
Also used : SchemaReference(javax.wsdl.extensions.schema.SchemaReference) Types(javax.wsdl.Types) XmlSchemaRedefine(org.apache.ws.commons.schema.XmlSchemaRedefine) XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) SchemaImport(javax.wsdl.extensions.schema.SchemaImport) Schema(javax.wsdl.extensions.schema.Schema) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) XmlSchemaInclude(org.apache.ws.commons.schema.XmlSchemaInclude) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Aggregations

XmlSchemaSerializerException (org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException)5 Document (org.w3c.dom.Document)4 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)3 XmlSchema (org.apache.ws.commons.schema.XmlSchema)3 XmlSchemaSerializer (org.apache.ws.commons.schema.XmlSchemaSerializer)3 Element (org.w3c.dom.Element)3 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 URL (java.net.URL)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 OASISCatalogManager (org.apache.cxf.catalog.OASISCatalogManager)2 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)2 DOMException (org.w3c.dom.DOMException)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 BadCommandLineException (com.sun.tools.xjc.BadCommandLineException)1