Search in sources :

Example 21 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class CXMLClientModule 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: C XML client will not be generated.");
        return;
    }
    if (usesUnmappableElements()) {
        warn("Web service API makes use of elements that cannot be handled by the C XML client. 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.");
    }
    File srcDir = getSourceDir();
    srcDir.mkdirs();
    Map<String, Object> model = new HashMap<String, Object>();
    String slug = getSlug();
    Map<String, String> ns2prefix = this.jaxbModule.getJaxbContext().getNamespacePrefixes();
    NameForTypeDefinitionMethod nameForTypeDefinition = new NameForTypeDefinitionMethod(getTypeDefinitionNamePattern(), slug, ns2prefix);
    model.put("nameForTypeDefinition", nameForTypeDefinition);
    model.put("nameForEnumConstant", new NameForEnumConstantMethod(getEnumConstantNamePattern(), slug, ns2prefix));
    TreeMap<String, String> conversions = new TreeMap<String, String>();
    for (SchemaInfo schemaInfo : this.jaxbModule.getJaxbContext().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(), "struct " + nameForTypeDefinition.calculateName(typeDefinition));
            }
        }
    }
    ClientClassnameForMethod classnameFor = new ClientClassnameForMethod(conversions, this.jaxbModule.getJaxbContext());
    model.put("classnameFor", classnameFor);
    String sourceFileName = getSourceFileName(slug);
    model.put("cFileName", sourceFileName);
    model.put("separateCommonCode", isSeparateCommonCode());
    model.put("findRootElement", new FindRootElementMethod(this.jaxbModule.getJaxbContext()));
    model.put("referencedNamespaces", new ReferencedNamespacesMethod(this.jaxbModule.getJaxbContext()));
    model.put("prefix", new PrefixMethod(ns2prefix));
    model.put("xmlFunctionIdentifier", new XmlFunctionIdentifierMethod(ns2prefix));
    model.put("accessorOverridesAnother", new AccessorOverridesAnotherMethod());
    model.put("filename", sourceFileName);
    model.put("file", new FileDirective(srcDir, this.enunciate.getLogger()));
    model.put("schemas", this.jaxbModule.getJaxbContext().getSchemas().values());
    model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
    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(), "c.client.library", "C Client Library");
    FileArtifact sourceScript = new FileArtifact(getName(), "c.client", new File(srcDir, sourceFileName));
    sourceScript.setArtifactType(ArtifactType.sources);
    sourceScript.setPublic(false);
    // read in the description from file
    String description = readResource("library_description.fmt", model, nameForTypeDefinition);
    artifactBundle.setDescription(description);
    artifactBundle.addArtifact(sourceScript);
    if (isSeparateCommonCode()) {
        FileArtifact commonSourceHeader = new FileArtifact(getName(), "c.common.client", new File(srcDir, "enunciate-common.c"));
        commonSourceHeader.setPublic(false);
        commonSourceHeader.setArtifactType(ArtifactType.sources);
        commonSourceHeader.setDescription("Common code needed for all projects.");
        artifactBundle.addArtifact(commonSourceHeader);
    }
    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) ReferencedNamespacesMethod(com.webcohesion.enunciate.modules.jaxb.util.ReferencedNamespacesMethod) EnunciateException(com.webcohesion.enunciate.EnunciateException) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo) FileDirective(com.webcohesion.enunciate.util.freemarker.FileDirective) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) AccessorOverridesAnotherMethod(com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod) FileArtifact(com.webcohesion.enunciate.artifacts.FileArtifact) File(java.io.File)

Example 22 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class JavaScriptClientModule method call.

@Override
public void call(EnunciateContext context) {
    if ((this.jacksonModule == null || this.jacksonModule.getJacksonContext() == null || this.jacksonModule.getJacksonContext().getTypeDefinitions().isEmpty()) && (this.jackson1Module == null || this.jackson1Module.getJacksonContext() == null || this.jackson1Module.getJacksonContext().getTypeDefinitions().isEmpty())) {
        info("No Jackson JSON data types: JavaScript client will not be generated.");
        return;
    }
    detectAccessorNamingErrors();
    Map<String, String> packageToNamespaceConversions = getPackageToNamespaceConversions();
    List<DecoratedTypeElement> schemaTypes = new ArrayList<DecoratedTypeElement>();
    ExtensionDepthComparator comparator = new ExtensionDepthComparator();
    EnunciateJacksonContext jacksonContext = null;
    EnunciateJackson1Context jackson1Context = null;
    if (this.jacksonModule != null) {
        jacksonContext = this.jacksonModule.getJacksonContext();
        for (TypeDefinition typeDefinition : jacksonContext.getTypeDefinitions()) {
            String pckg = typeDefinition.getPackage().getQualifiedName().toString();
            if (!packageToNamespaceConversions.containsKey(pckg)) {
                packageToNamespaceConversions.put(pckg, packageToNamespace(pckg));
            }
            int position = Collections.binarySearch(schemaTypes, typeDefinition, comparator);
            if (position < 0) {
                position = -position - 1;
            }
            schemaTypes.add(position, typeDefinition);
        }
    }
    if (this.jackson1Module != null) {
        jackson1Context = this.jackson1Module.getJacksonContext();
        for (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition typeDefinition : jackson1Context.getTypeDefinitions()) {
            String pckg = typeDefinition.getPackage().getQualifiedName().toString();
            if (!packageToNamespaceConversions.containsKey(pckg)) {
                packageToNamespaceConversions.put(pckg, packageToNamespace(pckg));
            }
            schemaTypes.add(typeDefinition);
        }
    }
    File srcDir = getSourceDir();
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("globalName", this.config.getString("[@global]", "javascriptClient"));
    model.put("schemaTypes", schemaTypes);
    model.put("namespaceFor", new ClientPackageForMethod(packageToNamespaceConversions, this.context));
    ClientClassnameForMethod classnameFor = new ClientClassnameForMethod(packageToNamespaceConversions, jacksonContext, jackson1Context);
    model.put("classnameFor", classnameFor);
    model.put("typeNameFor", new TypeNameForMethod(packageToNamespaceConversions, jacksonContext, jackson1Context));
    model.put("simpleNameFor", new SimpleNameWithParamsMethod(classnameFor));
    model.put("jsFileName", getSourceFileName());
    model.put("file", new FileDirective(srcDir, this.enunciate.getLogger()));
    model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
    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 JavaScript data classes...");
        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 JavaScript code generation because everything appears up-to-date.");
    }
    File packageDir = getPackageDir();
    packageDir.mkdirs();
    File bundle = new File(packageDir, getBundleFileName());
    boolean anyFiles = bundle.exists();
    if (!isUpToDateWithSources(packageDir)) {
        try {
            anyFiles = enunciate.zip(bundle, srcDir);
        } catch (IOException e) {
            throw new EnunciateException(e);
        }
    }
    if (anyFiles) {
        ClientLibraryArtifact artifactBundle = new ClientLibraryArtifact(getName(), "js.client.library", "JavaScript Client Library");
        artifactBundle.setPlatform("JavaScript");
        FileArtifact sourceScript = new FileArtifact(getName(), "javascript.client", bundle);
        // binaries and sources are the same thing in js
        sourceScript.setArtifactType(ArtifactType.binaries);
        sourceScript.setPublic(false);
        // read in the description from file
        String description = readResource("library_description.fmt", model);
        artifactBundle.setDescription(description);
        artifactBundle.addArtifact(sourceScript);
        this.enunciate.addArtifact(artifactBundle);
    }
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) ClientLibraryArtifact(com.webcohesion.enunciate.artifacts.ClientLibraryArtifact) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) DecoratedTypeElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateJackson1Context(com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context) TemplateException(freemarker.template.TemplateException) EnunciateJacksonContext(com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext) IOException(java.io.IOException) FileArtifact(com.webcohesion.enunciate.artifacts.FileArtifact) File(java.io.File)

Example 23 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class NamespaceImpl method getTypes.

@Override
public List<? extends DataType> getTypes() {
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    ArrayList<DataType> dataTypes = new ArrayList<DataType>();
    for (TypeDefinition typeDefinition : this.schema.getTypeDefinitions()) {
        if (!facetFilter.accept(typeDefinition)) {
            continue;
        }
        if (typeDefinition instanceof ComplexTypeDefinition) {
            dataTypes.add(new ComplexDataTypeImpl((ComplexTypeDefinition) typeDefinition, registrationContext));
        } else if (typeDefinition instanceof EnumTypeDefinition) {
            dataTypes.add(new EnumDataTypeImpl((EnumTypeDefinition) typeDefinition, registrationContext));
        }
    }
    return dataTypes;
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) EnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.EnumTypeDefinition) ArrayList(java.util.ArrayList) DataType(com.webcohesion.enunciate.api.datatype.DataType) ComplexTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.ComplexTypeDefinition) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition) EnumTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.EnumTypeDefinition) ComplexTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.ComplexTypeDefinition)

Example 24 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class ComplexTypeExampleImpl method build.

private String build(Element rootElement, ComplexTypeDefinition type, final Document document, Context context) {
    if (context.stack.size() > 2) {
        // don't go deeper than 2 for fear of the OOM (see https://github.com/stoicflame/enunciate/issues/139).
        return rootElement.getNamespaceURI();
    }
    if (context.stack.contains(type.getQualifiedName().toString())) {
        return rootElement.getNamespaceURI();
    }
    String defaultNamespace = rootElement.getNamespaceURI();
    context.stack.push(type.getQualifiedName().toString());
    try {
        FacetFilter facetFilter = registrationContext.getFacetFilter();
        for (Attribute attribute : type.getAttributes()) {
            if (ElementUtils.findDeprecationMessage(attribute, null) != null) {
                continue;
            }
            if (!facetFilter.accept(attribute)) {
                continue;
            }
            String example = "...";
            JavaDoc.JavaDocTagList tags = getDocumentationExampleTags(attribute);
            if (tags != null && tags.size() > 0) {
                String tag = tags.get(0).trim();
                example = tag.isEmpty() ? null : tag;
            }
            DocumentationExample documentationExample = getDocumentationExample(attribute);
            if (documentationExample != null) {
                if (documentationExample.exclude()) {
                    continue;
                } else if (context.currentIndex == 1 && !"##default".equals(documentationExample.value2())) {
                    example = documentationExample.value2();
                } else if (!"##default".equals(documentationExample.value())) {
                    example = documentationExample.value();
                }
            }
            String configuredExample = getConfiguredExample(attribute);
            if (configuredExample != null) {
                example = configuredExample;
            }
            rootElement.setAttributeNS(attribute.getNamespace(), attribute.getName(), example);
            if (attribute.getNamespace() == null) {
                defaultNamespace = null;
            }
        }
        if (type.getValue() != null) {
            String example = "...";
            JavaDoc.JavaDocTagList tags = getDocumentationExampleTags(type.getValue());
            if (tags != null && tags.size() > 0) {
                String tag = tags.get(0).trim();
                example = tag.isEmpty() ? null : tag;
            }
            DocumentationExample documentationExample = getDocumentationExample(type.getValue());
            if (documentationExample != null) {
                if (!"##default".equals(documentationExample.value())) {
                    example = documentationExample.value();
                }
            }
            String configuredExample = getConfiguredExample(type.getValue());
            if (configuredExample != null) {
                example = configuredExample;
            }
            rootElement.setTextContent(example);
        } else {
            for (com.webcohesion.enunciate.modules.jaxb.model.Element element : type.getElements()) {
                if (ElementUtils.findDeprecationMessage(element, null) != null) {
                    continue;
                }
                if (!facetFilter.accept(element)) {
                    continue;
                }
                Element currentElement = rootElement;
                if (element.isWrapped()) {
                    Element wrapper = document.createElementNS(element.getWrapperNamespace(), element.getWrapperName());
                    rootElement.appendChild(wrapper);
                    currentElement = wrapper;
                    if (element.getWrapperNamespace() == null) {
                        defaultNamespace = null;
                    }
                }
                for (com.webcohesion.enunciate.modules.jaxb.model.Element choice : element.getChoices()) {
                    Element childElement = document.createElementNS(choice.getNamespace(), choice.getName());
                    if (choice.getNamespace() == null) {
                        defaultNamespace = null;
                    }
                    XmlType baseType = choice.getXmlType();
                    JavaDoc.JavaDocTagList tags = choice.getJavaDoc().get("documentationType");
                    if (tags != null && tags.size() > 0) {
                        String tag = tags.get(0).trim();
                        if (!tag.isEmpty()) {
                            TypeElement typeElement = type.getContext().getContext().getProcessingEnvironment().getElementUtils().getTypeElement(tag);
                            if (typeElement != null) {
                                baseType = XmlTypeFactory.getXmlType(typeElement.asType(), type.getContext());
                            } else {
                                type.getContext().getContext().getLogger().warn("Invalid documentation type %s.", tag);
                            }
                        }
                    }
                    DocumentationExample documentationExample = getDocumentationExample(choice);
                    if (documentationExample != null) {
                        TypeMirror typeHint = TypeHintUtils.getTypeHint(documentationExample.type(), type.getContext().getContext().getProcessingEnvironment(), null);
                        if (typeHint != null) {
                            baseType = XmlTypeFactory.getXmlType(typeHint, type.getContext());
                        }
                    }
                    if (baseType instanceof XmlClassType && ((XmlClassType) baseType).getTypeDefinition() instanceof ComplexTypeDefinition) {
                        String defaultChildNs = build(childElement, (ComplexTypeDefinition) ((XmlClassType) baseType).getTypeDefinition(), document, context);
                        if (defaultChildNs == null) {
                            defaultNamespace = null;
                        }
                    } else {
                        String example = "...";
                        tags = getDocumentationExampleTags(choice);
                        if (tags != null && tags.size() > 0) {
                            String tag = tags.get(0).trim();
                            example = tag.isEmpty() ? null : tag;
                        }
                        if (documentationExample != null) {
                            if (documentationExample.exclude()) {
                                continue;
                            } else if (context.currentIndex == 1 && !"##default".equals(documentationExample.value2())) {
                                example = documentationExample.value2();
                            } else if (!"##default".equals(documentationExample.value())) {
                                example = documentationExample.value();
                            }
                        }
                        String configuredExample = getConfiguredExample(choice);
                        if (configuredExample != null) {
                            example = configuredExample;
                        }
                        childElement.setTextContent(example);
                    }
                    currentElement.appendChild(childElement);
                }
            }
        }
        XmlType supertype = type.getBaseType();
        if (supertype instanceof XmlClassType && ((XmlClassType) supertype).getTypeDefinition() instanceof ComplexTypeDefinition) {
            String defaultSuperNs = build(rootElement, (ComplexTypeDefinition) ((XmlClassType) supertype).getTypeDefinition(), document, context);
            if (defaultSuperNs == null) {
                defaultNamespace = null;
            }
        }
        if (type.getAnyElement() != null && ElementUtils.findDeprecationMessage(type.getAnyElement(), null) == null) {
            Element extension1 = document.createElementNS(defaultNamespace, "extension1");
            extension1.setTextContent("...");
            rootElement.appendChild(extension1);
            Element extension2 = document.createElementNS(defaultNamespace, "extension2");
            extension2.setTextContent("...");
            rootElement.appendChild(extension2);
        }
    } finally {
        context.stack.pop();
    }
    return defaultNamespace;
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) Attribute(com.webcohesion.enunciate.modules.jaxb.model.Attribute) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) Element(org.w3c.dom.Element) JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample) XmlType(com.webcohesion.enunciate.modules.jaxb.model.types.XmlType) XmlClassType(com.webcohesion.enunciate.modules.jaxb.model.types.XmlClassType) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ComplexTypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.ComplexTypeDefinition)

Example 25 with FacetFilter

use of com.webcohesion.enunciate.facets.FacetFilter in project enunciate by stoicflame.

the class EnumDataTypeImpl method getValues.

@Override
public List<? extends Value> getValues() {
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    List<EnumValue> enumValues = this.typeDefinition.getEnumValues();
    ArrayList<Value> values = new ArrayList<Value>(enumValues.size());
    for (EnumValue enumValue : enumValues) {
        if (enumValue.getValue() != null) {
            if (!facetFilter.accept(enumValue)) {
                continue;
            }
            values.add(createValue(enumValue));
        }
    }
    return values;
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) EnumValue(com.webcohesion.enunciate.modules.jaxb.model.EnumValue) Value(com.webcohesion.enunciate.api.datatype.Value) EnumValue(com.webcohesion.enunciate.modules.jaxb.model.EnumValue) ArrayList(java.util.ArrayList)

Aggregations

FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)33 EnunciateException (com.webcohesion.enunciate.EnunciateException)11 TemplateException (freemarker.template.TemplateException)10 URL (java.net.URL)10 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)7 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)7 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)7 ResourceGroup (com.webcohesion.enunciate.api.resources.ResourceGroup)6 File (java.io.File)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 TypeElement (javax.lang.model.element.TypeElement)6 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)5 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)5 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)5 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)5 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)5 FileDirective (com.webcohesion.enunciate.util.freemarker.FileDirective)5 TypeMirror (javax.lang.model.type.TypeMirror)5 EnunciateJaxbContext (com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext)4