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