Search in sources :

Example 1 with DataTypeReference

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

the class SimpleNameForMethod method simpleNameFor.

@Override
public String simpleNameFor(Object unwrapped, boolean noParams) throws TemplateModelException {
    if (unwrapped instanceof Entity) {
        List<? extends MediaTypeDescriptor> mediaTypes = ((Entity) unwrapped).getMediaTypes();
        for (MediaTypeDescriptor mediaType : mediaTypes) {
            if (this.jsonContext.getLabel().equals(mediaType.getSyntax())) {
                DataTypeReference dataType = mediaType.getDataType();
                unwrapped = this.jsonContext.findType(dataType);
                if (unwrapped == null) {
                    return "JavaScriptObject";
                }
            }
        }
    }
    if (unwrapped instanceof Entity) {
        return "JavaScriptObject";
    }
    return super.simpleNameFor(unwrapped, noParams);
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) Entity(com.webcohesion.enunciate.api.resources.Entity) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference)

Example 2 with DataTypeReference

use of com.webcohesion.enunciate.api.datatype.DataTypeReference 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 3 with DataTypeReference

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

the class DataFormatNameForMethod method exec.

@SuppressWarnings("rawtypes")
public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The dataFormatNameFor method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (!DataTypeReference.class.isAssignableFrom(unwrapped.getClass())) {
        return null;
    }
    DataTypeReference reference = DataTypeReference.class.cast(unwrapped);
    return reference.getBaseTypeFormat();
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) TemplateModel(freemarker.template.TemplateModel)

Example 4 with DataTypeReference

use of com.webcohesion.enunciate.api.datatype.DataTypeReference 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 5 with DataTypeReference

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

the class FindBestDataTypeMethod method findBestDataType.

protected static DataTypeReference findBestDataType(List<? extends MediaTypeDescriptor> mediaTypes) {
    if (mediaTypes == null || mediaTypes.isEmpty()) {
        return null;
    }
    float highestQuality = Float.MIN_VALUE;
    for (MediaTypeDescriptor mediaTypeDescriptor : mediaTypes) {
        highestQuality = Math.max(highestQuality, mediaTypeDescriptor.getQualityOfSourceFactor());
    }
    // first filter out all the media types of lower quality:
    mediaTypes = new ArrayList<MediaTypeDescriptor>(mediaTypes);
    Iterator<? extends MediaTypeDescriptor> iterator = mediaTypes.iterator();
    while (iterator.hasNext()) {
        MediaTypeDescriptor mediaTypeDescriptor = iterator.next();
        if (mediaTypeDescriptor.getQualityOfSourceFactor() < highestQuality) {
            iterator.remove();
        }
    }
    // return the first JSON-based media type.
    for (MediaTypeDescriptor mediaTypeDescriptor : mediaTypes) {
        if (mediaTypeDescriptor.getSyntax() != null && mediaTypeDescriptor.getSyntax().toLowerCase().contains("json")) {
            return mediaTypeDescriptor.getDataType();
        }
    }
    // return the first text-based media type.
    for (MediaTypeDescriptor mediaTypeDescriptor : mediaTypes) {
        String mt = mediaTypeDescriptor.getMediaType();
        if (mt != null) {
            mt = mt.toLowerCase();
            if (mt.startsWith("text") || mt.endsWith("json") || mt.endsWith("xml")) {
                DataTypeReference dataType = mediaTypeDescriptor.getDataType();
                return dataType == null || dataType.getValue() == null ? GENERIC_STRING_BASED_DATATYPE_REFERENCE : dataType;
            }
        }
    }
    // didn't find any text-based media types; try any other media types.
    for (MediaTypeDescriptor mediaTypeDescriptor : mediaTypes) {
        if (mediaTypeDescriptor.getDataType() != null) {
            return mediaTypeDescriptor.getDataType();
        }
    }
    return null;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference)

Aggregations

DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)8 TemplateModel (freemarker.template.TemplateModel)5 TemplateModelException (freemarker.template.TemplateModelException)5 MediaTypeDescriptor (com.webcohesion.enunciate.api.resources.MediaTypeDescriptor)4 Entity (com.webcohesion.enunciate.api.resources.Entity)3 DataType (com.webcohesion.enunciate.api.datatype.DataType)2 BaseType (com.webcohesion.enunciate.api.datatype.BaseType)1 Method (com.webcohesion.enunciate.api.resources.Method)1 StatusCode (com.webcohesion.enunciate.api.resources.StatusCode)1 DataTypeReferenceImpl (com.webcohesion.enunciate.modules.jaxb.api.impl.DataTypeReferenceImpl)1 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)1 XmlType (com.webcohesion.enunciate.modules.jaxb.model.types.XmlType)1