Search in sources :

Example 1 with DataType

use of com.webcohesion.enunciate.api.datatype.DataType in project enunciate by stoicflame.

the class QNameForMediaTypeMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The QNameForType method must have a type mirror as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (unwrapped instanceof MediaTypeDescriptor) {
        MediaTypeDescriptor mt = (MediaTypeDescriptor) unwrapped;
        DataTypeReference typeReference = mt.getDataType();
        if (typeReference != null) {
            if (typeReference instanceof DataTypeReferenceImpl) {
                return ((DataTypeReferenceImpl) typeReference).getElementQName();
            } else if (associateJsonWithXml && mt.getMediaType().endsWith("json")) {
                DataType dataType = typeReference.getValue();
                if (dataType != null) {
                    XmlType knownType = this.context.getKnownType(dataType.getJavaElement());
                    if (knownType != null) {
                        return knownType.getQname();
                    }
                    TypeDefinition typeDefinition = this.context.findTypeDefinition(dataType.getJavaElement());
                    if (typeDefinition != null) {
                        return typeDefinition.getQname();
                    }
                }
            }
        }
    }
    return null;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) TemplateModelException(freemarker.template.TemplateModelException) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) DataTypeReferenceImpl(com.webcohesion.enunciate.modules.jaxb.api.impl.DataTypeReferenceImpl) DataType(com.webcohesion.enunciate.api.datatype.DataType) TemplateModel(freemarker.template.TemplateModel) XmlType(com.webcohesion.enunciate.modules.jaxb.model.types.XmlType) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 2 with DataType

use of com.webcohesion.enunciate.api.datatype.DataType in project enunciate by stoicflame.

the class BaseDatatypeNameForMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The datatypeNameFor method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    BaseType baseType = null;
    if (unwrapped instanceof DataType) {
        DataType dataType = (DataType) unwrapped;
        baseType = dataType.getBaseType();
    }
    if (baseType == null) {
        throw new TemplateModelException("No base data type name for: " + unwrapped);
    }
    switch(baseType) {
        case bool:
            return "boolean";
        case number:
            return "number";
        case string:
            return "string";
        default:
            return "object";
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) BaseType(com.webcohesion.enunciate.api.datatype.BaseType) DataType(com.webcohesion.enunciate.api.datatype.DataType) TemplateModel(freemarker.template.TemplateModel)

Example 3 with DataType

use of com.webcohesion.enunciate.api.datatype.DataType in project enunciate by stoicflame.

the class DefinitionIdForMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The definitionId method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (unwrapped instanceof DataType) {
        return definitionIdFromSlug(((DataType) unwrapped).getSlug());
    } else if (unwrapped instanceof DataTypeReference) {
        return definitionIdFromSlug(((DataTypeReference) unwrapped).getSlug());
    }
    return null;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) DataType(com.webcohesion.enunciate.api.datatype.DataType) TemplateModel(freemarker.template.TemplateModel)

Example 4 with DataType

use of com.webcohesion.enunciate.api.datatype.DataType in project enunciate by stoicflame.

the class ApiDocsJavaDocTagHandler method onInlineTag.

@Override
public String onInlineTag(String tagName, String tagText, DecoratedElement context) {
    if ("link".equals(tagName)) {
        JavaDocLink link = JavaDocLink.parse(tagText);
        String classRef = link.getClassName();
        String subelementRef = link.getMemberName();
        String value = link.getLabel();
        // use the current context as the class ref.
        if ("".equals(classRef)) {
            DecoratedElement type = context;
            while (!(type instanceof DecoratedTypeElement)) {
                type = (DecoratedElement) type.getEnclosingElement();
                if (type == null || type instanceof PackageElement) {
                    break;
                }
            }
            if (type instanceof DecoratedTypeElement) {
                classRef = ((DecoratedTypeElement) type).getQualifiedName().toString();
            }
        }
        if (!"".equals(classRef)) {
            if (classRef.indexOf('.') < 0) {
                // if it's a local reference, assume it's in the current package.
                DecoratedElement pckg = context;
                while (!(pckg instanceof DecoratedPackageElement)) {
                    pckg = (DecoratedElement) pckg.getEnclosingElement();
                    if (pckg == null) {
                        break;
                    }
                }
                if (pckg != null) {
                    classRef = ((DecoratedPackageElement) pckg).getQualifiedName() + "." + classRef;
                }
            }
            // now find the reference
            Set<Syntax> syntaxes = this.registry.getSyntaxes(this.context);
            for (Syntax syntax : syntaxes) {
                List<DataType> dataTypes = syntax.findDataTypes(classRef);
                if (dataTypes != null && !dataTypes.isEmpty()) {
                    DataType dataType = dataTypes.get(0);
                    Value dataTypeValue = dataType.findValue(subelementRef);
                    if (dataTypeValue != null) {
                        return "<a href=\"" + dataType.getSlug() + ".html#" + dataTypeValue.getValue() + "\">" + (value != null ? value : dataTypeValue.getValue()) + "</a>";
                    }
                    Property property = dataType.findProperty(subelementRef);
                    if (property != null) {
                        return "<a href=\"" + dataType.getSlug() + ".html#prop-" + property.getName() + "\">" + (value != null ? value : property.getName()) + "</a>";
                    }
                    return "<a href=\"" + dataType.getSlug() + ".html\">" + (value != null ? value : (subelementRef.isEmpty() ? dataType.getLabel() : subelementRef)) + "</a>";
                }
            }
            List<ResourceApi> resourceApis = this.registry.getResourceApis(this.context);
            for (ResourceApi resourceApi : resourceApis) {
                Method method = resourceApi.findMethodFor(classRef, subelementRef);
                if (method != null) {
                    if (value == null) {
                        value = method.getLabel() + " " + method.getResource().getGroup().getLabel();
                    }
                    return "<a href=\"" + method.getResource().getGroup().getSlug() + ".html#" + method.getSlug() + "\">" + value + "</a>";
                } else {
                    ResourceGroup resourceGroup = resourceApi.findResourceGroupFor(classRef);
                    if (resourceGroup != null) {
                        if (value == null) {
                            value = resourceGroup.getLabel();
                        }
                        return "<a href=\"" + resourceGroup.getSlug() + ".html\">" + value + "</a>";
                    }
                }
            }
            List<ServiceApi> serviceApis = this.registry.getServiceApis(this.context);
            for (ServiceApi serviceApi : serviceApis) {
                Operation operation = serviceApi.findOperationFor(classRef, subelementRef);
                if (operation != null) {
                    if (value == null) {
                        value = operation.getName();
                    }
                    return "<a href=\"" + operation.getService().getSlug() + ".html#" + operation.getSlug() + "\">" + value + "</a>";
                } else {
                    Service service = serviceApi.findServiceFor(classRef);
                    if (service != null) {
                        if (value == null) {
                            value = service.getLabel();
                        }
                        return "<a href=\"" + service.getSlug() + ".html\">" + value + "</a>";
                    }
                }
            }
        }
        return value != null ? value : tagText.trim();
    } else if ("code".equals(tagName)) {
        return "<code>" + tagText + "</code>";
    }
    return tagText;
}
Also used : JavaDocLink(com.webcohesion.enunciate.javac.javadoc.JavaDocLink) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) ServiceApi(com.webcohesion.enunciate.api.services.ServiceApi) Service(com.webcohesion.enunciate.api.services.Service) Method(com.webcohesion.enunciate.api.resources.Method) Operation(com.webcohesion.enunciate.api.services.Operation) ResourceApi(com.webcohesion.enunciate.api.resources.ResourceApi) DecoratedTypeElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement) Value(com.webcohesion.enunciate.api.datatype.Value) DataType(com.webcohesion.enunciate.api.datatype.DataType) PackageElement(javax.lang.model.element.PackageElement) DecoratedPackageElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedPackageElement) Syntax(com.webcohesion.enunciate.api.datatype.Syntax) DecoratedPackageElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedPackageElement) Property(com.webcohesion.enunciate.api.datatype.Property) ResourceGroup(com.webcohesion.enunciate.api.resources.ResourceGroup)

Example 5 with DataType

use of com.webcohesion.enunciate.api.datatype.DataType 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)

Aggregations

DataType (com.webcohesion.enunciate.api.datatype.DataType)5 TemplateModel (freemarker.template.TemplateModel)3 TemplateModelException (freemarker.template.TemplateModelException)3 DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)2 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)2 BaseType (com.webcohesion.enunciate.api.datatype.BaseType)1 Property (com.webcohesion.enunciate.api.datatype.Property)1 Syntax (com.webcohesion.enunciate.api.datatype.Syntax)1 Value (com.webcohesion.enunciate.api.datatype.Value)1 MediaTypeDescriptor (com.webcohesion.enunciate.api.resources.MediaTypeDescriptor)1 Method (com.webcohesion.enunciate.api.resources.Method)1 ResourceApi (com.webcohesion.enunciate.api.resources.ResourceApi)1 ResourceGroup (com.webcohesion.enunciate.api.resources.ResourceGroup)1 Operation (com.webcohesion.enunciate.api.services.Operation)1 Service (com.webcohesion.enunciate.api.services.Service)1 ServiceApi (com.webcohesion.enunciate.api.services.ServiceApi)1 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)1 DecoratedElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedElement)1 DecoratedPackageElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedPackageElement)1 DecoratedTypeElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement)1