use of com.webcohesion.enunciate.api.datatype.Example in project enunciate by stoicflame.
the class JsonExamplesForMethod method exec.
public Object exec(List list) throws TemplateModelException {
if (list.size() < 1) {
throw new TemplateModelException("The uniqueMediaTypesFor method must have a parameter.");
}
TemplateModel from = (TemplateModel) list.get(0);
Object unwrapped = FreemarkerUtil.unwrap(from);
HashMap<String, String> uniqueMediaTypes = new HashMap<String, String>();
if (unwrapped instanceof Entity) {
Entity entity = (Entity) unwrapped;
List<? extends MediaTypeDescriptor> mts = entity.getMediaTypes();
if (mts != null) {
for (MediaTypeDescriptor mt : mts) {
if (mt.getMediaType().endsWith("json")) {
Example example = mt.getExample();
if (example != null) {
String body = example.getBody();
if (body != null) {
uniqueMediaTypes.put(mt.getMediaType(), body);
}
}
}
}
}
}
return uniqueMediaTypes;
}
use of com.webcohesion.enunciate.api.datatype.Example in project enunciate by stoicflame.
the class ExampleUtils method loadCustomExample.
public static Example loadCustomExample(Syntax syntax, String tagName, DecoratedElement element, EnunciateContext context) {
Example example = null;
JavaDoc.JavaDocTagList tagList = element.getJavaDoc().get(tagName);
if (tagList != null) {
for (String value : tagList) {
int firstSpace = JavaDoc.indexOfFirstWhitespace(value);
String mediaType = value.substring(0, firstSpace);
if (syntax.isAssignableToMediaType(mediaType) && (firstSpace + 1) < value.length()) {
String specifiedExample = value.substring(firstSpace + 1).trim();
Reader reader;
try {
if (specifiedExample.startsWith("classpath:")) {
String classpathResource = specifiedExample.substring(10);
if (classpathResource.startsWith("/")) {
classpathResource = classpathResource.substring(1);
}
InputStream resource = context.getResourceAsStream(classpathResource);
if (resource == null) {
throw new IllegalArgumentException("Unable to find /" + classpathResource + " on the classpath.");
}
reader = new InputStreamReader(resource, "UTF-8");
} else if (specifiedExample.startsWith("file:")) {
File file = context.getConfiguration().resolveFile(specifiedExample.substring(5));
if (!file.exists()) {
throw new IllegalArgumentException("Unable to find " + specifiedExample.substring(5) + ".");
}
reader = new FileReader(file);
} else {
reader = new StringReader(specifiedExample);
}
} catch (IOException e) {
throw new EnunciateException(e);
}
try {
example = syntax.parseExample(reader);
} catch (Exception e) {
context.getLogger().warn("Unable to parse @%s tag at %s: %s", tagName, example, e.getMessage());
}
}
}
}
return example;
}
Aggregations