Search in sources :

Example 11 with EnunciateException

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

the class TypeDefinition method aggregatePotentialAccessors.

/**
 * Aggregate the potential accessor into their separate buckets for the given class declaration, recursively including transient superclasses.
 *
 * @param bag        The collected fields and properties.
 * @param clazz      The class.
 * @param filter     The filter.
 */
protected void aggregatePotentialAccessors(AccessorBag bag, DecoratedTypeElement clazz, AccessorFilter filter, boolean inlineAccessorsOfSuperclasses) {
    String fqn = clazz.getQualifiedName().toString();
    if (Object.class.getName().equals(fqn) || Enum.class.getName().equals(fqn)) {
        return;
    }
    if (bag.typeIdProperty == null) {
        final JsonTypeInfo info = getAnnotation(JsonTypeInfo.class);
        if (info != null && !info.property().isEmpty()) {
            bag.typeIdProperty = info.property();
        }
    }
    DecoratedTypeElement superDeclaration = clazz.getSuperclass() != null ? (DecoratedTypeElement) this.env.getTypeUtils().asElement(clazz.getSuperclass()) : null;
    if (superDeclaration != null && (this.context.isIgnored(superDeclaration) || inlineAccessorsOfSuperclasses)) {
        inlineAccessorsOfSuperclasses = true;
        aggregatePotentialAccessors(bag, superDeclaration, filter, true);
    }
    TypeElement mixin = this.context.lookupMixin(clazz);
    List<VariableElement> fieldElements = new ArrayList<VariableElement>(ElementFilter.fieldsIn(clazz.getEnclosedElements()));
    if (mixin != null) {
        // replace all mixin fields.
        for (VariableElement mixinField : ElementFilter.fieldsIn(mixin.getEnclosedElements())) {
            int index = indexOf(fieldElements, mixinField.getSimpleName().toString());
            if (index >= 0) {
                fieldElements.set(index, mixinField);
            } else {
                fieldElements.add(mixinField);
            }
        }
    }
    Set<String> propsIgnore = new HashSet<String>();
    for (VariableElement fieldDeclaration : fieldElements) {
        JsonUnwrapped unwrapped = fieldDeclaration.getAnnotation(JsonUnwrapped.class);
        if (unwrapped != null && unwrapped.enabled()) {
            TypeMirror typeMirror = fieldDeclaration.asType();
            if (!(typeMirror instanceof DeclaredType)) {
                throw new EnunciateException(String.format("%s: %s cannot be JSON unwrapped.", fieldDeclaration, typeMirror));
            }
            aggregatePotentialAccessors(bag, (DecoratedTypeElement) ((DeclaredType) typeMirror).asElement(), filter, inlineAccessorsOfSuperclasses);
            // Fix issue #806
            propsIgnore.add(fieldDeclaration.getSimpleName().toString());
        } else if (!filter.accept((DecoratedElement) fieldDeclaration)) {
            bag.fields.removeByName(fieldDeclaration);
        } else {
            bag.fields.addOrReplace(fieldDeclaration);
        }
    }
    Jackson1PropertySpec propertySpec = new Jackson1PropertySpec(this.env);
    List<PropertyElement> propertyElements = new ArrayList<PropertyElement>(clazz.getProperties(propertySpec));
    if (mixin != null) {
        // replace all mixin properties.
        for (PropertyElement mixinProperty : ((DecoratedTypeElement) mixin).getProperties(propertySpec)) {
            int index = indexOf(propertyElements, mixinProperty.getSimpleName().toString());
            if (index >= 0) {
                propertyElements.set(index, mixinProperty);
            } else {
                propertyElements.add(mixinProperty);
            }
        }
    }
    for (PropertyElement propertyDeclaration : propertyElements) {
        JsonUnwrapped unwrapped = propertyDeclaration.getAnnotation(JsonUnwrapped.class);
        if (unwrapped != null && unwrapped.enabled()) {
            DecoratedTypeElement element;
            TypeMirror typeMirror = propertyDeclaration.asType();
            switch(typeMirror.getKind()) {
                case DECLARED:
                    element = (DecoratedTypeElement) ((DeclaredType) typeMirror).asElement();
                    break;
                case TYPEVAR:
                    typeMirror = ((TypeVariable) typeMirror).getUpperBound();
                    element = (DecoratedTypeElement) ((DeclaredType) typeMirror).asElement();
                    break;
                case WILDCARD:
                    TypeMirror bound = ((WildcardType) typeMirror).getExtendsBound();
                    if (bound == null) {
                        bound = ((WildcardType) typeMirror).getSuperBound();
                    }
                    if (!(bound instanceof DeclaredType)) {
                        bound = TypeMirrorUtils.objectType(this.env);
                    }
                    element = (DecoratedTypeElement) ((DeclaredType) bound).asElement();
                    break;
                default:
                    throw new EnunciateException(String.format("%s: %s cannot be JSON unwrapped.", propertyDeclaration, typeMirror));
            }
            aggregatePotentialAccessors(bag, element, filter, inlineAccessorsOfSuperclasses);
        } else if (!filter.accept(propertyDeclaration) || indexOf(bag.fields, propertyDeclaration.getSimpleName().toString()) >= 0 || propsIgnore.contains(propertyDeclaration.getSimpleName().toString())) {
            bag.properties.removeByName(propertyDeclaration);
        } else {
            bag.properties.addOrReplace(propertyDeclaration);
        }
    }
}
Also used : DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) EnunciateException(com.webcohesion.enunciate.EnunciateException)

Example 12 with EnunciateException

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

the class RubyJSONClientModule 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 = RubyJSONClientModule.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 13 with EnunciateException

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

the class ComplexTypeExampleImpl method getBody.

@Override
public String getBody() {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder domBuilder = builderFactory.newDocumentBuilder();
        Document document = domBuilder.newDocument();
        String rootName = Character.toLowerCase(this.typeDefinition.getSimpleName().charAt(0)) + "-----";
        String rootNamespace = this.typeDefinition.getNamespace();
        ElementDeclaration element = typeDefinition.getContext().findElementDeclaration(typeDefinition);
        if (element != null) {
            rootName = element.getName();
            rootNamespace = element.getNamespace();
        }
        Element rootElement = document.createElementNS(rootNamespace, rootName);
        Element outer = rootElement;
        for (DataTypeReference.ContainerType container : this.containers) {
            Element containerEl = document.createElementNS("", container.name());
            containerEl.appendChild(outer);
            outer = containerEl;
        }
        document.appendChild(outer);
        Context context = new Context();
        context.stack = new LinkedList<String>();
        build(rootElement, this.typeDefinition, document, context);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        DOMSource source = new DOMSource(document);
        StringWriter value = new StringWriter();
        transformer.transform(source, new StreamResult(value));
        return value.toString();
    } catch (ParserConfigurationException e) {
        throw new EnunciateException(e);
    } catch (TransformerException e) {
        throw new EnunciateException(e);
    }
}
Also used : ApiRegistrationContext(com.webcohesion.enunciate.api.ApiRegistrationContext) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TypeElement(javax.lang.model.element.TypeElement) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) EnunciateException(com.webcohesion.enunciate.EnunciateException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) ElementDeclaration(com.webcohesion.enunciate.modules.jaxb.model.ElementDeclaration)

Example 14 with EnunciateException

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

the class EnumTypeDefinition method loadEnumValues.

protected List<EnumValue> loadEnumValues() {
    List<VariableElement> enumConstants = enumValues();
    List<EnumValue> enumValues = new ArrayList<EnumValue>();
    HashSet<String> enumValueNames = new HashSet<String>(enumConstants.size());
    for (VariableElement enumConstant : enumConstants) {
        if (isIgnored(enumConstant)) {
            continue;
        }
        String value = enumConstant.getSimpleName().toString();
        XmlEnumValue enumValue = enumConstant.getAnnotation(XmlEnumValue.class);
        if (enumValue != null) {
            value = enumValue.value();
        }
        if (!enumValueNames.add(value)) {
            throw new EnunciateException(getQualifiedName() + ": duplicate enum value: " + value);
        }
        enumValues.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), value));
    }
    return enumValues;
}
Also used : EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue) VariableElement(javax.lang.model.element.VariableElement) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Example 15 with EnunciateException

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

the class PHPJSONClientModule 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 = PHPJSONClientModule.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)

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