Search in sources :

Example 26 with SchemaInfo

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

the class BasicNameManager method initialize.

private void initialize(ServiceInfo service, Endpoint endpoint) {
    nsPrefixMap = new HashMap<>();
    if (endpoint != null) {
        JavascriptOptionsFeature options = getOptions(endpoint);
        if (options.getNamespacePrefixMap() != null) {
            nsPrefixMap.putAll(options.getNamespacePrefixMap());
        }
    }
    Set<String> poorPrefixURIs = new HashSet<>();
    for (SchemaInfo schemaInfo : service.getSchemas()) {
        NamespacePrefixList schemaPrefixList = schemaInfo.getSchema().getNamespaceContext();
        for (String declaredPrefix : schemaPrefixList.getDeclaredPrefixes()) {
            String uri = schemaPrefixList.getNamespaceURI(declaredPrefix);
            if (!nsPrefixMap.containsKey(uri)) {
                // first schema to define a prefix wins.
                if (declaredPrefix.startsWith("ns") || "tns".equals(declaredPrefix)) {
                    poorPrefixURIs.add(uri);
                } else {
                    nsPrefixMap.put(uri, declaredPrefix.toUpperCase());
                }
            }
        }
    }
    for (String uri : poorPrefixURIs) {
        defineFallbackPrefix(uri);
    }
}
Also used : NamespacePrefixList(org.apache.ws.commons.schema.utils.NamespacePrefixList) HashSet(java.util.HashSet) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 27 with SchemaInfo

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

the class GenericAegisTest method testGenerateJavascript.

// the claim is that code generation makes this go boom.
@Test
public void testGenerateJavascript() throws Exception {
    // Create our service implementation
    GenericGenericClass<String> impl = new GenericGenericClass<String>();
    // Create our Server
    ServerFactoryBean svrFactory = new ServerFactoryBean();
    // we sure can't get a .class for the interface, can we?
    svrFactory.setServiceClass(impl.getClass());
    svrFactory.setAddress("http://localhost:" + PORT + "/aegisgeneric");
    svrFactory.setServiceBean(impl);
    Server server = svrFactory.create();
    ServiceInfo serviceInfo = ((EndpointImpl) server.getEndpoint()).getEndpointInfo().getService();
    Collection<SchemaInfo> schemata = serviceInfo.getSchemas();
    BasicNameManager nameManager = BasicNameManager.newNameManager(serviceInfo);
    NamespacePrefixAccumulator prefixManager = new NamespacePrefixAccumulator(serviceInfo.getXmlSchemaCollection());
    for (SchemaInfo schema : schemata) {
        SchemaJavascriptBuilder builder = new SchemaJavascriptBuilder(serviceInfo.getXmlSchemaCollection(), prefixManager, nameManager);
        String allThatJavascript = builder.generateCodeForSchema(schema.getSchema());
        assertNotNull(allThatJavascript);
    }
    ServiceJavascriptBuilder serviceBuilder = new ServiceJavascriptBuilder(serviceInfo, null, prefixManager, nameManager);
    serviceBuilder.walk();
    String serviceJavascript = serviceBuilder.getCode();
    assertNotNull(serviceJavascript);
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) ServiceJavascriptBuilder(org.apache.cxf.javascript.service.ServiceJavascriptBuilder) Server(org.apache.cxf.endpoint.Server) ServerFactoryBean(org.apache.cxf.frontend.ServerFactoryBean) GenericGenericClass(org.apache.cxf.javascript.fortest.GenericGenericClass) SchemaInfo(org.apache.cxf.service.model.SchemaInfo) SchemaJavascriptBuilder(org.apache.cxf.javascript.types.SchemaJavascriptBuilder) Test(org.junit.Test)

Example 28 with SchemaInfo

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

the class SerializationTest method setupClientAndRhino.

private void setupClientAndRhino(String clientProxyFactoryBeanId) throws IOException {
    testUtilities.setBus(getBean(Bus.class, "cxf"));
    testUtilities.initializeRhino();
    testUtilities.readResourceIntoRhino("/org/apache/cxf/javascript/cxf-utils.js");
    clientProxyFactory = getBean(JaxWsProxyFactoryBean.class, clientProxyFactoryBeanId);
    client = clientProxyFactory.getClientFactoryBean().create();
    serviceInfos = client.getEndpoint().getService().getServiceInfos();
    // there can only be one.
    assertEquals(1, serviceInfos.size());
    ServiceInfo serviceInfo = serviceInfos.get(0);
    schemata = serviceInfo.getSchemas();
    nameManager = BasicNameManager.newNameManager(serviceInfo);
    NamespacePrefixAccumulator prefixAccumulator = new NamespacePrefixAccumulator(serviceInfo.getXmlSchemaCollection());
    for (SchemaInfo schema : schemata) {
        SchemaJavascriptBuilder builder = new SchemaJavascriptBuilder(serviceInfo.getXmlSchemaCollection(), prefixAccumulator, nameManager);
        String allThatJavascript = builder.generateCodeForSchema(schema.getSchema());
        assertNotNull(allThatJavascript);
        testUtilities.readStringIntoRhino(allThatJavascript, schema.toString() + ".js");
    }
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) Bus(org.apache.cxf.Bus) JaxWsProxyFactoryBean(org.apache.cxf.jaxws.JaxWsProxyFactoryBean) NamespacePrefixAccumulator(org.apache.cxf.javascript.NamespacePrefixAccumulator) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 29 with SchemaInfo

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

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

the class JavaToJSProcessor method process.

public void process() throws ToolException {
    String oldClassPath = System.getProperty(JAVA_CLASS_PATH);
    LOG.log(Level.INFO, "OLD_CP", oldClassPath);
    if (context.get(ToolConstants.CFG_CLASSPATH) != null) {
        String newCp = (String) context.get(ToolConstants.CFG_CLASSPATH);
        System.setProperty(JAVA_CLASS_PATH, newCp + File.pathSeparator + oldClassPath);
        LOG.log(Level.INFO, "NEW_CP", newCp);
    }
    // check for command line specification of data binding.
    ServiceBuilder builder = getServiceBuilder();
    ServiceInfo serviceInfo = builder.createService();
    File jsFile = getOutputFile(builder.getOutputFile(), serviceInfo.getName().getLocalPart() + ".js");
    BasicNameManager nameManager = BasicNameManager.newNameManager(serviceInfo, null);
    NamespacePrefixAccumulator prefixManager = new NamespacePrefixAccumulator(serviceInfo.getXmlSchemaCollection());
    Collection<SchemaInfo> schemata = serviceInfo.getSchemas();
    BufferedWriter writer = null;
    try {
        OutputStream outputStream = Files.newOutputStream(jsFile.toPath());
        if (null != context.get(ToolConstants.CFG_JAVASCRIPT_UTILS)) {
            JavascriptGetInterceptor.writeUtilsToResponseStream(JavaToJSProcessor.class, outputStream);
        }
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, UTF8);
        writer = new BufferedWriter(outputStreamWriter);
        for (SchemaInfo schema : schemata) {
            SchemaJavascriptBuilder jsBuilder = new SchemaJavascriptBuilder(serviceInfo.getXmlSchemaCollection(), prefixManager, nameManager);
            String allThatJavascript = jsBuilder.generateCodeForSchema(schema.getSchema());
            writer.append(allThatJavascript);
        }
        ServiceJavascriptBuilder serviceBuilder = new ServiceJavascriptBuilder(serviceInfo, null, prefixManager, nameManager);
        serviceBuilder.walk();
        String serviceJavascript = serviceBuilder.getCode();
        writer.append(serviceJavascript);
    } catch (FileNotFoundException e) {
        throw new ToolException(e);
    } catch (IOException e) {
        throw new ToolException(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                throw new ToolException(e);
            }
        }
    }
    System.setProperty(JAVA_CLASS_PATH, oldClassPath);
    LOG.log(Level.INFO, "RESUME_CP", oldClassPath);
}
Also used : ServiceJavascriptBuilder(org.apache.cxf.javascript.service.ServiceJavascriptBuilder) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ServiceBuilder(org.apache.cxf.service.ServiceBuilder) BufferedWriter(java.io.BufferedWriter) SchemaJavascriptBuilder(org.apache.cxf.javascript.types.SchemaJavascriptBuilder) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) BasicNameManager(org.apache.cxf.javascript.BasicNameManager) OutputStreamWriter(java.io.OutputStreamWriter) ToolException(org.apache.cxf.tools.common.ToolException) NamespacePrefixAccumulator(org.apache.cxf.javascript.NamespacePrefixAccumulator) File(java.io.File) 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