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/06/13 08:04:49 UTC

svn commit: r1349646 [2/2] - in /ofbiz/trunk/framework/minilang: dtd/ src/META-INF/services/ src/org/ofbiz/minilang/method/envops/

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/StringToList.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/StringToList.java?rev=1349646&r1=1349645&r2=1349646&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/StringToList.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/StringToList.java Wed Jun 13 06:04:49 2012
@@ -23,43 +23,52 @@ import java.util.List;
 
 import javolution.util.FastList;
 
-import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.MessageString;
 import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.collections.FlexibleMapAccessor;
+import org.ofbiz.base.util.string.FlexibleStringExpander;
 import org.ofbiz.minilang.MiniLangException;
+import org.ofbiz.minilang.MiniLangRuntimeException;
+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;
 
 /**
- * Appends the specified String to a List
+ * Implements the <string-to-list> element.
  */
-public class StringToList extends MethodOperation {
+public final class StringToList extends MethodOperation {
 
-    public static final String module = StringToList.class.getName();
-
-    ContextAccessor<List<? extends Object>> argListAcsr;
-    ContextAccessor<List<Object>> listAcsr;
-    String messageFieldName;
-    String string;
+    private final FlexibleMapAccessor<List<? extends Object>> argListFma;
+    private final FlexibleMapAccessor<List<Object>> listFma;
+    private final String messageFieldName;
+    private final FlexibleStringExpander stringFse;
 
     public StringToList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
         super(element, simpleMethod);
-        string = element.getAttribute("string");
-        listAcsr = new ContextAccessor<List<Object>>(element.getAttribute("list"), element.getAttribute("list-name"));
-        argListAcsr = new ContextAccessor<List<? extends Object>>(element.getAttribute("arg-list"), element.getAttribute("arg-list-name"));
-        messageFieldName = UtilValidate.isNotEmpty(element.getAttribute("message-field")) ? element.getAttribute("message-field") : element.getAttribute("message-field-name");
+        if (MiniLangValidate.validationOn()) {
+            MiniLangValidate.handleError("<string-to-list> element is deprecated (use <set>)", simpleMethod, element);
+            MiniLangValidate.attributeNames(simpleMethod, element, "list", "arg-list", "string", "message-field");
+            MiniLangValidate.requiredAttributes(simpleMethod, element, "list", "string");
+            MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "arg-list");
+            MiniLangValidate.noChildElements(simpleMethod, element);
+        }
+        stringFse = FlexibleStringExpander.getInstance(element.getAttribute("string"));
+        listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
+        argListFma = FlexibleMapAccessor.getInstance(element.getAttribute("arg-list"));
+        messageFieldName = element.getAttribute("message-field");
     }
 
     @Override
     public boolean exec(MethodContext methodContext) throws MiniLangException {
-        String valueStr = methodContext.expandString(string);
-        if (!argListAcsr.isEmpty()) {
-            List<? extends Object> argList = argListAcsr.get(methodContext);
-            if (UtilValidate.isNotEmpty(argList)) {
+        String valueStr = stringFse.expandString(methodContext.getEnvMap());
+        List<? extends Object> argList = argListFma.get(methodContext.getEnvMap());
+        if (argList != null) {
+            try {
                 valueStr = MessageFormat.format(valueStr, argList.toArray());
+            } catch (IllegalArgumentException e) {
+                throw new MiniLangRuntimeException("Exception thrown while formatting the string attribute: " + e.getMessage(), this);
             }
         }
         Object value;
@@ -68,12 +77,10 @@ public class StringToList extends Method
         } else {
             value = valueStr;
         }
-        List<Object> toList = listAcsr.get(methodContext);
+        List<Object> toList = listFma.get(methodContext.getEnvMap());
         if (toList == null) {
-            if (Debug.verboseOn())
-                Debug.logVerbose("List not found with name " + listAcsr + ", creating new List", module);
             toList = FastList.newInstance();
-            listAcsr.put(methodContext, toList);
+            listFma.put(methodContext.getEnvMap(), toList);
         }
         toList.add(value);
         return true;
@@ -81,21 +88,39 @@ public class StringToList extends Method
 
     @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 "<string-to-list string=\"" + this.string + "\" list-name=\"" + this.listAcsr + "\"/>";
+        return toString();
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("<string-to-list ");
+        sb.append("string=\"").append(this.stringFse).append("\" ");
+        sb.append("list=\"").append(this.listFma).append("\" ");
+        if (!this.argListFma.isEmpty()) {
+            sb.append("arg-list=\"").append(this.argListFma).append("\" ");
+        }
+        if (!this.messageFieldName.isEmpty()) {
+            sb.append("message-field=\"").append(this.messageFieldName).append("\" ");
+        }
+        sb.append("/>");
+        return sb.toString();
     }
 
+    /**
+     * A factory for the &lt;string-to-list&gt; element.
+     */
     public static final class StringToListFactory implements Factory<StringToList> {
+        @Override
         public StringToList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
             return new StringToList(element, simpleMethod);
         }
 
+        @Override
         public String getName() {
             return "string-to-list";
         }

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/ToString.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/ToString.java?rev=1349646&r1=1349645&r2=1349646&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/ToString.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/ToString.java Wed Jun 13 06:04:49 2012
@@ -18,111 +18,106 @@
  *******************************************************************************/
 package org.ofbiz.minilang.method.envops;
 
-import java.util.Map;
-
-import javolution.util.FastMap;
-
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.GeneralException;
-import org.ofbiz.base.util.ObjectType;
 import org.ofbiz.base.util.StringUtil;
-import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.collections.FlexibleMapAccessor;
+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;
 
 /**
- * Converts the specified field to a String, using toString()
+ * Implements the &lt;to-string&gt; element.
  */
-public class ToString extends MethodOperation {
+public final class ToString extends MethodOperation {
 
-    public static final String module = ToString.class.getName();
-
-    ContextAccessor<Object> fieldAcsr;
-    String format;
-    ContextAccessor<Map<String, Object>> mapAcsr;
-    Integer numericPadding;
+    private final FlexibleMapAccessor<Object> fieldFma;
+    private final String format;
+    private final Integer numericPadding;
 
     public ToString(Element element, SimpleMethod simpleMethod) throws MiniLangException {
         super(element, simpleMethod);
-        // the schema for this element now just has the "field" attribute, though the old "field-name" and "map-name" pair is still supported
-        fieldAcsr = new ContextAccessor<Object>(element.getAttribute("field"), element.getAttribute("field-name"));
-        mapAcsr = new ContextAccessor<Map<String, Object>>(element.getAttribute("map-name"));
+        if (MiniLangValidate.validationOn()) {
+            MiniLangValidate.handleError("<to-string> element is deprecated (use <set>)", simpleMethod, element);
+            MiniLangValidate.attributeNames(simpleMethod, element, "field", "format", "numeric-padding");
+            MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
+            MiniLangValidate.constantAttributes(simpleMethod, element, "format", "numeric-padding");
+            MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
+            MiniLangValidate.noChildElements(simpleMethod, element);
+        }
+        fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
         format = element.getAttribute("format");
-        String npStr = element.getAttribute("numeric-padding");
-        if (UtilValidate.isNotEmpty(npStr)) {
+        Integer numericPadding = null;
+        String npAttribute = element.getAttribute("numeric-padding");
+        if (!npAttribute.isEmpty()) {
             try {
-                this.numericPadding = Integer.valueOf(npStr);
+                numericPadding = Integer.valueOf(npAttribute);
             } catch (Exception e) {
-                Debug.logError(e, "Error parsing numeric-padding attribute value on the to-string element", module);
+                MiniLangValidate.handleError("Exception thrown while parsing numeric-padding attribute: " + e.getMessage(), simpleMethod, element);
             }
         }
-    }
-
-    public String doToString(Object obj, MethodContext methodContext) {
-        String outStr = null;
-        try {
-            if (UtilValidate.isNotEmpty(format)) {
-                outStr = (String) MiniLangUtil.convertType(obj, String.class, methodContext.getLocale(), methodContext.getTimeZone(), format);
-            } else {
-                outStr = obj.toString();
-            }
-        } catch (Exception e) {
-            Debug.logError(e, "", module);
-            outStr = obj.toString();
-        }
-        if (this.numericPadding != null) {
-            outStr = StringUtil.padNumberString(outStr, this.numericPadding.intValue());
-        }
-        return outStr;
+        this.numericPadding = numericPadding;
     }
 
     @Override
     public boolean exec(MethodContext methodContext) throws MiniLangException {
-        if (!mapAcsr.isEmpty()) {
-            Map<String, Object> toMap = mapAcsr.get(methodContext);
-            if (toMap == null) {
-                // it seems silly to create a new map, but necessary since whenever
-                // an env field like a Map or List is referenced it should be created, even if empty
-                if (Debug.verboseOn())
-                    Debug.logVerbose("Map not found with name " + mapAcsr + ", creating new map", module);
-                toMap = FastMap.newInstance();
-                mapAcsr.put(methodContext, toMap);
-            }
-            Object obj = fieldAcsr.get(toMap, methodContext);
-            if (obj != null) {
-                fieldAcsr.put(toMap, doToString(obj, methodContext), methodContext);
+        Object value = fieldFma.get(methodContext.getEnvMap());
+        if (value != null) {
+            try {
+                if (!format.isEmpty()) {
+                    value = MiniLangUtil.convertType(value, String.class, methodContext.getLocale(), methodContext.getTimeZone(), format);
+                } else {
+                    value = value.toString();
+                }
+            } catch (Exception e) {
+                throw new MiniLangRuntimeException("Exception thrown while converting field to a string: " + e.getMessage(), this);
             }
-        } else {
-            Object obj = fieldAcsr.get(methodContext);
-            if (obj != null) {
-                fieldAcsr.put(methodContext, doToString(obj, methodContext));
+            if (this.numericPadding != null) {
+                value = StringUtil.padNumberString(value.toString(), this.numericPadding.intValue());
             }
+            fieldFma.put(methodContext.getEnvMap(), value);
         }
         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 "<to-string field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
+        return toString();
     }
 
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("<to-string ");
+        sb.append("field=\"").append(this.fieldFma).append("\" ");
+        if (!this.format.isEmpty()) {
+            sb.append("format=\"").append(this.format).append("\" ");
+        }
+        if (numericPadding != null) {
+            sb.append("numeric-padding=\"").append(this.numericPadding).append("\" ");
+        }
+        sb.append("/>");
+        return sb.toString();
+    }
+
+    /**
+     * A factory for the &lt;to-string&gt; element.
+     */
     public static final class ToStringFactory implements Factory<ToString> {
+        @Override
         public ToString createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
             return new ToString(element, simpleMethod);
         }
 
+        @Override
         public String getName() {
             return "to-string";
         }