Author: jaz
Date: Thu Apr 15 18:32:35 2010
New Revision: 934510

URL: http://svn.apache.org/viewvc?rev=934510&view=rev
Log:
implemented <script> tag in minilang that works exactly the same way as the 
screen widget version

Added:
    
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java
Modified:
    ofbiz/trunk/framework/minilang/dtd/simple-methods.xsd
    
ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory

Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods.xsd
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/dtd/simple-methods.xsd?rev=934510&r1=934509&r2=934510&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/dtd/simple-methods.xsd (original)
+++ ofbiz/trunk/framework/minilang/dtd/simple-methods.xsd Thu Apr 15 18:32:35 
2010
@@ -835,6 +835,34 @@ under the License.
             </xs:simpleType>
         </xs:attribute>
     </xs:attributeGroup>
+    <xs:element name="script" substitutionGroup="CallOperations">
+        <xs:annotation>
+            <xs:documentation>
+                Runs an external script (minilang, bsh, groovy) from the 
expanded location provided.               
+                Error messages go on the error list and are handled with the 
check-errors tag.
+            </xs:documentation>
+        </xs:annotation>
+        <xs:complexType mixed="true">
+            <xs:attributeGroup ref="attlist.script"/>
+        </xs:complexType>
+    </xs:element>
+    <xs:attributeGroup name="attlist.script">
+        <xs:attribute type="xs:string" name="location">
+            <xs:annotation>
+                <xs:documentation>
+                    Script location (component://...)
+                </xs:documentation>
+            </xs:annotation>
+        </xs:attribute>
+        <xs:attribute type="xs:string" name="error-list-name" 
default="error_list">
+            <xs:annotation>
+                <xs:documentation>
+                    The name of the list in the method environment to check 
for error messages.
+                    Defaults to "error_list".
+                </xs:documentation>
+            </xs:annotation>
+        </xs:attribute>
+    </xs:attributeGroup>
     <xs:element name="call-bsh" substitutionGroup="CallOperations">
         <xs:annotation>
             <xs:documentation>

Modified: 
ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation%24Factory?rev=934510&r1=934509&r2=934510&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory
 (original)
+++ 
ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory
 Thu Apr 15 18:32:35 2010
@@ -19,6 +19,7 @@ org.ofbiz.minilang.method.callops.AddErr
 org.ofbiz.minilang.method.callops.CallBsh$CallBshFactory
 org.ofbiz.minilang.method.callops.CallClassMethod$CallClassMethodFactory
 org.ofbiz.minilang.method.callops.CallObjectMethod$CallObjectMethodFactory
+org.ofbiz.minilang.method.callops.CallScript$CallScriptFactory
 org.ofbiz.minilang.method.callops.CallService$CallServiceFactory
 org.ofbiz.minilang.method.callops.CallServiceAsynch$CallServiceAsynchFactory
 
org.ofbiz.minilang.method.callops.CallSimpleMapProcessor$CallSimpleMapProcessorFactory

Added: 
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java?rev=934510&view=auto
==============================================================================
--- 
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java
 (added)
+++ 
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java
 Thu Apr 15 18:32:35 2010
@@ -0,0 +1,135 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ 
*******************************************************************************/
+package org.ofbiz.minilang.method.callops;
+
+import java.util.List;
+import java.util.Map;
+
+import javolution.util.FastList;
+
+import org.codehaus.groovy.runtime.InvokerHelper;
+import org.ofbiz.base.util.BshUtil;
+import org.ofbiz.base.util.GeneralException;
+import org.ofbiz.base.util.GroovyUtil;
+import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.minilang.MiniLangException;
+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 {
+    public static final class CallScriptFactory implements Factory<CallScript> 
{
+        public CallScript createMethodOperation(Element element, SimpleMethod 
simpleMethod) {
+            return new CallScript(element, simpleMethod);
+        }
+
+        public String getName() {
+            return "script";
+        }
+    }
+    
+    public static final String module = CallScript.class.getName();
+    protected static final Object[] EMPTY_ARGS = {};
+    
+    private ContextAccessor<List<Object>> errorListAcsr;
+    private String location;
+    private String method;
+
+    public CallScript(Element element, SimpleMethod simpleMethod) {
+        super(element, simpleMethod);        
+        String scriptLocation = element.getAttribute("location");
+        this.location = getScriptLocation(scriptLocation);
+        this.method = getScriptMethodName(scriptLocation);
+        this.errorListAcsr = new 
ContextAccessor<List<Object>>(element.getAttribute("error-list-name"), 
"error_list");
+    }
+    
+    @Override
+    public boolean exec(MethodContext methodContext) {        
+        String location = methodContext.expandString(this.location);
+        String method = methodContext.expandString(this.method);
+        
+        List<Object> messages = errorListAcsr.get(methodContext);
+        if (messages == null) {
+            messages = FastList.newInstance();
+            errorListAcsr.put(methodContext, messages);
+        }
+
+        Map<String, Object> context = methodContext.getEnvMap();        
+        if (location.endsWith(".bsh")) {
+            try {
+                BshUtil.runBshAtLocation(location, context);
+            } catch (GeneralException e) {
+                messages.add("Error running BSH script at location [" + 
location + "]: " + e.getMessage());
+            }
+        } else if (location.endsWith(".groovy")) {
+            try {
+                groovy.lang.Script script = 
InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), 
GroovyUtil.getBinding(context));
+                if (UtilValidate.isEmpty(method)) {
+                    script.run();
+                } else {
+                    script.invokeMethod(method, EMPTY_ARGS);
+                }
+            } catch (GeneralException e) {                
+                messages.add("Error running Groovy script at location [" + 
location + "]: " + e.getMessage());
+            }
+        } else if (location.endsWith(".xml")) {                                
   
+            try {
+                SimpleMethod.runSimpleMethod(location, method, methodContext); 
               
+            } catch (MiniLangException e) {
+                messages.add("Error running simple method at location [" + 
location + "]: " + e.getMessage());
+            }
+        } else {
+            messages.add("Unsupported script type [" + location + "]");        
    
+        }
+        
+        // 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();
+    }
+
+    @Override
+    public String rawString() {
+        return "<script/>";
+    }
+    
+    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);
+    }
+}


Reply via email to