Search in sources :

Example 41 with EnunciateException

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

the class CXMLClientModule 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));
        }
    }
    URL res = CXMLClientModule.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) FindRootElementMethod(com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod) IsFacetExcludedMethod(com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod) Method(com.webcohesion.enunciate.api.resources.Method) ReferencedNamespacesMethod(com.webcohesion.enunciate.modules.jaxb.util.ReferencedNamespacesMethod) AccessorOverridesAnotherMethod(com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod) IOException(java.io.IOException) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 42 with EnunciateException

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

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

the class JavaScriptClientModule 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 = JavaScriptClientModule.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 44 with EnunciateException

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

the class QNameEnumTypeDefinition method loadEnumValues.

@Override
protected List<EnumValue> loadEnumValues() {
    String namespace = getPackage().getNamespace();
    XmlQNameEnum xmlQNameEnum = getAnnotation(XmlQNameEnum.class);
    if (xmlQNameEnum != null && !"##default".equals(xmlQNameEnum.namespace())) {
        namespace = xmlQNameEnum.namespace();
    }
    if (namespace == null) {
        namespace = "";
    }
    List<VariableElement> enumConstants = enumValues();
    List<EnumValue> enumValues = new ArrayList<EnumValue>();
    HashSet<QName> enumValueValues = new HashSet<QName>(enumConstants.size());
    VariableElement unknownQNameConstant = null;
    for (VariableElement enumConstant : enumConstants) {
        if (isIgnored(enumConstant)) {
            continue;
        }
        XmlUnknownQNameEnumValue unknownQNameEnumValue = enumConstant.getAnnotation(XmlUnknownQNameEnumValue.class);
        if (unknownQNameEnumValue != null) {
            if (unknownQNameConstant != null) {
                throw new EnunciateException(getQualifiedName() + ": no more than two constants can be annotated with @XmlUnknownQNameEnumValue.");
            }
            unknownQNameConstant = enumConstant;
            continue;
        }
        String ns = namespace;
        String localPart = enumConstant.getSimpleName().toString();
        XmlQNameEnumValue enumValueInfo = enumConstant.getAnnotation(XmlQNameEnumValue.class);
        if (enumValueInfo != null) {
            if (enumValueInfo.exclude()) {
                continue;
            }
            if (!"##default".equals(enumValueInfo.namespace())) {
                ns = enumValueInfo.namespace();
            }
            if (!"##default".equals(enumValueInfo.localPart())) {
                localPart = enumValueInfo.localPart();
            }
        }
        QName qname = new QName(ns, localPart);
        if (!enumValueValues.add(qname)) {
            throw new EnunciateException(getQualifiedName() + ": duplicate qname enum value: " + qname);
        }
        enumValues.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), qname));
    }
    if (unknownQNameConstant != null) {
        // enter the unknown qname constant as a null qname.
        enumValues.add(new EnumValue(this, unknownQNameConstant, unknownQNameConstant.getSimpleName().toString(), null));
    }
    return enumValues;
}
Also used : XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) QName(javax.xml.namespace.QName) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) VariableElement(javax.lang.model.element.VariableElement) EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlQNameEnum(com.webcohesion.enunciate.metadata.qname.XmlQNameEnum)

Example 45 with EnunciateException

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

the class ElementComparator method compare.

// Inherited.
public int compare(Element accessor1, Element accessor2) {
    if (isSameId(accessor1, accessor2)) {
        // if the elements have the same identifier.
        return 0;
    }
    // they're not the same, so now determine relative order:
    String propertyName1 = accessor1.getSimpleName().toString();
    String propertyName2 = accessor2.getSimpleName().toString();
    if (this.propOrder != null) {
        // apply the specified property order
        int propertyIndex1 = find(this.propOrder, propertyName1);
        int propertyIndex2 = find(this.propOrder, propertyName2);
        if (propertyIndex1 < 0) {
            throw new EnunciateException("Property '" + propertyName1 + "' isn't included in the specified property order.");
        }
        if (propertyIndex2 < 0) {
            throw new EnunciateException("Property '" + propertyName2 + "' isn't included in the specified property order.");
        }
        return propertyIndex1 - propertyIndex2;
    } else if (accessOrder == XmlAccessOrder.ALPHABETICAL) {
        return propertyName1.compareTo(propertyName2);
    }
    // If no order is specified, it's undefined. We'll put it in source order.
    SourcePosition position1 = this.env.findSourcePosition(accessor1);
    SourcePosition position2 = this.env.findSourcePosition(accessor2);
    if (position1 != null && position2 != null) {
        return position1.compareTo(position2);
    } else {
        // don't have source positions... just provide a random sort order.
        return accessor1.hashCode() - accessor2.hashCode();
    }
}
Also used : EnunciateException(com.webcohesion.enunciate.EnunciateException) SourcePosition(com.webcohesion.enunciate.javac.decorations.SourcePosition)

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