Search in sources :

Example 1 with Example

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;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) TemplateModelException(freemarker.template.TemplateModelException) Entity(com.webcohesion.enunciate.api.resources.Entity) HashMap(java.util.HashMap) Example(com.webcohesion.enunciate.api.datatype.Example) TemplateModel(freemarker.template.TemplateModel)

Example 2 with Example

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;
}
Also used : JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) Example(com.webcohesion.enunciate.api.datatype.Example) DocumentationExample(com.webcohesion.enunciate.metadata.DocumentationExample)

Aggregations

Example (com.webcohesion.enunciate.api.datatype.Example)2 EnunciateException (com.webcohesion.enunciate.EnunciateException)1 Entity (com.webcohesion.enunciate.api.resources.Entity)1 MediaTypeDescriptor (com.webcohesion.enunciate.api.resources.MediaTypeDescriptor)1 JavaDoc (com.webcohesion.enunciate.javac.javadoc.JavaDoc)1 DocumentationExample (com.webcohesion.enunciate.metadata.DocumentationExample)1 TemplateModel (freemarker.template.TemplateModel)1 TemplateModelException (freemarker.template.TemplateModelException)1 HashMap (java.util.HashMap)1