You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2012/04/28 22:35:49 UTC

svn commit: r1331827 - in /ofbiz/trunk/framework/minilang: dtd/simple-methods-v2.xsd src/org/ofbiz/minilang/method/callops/AddError.java

Author: adrianc
Date: Sat Apr 28 20:35:48 2012
New Revision: 1331827

URL: http://svn.apache.org/viewvc?rev=1331827&view=rev
Log:
Overhauled Mini-language <add-error> element.

Modified:
    ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/AddError.java

Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd?rev=1331827&r1=1331826&r2=1331827&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original)
+++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Sat Apr 28 20:35:48 2012
@@ -3560,16 +3560,16 @@ under the License.
     <xs:element name="check-errors" substitutionGroup="ControlOperations">
         <xs:annotation>
             <xs:documentation>
-                Halts script execution if the error message list contains any messages.
+                Halts script execution if errors were encountered in previous operations.
             </xs:documentation>
         </xs:annotation>
         <xs:complexType>
             <xs:attribute type="xs:string" name="error-code">
                 <xs:annotation>
                     <xs:documentation>
-                    The error code to return to the calling process. Defaults to "error".
-                    &lt;br/&gt;&lt;br/&gt;
-                    Optional. Attribute type: constant+expr
+                        The error code to return to the calling process. Defaults to "error".
+                        &lt;br/&gt;&lt;br/&gt;
+                        Optional. Attribute type: constant+expr
                     </xs:documentation>
                 </xs:annotation>
             </xs:attribute>
@@ -3578,14 +3578,23 @@ under the License.
     <xs:element name="add-error" substitutionGroup="ControlOperations">
         <xs:annotation>
             <xs:documentation>
-                Adds an error message to the error message list.
+                Adds an error message to an error message list.
             </xs:documentation>
         </xs:annotation>
         <xs:complexType>
             <xs:choice>
-                <xs:element ref="fail-message"/>
-                <xs:element ref="fail-property"/>
+                <xs:element ref="fail-message" />
+                <xs:element ref="fail-property" />
             </xs:choice>
+            <xs:attribute type="xs:string" name="error-list-name">
+                <xs:annotation>
+                    <xs:documentation>
+                        The name of the error message list. Defaults to "error_list".
+                        &lt;br/&gt;&lt;br/&gt;
+                        Optional. Attribute type: constant
+                    </xs:documentation>
+                </xs:annotation>
+            </xs:attribute>
         </xs:complexType>
     </xs:element>
     <xs:element name="break" substitutionGroup="ControlOperations">

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/AddError.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/AddError.java?rev=1331827&r1=1331826&r2=1331827&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/AddError.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/AddError.java Sat Apr 28 20:35:48 2012
@@ -23,77 +23,99 @@ import java.util.List;
 import javolution.util.FastList;
 
 import org.ofbiz.base.util.UtilProperties;
-import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.base.util.UtilXml;
+import org.ofbiz.base.util.string.FlexibleStringExpander;
 import org.ofbiz.minilang.MiniLangException;
+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;
 
 /**
- * Adds the fail-message or fail-property value to the error-list.
+ * Adds an error message to an error message list.
  */
-public class AddError extends MethodOperation {
+public final class AddError extends MethodOperation {
 
-    ContextAccessor<List<Object>> errorListAcsr;
-    boolean isProperty = false;
-    String message = null;
-    String propertyResource = null;
+    private final String listName;
+    private final FlexibleStringExpander messageFse;
+    private final String propertykey;
+    private final String propertyResource;
 
     public AddError(Element element, SimpleMethod simpleMethod) throws MiniLangException {
         super(element, simpleMethod);
-        errorListAcsr = new ContextAccessor<List<Object>>(element.getAttribute("error-list-name"), "error_list");
-        Element failMessage = UtilXml.firstChildElement(element, "fail-message");
-        Element failProperty = UtilXml.firstChildElement(element, "fail-property");
-        if (failMessage != null) {
-            this.message = failMessage.getAttribute("message");
-            this.isProperty = false;
-        } else if (failProperty != null) {
-            this.propertyResource = failProperty.getAttribute("resource");
-            this.message = failProperty.getAttribute("property");
-            this.isProperty = true;
+        if (MiniLangValidate.validationOn()) {
+            MiniLangValidate.childElements(simpleMethod, element, "fail-message", "fail-property");
+            MiniLangValidate.requireAnyChildElement(simpleMethod, element, "fail-message", "fail-property");
         }
-    }
-
-    public void addMessage(List<Object> messages, ClassLoader loader, MethodContext methodContext) {
-        String message = methodContext.expandString(this.message);
-        String propertyResource = methodContext.expandString(this.propertyResource);
-        if (!isProperty && message != null) {
-            messages.add(message);
-        } else if (isProperty && propertyResource != null && message != null) {
-            String propMsg = UtilProperties.getMessage(propertyResource, message, methodContext.getEnvMap(), methodContext.getLocale());
-            if (UtilValidate.isEmpty(propMsg)) {
-                messages.add("Simple Method error occurred, but no message was found, sorry.");
-            } else {
-                messages.add(methodContext.expandString(propMsg));
+        MiniLangValidate.attributeNames(simpleMethod, element, "error-list-name");
+        MiniLangValidate.constantAttributes(simpleMethod, element, "error-list-name");
+        String listNameAttribute = element.getAttribute("error-list-name");
+        if (listNameAttribute.length() == 0) {
+            this.listName = "error_list";
+        } else {
+            this.listName = listNameAttribute;
+        }
+        Element childElement = UtilXml.firstChildElement(element, "fail-message");
+        if (childElement != null) {
+            if (MiniLangValidate.validationOn()) {
+                MiniLangValidate.attributeNames(simpleMethod, childElement, "message");
+                MiniLangValidate.requiredAttributes(simpleMethod, childElement, "message");
+                MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, childElement, "message");
             }
+            this.messageFse = FlexibleStringExpander.getInstance(childElement.getAttribute("message"));
+            this.propertykey = null;
+            this.propertyResource = null;
         } else {
-            messages.add("Simple Method error occurred, but no message was found, sorry.");
+            childElement = UtilXml.firstChildElement(element, "fail-property");
+            if (childElement != null) {
+                if (MiniLangValidate.validationOn()) {
+                    MiniLangValidate.attributeNames(simpleMethod, childElement, "property", "resource");
+                    MiniLangValidate.requiredAttributes(simpleMethod, childElement, "property", "resource");
+                    MiniLangValidate.constantAttributes(simpleMethod, childElement, "property", "resource");
+                }
+                this.messageFse = FlexibleStringExpander.getInstance(null);
+                this.propertykey = childElement.getAttribute("property");
+                this.propertyResource = childElement.getAttribute("resource");
+            } else {
+                this.messageFse = FlexibleStringExpander.getInstance(null);
+                this.propertykey = null;
+                this.propertyResource = null;
+            }
         }
     }
 
     @Override
     public boolean exec(MethodContext methodContext) throws MiniLangException {
-        List<Object> messages = errorListAcsr.get(methodContext);
-        if (messages == null) {
-            messages = FastList.newInstance();
-            errorListAcsr.put(methodContext, messages);
+        String message = null;
+        if (!this.messageFse.isEmpty()) {
+            message = this.messageFse.expandString(methodContext.getEnvMap());
+        } else if (this.propertyResource != null) {
+            message = UtilProperties.getMessage(this.propertyResource, this.propertykey, methodContext.getEnvMap(), methodContext.getLocale());
+        }
+        if (message != null) {
+            List<String> messages = methodContext.getEnv(this.listName);
+            if (messages == null) {
+                messages = FastList.newInstance();
+            }
+            methodContext.putEnv(this.listName, messages);
+            messages.add(message);
         }
-        this.addMessage(messages, methodContext.getLoader(), methodContext);
         return true;
     }
 
     @Override
     public String expandedString(MethodContext methodContext) {
-        // TODO: something more than a stub/dummy
-        return this.rawString();
+        return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap());
     }
 
     @Override
     public String rawString() {
-        // TODO: something more than the empty tag
+        return toString();
+    }
+
+    @Override
+    public String toString() {
         return "<add-error/>";
     }