List of usage examples for org.apache.commons.jelly Script Script
Script
From source file:com.cyclopsgroup.waterview.web.taglib.SelectTag.java
/** * Overwrite or implement method processTag() * * @see com.cyclopsgroup.waterview.utils.TagSupportBase#processTag(org.apache.commons.jelly.XMLOutput) *///from w ww . j av a 2 s . c o m protected void processTag(XMLOutput output) throws Exception { FieldTag fieldTag = (FieldTag) requireParent(FieldTag.class); options = ListOrderedMap.decorate(new HashMap()); invokeBody(output); if (getItems() != null) { Iterator i = Collections.EMPTY_LIST.iterator(); if (TypeUtils.isIteratable(getItems())) { i = TypeUtils.iterate(getItems()); } else if (getItems() instanceof Map) { i = ((Map) getItems()).entrySet().iterator(); } while (i.hasNext()) { Object item = i.next(); SelectOption option = null; if (item instanceof SelectOption) { option = (SelectOption) item; } else if (item instanceof Map.Entry) { Map.Entry e = (Map.Entry) item; String name = TypeUtils.toString(e.getKey()); option = new DefaultSelectOption(name, TypeUtils.toString(e.getValue())); } else { String name = TypeUtils.toString(item); option = new DefaultSelectOption(name, name); } addOption(option); } } JellyEngine je = (JellyEngine) getServiceManager().lookup(JellyEngine.ROLE); final Script script = je.getScript("/waterview/FormSelectInput.jelly"); Script s = new Script() { public Script compile() throws JellyException { return this; } public void run(JellyContext context, XMLOutput output) throws JellyTagException { context.setVariable("selectTag", SelectTag.this); script.run(context, output); } }; fieldTag.setBodyScript(s); }
From source file:com.cyclopsgroup.waterview.jelly.JellyEngine.java
/** * Get script with give path and package name * * @param scriptPath Script path// w ww.j ava 2s . co m * @param packageName Look for it in this package * @param defaultScript Return as default value * @return Script object * @throws JellyException Throw it out */ public Script getScript(String packageName, String scriptPath, Script defaultScript) throws JellyException { String fullPath = scriptPath; String pkg = moduleManager.getPackageName(packageName); if (StringUtils.isNotEmpty(pkg)) { fullPath = pkg.replace('.', '/') + scriptPath; } Script script = null; synchronized (this) { if (getCacheManager().contains(this, fullPath)) { script = (Script) getCacheManager().get(this, fullPath); } else { final URL resource = getClass().getClassLoader().getResource(fullPath); if (resource == null) { script = DUMMY_SCRIPT; } else { JellyContext jc = new JellyContext(getGlobalContext()); final Script s = jc.compileScript(resource); script = new Script() { public Script compile() throws JellyException { return this; } public void run(JellyContext context, XMLOutput output) throws JellyTagException { TagSupport.addScriptResource(resource, context); try { s.run(context, output); } catch (JellyTagException e) { throw e; } finally { TagSupport.removeScriptResource(resource, context); } } }; } getCacheManager().put(this, fullPath, script); } } return script == DUMMY_SCRIPT ? defaultScript : script; }
From source file:org.kohsuke.stapler.jelly.groovy.JellyBuilder.java
@SuppressWarnings({ "ChainOfInstanceofChecks" })
protected void doInvokeMethod(QName name, Object args) {
List list = InvokerHelper.asList(args);
Map attributes = Collections.EMPTY_MAP;
Closure closure = null;// w w w . java2s .c om
String innerText = null;
// figure out what parameters are what
switch (list.size()) {
case 0:
break;
case 1: {
Object object = list.get(0);
if (object instanceof Map) {
attributes = (Map) object;
} else if (object instanceof Closure) {
closure = (Closure) object;
break;
} else {
if (object != null)
innerText = object.toString();
}
break;
}
case 2: {
Object object1 = list.get(0);
Object object2 = list.get(1);
if (object1 instanceof Map) {
attributes = (Map) object1;
if (object2 instanceof Closure) {
closure = (Closure) object2;
} else if (object2 != null) {
innerText = object2.toString();
}
} else {
innerText = object1.toString();
if (object2 instanceof Closure) {
closure = (Closure) object2;
} else if (object2 instanceof Map) {
attributes = (Map) object2;
} else {
throw new MissingMethodException(name.toString(), getClass(), list.toArray());
}
}
break;
}
case 3: {
Object arg0 = list.get(0);
Object arg1 = list.get(1);
Object arg2 = list.get(2);
if (arg0 instanceof Map && arg2 instanceof Closure) {
closure = (Closure) arg2;
attributes = (Map) arg0;
innerText = arg1.toString();
} else if (arg1 instanceof Map && arg2 instanceof Closure) {
closure = (Closure) arg2;
attributes = (Map) arg1;
innerText = arg0.toString();
} else {
throw new MissingMethodException(name.toString(), getClass(), list.toArray());
}
break;
}
default:
throw new MissingMethodException(name.toString(), getClass(), list.toArray());
}
if (isTag(name)) {// bridge to other Jelly tags
try {
TagScript tagScript = createTagScript(name, attributes);
if (tagScript != null) {
Script body = NULL_SCRIPT;
if (closure != null) {
final Closure theClosure = closure;
body = new Script() {
public Script compile() throws JellyException {
return this;
}
public void run(JellyContext context, XMLOutput output) throws JellyTagException {
JellyContext oldc = setContext(context);
XMLOutput oldo = setOutput(output);
try {
theClosure.setDelegate(JellyBuilder.this);
theClosure.call();
} finally {
setContext(oldc);
setOutput(oldo);
}
}
};
} else if (innerText != null)
body = new TextScript(innerText);
tagScript.setTagBody(body);
tagScript.run(context, output);
return;
}
} catch (JellyException e) {
throw new RuntimeException(e);
}
}
// static tag
this.attributes.clear();
for (Entry e : ((Map<?, ?>) attributes).entrySet()) {
Object v = e.getValue();
if (v == null)
continue;
String attName = e.getKey().toString();
this.attributes.addAttribute("", attName, attName, "CDATA", v.toString());
}
try {
output.startElement(name.getNamespaceURI(), name.getLocalPart(), name.getQualifiedName(),
this.attributes);
if (closure != null) {
closure.setDelegate(this);
closure.call();
}
if (innerText != null)
text(innerText);
output.endElement(name.getNamespaceURI(), name.getLocalPart(), name.getQualifiedName());
} catch (SAXException e) {
throw new RuntimeException(e); // what's the proper way to handle exceptions in Groovy?
}
}