Search in sources :

Example 31 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class PHPXMLClientModule 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: PHP XML client will not be generated.");
        return;
    }
    if (usesUnmappableElements()) {
        warn("Web service API makes use of elements that cannot be handled by the PHP XML client. PHP 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.");
    }
    Map<String, String> packageToNamespaceConversions = getPackageToNamespaceConversions();
    List<TypeDefinition> schemaTypes = new ArrayList<TypeDefinition>();
    ExtensionDepthComparator comparator = new ExtensionDepthComparator();
    EnunciateJaxbContext jaxbContext = this.jaxbModule.getJaxbContext();
    for (SchemaInfo schemaInfo : jaxbContext.getSchemas().values()) {
        for (TypeDefinition typeDefinition : schemaInfo.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);
        }
    }
    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, jaxbContext);
    model.put("classnameFor", classnameFor);
    model.put("typeNameFor", new TypeNameForMethod(packageToNamespaceConversions, jaxbContext));
    model.put("simpleNameFor", new SimpleNameWithParamsMethod(classnameFor));
    model.put("phpFileName", getSourceFileName());
    model.put("findRootElement", new FindRootElementMethod(jaxbContext));
    model.put("referencedNamespaces", new ReferencedNamespacesMethod(jaxbContext));
    model.put("prefix", new PrefixMethod(jaxbContext.getNamespacePrefixes()));
    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 XML 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 XML 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.xml.client.library", "PHP XML Client Library");
        artifactBundle.setPlatform("PHP");
        FileArtifact sourceScript = new FileArtifact(getName(), "php.xml.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.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) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) EnunciateJaxbContext(com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext) FileArtifact(com.webcohesion.enunciate.artifacts.FileArtifact) PrefixMethod(com.webcohesion.enunciate.modules.jaxb.util.PrefixMethod) File(java.io.File)

Example 32 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class PHPXMLClientModule 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) {
    model.put("sample_resource", findExampleResourceMethod());
    URL res = PHPXMLClientModule.class.getResource(resource);
    try {
        return processTemplate(res, model);
    } catch (TemplateException e) {
        throw new EnunciateException(e);
    } catch (IOException e) {
        throw new EnunciateException(e);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) IOException(java.io.IOException) URL(java.net.URL)

Example 33 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException 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 34 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class JavaJSONClientModule method readLibraryDescription.

/**
 * Reads a resource into string form.
 *
 * @return The string form of the resource.
 */
protected String readLibraryDescription(Map<String, Object> model) {
    model.put("sample_resource", findExampleResourceMethod());
    model.put("mediaTypeFor", new MediaTypeForMethod());
    URL res = JavaJSONClientModule.class.getResource("library_description.fmt");
    try {
        return processTemplate(res, model);
    } catch (TemplateException e) {
        throw new EnunciateException(e);
    } catch (IOException e) {
        throw new EnunciateException(e);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) URL(java.net.URL)

Example 35 with EnunciateException

use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.

the class JacksonUtil method getAdaptersOfPackage.

/**
 * Gets the adapters of the specified package.
 *
 * @param pckg the package for which to get the adapters.
 * @param context The context.
 * @return The adapters for the package.
 */
private static Map<String, XmlJavaTypeAdapter> getAdaptersOfPackage(PackageElement pckg, EnunciateJackson1Context context) {
    if (pckg == null) {
        return null;
    }
    Map<String, Map<String, XmlJavaTypeAdapter>> adaptersOfAllPackages = (Map<String, Map<String, XmlJavaTypeAdapter>>) context.getContext().getProperty(ADAPTERS_BY_PACKAGE_PROPERTY);
    if (adaptersOfAllPackages == null) {
        adaptersOfAllPackages = new HashMap<String, Map<String, XmlJavaTypeAdapter>>();
        context.getContext().setProperty(ADAPTERS_BY_PACKAGE_PROPERTY, adaptersOfAllPackages);
    }
    Map<String, XmlJavaTypeAdapter> adaptersOfPackage = adaptersOfAllPackages.get(pckg.getQualifiedName().toString());
    if (adaptersOfPackage == null) {
        adaptersOfPackage = new HashMap<String, XmlJavaTypeAdapter>();
        adaptersOfAllPackages.put(pckg.getQualifiedName().toString(), adaptersOfPackage);
        XmlJavaTypeAdapter javaType = pckg.getAnnotation(XmlJavaTypeAdapter.class);
        XmlJavaTypeAdapters javaTypes = pckg.getAnnotation(XmlJavaTypeAdapters.class);
        if ((javaType != null) || (javaTypes != null)) {
            ArrayList<XmlJavaTypeAdapter> allAdaptedTypes = new ArrayList<XmlJavaTypeAdapter>();
            if (javaType != null) {
                allAdaptedTypes.add(javaType);
            }
            if (javaTypes != null) {
                allAdaptedTypes.addAll(Arrays.asList(javaTypes.value()));
            }
            for (final XmlJavaTypeAdapter adaptedTypeInfo : allAdaptedTypes) {
                DecoratedTypeMirror typeMirror = Annotations.mirrorOf(new Callable<Class<?>>() {

                    @Override
                    public Class<?> call() throws Exception {
                        return adaptedTypeInfo.type();
                    }
                }, context.getContext().getProcessingEnvironment(), XmlJavaTypeAdapter.DEFAULT.class);
                if (typeMirror == null) {
                    throw new EnunciateException("Package " + pckg.getQualifiedName() + ": a type must be specified in " + XmlJavaTypeAdapter.class.getName() + " at the package-level.");
                }
                if (!(typeMirror instanceof DeclaredType)) {
                    throw new EnunciateException("Package " + pckg.getQualifiedName() + ": unadaptable type: " + typeMirror);
                }
                TypeElement typeDeclaration = (TypeElement) ((DeclaredType) typeMirror).asElement();
                if (typeDeclaration == null) {
                    throw new EnunciateException("Element not found: " + typeMirror);
                }
                adaptersOfPackage.put(typeDeclaration.getQualifiedName().toString(), adaptedTypeInfo);
            }
        }
    }
    return adaptersOfPackage;
}
Also used : DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeElement(javax.lang.model.element.TypeElement) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlJavaTypeAdapters(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters) DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

EnunciateException (com.webcohesion.enunciate.EnunciateException)53 URL (java.net.URL)21 TemplateException (freemarker.template.TemplateException)19 IOException (java.io.IOException)16 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)12 File (java.io.File)12 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)11 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)8 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)7 TypeElement (javax.lang.model.element.TypeElement)7 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)6 FindRootElementMethod (com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod)6 IsFacetExcludedMethod (com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod)6 VariableElement (javax.lang.model.element.VariableElement)6 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 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)5 AccessorOverridesAnotherMethod (com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod)5 FileDirective (com.webcohesion.enunciate.util.freemarker.FileDirective)5