diff --git a/framework/minilang/dtd/simple-methods-v2.xsd b/framework/minilang/dtd/simple-methods-v2.xsd index 6f66ab3d2d..580dae802b 100644 --- a/framework/minilang/dtd/simple-methods-v2.xsd +++ b/framework/minilang/dtd/simple-methods-v2.xsd @@ -854,31 +854,34 @@ under the License. - Runs an external script (minilang, bsh, groovy) from the expanded location provided. + Runs an external script or a short inline script (scriptlet). Error messages go on the error list and are handled with the check-errors tag. - + + + + The script location. The location attribute accepts the component:// file location + protocol. Script functions/methods can be invoked by appending a hash (#) and the + function/method name. + <br/><br/> + Required if the script attribute is empty. Attribute type: constant. + + + + + + + A short script (scriptlet). Can be used instead of a file. + The script must be prefixed with the script language followed by a colon (":"). + <br/><br/> + Required if the location attribute is empty. Attribute type: script. + + + - - - - - Script location (component://...) - - - - - - - The name of the list in the method environment to check for error messages. - Defaults to "error_list". - - - - @@ -921,8 +924,7 @@ under the License. Calls another simple-method in the same context as the current one. The called simple-method will have the same environment as the calling simple-method, including all environment fields, and either the event or service objects - that the calling simple-method was called - with. + that the calling simple-method was called with. @@ -930,7 +932,7 @@ under the License. - Used when memory-model="function". Copies the called method fields + Used when scope="function". Copies the called method fields to the calling method fields. diff --git a/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java b/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java index a169ae1c91..b38a1d064e 100644 --- a/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java +++ b/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java @@ -18,84 +18,101 @@ *******************************************************************************/ package org.ofbiz.minilang.method.callops; -import java.util.List; -import java.util.Map; - -import javolution.util.FastList; - import org.ofbiz.base.util.ScriptUtil; +import org.ofbiz.base.util.Scriptlet; +import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangRuntimeException; +import org.ofbiz.minilang.MiniLangUtil; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; -public class CallScript extends MethodOperation { +/** + * Executes a script. + */ +public final class CallScript extends MethodOperation { public static final String module = CallScript.class.getName(); - private static String getScriptLocation(String combinedName) { - int pos = combinedName.lastIndexOf("#"); - if (pos == -1) { - return combinedName; - } - return combinedName.substring(0, pos); - } - - private static String getScriptMethodName(String combinedName) { - int pos = combinedName.lastIndexOf("#"); - if (pos == -1) { - return null; - } - return combinedName.substring(pos + 1); - } - - private ContextAccessor> errorListAcsr; - private String location; - private String method; + private final String location; + private final String method; + private final Scriptlet scriptlet; public CallScript(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - String scriptLocation = element.getAttribute("location"); - this.location = getScriptLocation(scriptLocation); - this.method = getScriptMethodName(scriptLocation); - this.errorListAcsr = new ContextAccessor>(element.getAttribute("error-list-name"), "error_list"); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "location", "script"); + MiniLangValidate.requireAnyAttribute(simpleMethod, element, "location", "script"); + MiniLangValidate.constantAttributes(simpleMethod, element, "location"); + MiniLangValidate.scriptAttributes(simpleMethod, element, "script"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + String scriptAttribute = element.getAttribute("script"); + if (MiniLangUtil.containsScript(scriptAttribute)) { + this.scriptlet = new Scriptlet(StringUtil.convertOperatorSubstitutions(scriptAttribute)); + this.location = null; + this.method = null; + } else { + this.scriptlet = null; + String scriptLocation = element.getAttribute("location"); + int pos = scriptLocation.lastIndexOf("#"); + if (pos == -1) { + this.location = scriptLocation; + this.method = null; + } else { + this.location = scriptLocation.substring(0, pos); + this.method = scriptLocation.substring(pos + 1); + } + } } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - String location = methodContext.expandString(this.location); - String method = methodContext.expandString(this.method); - List messages = errorListAcsr.get(methodContext); - if (messages == null) { - messages = FastList.newInstance(); - errorListAcsr.put(methodContext, messages); - } - Map context = methodContext.getEnvMap(); - if (location.endsWith(".xml")) { + if (this.scriptlet != null) { try { - SimpleMethod.runSimpleMethod(location, method, methodContext); - } catch (MiniLangException e) { - messages.add("Error running simple method at location [" + location + "]: " + e.getMessage()); + this.scriptlet.executeScript(methodContext.getEnvMap()); + } catch (Exception e) { + throw new MiniLangRuntimeException(e.getMessage(), this); } + return true; + } + if (location.endsWith(".xml")) { + SimpleMethod.runSimpleMethod(location, method, methodContext); } else { - ScriptUtil.executeScript(this.location, this.method, context); + ScriptUtil.executeScript(this.location, this.method, methodContext.getEnvMap()); } - // update the method environment - methodContext.putAllEnv(context); - // always return true, error messages just go on the error list return true; } @Override public String expandedString(MethodContext methodContext) { - return rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public String rawString() { - return "