Search in sources :

Example 1 with SchemaInfo

use of com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo in project enunciate by stoicflame.

the class TestEnunciateIDLModule method testAgainstFullAPI.

/**
 * Tests the xml artifact generation against the "full" API.
 */
public void testAgainstFullAPI() throws Exception {
    Map<String, String> prefixes = new HashMap<String, String>();
    prefixes.put(FULL_NAMESPACE, "full");
    prefixes.put(DATA_NAMESPACE, "data");
    prefixes.put(CITE_NAMESPACE, "cite");
    Properties testProperties = new Properties();
    testProperties.load(TestEnunciateIDLModule.class.getResourceAsStream("/test.properties"));
    String samplePath = testProperties.getProperty("api.sample.dir");
    assertNotNull(samplePath);
    File sampleDir = new File(samplePath);
    assertTrue(sampleDir.exists());
    Enunciate engine = new Enunciate().addSourceDir(sampleDir).loadConfiguration(TestEnunciateIDLModule.class.getResourceAsStream("test-idl-module-config.xml")).loadDiscoveredModules();
    String cp = System.getProperty("java.class.path");
    String[] path = cp.split(File.pathSeparator);
    List<File> classpath = new ArrayList<File>(path.length);
    for (String element : path) {
        File entry = new File(element);
        if (entry.exists() && !new File(entry, "test.properties").exists()) {
            classpath.add(entry);
        }
    }
    engine.setClasspath(classpath);
    engine.run();
    IDLModule idlModule = null;
    for (EnunciateModule candidate : engine.getModules()) {
        if (candidate instanceof IDLModule) {
            idlModule = (IDLModule) candidate;
            break;
        }
    }
    assertNotNull(idlModule);
    assertNotNull(idlModule.jaxbModule);
    assertNotNull(idlModule.jaxwsModule);
    assertNotNull(idlModule.jaxrsModule);
    final Map<String, String> schemas = new HashMap<String, String>();
    for (SchemaInfo schemaInfo : idlModule.jaxbModule.getJaxbContext().getSchemas().values()) {
        assertNotNull(schemaInfo.getSchemaFile());
        assertTrue(schemaInfo.getSchemaFile() instanceof JaxbSchemaFile);
        String filename = ((JaxbSchemaFile) schemaInfo.getSchemaFile()).filename;
        assertNotNull(filename);
        if (prefixes.containsKey(schemaInfo.getNamespace())) {
            assertEquals(prefixes.get(schemaInfo.getNamespace()) + ".xsd", filename);
        }
        StringWriter schemaOut = new StringWriter();
        ((JaxbSchemaFile) schemaInfo.getSchemaFile()).writeTo(schemaOut);
        schemaOut.flush();
        schemas.put(filename, schemaOut.toString());
    }
    WsdlInfo fullWsdlInfo = idlModule.jaxwsModule.getJaxwsContext().getWsdls().get(FULL_NAMESPACE);
    assertNotNull(fullWsdlInfo);
    assertEquals("full.wsdl", fullWsdlInfo.getFilename());
    assertNotNull(fullWsdlInfo.getWsdlFile());
    assertTrue(fullWsdlInfo.getWsdlFile() instanceof JaxwsWsdlFile);
    JaxwsWsdlFile fullWsdl = (JaxwsWsdlFile) fullWsdlInfo.getWsdlFile();
    assertEquals("full.wsdl", fullWsdl.filename);
    final StringWriter output = new StringWriter();
    fullWsdl.writeTo(output);
    output.flush();
    // make sure the wsdl is built correctly
    WSDLReader wsdlReader = WSDLFactory.newInstance().newWSDLReader();
    Definition definition = wsdlReader.readWSDL(new InMemoryWSDLLocator(output, schemas));
    assertEquals(FULL_NAMESPACE, definition.getTargetNamespace());
    Types types = definition.getTypes();
    List extensibilityElements = types.getExtensibilityElements();
    assertEquals(1, extensibilityElements.size());
    ExtensibilityElement ee = (ExtensibilityElement) extensibilityElements.get(0);
    assertEquals(new QName(W3C_XML_SCHEMA_NS_URI, "schema"), ee.getElementType());
    Schema schema = (Schema) ee;
    Map imports = schema.getImports();
    assertEquals(3, imports.size());
    assertNotNull(imports.get(DATA_NAMESPACE));
    assertNotNull(imports.get(CITE_NAMESPACE));
    assertNotNull(imports.get(null));
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    Element schemaElement = schema.getElement();
    // these namespaces need to be explicitly added because the transformer won't see any references to them...
    schemaElement.setAttribute("xmlns:data", DATA_NAMESPACE);
    schemaElement.setAttribute("xmlns:cite", CITE_NAMESPACE);
    schemaElement.setAttribute("xmlns:full", FULL_NAMESPACE);
    // write out the wsdl schema to its xml form so we can parse it with XSOM...
    DOMSource source = new DOMSource(schemaElement);
    StringWriter fullSchemaOutput = new StringWriter();
    StreamResult result = new StreamResult(fullSchemaOutput);
    transformer.transform(source, result);
    fullSchemaOutput.flush();
    // set up the XSOM Parser.
    final InputSource fullSource = new InputSource(new StringReader(fullSchemaOutput.toString()));
    fullSource.setSystemId("file:/");
    XSOMParser parser = new XSOMParser(new JAXPParser());
    // throw all errors and warnings.
    parser.setErrorHandler(new ThrowEverythingHandler());
    parser.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            String xsd = schemas.get(systemId.substring("file:/".length()));
            if (xsd == null) {
                return null;
            }
            InputSource source = new InputSource(new StringReader(xsd));
            source.setSystemId("file:/");
            return source;
        }
    });
    // make sure the schema included in the wsdl is correct.
    parser.parse(fullSource);
    XSSchemaSet schemaSet = parser.getResult();
    XSSchema wsdlSchema = schemaSet.getSchema(FULL_NAMESPACE);
    assertNotNull(wsdlSchema);
    // make sure the data schema is imported and correct.
    XSSchema dataSchema = schemaSet.getSchema(DATA_NAMESPACE);
    assertNotNull(dataSchema);
    assertDataSchemaStructure(dataSchema);
    // make sure the cite schema is imported and correct.
    XSSchema citeSchema = schemaSet.getSchema(CITE_NAMESPACE);
    assertNotNull(citeSchema);
    assertCiteSchemaStructure(citeSchema);
    // now verify the rest of the WSDL...
    assertWebServiceDefinition(definition);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) Schema(javax.wsdl.extensions.schema.Schema) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Element(org.w3c.dom.Element) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Enunciate(com.webcohesion.enunciate.Enunciate) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo) EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) TransformerFactory(javax.xml.transform.TransformerFactory) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) StreamResult(javax.xml.transform.stream.StreamResult) QName(javax.xml.namespace.QName) JAXPParser(com.sun.xml.xsom.parser.JAXPParser) IOException(java.io.IOException) File(java.io.File) WsdlInfo(com.webcohesion.enunciate.modules.jaxws.WsdlInfo) WSDLReader(javax.wsdl.xml.WSDLReader)

Example 2 with SchemaInfo

use of com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo in project enunciate by stoicflame.

the class JavaXMLClientModule method generateClientSources.

protected File generateClientSources() {
    File sourceDir = getSourceDir();
    sourceDir.mkdirs();
    Map<String, Object> model = new HashMap<String, Object>();
    Map<String, String> conversions = getClientPackageConversions();
    EnunciateJaxbContext jaxbContext = this.jaxbModule.getJaxbContext();
    model.put("packageFor", new ClientPackageForMethod(conversions, this.context));
    model.put("classnameFor", new ClientClassnameForMethod(conversions, jaxbContext));
    model.put("simpleNameFor", new SimpleNameForMethod(new ClientClassnameForMethod(conversions, jaxbContext, true)));
    model.put("file", new FileDirective(sourceDir, this.enunciate.getLogger()));
    model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
    model.put("annotationValue", new AnnotationValueMethod());
    Set<String> facetIncludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetIncludes());
    facetIncludes.addAll(getFacetIncludes());
    Set<String> facetExcludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetExcludes());
    facetExcludes.addAll(getFacetExcludes());
    FacetFilter facetFilter = new FacetFilter(facetIncludes, facetExcludes);
    model.put("isFacetExcluded", new IsFacetExcludedMethod(facetFilter));
    boolean upToDate = isUpToDateWithSources(sourceDir);
    if (!upToDate) {
        try {
            debug("Generating the Java client classes...");
            HashMap<String, WebFault> allFaults = new HashMap<String, WebFault>();
            AntPatternMatcher matcher = new AntPatternMatcher();
            matcher.setPathSeparator(".");
            if (this.jaxwsModule != null) {
                Set<String> seeAlsos = new TreeSet<String>();
                // for each endpoint interface.
                for (WsdlInfo wsdlInfo : this.jaxwsModule.getJaxwsContext().getWsdls().values()) {
                    for (EndpointInterface ei : wsdlInfo.getEndpointInterfaces()) {
                        if (facetFilter.accept(ei)) {
                            for (WebMethod webMethod : ei.getWebMethods()) {
                                if (facetFilter.accept(webMethod)) {
                                    for (WebMessage webMessage : webMethod.getMessages()) {
                                        if (webMessage instanceof RequestWrapper) {
                                            model.put("message", webMessage);
                                            processTemplate(getTemplateURL("client-request-bean.fmt"), model);
                                            seeAlsos.add(getBeanName(new ClientClassnameForMethod(conversions, jaxbContext), ((RequestWrapper) webMessage).getRequestBeanName()));
                                        } else if (webMessage instanceof ResponseWrapper) {
                                            model.put("message", webMessage);
                                            processTemplate(getTemplateURL("client-response-bean.fmt"), model);
                                            seeAlsos.add(getBeanName(new ClientClassnameForMethod(conversions, jaxbContext), ((ResponseWrapper) webMessage).getResponseBeanName()));
                                        } else if (webMessage instanceof WebFault) {
                                            WebFault fault = (WebFault) webMessage;
                                            allFaults.put(fault.getQualifiedName().toString(), fault);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // gather the annotation information and process the possible beans for each web fault.
                for (WebFault webFault : allFaults.values()) {
                    boolean implicit = webFault.isImplicitSchemaElement();
                    String faultBean = implicit ? getBeanName(new ClientClassnameForMethod(conversions, jaxbContext), webFault.getImplicitFaultBeanQualifiedName()) : new ClientClassnameForMethod(conversions, jaxbContext).convert(webFault.getExplicitFaultBeanType());
                    seeAlsos.add(faultBean);
                    if (implicit) {
                        model.put("fault", webFault);
                        processTemplate(getTemplateURL("client-fault-bean.fmt"), model);
                    }
                }
                model.put("seeAlsoBeans", seeAlsos);
                model.put("baseUri", this.enunciate.getConfiguration().getApplicationRoot());
                for (WsdlInfo wsdlInfo : this.jaxwsModule.getJaxwsContext().getWsdls().values()) {
                    if (wsdlInfo.getWsdlFile() == null) {
                        throw new EnunciateException("WSDL " + wsdlInfo.getId() + " doesn't have a filename.");
                    }
                    for (EndpointInterface ei : wsdlInfo.getEndpointInterfaces()) {
                        if (facetFilter.accept(ei)) {
                            model.put("endpointInterface", ei);
                            model.put("wsdlFileName", wsdlInfo.getFilename());
                            processTemplate(getTemplateURL("client-endpoint-interface.fmt"), model);
                            processTemplate(getTemplateURL("client-soap-endpoint-impl.fmt"), model);
                        }
                    }
                }
                for (WebFault webFault : allFaults.values()) {
                    if (useServerSide(webFault, matcher)) {
                        copyServerSideType(sourceDir, webFault);
                    } else {
                        TypeElement superFault = (TypeElement) ((DeclaredType) webFault.getSuperclass()).asElement();
                        if (superFault != null && allFaults.containsKey(superFault.getQualifiedName().toString()) && allFaults.get(superFault.getQualifiedName().toString()).isImplicitSchemaElement()) {
                            model.put("superFault", allFaults.get(superFault.getQualifiedName().toString()));
                        } else {
                            model.remove("superFault");
                        }
                        model.put("fault", webFault);
                        processTemplate(getTemplateURL("client-web-fault.fmt"), model);
                    }
                }
            }
            for (SchemaInfo schemaInfo : this.jaxbModule.getJaxbContext().getSchemas().values()) {
                for (TypeDefinition typeDefinition : schemaInfo.getTypeDefinitions()) {
                    if (facetFilter.accept(typeDefinition)) {
                        if (useServerSide(typeDefinition, matcher)) {
                            copyServerSideType(sourceDir, typeDefinition);
                        } else {
                            model.put("rootEl", this.jaxbModule.getJaxbContext().findElementDeclaration(typeDefinition));
                            model.put("type", typeDefinition);
                            URL template = typeDefinition.isEnum() ? typeDefinition instanceof QNameEnumTypeDefinition ? getTemplateURL("client-qname-enum-type.fmt") : getTemplateURL("client-enum-type.fmt") : typeDefinition.isSimple() ? getTemplateURL("client-simple-type.fmt") : getTemplateURL("client-complex-type.fmt");
                            processTemplate(template, model);
                        }
                    }
                }
                for (Registry registry : schemaInfo.getRegistries()) {
                    model.put("registry", registry);
                    processTemplate(getTemplateURL("client-registry.fmt"), model);
                }
            }
        } catch (IOException e) {
            throw new EnunciateException(e);
        } catch (TemplateException e) {
            throw new EnunciateException(e);
        }
    } else {
        info("Skipping generation of Java client sources as everything appears up-to-date...");
    }
    context.setProperty(LIRBARY_DESCRIPTION_PROPERTY, readLibraryDescription(model));
    return sourceDir;
}
Also used : QNameEnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.QNameEnumTypeDefinition) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition) QNameEnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.QNameEnumTypeDefinition) EnunciateException(com.webcohesion.enunciate.EnunciateException) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo) TemplateException(freemarker.template.TemplateException) TypeElement(javax.lang.model.element.TypeElement) Registry(com.webcohesion.enunciate.modules.jaxb.model.Registry) EnunciateJaxbContext(com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext) AntPatternMatcher(com.webcohesion.enunciate.util.AntPatternMatcher) JavaFileObject(javax.tools.JavaFileObject) WsdlInfo(com.webcohesion.enunciate.modules.jaxws.WsdlInfo)

Example 3 with SchemaInfo

use of com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo in project enunciate by stoicflame.

the class JaxbContextClassListArtifact method exportTo.

@Override
public void exportTo(File fileOrDirectory, Enunciate enunciate) throws IOException {
    FileWriter out = new FileWriter(fileOrDirectory.isDirectory() ? new File(fileOrDirectory, getName()) : fileOrDirectory);
    for (SchemaInfo schemaInfo : this.jaxbContext.getSchemas().values()) {
        for (Registry registry : schemaInfo.getRegistries()) {
            out.write(registry.getQualifiedName() + "\n");
        }
        Collection<RootElementDeclaration> elements = schemaInfo.getRootElements();
        for (RootElementDeclaration element : elements) {
            out.write(element.getQualifiedName() + "\n");
        }
    }
    out.flush();
    out.close();
}
Also used : FileWriter(java.io.FileWriter) RootElementDeclaration(com.webcohesion.enunciate.modules.jaxb.model.RootElementDeclaration) Registry(com.webcohesion.enunciate.modules.jaxb.model.Registry) File(java.io.File) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)

Example 4 with SchemaInfo

use of com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo in project enunciate by stoicflame.

the class JAXBCodeErrors method findConflictingAccessorNamingErrors.

public static List<String> findConflictingAccessorNamingErrors(EnunciateJaxbContext context) {
    List<String> errors = (List<String>) context.getContext().getProperty(CONFLICTING_JAXB_ACCESSOR_NAMING_ERRORS_PROPERTY);
    if (errors == null) {
        errors = new ArrayList<String>();
        context.getContext().setProperty(CONFLICTING_JAXB_ACCESSOR_NAMING_ERRORS_PROPERTY, errors);
        for (SchemaInfo schemaInfo : context.getSchemas().values()) {
            for (TypeDefinition typeDefinition : schemaInfo.getTypeDefinitions()) {
                Map<String, Accessor> accessorsBySimpleName = new HashMap<String, Accessor>();
                for (Accessor accessor : typeDefinition.getAllAccessors()) {
                    String name = accessor.getClientSimpleName();
                    Accessor conflict = accessorsBySimpleName.get(name);
                    if (conflict != null) {
                        errors.add(String.format("%s: accessor \"%s\" conflicts with accessor \"%s\" of %s: both are named \"%s\".", typeDefinition.getQualifiedName(), accessor, conflict, conflict.getTypeDefinition().getQualifiedName(), name));
                    } else {
                        accessorsBySimpleName.put(name, accessor);
                    }
                }
            }
        }
    }
    return errors;
}
Also used : HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) Accessor(com.webcohesion.enunciate.modules.jaxb.model.Accessor) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 5 with SchemaInfo

use of com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo in project enunciate by stoicflame.

the class EnunciateJaxwsContext method add.

/**
 * Add an endpoint interface to the model.
 *
 * @param ei The endpoint interface to add to the model.
 */
public void add(EndpointInterface ei) {
    String namespace = ei.getTargetNamespace();
    String prefix = this.jaxbContext.addNamespace(namespace);
    WsdlInfo wsdlInfo = wsdls.get(namespace);
    if (wsdlInfo == null) {
        wsdlInfo = new WsdlInfo(jaxbContext);
        wsdlInfo.setId(prefix);
        wsdls.put(namespace, wsdlInfo);
        wsdlInfo.setTargetNamespace(namespace);
    }
    for (WebMethod webMethod : ei.getWebMethods()) {
        for (WebMessage webMessage : webMethod.getMessages()) {
            for (WebMessagePart messagePart : webMessage.getParts()) {
                if (messagePart.isImplicitSchemaElement()) {
                    ImplicitSchemaElement implicitElement = (ImplicitSchemaElement) messagePart;
                    String particleNamespace = messagePart.getParticleQName().getNamespaceURI();
                    SchemaInfo schemaInfo = this.jaxbContext.getSchemas().get(particleNamespace);
                    if (schemaInfo == null) {
                        schemaInfo = new SchemaInfo(this.jaxbContext);
                        schemaInfo.setId(this.jaxbContext.addNamespace(particleNamespace));
                        schemaInfo.setNamespace(particleNamespace);
                        this.jaxbContext.getSchemas().put(particleNamespace, schemaInfo);
                    }
                    schemaInfo.getImplicitSchemaElements().add(implicitElement);
                }
            }
        }
    }
    wsdlInfo.getEndpointInterfaces().add(ei);
    this.endpointInterfaces.add(ei);
    debug("Added %s as a JAX-WS endpoint interface.", ei.getQualifiedName());
    if (getContext().getProcessingEnvironment().findSourcePosition(ei) == null) {
        OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.log(getContext());
        if (OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.getLogged() <= 3) {
            info("Unable to find source file for %s.", ei.getQualifiedName());
        } else {
            debug("Unable to find source file for %s.", ei.getQualifiedName());
        }
    }
}
Also used : WebMethod(com.webcohesion.enunciate.modules.jaxws.model.WebMethod) ImplicitSchemaElement(com.webcohesion.enunciate.modules.jaxb.model.ImplicitSchemaElement) WebMessagePart(com.webcohesion.enunciate.modules.jaxws.model.WebMessagePart) WebMessage(com.webcohesion.enunciate.modules.jaxws.model.WebMessage) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)

Aggregations

SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)17 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)8 EnunciateException (com.webcohesion.enunciate.EnunciateException)5 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)5 File (java.io.File)5 WsdlInfo (com.webcohesion.enunciate.modules.jaxws.WsdlInfo)4 URL (java.net.URL)4 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)3 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)3 EnunciateJaxbContext (com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext)3 Element (com.webcohesion.enunciate.modules.jaxb.model.Element)3 AccessorOverridesAnotherMethod (com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod)3 FindRootElementMethod (com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod)3 TemplateException (freemarker.template.TemplateException)3 IOException (java.io.IOException)3 Attribute (com.webcohesion.enunciate.modules.jaxb.model.Attribute)2 Registry (com.webcohesion.enunciate.modules.jaxb.model.Registry)2 MapType (com.webcohesion.enunciate.modules.jaxb.model.util.MapType)2 ReferencedNamespacesMethod (com.webcohesion.enunciate.modules.jaxb.util.ReferencedNamespacesMethod)2 FileDirective (com.webcohesion.enunciate.util.freemarker.FileDirective)2