Search in sources :

Example 1 with EnunciateJackson1Context

use of com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context in project enunciate by stoicflame.

the class GWTJSONOverlayModule method generateClientSources.

protected File generateClientSources() {
    File sourceDir = getSourceDir();
    sourceDir.mkdirs();
    Map<String, Object> model = new HashMap<String, Object>();
    Map<String, String> conversions = getClientPackageConversions();
    EnunciateJacksonContext jacksonContext = this.jacksonModule != null ? this.jacksonModule.getJacksonContext() : null;
    EnunciateJackson1Context jackson1Context = this.jackson1Module != null ? this.jackson1Module.getJacksonContext() : null;
    MergedJsonContext jsonContext = new MergedJsonContext(jacksonContext, jackson1Context);
    ClientClassnameForMethod classnameFor = new ClientClassnameForMethod(conversions, jsonContext);
    model.put("packageFor", new ClientPackageForMethod(conversions, this.context));
    model.put("classnameFor", classnameFor);
    model.put("simpleNameFor", new SimpleNameForMethod(classnameFor, jsonContext));
    model.put("isAccessorOfTypeLong", new IsAccessorOfTypeLongMethod());
    model.put("file", new FileDirective(sourceDir, 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));
    AntPatternMatcher matcher = new AntPatternMatcher();
    matcher.setPathSeparator(".");
    boolean upToDate = isUpToDateWithSources(sourceDir);
    if (!upToDate) {
        try {
            debug("Generating the GWT JSON Overlay...");
            if (jacksonContext != null) {
                for (TypeDefinition typeDefinition : jacksonContext.getTypeDefinitions()) {
                    if (!typeDefinition.isSimple() && facetFilter.accept(typeDefinition)) {
                        model.put("type", typeDefinition);
                        URL template = typeDefinition.isEnum() ? getTemplateURL("gwt-enum-type.fmt") : getTemplateURL("gwt-type.fmt");
                        processTemplate(template, model);
                    }
                }
            }
            if (jackson1Context != null) {
                for (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition typeDefinition : jackson1Context.getTypeDefinitions()) {
                    if (!typeDefinition.isSimple() && facetFilter.accept(typeDefinition)) {
                        model.put("type", typeDefinition);
                        URL template = typeDefinition.isEnum() ? getTemplateURL("gwt-enum-type.fmt") : getTemplateURL("gwt-type.fmt");
                        processTemplate(template, model);
                    }
                }
            }
        } catch (IOException e) {
            throw new EnunciateException(e);
        } catch (TemplateException e) {
            throw new EnunciateException(e);
        }
    } else {
        info("Skipping generation of GWT JSON Overlay as everything appears up-to-date...");
    }
    context.setProperty(LIRBARY_DESCRIPTION_PROPERTY, readLibraryDescription(model));
    return sourceDir;
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) IsFacetExcludedMethod(com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod) ClientPackageForMethod(com.webcohesion.enunciate.util.freemarker.ClientPackageForMethod) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateJackson1Context(com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context) FileDirective(com.webcohesion.enunciate.util.freemarker.FileDirective) TemplateException(freemarker.template.TemplateException) EnunciateJacksonContext(com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext) IOException(java.io.IOException) AntPatternMatcher(com.webcohesion.enunciate.util.AntPatternMatcher) File(java.io.File)

Example 2 with EnunciateJackson1Context

use of com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context in project enunciate by stoicflame.

the class PHPJSONClientModule 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: PHP JSON client will not be generated.");
        return;
    }
    detectAccessorNamingErrors();
    if (usesUnmappableElements()) {
        warn("Web service API makes use of elements that cannot be handled by the PHP JSON client. PHP JSON client will not be generated.");
        return;
    }
    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("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("phpFileName", 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 PHP data classes...");
        URL apiTemplate = isSingleFilePerClass() ? getTemplateURL("api-multiple-files.fmt") : getTemplateURL("api.fmt");
        try {
            processTemplate(apiTemplate, model);
        } catch (IOException e) {
            throw new EnunciateException(e);
        } catch (TemplateException e) {
            throw new EnunciateException(e);
        }
    } else {
        info("Skipping PHP 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(), "php.json.client.library", "PHP JSON Client Library");
        artifactBundle.setPlatform("PHP");
        FileArtifact sourceScript = new FileArtifact(getName(), "php.json.client", bundle);
        // binaries and sources are the same thing in php
        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 3 with EnunciateJackson1Context

use of com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context in project enunciate by stoicflame.

the class JavaJSONClientModule method generateClientSources.

protected File generateClientSources() {
    File sourceDir = getSourceDir();
    sourceDir.mkdirs();
    Map<String, Object> model = new HashMap<String, Object>();
    Map<String, String> conversions = getClientPackageConversions();
    EnunciateJacksonContext jacksonContext = this.jacksonModule != null ? this.jacksonModule.getJacksonContext() : null;
    EnunciateJackson1Context jackson1Context = this.jackson1Module != null ? this.jackson1Module.getJacksonContext() : null;
    MergedJsonContext jsonContext = new MergedJsonContext(jacksonContext, jackson1Context);
    model.put("packageFor", new ClientPackageForMethod(conversions, this.context));
    model.put("classnameFor", new ClientClassnameForMethod(conversions, jsonContext));
    model.put("simpleNameFor", new SimpleNameForMethod(new ClientClassnameForMethod(conversions, jsonContext, true), jsonContext));
    model.put("file", new FileDirective(sourceDir, this.enunciate.getLogger()));
    model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
    model.put("annotationValue", new AnnotationValueMethod());
    model.put("wrapRootValue", this.jacksonModule == null ? this.jackson1Module.isWrapRootValue() : this.jacksonModule.isWrapRootValue());
    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));
    AntPatternMatcher matcher = new AntPatternMatcher();
    matcher.setPathSeparator(".");
    boolean upToDate = isUpToDateWithSources(sourceDir);
    if (!upToDate) {
        try {
            debug("Generating the Java client classes...");
            if (jacksonContext != null) {
                for (TypeDefinition typeDefinition : jacksonContext.getTypeDefinitions()) {
                    if (facetFilter.accept(typeDefinition)) {
                        if (useServerSide(typeDefinition, matcher)) {
                            copyServerSideType(sourceDir, typeDefinition);
                        } else {
                            model.put("type", typeDefinition);
                            URL template = typeDefinition.isEnum() ? getTemplateURL("client-enum-type.fmt") : typeDefinition.isSimple() ? getTemplateURL("client-simple-type.fmt") : getTemplateURL("client-complex-type.fmt");
                            processTemplate(template, model);
                        }
                    }
                }
            }
            if (jackson1Context != null) {
                for (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition typeDefinition : jackson1Context.getTypeDefinitions()) {
                    if (facetFilter.accept(typeDefinition)) {
                        if (useServerSide(typeDefinition, matcher)) {
                            copyServerSideType(sourceDir, typeDefinition);
                        } else {
                            model.put("type", typeDefinition);
                            URL template = typeDefinition.isEnum() ? getTemplateURL("client-enum-type.fmt") : typeDefinition.isSimple() ? getTemplateURL("client-simple-type.fmt") : getTemplateURL("client-complex-type.fmt");
                            processTemplate(template, 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 : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateJackson1Context(com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context) TemplateException(freemarker.template.TemplateException) EnunciateJacksonContext(com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext) AntPatternMatcher(com.webcohesion.enunciate.util.AntPatternMatcher) JavaFileObject(javax.tools.JavaFileObject)

Example 4 with EnunciateJackson1Context

use of com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context in project enunciate by stoicflame.

the class JsonTypeFactory method findSpecifiedType.

/**
 * Find the specified type of the given adaptable element, if it exists.
 *
 * @param adaptable The adaptable element for which to find the specified type.
 * @param context The context
 * @return The specified JSON type, or null if it doesn't exist.
 */
public static JsonType findSpecifiedType(Adaptable adaptable, EnunciateJackson1Context context) {
    JsonType jsonType = null;
    if (adaptable instanceof Accessor) {
        Accessor accessor = (Accessor) adaptable;
        TypeHint typeHint = accessor.getAnnotation(TypeHint.class);
        if (typeHint != null) {
            TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getProcessingEnvironment(), null);
            if (hint != null) {
                return getJsonType(hint, context);
            }
        }
        final JsonSerialize serializeInfo = accessor.getAnnotation(JsonSerialize.class);
        if (serializeInfo != null) {
            DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();
            DecoratedTypeMirror using = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return serializeInfo.using();
                }
            }, env, JsonSerializer.None.class);
            if (using != null) {
                // we're using some custom serialization, so we just have to return a generic object.
                return KnownJsonType.OBJECT;
            } else {
                DecoratedTypeMirror as = Annotations.mirrorOf(new Callable<Class<?>>() {

                    @Override
                    public Class<?> call() throws Exception {
                        return serializeInfo.as();
                    }
                }, env, Void.class);
                if (as != null) {
                    return getJsonType(as, context);
                } else {
                    DecoratedTypeMirror contentAs = Annotations.mirrorOf(new Callable<Class<?>>() {

                        @Override
                        public Class<?> call() throws Exception {
                            return serializeInfo.contentAs();
                        }
                    }, env, Void.class);
                    DecoratedTypeMirror contentUsing = Annotations.mirrorOf(new Callable<Class<?>>() {

                        @Override
                        public Class<?> call() throws Exception {
                            return serializeInfo.contentUsing();
                        }
                    }, env, JsonSerializer.None.class);
                    DecoratedTypeMirror accessorType = (DecoratedTypeMirror) accessor.asType();
                    if (accessorType.isCollection() || accessorType.isArray() || accessorType.isStream()) {
                        if (contentUsing != null) {
                            // the json type has to be just a list of object.
                            return new JsonArrayType(KnownJsonType.OBJECT);
                        } else if (contentAs != null) {
                            return new JsonArrayType(getJsonType(contentAs, context));
                        }
                    } else {
                        MapType mapType = MapType.findMapType(accessorType, context);
                        if (mapType != null) {
                            DecoratedTypeMirror keyAs = Annotations.mirrorOf(new Callable<Class<?>>() {

                                @Override
                                public Class<?> call() throws Exception {
                                    return serializeInfo.keyAs();
                                }
                            }, env, Void.class);
                            DecoratedTypeMirror keyUsing = Annotations.mirrorOf(new Callable<Class<?>>() {

                                @Override
                                public Class<?> call() throws Exception {
                                    return serializeInfo.keyUsing();
                                }
                            }, env, JsonSerializer.None.class);
                            if (keyAs != null || contentAs != null) {
                                JsonType keyType = keyUsing == null ? getJsonType(keyAs == null ? (DecoratedTypeMirror) mapType.getKeyType() : keyAs, context) : KnownJsonType.OBJECT;
                                JsonType valueType = contentUsing == null ? getJsonType(contentAs == null ? (DecoratedTypeMirror) mapType.getValueType() : contentAs, context) : KnownJsonType.OBJECT;
                                return new JsonMapType(keyType, valueType);
                            }
                        }
                    }
                }
            }
        }
    }
    if (adaptable.isAdapted()) {
        jsonType = getJsonType(adaptable.getAdapterType().getAdaptingType(), context);
    }
    return jsonType;
}
Also used : TypeHint(com.webcohesion.enunciate.metadata.rs.TypeHint) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) JsonSerializer(org.codehaus.jackson.map.JsonSerializer) Accessor(com.webcohesion.enunciate.modules.jackson1.model.Accessor) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) MapType(com.webcohesion.enunciate.modules.jackson1.model.util.MapType) JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) TypeMirror(javax.lang.model.type.TypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 5 with EnunciateJackson1Context

use of com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context in project enunciate by stoicflame.

the class Jackson1CodeErrors method findConflictingAccessorNamingErrors.

public static List<String> findConflictingAccessorNamingErrors(EnunciateJackson1Context 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 (TypeDefinition typeDefinition : context.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.jackson1.model.Accessor) TypeDefinition(com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition)

Aggregations

EnunciateException (com.webcohesion.enunciate.EnunciateException)6 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)5 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)5 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)5 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)5 TemplateException (freemarker.template.TemplateException)5 URL (java.net.URL)5 File (java.io.File)4 IOException (java.io.IOException)4 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)3 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)3 DecoratedTypeElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)2 DecoratedDeclaredType (com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType)2 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)2 Accessor (com.webcohesion.enunciate.modules.jackson1.model.Accessor)2 AdapterType (com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType)2 AntPatternMatcher (com.webcohesion.enunciate.util.AntPatternMatcher)2 HashMap (java.util.HashMap)2