Search in sources :

Example 6 with TypeDefinition

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

the class NameForTypeDefinitionMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The nameForTypeDefinition method must have a type definition as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (!(unwrapped instanceof TypeDefinition)) {
        throw new TemplateModelException("The nameForTypeDefinition method must have a type definition as a parameter.");
    }
    return calculateName((TypeDefinition) unwrapped);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 7 with TypeDefinition

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

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

the class QNameForTypeMethod 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 DeclaredType) {
        TypeDefinition typeDefinition = context.findTypeDefinition(((DeclaredType) unwrapped).asElement());
        if (typeDefinition != null) {
            return new QName(typeDefinition.getNamespace() == null ? "" : typeDefinition.getNamespace(), typeDefinition.getName());
        }
    }
    throw new TemplateModelException("Unable to find qname for " + unwrapped);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) QName(javax.xml.namespace.QName) TemplateModel(freemarker.template.TemplateModel) DeclaredType(javax.lang.model.type.DeclaredType) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 9 with TypeDefinition

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

the class ObjCXMLClientModule method call.

@Override
public void call(EnunciateContext context) {
    if (this.jaxbModule == null || this.jaxbModule.getJaxbContext() == null || this.jaxbModule.getJaxbContext().getSchemas().isEmpty()) {
        info("No JAXB XML data types: Objective-C XML client will not be generated.");
        return;
    }
    if (usesUnmappableElements()) {
        warn("Web service API makes use of elements that cannot be handled by the Objective-C XML client. Objective-C XML client will not be generated.");
        return;
    }
    List<String> namingConflicts = JAXBCodeErrors.findConflictingAccessorNamingErrors(this.jaxbModule.getJaxbContext());
    if (namingConflicts != null && !namingConflicts.isEmpty()) {
        error("JAXB naming conflicts have been found:");
        for (String namingConflict : namingConflicts) {
            error(namingConflict);
        }
        error("These naming conflicts are often between the field and it's associated property, in which case you need to use one or two of the following strategies to avoid the conflicts:");
        error("1. Explicitly exclude one or the other.");
        error("2. Put the annotations on the property instead of the field.");
        error("3. Tell JAXB to use a different process for detecting accessors using the @XmlAccessorType annotation.");
        throw new EnunciateException("JAXB naming conflicts detected.");
    }
    EnunciateJaxbContext jaxbContext = this.jaxbModule.getJaxbContext();
    Map<String, String> packageIdentifiers = getPackageIdentifiers();
    String packageIdentifierPattern = getPackageIdentifierPattern();
    if ((packageIdentifierPattern != null)) {
        for (SchemaInfo schemaInfo : jaxbContext.getSchemas().values()) {
            for (TypeDefinition typeDefinition : schemaInfo.getTypeDefinitions()) {
                String pckg = typeDefinition.getPackage().getQualifiedName().toString();
                if (!packageIdentifiers.containsKey(pckg)) {
                    try {
                        packageIdentifiers.put(pckg, String.format(packageIdentifierPattern, pckg.split("\\.", 9)));
                    } catch (IllegalFormatException e) {
                        warn("Unable to format package %s with format pattern %s (%s)", pckg, packageIdentifierPattern, e.getMessage());
                    }
                }
            }
        }
    }
    Map<String, Object> model = new HashMap<String, Object>();
    String slug = getSlug();
    model.put("slug", slug);
    File srcDir = getSourceDir();
    TreeMap<String, String> translations = new TreeMap<String, String>();
    translations.put("id", getTranslateIdTo());
    model.put("clientSimpleName", new ClientSimpleNameMethod(translations));
    List<TypeDefinition> schemaTypes = new ArrayList<TypeDefinition>();
    ExtensionDepthComparator comparator = new ExtensionDepthComparator();
    for (SchemaInfo schemaInfo : jaxbContext.getSchemas().values()) {
        for (TypeDefinition typeDefinition : schemaInfo.getTypeDefinitions()) {
            int position = Collections.binarySearch(schemaTypes, typeDefinition, comparator);
            if (position < 0) {
                position = -position - 1;
            }
            schemaTypes.add(position, typeDefinition);
        }
    }
    model.put("schemaTypes", schemaTypes);
    NameForTypeDefinitionMethod nameForTypeDefinition = new NameForTypeDefinitionMethod(getTypeDefinitionNamePattern(), slug, jaxbContext.getNamespacePrefixes(), packageIdentifiers);
    model.put("nameForTypeDefinition", nameForTypeDefinition);
    model.put("nameForEnumConstant", new NameForEnumConstantMethod(getEnumConstantNamePattern(), slug, jaxbContext.getNamespacePrefixes(), packageIdentifiers));
    TreeMap<String, String> conversions = new TreeMap<String, String>();
    for (SchemaInfo schemaInfo : jaxbContext.getSchemas().values()) {
        for (TypeDefinition typeDefinition : schemaInfo.getTypeDefinitions()) {
            if (typeDefinition.isEnum()) {
                conversions.put(typeDefinition.getQualifiedName().toString(), "enum " + nameForTypeDefinition.calculateName(typeDefinition));
            } else {
                conversions.put(typeDefinition.getQualifiedName().toString(), (String) nameForTypeDefinition.calculateName(typeDefinition));
            }
        }
    }
    ClientClassnameForMethod classnameFor = new ClientClassnameForMethod(conversions, jaxbContext);
    model.put("classnameFor", classnameFor);
    model.put("functionIdentifierFor", new FunctionIdentifierForMethod(nameForTypeDefinition, jaxbContext));
    model.put("objcBaseName", slug);
    model.put("separateCommonCode", isSeparateCommonCode());
    model.put("findRootElement", new FindRootElementMethod(jaxbContext));
    model.put("referencedNamespaces", new ReferencedNamespacesMethod(jaxbContext));
    model.put("prefix", new PrefixMethod(jaxbContext.getNamespacePrefixes()));
    model.put("accessorOverridesAnother", new AccessorOverridesAnotherMethod());
    model.put("file", new FileDirective(srcDir, this.enunciate.getLogger()));
    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));
    if (!isUpToDateWithSources(srcDir)) {
        debug("Generating the C data structures and (de)serialization functions...");
        URL apiTemplate = getTemplateURL("api.fmt");
        try {
            processTemplate(apiTemplate, model);
        } catch (IOException e) {
            throw new EnunciateException(e);
        } catch (TemplateException e) {
            throw new EnunciateException(e);
        }
    } else {
        info("Skipping C code generation because everything appears up-to-date.");
    }
    ClientLibraryArtifact artifactBundle = new ClientLibraryArtifact(getName(), "objc.client.library", "Objective C Client Library");
    FileArtifact sourceHeader = new FileArtifact(getName(), "objc.client.h", new File(srcDir, slug + ".h"));
    sourceHeader.setPublic(false);
    sourceHeader.setArtifactType(ArtifactType.sources);
    FileArtifact sourceImpl = new FileArtifact(getName(), "objc.client.m", new File(srcDir, slug + ".m"));
    sourceImpl.setPublic(false);
    sourceImpl.setArtifactType(ArtifactType.sources);
    // read in the description from file
    String description = readResource("library_description.fmt", model, nameForTypeDefinition);
    artifactBundle.setDescription(description);
    artifactBundle.addArtifact(sourceHeader);
    artifactBundle.addArtifact(sourceImpl);
    if (isSeparateCommonCode()) {
        FileArtifact commonSourceHeader = new FileArtifact(getName(), "objc.common.client.h", new File(srcDir, "enunciate-common.h"));
        commonSourceHeader.setPublic(false);
        commonSourceHeader.setArtifactType(ArtifactType.sources);
        commonSourceHeader.setDescription("Common header needed for all projects.");
        FileArtifact commonSourceImpl = new FileArtifact(getName(), "objc.common.client.m", new File(srcDir, "enunciate-common.m"));
        commonSourceImpl.setPublic(false);
        commonSourceImpl.setArtifactType(ArtifactType.sources);
        commonSourceImpl.setDescription("Common implementation code needed for all projects.");
        artifactBundle.addArtifact(commonSourceHeader);
        artifactBundle.addArtifact(commonSourceImpl);
    }
    this.enunciate.addArtifact(artifactBundle);
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) ClientLibraryArtifact(com.webcohesion.enunciate.artifacts.ClientLibraryArtifact) IsFacetExcludedMethod(com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition) FindRootElementMethod(com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod) EnunciateException(com.webcohesion.enunciate.EnunciateException) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo) FileDirective(com.webcohesion.enunciate.util.freemarker.FileDirective) EnunciateJaxbContext(com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext) AccessorOverridesAnotherMethod(com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod) FileArtifact(com.webcohesion.enunciate.artifacts.FileArtifact)

Example 10 with TypeDefinition

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

the class ObjCXMLClientModule method readResource.

/**
 * Reads a resource into string form.
 *
 * @param resource The resource to read.
 * @return The string form of the resource.
 */
protected String readResource(String resource, Map<String, Object> model, NameForTypeDefinitionMethod nameForTypeDefinition) {
    Method exampleResource = findExampleResourceMethod();
    if (exampleResource != null) {
        TypeDefinition typeDefinition = findRequestElement(exampleResource);
        if (typeDefinition != null) {
            model.put("input_element_name", nameForTypeDefinition.calculateName(typeDefinition));
        }
        typeDefinition = findResponseElement(exampleResource);
        if (typeDefinition != null) {
            model.put("output_element_name", nameForTypeDefinition.calculateName(typeDefinition));
        }
        model.put("resource_url", exampleResource.getResource().getPath());
        model.put("resource_method", exampleResource.getHttpMethod());
    }
    URL res = ObjCXMLClientModule.class.getResource(resource);
    try {
        return processTemplate(res, model);
    } catch (TemplateException e) {
        throw new EnunciateException(e);
    } catch (IOException e) {
        throw new EnunciateException(e);
    }
}
Also used : EnunciateException(com.webcohesion.enunciate.EnunciateException) FindRootElementMethod(com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod) IsFacetExcludedMethod(com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod) Method(com.webcohesion.enunciate.api.resources.Method) AccessorOverridesAnotherMethod(com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

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