use of groovy.lang.Closure in project gradle by gradle.
the class AntFileCollectionBuilder method addToAntBuilder.
@Override
public Object addToAntBuilder(Object node, String childNodeName) {
final DynamicObject dynamicObject = new BeanDynamicObject(node);
dynamicObject.invokeMethod(childNodeName == null ? "resources" : childNodeName, new Closure(this) {
public Object doCall(Object ignore) {
for (File file : files) {
dynamicObject.invokeMethod("file", Collections.singletonMap("file", AntUtil.maskFilename(file.getAbsolutePath())));
}
return null;
}
});
return node;
}
use of groovy.lang.Closure in project gradle by gradle.
the class AntFileCollectionMatchingTaskBuilder method addToAntBuilder.
public Object addToAntBuilder(final Object node, final String childNodeName) {
final DynamicObject dynamicObject = new BeanDynamicObject(node);
final Iterable<DirectoryFileTree> existing = Lists.newLinkedList(FluentIterable.from(fileTrees).filter(new Predicate<DirectoryFileTree>() {
@Override
public boolean apply(DirectoryFileTree input) {
return input.getDir().exists();
}
}));
for (DirectoryFileTree fileTree : existing) {
dynamicObject.invokeMethod(childNodeName, Collections.singletonMap("location", fileTree.getDir()));
}
dynamicObject.invokeMethod("or", new Closure<Void>(this) {
public Object doCall(Object ignore) {
for (final DirectoryFileTree fileTree : existing) {
dynamicObject.invokeMethod("and", new Closure<Void>(this) {
public Object doCall(Object ignore) {
dynamicObject.invokeMethod("gradleBaseDirSelector", Collections.singletonMap("baseDir", fileTree.getDir()));
fileTree.getPatterns().addToAntBuilder(node, null);
return null;
}
});
}
return null;
}
});
return node;
}
use of groovy.lang.Closure in project gradle by gradle.
the class AntFileTreeBuilder method addToAntBuilder.
@Override
public Object addToAntBuilder(Object node, String childNodeName) {
final DynamicObject dynamicObject = new BeanDynamicObject(node);
dynamicObject.invokeMethod(childNodeName == null ? "resources" : childNodeName, new Closure(this) {
public Object doCall(Object ignore) {
for (Map.Entry<String, File> entry : files.entrySet()) {
String name = entry.getKey();
File file = entry.getValue();
// gradleFileResource type is mapped to AntFileResource
dynamicObject.invokeMethod("gradleFileResource", ImmutableMap.of("file", file.getAbsolutePath(), "name", name));
}
return null;
}
});
return node;
}
use of groovy.lang.Closure in project grails-core by grails.
the class GroovyPage method invokeTagLibClosure.
private void invokeTagLibClosure(String tagName, String tagNamespace, Closure<?> tagLibClosure, Map<?, ?> attrs, Closure<?> body, boolean returnsObject, Map<String, Object> defaultEncodeAs) {
Closure<?> tag = (Closure<?>) tagLibClosure.clone();
if (!(attrs instanceof GroovyPageAttributes)) {
attrs = new GroovyPageAttributes(attrs);
}
((GroovyPageAttributes) attrs).setGspTagSyntaxCall(true);
boolean encodeAsPushedToStack = false;
try {
Map<String, Object> codecSettings = TagOutput.createCodecSettings(tagNamespace, tagName, attrs, defaultEncodeAs);
if (codecSettings != null) {
outputStack.push(WithCodecHelper.createOutputStackAttributesBuilder(codecSettings, outputContext.getGrailsApplication()).build());
encodeAsPushedToStack = true;
}
Object tagresult = null;
switch(tag.getParameterTypes().length) {
case 1:
tagresult = tag.call(new Object[] { attrs });
outputTagResult(returnsObject, tagresult);
if (body != null && body != TagOutput.EMPTY_BODY_CLOSURE) {
body.call();
}
break;
case 2:
tagresult = tag.call(new Object[] { attrs, (body != null) ? body : TagOutput.EMPTY_BODY_CLOSURE });
outputTagResult(returnsObject, tagresult);
break;
}
} finally {
if (encodeAsPushedToStack)
outputStack.pop();
}
}
use of groovy.lang.Closure in project grails-core by grails.
the class GroovyPage method invokeTag.
/**
* Attempts to invokes a dynamic tag
*
* @param tagName The name of the tag
* @param tagNamespace The taglib's namespace
* @param lineNumber GSP source lineNumber
* @param attrs The tags attributes
* @param bodyClosureIndex The index of the body variable
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public final void invokeTag(String tagName, String tagNamespace, int lineNumber, Map attrs, int bodyClosureIndex) {
Closure body = getBodyClosure(bodyClosureIndex);
// TODO custom namespace stuff needs to be generalized and pluggable
if (tagNamespace.equals(TEMPLATE_NAMESPACE) || tagNamespace.equals(LINK_NAMESPACE)) {
final String tmpTagName = tagName;
final Map tmpAttrs = attrs;
Object encodeAs = tmpAttrs.remove(ENCODE_AS_ATTRIBUTE_NAME);
if (tagNamespace.equals(TEMPLATE_NAMESPACE)) {
tagName = "render";
attrs = CollectionUtils.newMap("model", tmpAttrs, "template", tmpTagName);
} else if (tagNamespace.equals(LINK_NAMESPACE)) {
tagName = "link";
attrs = CollectionUtils.newMap("mapping", tmpTagName);
if (!tmpAttrs.isEmpty()) {
attrs.put("params", tmpAttrs);
}
}
if (encodeAs != null) {
attrs.put(ENCODE_AS_ATTRIBUTE_NAME, encodeAs);
}
tagNamespace = DEFAULT_NAMESPACE;
}
try {
GroovyObject tagLib = getTagLib(tagNamespace, tagName);
if (tagLib != null || (gspTagLibraryLookup != null && gspTagLibraryLookup.hasNamespace(tagNamespace))) {
if (tagLib != null) {
boolean returnsObject = gspTagLibraryLookup.doesTagReturnObject(tagNamespace, tagName);
Object tagLibClosure = tagLib.getProperty(tagName);
if (tagLibClosure instanceof Closure) {
Map<String, Object> encodeAsForTag = gspTagLibraryLookup.getEncodeAsForTag(tagNamespace, tagName);
invokeTagLibClosure(tagName, tagNamespace, (Closure) tagLibClosure, attrs, body, returnsObject, encodeAsForTag);
} else {
throw new GrailsTagException("Tag [" + tagName + "] does not exist in tag library [" + tagLib.getClass().getName() + "]", getGroovyPageFileName(), lineNumber);
}
} else {
throw new GrailsTagException("Tag [" + tagName + "] does not exist. No tag library found for namespace: " + tagNamespace, getGroovyPageFileName(), lineNumber);
}
} else {
staticOut.append('<').append(tagNamespace).append(':').append(tagName);
for (Object o : attrs.entrySet()) {
Map.Entry entry = (Map.Entry) o;
staticOut.append(' ');
staticOut.append(entry.getKey()).append('=');
String value = String.valueOf(entry.getValue());
// handle attribute value quotes & possible escaping " -> "
boolean containsQuotes = (value.indexOf('"') > -1);
boolean containsSingleQuote = (value.indexOf('\'') > -1);
if (containsQuotes && !containsSingleQuote) {
staticOut.append('\'').append(value).append('\'');
} else if (containsQuotes & containsSingleQuote) {
staticOut.append('\"').append(value.replaceAll("\"", """)).append('\"');
} else {
staticOut.append('\"').append(value).append('\"');
}
}
if (body == null) {
staticOut.append("/>");
} else {
staticOut.append('>');
Object bodyOutput = body.call();
if (bodyOutput != null)
staticOut.print(bodyOutput);
staticOut.append("</").append(tagNamespace).append(':').append(tagName).append('>');
}
}
} catch (Throwable e) {
if (LOG.isTraceEnabled()) {
LOG.trace("Full exception for problem at " + getGroovyPageFileName() + ":" + lineNumber, e);
}
// hence we don't wrap the exception and simple rethrow it
if (tagName.matches("capture(Body|Head|Meta|Title|Component)")) {
RuntimeException rte = ExceptionUtils.getFirstRuntimeException(e);
if (rte == null) {
throwRootCause(tagName, tagNamespace, lineNumber, e);
} else {
throw rte;
}
} else {
throwRootCause(tagName, tagNamespace, lineNumber, e);
}
}
}
Aggregations