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);
}
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;
}
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();
}
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;
}
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;
}
Aggregations