Search in sources :

Example 21 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo 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 22 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class WSDL11Generator method generate.

public Definition generate(final File dir) {
    File file = getOutputBase();
    if (file == null && dir != null) {
        if (dir.isDirectory()) {
            file = new File(dir, getServiceModel().getName().getLocalPart() + ".wsdl");
        } else {
            file = dir;
        }
    } else if (dir == null) {
        file = new File(getServiceModel().getName().getLocalPart() + ".wsdl");
    }
    File outputdir = createOutputDir(file);
    Definition def = null;
    try {
        Writer os = new FileWriterUtil(file.getParent(), getOutputStreamCreator()).getWriter(file, StandardCharsets.UTF_8.name());
        WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
        ServiceWSDLBuilder builder = new ServiceWSDLBuilder(getBus(), getServiceModel());
        builder.setUseSchemaImports(this.allowImports());
        String name = file.getName();
        if (name.endsWith(".wsdl")) {
            name = name.substring(0, name.lastIndexOf(".wsdl"));
        }
        builder.setBaseFileName(name);
        Map<String, SchemaInfo> imports = new HashMap<>();
        def = builder.build(imports);
        wsdlWriter.writeWSDL(def, os);
        os.close();
        if (def.getImports().size() > 0) {
            for (Import wsdlImport : WSDLDefinitionBuilder.getImports(def)) {
                Definition wsdlDef = wsdlImport.getDefinition();
                File wsdlFile = null;
                if (!StringUtils.isEmpty(wsdlImport.getLocationURI())) {
                    wsdlFile = new File(outputdir, wsdlImport.getLocationURI());
                } else {
                    wsdlFile = new File(outputdir, wsdlDef.getQName().getLocalPart() + ".wsdl");
                }
                try (OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()))) {
                    wsdlWriter.writeWSDL(wsdlDef, wsdlOs);
                }
            }
        }
        for (Map.Entry<String, SchemaInfo> imp : imports.entrySet()) {
            File impfile = new File(file.getParentFile(), imp.getKey());
            Element el = imp.getValue().getElement();
            updateImports(el, imports);
            FileWriterUtil fileWriterUtil = new FileWriterUtil(impfile.getParent(), getToolContext().get(OutputStreamCreator.class));
            os = fileWriterUtil.getWriter(impfile, StandardCharsets.UTF_8.name());
            StaxUtils.writeTo(el, os, 2);
            os.close();
        }
        customizing(outputdir, name, imports.keySet());
    } catch (WSDLException wex) {
        wex.printStackTrace();
    } catch (FileNotFoundException fnfe) {
        throw new ToolException("Output file " + file + " not found", fnfe);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return def;
}
Also used : Import(javax.wsdl.Import) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) HashMap(java.util.HashMap) WSDLException(javax.wsdl.WSDLException) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) Element(org.w3c.dom.Element) Definition(javax.wsdl.Definition) FileNotFoundException(java.io.FileNotFoundException) WSDLWriter(javax.wsdl.xml.WSDLWriter) IOException(java.io.IOException) ServiceWSDLBuilder(org.apache.cxf.wsdl11.ServiceWSDLBuilder) XMLStreamException(javax.xml.stream.XMLStreamException) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) ToolException(org.apache.cxf.tools.common.ToolException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) WSDLWriter(javax.wsdl.xml.WSDLWriter) Writer(java.io.Writer) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 23 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class SourceGenerator method getGrammarInfo.

private GrammarInfo getGrammarInfo(Application app, List<SchemaInfo> schemaElements) {
    if (schemaElements == null || schemaElements.isEmpty()) {
        return new GrammarInfo();
    }
    Map<String, String> nsMap = new HashMap<>();
    NamedNodeMap attrMap = app.getAppElement().getAttributes();
    for (int i = 0; i < attrMap.getLength(); i++) {
        Node node = attrMap.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.startsWith("xmlns:")) {
            String nsValue = node.getNodeValue();
            nsMap.put(nodeName.substring(6), nsValue);
        }
    }
    Map<String, String> elementTypeMap = new HashMap<>();
    for (SchemaInfo schemaEl : schemaElements) {
        populateElementTypeMap(app, schemaEl.getElement(), schemaEl.getSystemId(), elementTypeMap);
    }
    boolean noTargetNamespace = schemaElements.size() == 1 && schemaElements.get(0).getNamespaceURI().isEmpty();
    return new GrammarInfo(nsMap, elementTypeMap, noTargetNamespace);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) Node(org.w3c.dom.Node) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 24 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class SourceGenerator method getSchemaElements.

private List<SchemaInfo> getSchemaElements(Application app) {
    List<Element> grammarEls = getWadlElements(app.getAppElement(), "grammars");
    if (grammarEls.size() != 1) {
        return null;
    }
    List<SchemaInfo> schemas = new ArrayList<>();
    List<Element> schemasEls = DOMUtils.getChildrenWithName(grammarEls.get(0), Constants.URI_2001_SCHEMA_XSD, "schema");
    for (int i = 0; i < schemasEls.size(); i++) {
        String systemId = app.getWadlPath();
        if (schemasEls.size() > 1) {
            systemId += "#grammar" + (i + 1);
        }
        schemas.add(createSchemaInfo(schemasEls.get(i), systemId));
    }
    List<Element> includeEls = getWadlElements(grammarEls.get(0), "include");
    for (Element includeEl : includeEls) {
        String href = includeEl.getAttribute("href");
        String schemaURI = resolveLocationWithCatalog(href);
        if (schemaURI == null) {
            if (!URI.create(href).isAbsolute() && app.getWadlPath() != null) {
                String baseWadlPath = getBaseWadlPath(app.getWadlPath());
                if (!href.startsWith("/") && !href.contains("..")) {
                    schemaURI = baseWadlPath + href;
                } else {
                    try {
                        schemaURI = new URL(new URL(baseWadlPath), href).toString();
                    } catch (Exception ex) {
                        schemaURI = URI.create(baseWadlPath).resolve(href).toString();
                    }
                }
            } else {
                schemaURI = href;
            }
        }
        schemas.add(createSchemaInfo(readIncludedDocument(schemaURI), schemaURI));
    }
    return schemas;
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) URL(java.net.URL) 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 25 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class EndpointReferenceUtils method createSchema.

private static synchronized Schema createSchema(ServiceInfo serviceInfo, Bus b) {
    if (b == null) {
        b = BusFactory.getThreadDefaultBus(false);
    }
    Schema schema = serviceInfo.getProperty(Schema.class.getName(), Schema.class);
    if (schema == null) {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Map<String, byte[]> schemaSourcesMap = new LinkedHashMap<String, byte[]>();
        Map<String, Source> schemaSourcesMap2 = new LinkedHashMap<String, Source>();
        XMLStreamWriter writer = null;
        try {
            for (SchemaInfo si : serviceInfo.getSchemas()) {
                Element el = si.getElement();
                unsetReadonly(el);
                String baseURI = null;
                try {
                    baseURI = el.getBaseURI();
                } catch (Exception ex) {
                // ignore - not DOM level 3
                }
                if (baseURI == null) {
                    baseURI = si.getSystemId();
                }
                DOMSource ds = new DOMSource(el, baseURI);
                schemaSourcesMap2.put(si.getSystemId() + ":" + si.getNamespaceURI(), ds);
                LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
                writer = StaxUtils.createXMLStreamWriter(out);
                StaxUtils.copy(el, writer);
                writer.flush();
                schemaSourcesMap.put(si.getSystemId() + ":" + si.getNamespaceURI(), out.toByteArray());
            }
            for (XmlSchema sch : serviceInfo.getXmlSchemaCollection().getXmlSchemas()) {
                if (sch.getSourceURI() != null && !schemaSourcesMap.containsKey(sch.getSourceURI() + ":" + sch.getTargetNamespace())) {
                    InputStream ins = null;
                    try {
                        URL url = new URL(sch.getSourceURI());
                        ins = url.openStream();
                    } catch (Exception e) {
                    // ignore, we'll just use what we have.  (though
                    // bugs in XmlSchema could make this less useful)
                    }
                    LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream();
                    if (ins == null) {
                        sch.write(out);
                    } else {
                        IOUtils.copyAndCloseInput(ins, out);
                    }
                    schemaSourcesMap.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), out.toByteArray());
                    Source source = new StreamSource(out.createInputStream(), sch.getSourceURI());
                    schemaSourcesMap2.put(sch.getSourceURI() + ":" + sch.getTargetNamespace(), source);
                }
            }
            factory.setResourceResolver(new SchemaLSResourceResolver(schemaSourcesMap, b));
            schema = factory.newSchema(schemaSourcesMap2.values().toArray(new Source[schemaSourcesMap2.size()]));
        } catch (Exception ex) {
            // Something not right with the schema from the wsdl.
            LOG.log(Level.WARNING, "SAXException for newSchema()", ex);
            for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
                String s = StaxUtils.toString(schemaInfo.getElement(), 4);
                LOG.log(Level.INFO, "Schema for: " + schemaInfo.getNamespaceURI() + "\n" + s);
            }
        } finally {
            for (Source src : schemaSourcesMap2.values()) {
                if (src instanceof DOMSource) {
                    Node nd = ((DOMSource) src).getNode();
                    unsetReadonly(nd);
                }
            }
            StaxUtils.close(writer);
        }
        serviceInfo.setProperty(Schema.class.getName(), schema);
    }
    return schema;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) XmlSchema(org.apache.ws.commons.schema.XmlSchema) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Aggregations

SchemaInfo (org.apache.cxf.service.model.SchemaInfo)42 XmlSchema (org.apache.ws.commons.schema.XmlSchema)17 QName (javax.xml.namespace.QName)11 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)11 Element (org.w3c.dom.Element)10 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)9 IOException (java.io.IOException)8 HashMap (java.util.HashMap)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 Message (org.apache.cxf.common.i18n.Message)6 FileNotFoundException (java.io.FileNotFoundException)5 URL (java.net.URL)5 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)5 InputSource (org.xml.sax.InputSource)5 SAXException (org.xml.sax.SAXException)5 SAXParseException (org.xml.sax.SAXParseException)5 File (java.io.File)4 Method (java.lang.reflect.Method)4 HashSet (java.util.HashSet)4 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)4