svn commit: r1339290 - in /ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method: MessageElement.java envops/AddError.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1339290 - in /ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method: MessageElement.java envops/AddError.java

adrianc
Author: adrianc
Date: Wed May 16 18:04:30 2012
New Revision: 1339290

URL: http://svn.apache.org/viewvc?rev=1339290&view=rev
Log:
Added new class to model the Mini-language <fail-message> and <fail-property> elements. Updated the <add-error> element to use the new class.

Added:
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MessageElement.java   (with props)
Modified:
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/AddError.java

Added: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MessageElement.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MessageElement.java?rev=1339290&view=auto
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MessageElement.java (added)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MessageElement.java Wed May 16 18:04:30 2012
@@ -0,0 +1,95 @@
+/*******************************************************************************
+ * 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;
+
+import org.ofbiz.base.util.UtilProperties;
+import org.ofbiz.base.util.UtilXml;
+import org.ofbiz.base.util.string.FlexibleStringExpander;
+import org.ofbiz.minilang.MiniLangElement;
+import org.ofbiz.minilang.MiniLangException;
+import org.ofbiz.minilang.MiniLangValidate;
+import org.ofbiz.minilang.SimpleMethod;
+import org.w3c.dom.Element;
+
+/**
+ * Implements the &lt;fail-message&gt; and &lt;fail-property&gt; elements.
+ */
+public final class MessageElement extends MiniLangElement {
+
+    public static MessageElement fromParentElement(Element element, SimpleMethod simpleMethod) throws MiniLangException {
+        Element childElement = UtilXml.firstChildElement(element, "fail-message");
+        if (childElement != null) {
+            return new MessageElement(childElement, simpleMethod);
+        } else {
+            childElement = UtilXml.firstChildElement(element, "fail-property");
+            if (childElement != null) {
+                return new MessageElement(childElement, simpleMethod);
+            } else {
+                return null;
+            }
+        }
+    }
+
+    private final FlexibleStringExpander messageFse;
+    private final String propertykey;
+    private final String propertyResource;
+
+    public MessageElement(Element element, SimpleMethod simpleMethod) throws MiniLangException {
+        super(element, simpleMethod);
+        if ("fail-message".equals(element.getTagName())) {
+            if (MiniLangValidate.validationOn()) {
+                MiniLangValidate.attributeNames(simpleMethod, element, "message");
+                MiniLangValidate.requiredAttributes(simpleMethod, element, "message");
+                MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "message");
+            }
+            this.messageFse = FlexibleStringExpander.getInstance(element.getAttribute("message"));
+            this.propertykey = null;
+            this.propertyResource = null;
+        } else {
+            if (MiniLangValidate.validationOn()) {
+                MiniLangValidate.attributeNames(simpleMethod, element, "property", "resource");
+                MiniLangValidate.requiredAttributes(simpleMethod, element, "property", "resource");
+                MiniLangValidate.constantAttributes(simpleMethod, element, "property", "resource");
+            }
+            this.messageFse = null;
+            this.propertykey = element.getAttribute("property");
+            this.propertyResource = element.getAttribute("resource");
+        }
+    }
+
+    public String getMessage(MethodContext methodContext) {
+        if (messageFse != null) {
+            return messageFse.expandString(methodContext.getEnvMap());
+        } else {
+            return UtilProperties.getMessage(propertyResource, propertykey, methodContext.getEnvMap(), methodContext.getLocale());
+        }
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        if (this.messageFse != null) {
+            sb.append("<fail-message message=\"").append(this.messageFse).append("\" />");
+        }
+        if (this.propertykey != null) {
+            sb.append("<fail-property property=\"").append(this.propertykey).append(" resource=\"").append(this.propertyResource).append("\" />");
+        }
+        return sb.toString();
+    }
+}

Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MessageElement.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/MessageElement.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/AddError.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/AddError.java?rev=1339290&r1=1339289&r2=1339290&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/AddError.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/AddError.java Wed May 16 18:04:30 2012
@@ -22,13 +22,12 @@ import java.util.List;
 
 import javolution.util.FastList;
 
-import org.ofbiz.base.util.UtilProperties;
-import org.ofbiz.base.util.UtilXml;
 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
 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.MessageElement;
 import org.ofbiz.minilang.method.MethodContext;
 import org.ofbiz.minilang.method.MethodOperation;
 import org.w3c.dom.Element;
@@ -39,9 +38,7 @@ import org.w3c.dom.Element;
 public final class AddError extends MethodOperation {
 
     private final FlexibleMapAccessor<List<String>> errorListFma;
-    private final FlexibleStringExpander messageFse;
-    private final String propertykey;
-    private final String propertyResource;
+    private final MessageElement messageElement;
 
     public AddError(Element element, SimpleMethod simpleMethod) throws MiniLangException {
         super(element, simpleMethod);
@@ -52,50 +49,21 @@ public final class AddError extends Meth
             MiniLangValidate.requireAnyChildElement(simpleMethod, element, "fail-message", "fail-property");
         }
         errorListFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list"));
-        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 {
-            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;
-            }
-        }
+        messageElement = MessageElement.fromParentElement(element, simpleMethod);
     }
 
     @Override
     public boolean exec(MethodContext methodContext) throws MiniLangException {
-        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 = errorListFma.get(methodContext.getEnvMap());
-            if (messages == null) {
-                messages = FastList.newInstance();
+        if (messageElement != null) {
+            String message = messageElement.getMessage(methodContext);
+            if (message != null) {
+                List<String> messages = errorListFma.get(methodContext.getEnvMap());
+                if (messages == null) {
+                    messages = FastList.newInstance();
+                    errorListFma.put(methodContext.getEnvMap(), messages);
+                }
+                messages.add(message);
             }
-            errorListFma.put(methodContext.getEnvMap(), messages);
-            messages.add(message);
         }
         return true;
     }
@@ -116,14 +84,11 @@ public final class AddError extends Meth
         if (!"error_list".equals(this.errorListFma.getOriginalName())) {
             sb.append("error-list-name=\"").append(this.errorListFma).append("\"");
         }
-        sb.append(">");
-        if (!this.messageFse.isEmpty()) {
-            sb.append("<fail-message message=\"").append(this.messageFse).append("\" />");
-        }
-        if (this.propertykey != null) {
-            sb.append("<fail-property property=\"").append(this.propertykey).append(" resource=\"").append(this.propertyResource).append("\" />");
+        if (messageElement != null) {
+            sb.append(">").append(messageElement).append("</add-error>");
+        } else {
+            sb.append("/>");
         }
-        sb.append("</add-error>");
         return sb.toString();
     }