Search in sources :

Example 1 with ModelWalker

use of com.disney.groovity.model.ModelWalker in project groovity by disney.

the class Attachment method find.

public static Attachment find(Object model, final String name) throws Exception {
    AtomicReference<Attachment> a = new AtomicReference<>();
    new ModelWalker() {

        public void visitObject(Object o) throws Exception {
            if (o instanceof Attachment) {
                Attachment c = (Attachment) o;
                if (c.getName().equals(name)) {
                    a.set(c);
                }
            } else {
                super.visitObject(o);
            }
        }
    }.visit(model);
    return a.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ModelWalker(com.disney.groovity.model.ModelWalker) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with ModelWalker

use of com.disney.groovity.model.ModelWalker in project groovity by disney.

the class Attachment method findAll.

public static List<Attachment> findAll(Object model) throws Exception {
    ArrayList<Attachment> a = new ArrayList<>();
    new ModelWalker() {

        public void visitObject(Object o) throws Exception {
            if (o instanceof Attachment) {
                Attachment c = (Attachment) o;
                a.add(c);
            } else {
                super.visitObject(o);
            }
        }
    }.visit(model);
    return a;
}
Also used : ArrayList(java.util.ArrayList) ModelWalker(com.disney.groovity.model.ModelWalker) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with ModelWalker

use of com.disney.groovity.model.ModelWalker in project groovity by disney.

the class Write method tag.

@SuppressWarnings({ "rawtypes", "resource" })
public Object tag(Map attributes, Closure body) throws Exception {
    boolean valueDeclared = attributes.containsKey(VALUE);
    Object value = null;
    if (valueDeclared) {
        value = resolve(attributes, VALUE);
        if (value == null) {
            value = resolve(attributes, "null");
        }
    }
    Object format = resolve(attributes, "format");
    Object escape = resolve(attributes, "escape");
    Object pretty = resolve(attributes, "pretty");
    if (pretty != null && !(pretty instanceof Boolean)) {
        pretty = Boolean.valueOf(pretty.toString());
    }
    Object to = resolve(attributes, "to");
    boolean returnTo = true;
    if (to == null) {
        returnTo = false;
        // fall back on body out
        to = get(body, OUT);
    }
    if (to == null) {
        throw new IllegalArgumentException("write tag requires valid Writer or String.class for 'to'");
    }
    Object filter = resolve(attributes, "filter");
    Writer writer;
    boolean returnString = false;
    if (to instanceof Writer) {
        writer = (Writer) to;
    } else if (to.equals(String.class) || to instanceof CharSequence) {
        writer = new CharArrayWriter();
        returnString = true;
    } else {
        throw new IllegalArgumentException("write tag requires valid Writer or String.class for 'to', unrecognized option " + to);
    }
    if (escape != null) {
        String esc = escape.toString();
        if (esc.equalsIgnoreCase("xml")) {
            writer = new XmlEscapingWriter(writer);
        } else if (esc.equalsIgnoreCase("json")) {
            writer = new JsonEscapingWriter(writer);
        } else if (esc.equalsIgnoreCase("html")) {
            writer = new HtmlEscapingWriter(writer);
        } else {
            throw new IllegalArgumentException("Unrecognized escape value " + esc + ", try xml or json");
        }
    }
    if (value == null) {
        Object oldOut = get(body, OUT);
        bind(body, OUT, writer);
        try {
            value = body.call();
        } finally {
            bind(body, OUT, oldOut);
        }
    }
    if (value != null) {
        if (format != null && !"json".equals(format) && !"xml".equals(format)) {
            // we don't want to close the formatter because it's not our job to close the writer
            Formatter formatter = new Formatter(writer);
            if (value instanceof Collection) {
                formatter.format(format.toString(), ((Collection) value).toArray());
            } else {
                formatter.format(format.toString(), value);
            }
        } else {
            if (value instanceof CharSequence) {
                writer.append((CharSequence) value);
            } else if ((filter == null) && (value instanceof Writable) && (value != body.getOwner())) {
                ((Writable) value).writeTo(writer);
            } else {
                ModelWalker mw;
                if (format == null) {
                    Object response = get(body, "response");
                    if (response != null) {
                        MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(response.getClass());
                        MetaProperty mp = mc.hasProperty(response, "contentType");
                        if (mp != null) {
                            String ct = (String) mp.getProperty(response);
                            if (ct != null && ct.contains("xml")) {
                                format = "xml";
                            }
                        }
                    }
                }
                if ("xml".equals(format)) {
                    if (pretty != null && ((Boolean) pretty).booleanValue()) {
                        mw = new ModelXmlWriter(writer, "\t");
                        // prevent secondary pretty, just write it pretty up front!
                        pretty = Boolean.FALSE;
                    } else {
                        mw = new ModelXmlWriter(writer);
                    }
                    String root = resolve(attributes, "root", String.class);
                    if (root != null) {
                        ((ModelXmlWriter) mw).setRootElementName(root);
                    }
                    @SuppressWarnings("unchecked") Map<String, String> prefixes = resolve(attributes, "namespaces", Map.class);
                    if (prefixes != null) {
                        ((ModelXmlWriter) mw).setNamespacePrefixes(prefixes);
                    }
                } else {
                    if (pretty != null && ((Boolean) pretty).booleanValue()) {
                        mw = new ModelJsonWriter(writer, "\t");
                        // prevent secondary pretty, just write it pretty up front!
                        pretty = Boolean.FALSE;
                    } else {
                        mw = new ModelJsonWriter(writer);
                    }
                }
                if (filter != null) {
                    ModelFilter[] mfa = null;
                    if (filter instanceof Collection) {
                        Collection src = (Collection) filter;
                        mfa = new ModelFilter[src.size()];
                        int pos = 0;
                        for (Object mf : src) {
                            mfa[pos++] = (ModelFilter) DefaultTypeTransformation.castToType(mf, ModelFilter.class);
                        }
                    } else if (filter.getClass().isArray() && filter.getClass().getComponentType().equals(ModelFilter.class)) {
                        mfa = (ModelFilter[]) filter;
                    } else if (filter instanceof ModelFilter) {
                        mfa = new ModelFilter[] { (ModelFilter) filter };
                    } else {
                        mfa = new ModelFilter[] { (ModelFilter) DefaultTypeTransformation.castToType(filter, ModelFilter.class) };
                    }
                    mw.setFilters(mfa);
                }
                mw.visit(value);
            }
        }
    }
    if (!returnTo) {
        return null;
    }
    Object rval = returnString ? writer.toString() : writer;
    if (attributes.get(VAR) != null) {
        bind(body, attributes.get(VAR).toString(), rval);
    }
    return rval;
}
Also used : ModelFilter(com.disney.groovity.model.ModelFilter) Formatter(java.util.Formatter) Writable(groovy.lang.Writable) ModelWalker(com.disney.groovity.model.ModelWalker) CharArrayWriter(java.io.CharArrayWriter) HtmlEscapingWriter(com.disney.groovity.util.HtmlEscapingWriter) MetaClass(groovy.lang.MetaClass) ModelXmlWriter(com.disney.groovity.model.ModelXmlWriter) Collection(java.util.Collection) XmlEscapingWriter(com.disney.groovity.util.XmlEscapingWriter) JsonEscapingWriter(com.disney.groovity.util.JsonEscapingWriter) ModelJsonWriter(com.disney.groovity.model.ModelJsonWriter) MetaProperty(groovy.lang.MetaProperty) Map(java.util.Map) CharArrayWriter(java.io.CharArrayWriter) JsonEscapingWriter(com.disney.groovity.util.JsonEscapingWriter) ModelJsonWriter(com.disney.groovity.model.ModelJsonWriter) Writer(java.io.Writer) ModelXmlWriter(com.disney.groovity.model.ModelXmlWriter) HtmlEscapingWriter(com.disney.groovity.util.HtmlEscapingWriter) XmlEscapingWriter(com.disney.groovity.util.XmlEscapingWriter)

Aggregations

ModelWalker (com.disney.groovity.model.ModelWalker)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ModelFilter (com.disney.groovity.model.ModelFilter)1 ModelJsonWriter (com.disney.groovity.model.ModelJsonWriter)1 ModelXmlWriter (com.disney.groovity.model.ModelXmlWriter)1 HtmlEscapingWriter (com.disney.groovity.util.HtmlEscapingWriter)1 JsonEscapingWriter (com.disney.groovity.util.JsonEscapingWriter)1 XmlEscapingWriter (com.disney.groovity.util.XmlEscapingWriter)1 MetaClass (groovy.lang.MetaClass)1 MetaProperty (groovy.lang.MetaProperty)1 Writable (groovy.lang.Writable)1 CharArrayWriter (java.io.CharArrayWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Formatter (java.util.Formatter)1 Map (java.util.Map)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1