Search in sources :

Example 1 with TypeDefinition

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

the class XmlTypeVisitor method visitDeclared.

@Override
public XmlType visitDeclared(DeclaredType declaredType, Context context) {
    Element declaredElement = declaredType.asElement();
    String fqn = declaredElement instanceof TypeElement ? ((TypeElement) declaredElement).getQualifiedName().toString() : declaredType.toString();
    if (context.getStack().contains(fqn)) {
        // break the recursion.
        return KnownXmlType.ANY_TYPE;
    }
    context.getStack().push(fqn);
    try {
        AdapterType adapterType = JAXBUtil.findAdapterType(declaredElement, context.getContext());
        if (adapterType != null) {
            adapterType.getAdaptingType().accept(this, context);
        } else {
            MapType mapType = MapType.findMapType(declaredType, context.getContext());
            if (mapType != null) {
                XmlType keyType = mapType.getKeyType().accept(this, new Context(context.getContext(), false, false, context.stack));
                XmlType valueType = mapType.getValueType().accept(this, new Context(context.getContext(), false, false, context.stack));
                return new MapXmlType(keyType, valueType);
            } else {
                switch(declaredElement.getKind()) {
                    case ENUM:
                    case CLASS:
                        XmlType knownType = context.getContext().getKnownType(declaredElement);
                        if (knownType != null) {
                            return knownType;
                        } else {
                            // type not known, not specified.  Last chance: look for the type definition.
                            TypeDefinition typeDefinition = context.getContext().findTypeDefinition(declaredElement);
                            if (typeDefinition != null) {
                                return new XmlClassType(typeDefinition);
                            }
                        }
                        break;
                }
            }
        }
        return KnownXmlType.ANY_TYPE;
    } finally {
        context.getStack().pop();
    }
}
Also used : EnunciateJaxbContext(com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) AdapterType(com.webcohesion.enunciate.modules.jaxb.model.adapters.AdapterType) MapType(com.webcohesion.enunciate.modules.jaxb.model.util.MapType) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 2 with TypeDefinition

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

the class AccessorOverridesAnotherMethod method overridesAnother.

public Boolean overridesAnother(Accessor a) {
    TypeDefinition typeDefinition = a.getTypeDefinition();
    XmlType baseType = typeDefinition.getBaseType();
    if (baseType instanceof XmlClassType) {
        typeDefinition = ((XmlClassType) baseType).getTypeDefinition();
        while (typeDefinition != null) {
            ArrayList<Accessor> accessors = new ArrayList<Accessor>();
            accessors.addAll(typeDefinition.getAttributes());
            accessors.add(typeDefinition.getValue());
            accessors.addAll(typeDefinition.getElements());
            for (Accessor accessor : accessors) {
                if (a.overrides(accessor)) {
                    return true;
                }
            }
            baseType = typeDefinition.getBaseType();
            typeDefinition = baseType instanceof XmlClassType ? ((XmlClassType) baseType).getTypeDefinition() : null;
        }
    }
    return Boolean.FALSE;
}
Also used : XmlClassType(com.webcohesion.enunciate.modules.jaxb.model.types.XmlClassType) ArrayList(java.util.ArrayList) Accessor(com.webcohesion.enunciate.modules.jaxb.model.Accessor) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition) XmlType(com.webcohesion.enunciate.modules.jaxb.model.types.XmlType)

Example 3 with TypeDefinition

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

the class QNameForMediaTypeMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The QNameForType method must have a type mirror as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (unwrapped instanceof MediaTypeDescriptor) {
        MediaTypeDescriptor mt = (MediaTypeDescriptor) unwrapped;
        DataTypeReference typeReference = mt.getDataType();
        if (typeReference != null) {
            if (typeReference instanceof DataTypeReferenceImpl) {
                return ((DataTypeReferenceImpl) typeReference).getElementQName();
            } else if (associateJsonWithXml && mt.getMediaType().endsWith("json")) {
                DataType dataType = typeReference.getValue();
                if (dataType != null) {
                    XmlType knownType = this.context.getKnownType(dataType.getJavaElement());
                    if (knownType != null) {
                        return knownType.getQname();
                    }
                    TypeDefinition typeDefinition = this.context.findTypeDefinition(dataType.getJavaElement());
                    if (typeDefinition != null) {
                        return typeDefinition.getQname();
                    }
                }
            }
        }
    }
    return null;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) TemplateModelException(freemarker.template.TemplateModelException) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) DataTypeReferenceImpl(com.webcohesion.enunciate.modules.jaxb.api.impl.DataTypeReferenceImpl) DataType(com.webcohesion.enunciate.api.datatype.DataType) TemplateModel(freemarker.template.TemplateModel) XmlType(com.webcohesion.enunciate.modules.jaxb.model.types.XmlType) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 4 with TypeDefinition

use of com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition 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 5 with TypeDefinition

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

the class FunctionIdentifierForMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The functionIdentifierFor method must have an accessor or type mirror as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    TypeMirror typeMirror;
    if (unwrapped instanceof Accessor) {
        Accessor accessor = (Accessor) unwrapped;
        if (accessor.isAdapted()) {
            typeMirror = accessor.getAdapterType().getAdaptingType(accessor.getAccessorType(), this.context.getContext());
        } else {
            typeMirror = accessor.getAccessorType();
        }
    } else if (unwrapped instanceof TypeMirror) {
        typeMirror = (TypeMirror) unwrapped;
    } else {
        throw new TemplateModelException("The functionIdentifierFor method must have an accessor or type mirror as a parameter.");
    }
    if (typeMirror instanceof PrimitiveType) {
        switch(typeMirror.getKind()) {
            case BOOLEAN:
                return "Boolean";
            case BYTE:
                return "Byte";
            case CHAR:
                return "Character";
            case DOUBLE:
                return "Double";
            case FLOAT:
                return "Float";
            case INT:
                return "Int";
            case LONG:
                return "Long";
            case SHORT:
                return "Short";
            default:
                return (typeMirror.getKind()).toString();
        }
    } else if (typeMirror instanceof DeclaredType) {
        TypeElement declaration = (TypeElement) ((DeclaredType) typeMirror).asElement();
        TypeDefinition typeDefinition = this.context.findTypeDefinition(declaration);
        if (typeDefinition != null) {
            if (typeDefinition instanceof EnumTypeDefinition) {
                return typeDefName.calculateName(typeDefinition);
            }
        } else {
            String classname = declaration.getQualifiedName().toString();
            if (Boolean.class.getName().equals(classname)) {
                return "Boolean";
            } else if (Byte.class.getName().equals(classname)) {
                return "Byte";
            } else if (Character.class.getName().equals(classname)) {
                return "UnsignedShort";
            } else if (Double.class.getName().equals(classname)) {
                return "Double";
            } else if (Float.class.getName().equals(classname)) {
                return "Float";
            } else if (Integer.class.getName().equals(classname)) {
                return "Int";
            } else if (Long.class.getName().equals(classname)) {
                return "Long";
            } else if (Short.class.getName().equals(classname)) {
                return "Short";
            }
        }
    }
    return null;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TypeElement(javax.lang.model.element.TypeElement) EnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.EnumTypeDefinition) TemplateModel(freemarker.template.TemplateModel) Accessor(com.webcohesion.enunciate.modules.jaxb.model.Accessor) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition) EnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.EnumTypeDefinition) TypeMirror(javax.lang.model.type.TypeMirror) PrimitiveType(javax.lang.model.type.PrimitiveType) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)19 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)8 EnunciateException (com.webcohesion.enunciate.EnunciateException)6 TemplateModel (freemarker.template.TemplateModel)6 TemplateModelException (freemarker.template.TemplateModelException)6 URL (java.net.URL)6 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)5 FindRootElementMethod (com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod)5 EnunciateJaxbContext (com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext)4 Accessor (com.webcohesion.enunciate.modules.jaxb.model.Accessor)4 Element (com.webcohesion.enunciate.modules.jaxb.model.Element)4 AccessorOverridesAnotherMethod (com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod)4 IsFacetExcludedMethod (com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod)4 TemplateException (freemarker.template.TemplateException)4 TypeElement (javax.lang.model.element.TypeElement)4 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)3 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)3 MapType (com.webcohesion.enunciate.modules.jaxb.model.util.MapType)3 ReferencedNamespacesMethod (com.webcohesion.enunciate.modules.jaxb.util.ReferencedNamespacesMethod)3 IOException (java.io.IOException)3