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/05/17 20:23:20 UTC

svn commit: r1339771 - in /ofbiz/trunk/framework/minilang: dtd/simple-methods-v2.xsd src/org/ofbiz/minilang/method/ifops/CheckPermission.java src/org/ofbiz/minilang/method/ifops/IfInstanceOf.java src/org/ofbiz/minilang/method/ifops/IfNotEmpty.java

Author: adrianc
Date: Thu May 17 18:23:20 2012
New Revision: 1339771

URL: http://svn.apache.org/viewvc?rev=1339771&view=rev
Log:
Overhauled Mini-language <if-instance-of> and <if-not-empty> elements.

Modified:
    ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckPermission.java
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfInstanceOf.java
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfNotEmpty.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=1339771&r1=1339770&r2=1339771&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original)
+++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Thu May 17 18:23:20 2012
@@ -3921,32 +3921,32 @@ under the License.
                         <xs:documentation>
                             The else tag can be used to contain operations that will run if the condition fails,
                             or in other words if the operations under the if tag do not run.
-
                             It can contain any simple-method operation.
                             The else tag must be placed as the last tag under the if-* tag.
                         </xs:documentation>
                     </xs:annotation>
                 </xs:element>
             </xs:sequence>
-            <xs:attributeGroup ref="attlist.if-instance-of"/>
+            <xs:attribute type="xs:string" name="field" use="required">
+                <xs:annotation>
+                    <xs:documentation>
+                        The name of the field to test.
+                        &lt;br/&gt;&lt;br/&gt;
+                        Required. Attribute type: expression.
+                    </xs:documentation>
+                </xs:annotation>
+            </xs:attribute>
+            <xs:attribute type="xs:string" name="class" use="required">
+                <xs:annotation>
+                    <xs:documentation>
+                        The name of the class to test for.
+                        &lt;br/&gt;&lt;br/&gt;
+                        Required. Attribute type: constant.
+                    </xs:documentation>
+                </xs:annotation>
+            </xs:attribute>
         </xs:complexType>
     </xs:element>
-    <xs:attributeGroup name="attlist.if-instance-of">
-        <xs:attribute type="xs:string" name="field" use="required">
-            <xs:annotation>
-                <xs:documentation>
-                    The name of the map field that will be validated as being an instance of the named class.
-                </xs:documentation>
-            </xs:annotation>
-        </xs:attribute>
-        <xs:attribute type="xs:string" name="class" use="required">
-            <xs:annotation>
-                <xs:documentation>
-                    The name of the class that named instance in field-name is supposed to belong.
-                </xs:documentation>
-            </xs:annotation>
-        </xs:attribute>
-    </xs:attributeGroup>
     <xs:element name="if-compare" substitutionGroup="IfBasicOperations">
         <xs:annotation>
             <xs:documentation>
@@ -4125,25 +4125,23 @@ under the License.
                         <xs:documentation>
                             The else tag can be used to contain operations that will run if the condition fails,
                             or in other words if the operations under the if tag do not run.
-
                             It can contain any simple-method operation.
                             The else tag must be placed as the last tag under the if-* tag.
                         </xs:documentation>
                     </xs:annotation>
                 </xs:element>
             </xs:sequence>
-            <xs:attributeGroup ref="attlist.if-not-empty"/>
+            <xs:attribute type="xs:string" name="field" use="required">
+                <xs:annotation>
+                    <xs:documentation>
+                        The name of the context field that will be compared.
+                        &lt;br/&gt;&lt;br/&gt;
+                        Required. Attribute type: expression.
+                    </xs:documentation>
+                </xs:annotation>
+            </xs:attribute>
         </xs:complexType>
     </xs:element>
-    <xs:attributeGroup name="attlist.if-not-empty">
-        <xs:attribute type="xs:string" name="field" use="required">
-            <xs:annotation>
-                <xs:documentation>
-                    The name of the context field that will be compared.
-                </xs:documentation>
-            </xs:annotation>
-        </xs:attribute>
-    </xs:attributeGroup>
     <xs:element name="if-has-permission" substitutionGroup="IfBasicOperations">
         <xs:annotation>
             <xs:documentation>

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckPermission.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckPermission.java?rev=1339771&r1=1339770&r2=1339771&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckPermission.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckPermission.java Thu May 17 18:23:20 2012
@@ -39,7 +39,7 @@ import org.ofbiz.security.authz.Authoriz
 import org.w3c.dom.Element;
 
 /**
- * If the user does not have the specified permission the fail-message or fail-property sub-elements are used to add a message to the error-list.
+ * Implements the &lt;check-permission&gt; element.
  */
 public final class CheckPermission extends MethodOperation {
 
@@ -127,11 +127,16 @@ public final class CheckPermission exten
         return sb.toString();
     }
 
+    /**
+     * A &lt;check-permission&gt; element factory. 
+     */
     public static final class CheckPermissionFactory implements Factory<CheckPermission> {
+        @Override
         public CheckPermission createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
             return new CheckPermission(element, simpleMethod);
         }
 
+        @Override
         public String getName() {
             return "check-permission";
         }

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfInstanceOf.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfInstanceOf.java?rev=1339771&r1=1339770&r2=1339771&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfInstanceOf.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfInstanceOf.java Thu May 17 18:23:20 2012
@@ -20,61 +20,70 @@ package org.ofbiz.minilang.method.ifops;
 
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
 
 import javolution.util.FastList;
 
-import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.ObjectType;
 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.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;
 
-public class IfInstanceOf extends MethodOperation {
-
-    public static final String module = IfInstanceOf.class.getName();
-
-    protected String className = null;
-    protected List<MethodOperation> elseSubOps = null;
-    protected ContextAccessor<Object> fieldAcsr = null;
-    protected ContextAccessor<Map<String, ? extends Object>> mapAcsr = null;
-    protected List<MethodOperation> subOps;
+/**
+ * Implements the &lt;if-instance-of&gt; element.
+ */
+public final class IfInstanceOf extends MethodOperation {
+
+    private final String className;
+    private final Class<?> compareClass;
+    private final List<MethodOperation> elseSubOps;
+    private final FlexibleMapAccessor<Object> fieldFma;
+    private final List<MethodOperation> subOps;
 
     public IfInstanceOf(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
-        this.fieldAcsr = new ContextAccessor<Object>(element.getAttribute("field"), element.getAttribute("field-name"));
-        this.mapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("map-name"));
+        if (MiniLangValidate.validationOn()) {
+            MiniLangValidate.attributeNames(simpleMethod, element, "field", "class");
+            MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "class");
+            MiniLangValidate.constantAttributes(simpleMethod, element, "class");
+            MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
+        }
+        this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
         this.className = element.getAttribute("class");
+        Class<?> compareClass = null;
+        if (!className.isEmpty()) {
+            try {
+                compareClass = ObjectType.loadClass(className);
+            } catch (ClassNotFoundException e) {
+                MiniLangValidate.handleError("Invalid class name " + className, simpleMethod, element);
+            }
+        }
+        this.compareClass = compareClass;
         this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
         Element elseElement = UtilXml.firstChildElement(element, "else");
         if (elseElement != null) {
             this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod));
+        } else {
+            this.elseSubOps = null;
         }
     }
 
     @Override
     public boolean exec(MethodContext methodContext) throws MiniLangException {
-        // only run subOps if element is instanceOf
+        if (this.compareClass == null) {
+            throw new MiniLangRuntimeException("Invalid class name " + className, this);
+        }
         boolean runSubOps = false;
-        Object fieldVal = null;
-        if (!mapAcsr.isEmpty()) {
-            Map<String, ? extends Object> fromMap = mapAcsr.get(methodContext);
-            if (fromMap == null) {
-                if (Debug.infoOn())
-                    Debug.logInfo("Map not found with name " + mapAcsr + ", running operations", module);
-            } else {
-                fieldVal = fieldAcsr.get(fromMap, methodContext);
-            }
-        } else {
-            // no map name, try the env
-            fieldVal = fieldAcsr.get(methodContext);
+        Object fieldVal = fieldFma.get(methodContext.getEnvMap());
+        if (fieldVal != null) {
+            runSubOps = ObjectType.instanceOf(fieldVal.getClass(), compareClass);
         }
-        runSubOps = ObjectType.instanceOf(fieldVal, className);
         if (runSubOps) {
             return SimpleMethod.runSubOps(subOps, methodContext);
         } else {
@@ -88,8 +97,7 @@ public class IfInstanceOf extends Method
 
     @Override
     public String expandedString(MethodContext methodContext) {
-        // TODO: something more than a stub/dummy
-        return this.rawString();
+        return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap());
     }
 
     public List<MethodOperation> getAllSubOps() {
@@ -102,15 +110,30 @@ public class IfInstanceOf extends Method
 
     @Override
     public String rawString() {
-        // TODO: add all attributes and other info
-        return "<if-instance-of field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
+        return toString();
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("<if-instance-of ");
+        sb.append("field=\"").append(this.fieldFma).append("\" ");
+        if (compareClass != null) {
+            sb.append("class=\"").append(compareClass.getName()).append("\" ");
+        }
+        sb.append("/>");
+        return sb.toString();
     }
 
+    /**
+     * A &lt;if-instance-of&gt; element factory. 
+     */
     public static final class IfInstanceOfFactory implements Factory<IfInstanceOf> {
+        @Override
         public IfInstanceOf createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
             return new IfInstanceOf(element, simpleMethod);
         }
 
+        @Override
         public String getName() {
             return "if-instance-of";
         }

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfNotEmpty.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfNotEmpty.java?rev=1339771&r1=1339770&r2=1339771&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfNotEmpty.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfNotEmpty.java Thu May 17 18:23:20 2012
@@ -20,81 +20,49 @@ package org.ofbiz.minilang.method.ifops;
 
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
 
 import javolution.util.FastList;
 
-import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.ObjectType;
 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.SimpleMethod;
-import org.ofbiz.minilang.method.ContextAccessor;
 import org.ofbiz.minilang.method.MethodContext;
 import org.ofbiz.minilang.method.MethodOperation;
 import org.w3c.dom.Element;
 
 /**
- * If the specified field is not empty process sub-operations
+ * Implements the &lt;if-not-empty&gt; element.
  */
-public class IfNotEmpty extends MethodOperation {
+public final class IfNotEmpty extends MethodOperation {
 
-    public static final String module = IfNotEmpty.class.getName();
-
-    protected List<MethodOperation> elseSubOps = null;
-    protected ContextAccessor<Object> fieldAcsr;
-    protected ContextAccessor<Map<String, ? extends Object>> mapAcsr;
-    protected List<MethodOperation> subOps;
+    private final List<MethodOperation> elseSubOps;
+    private final FlexibleMapAccessor<Object> fieldFma;
+    private final List<MethodOperation> subOps;
 
     public IfNotEmpty(Element element, SimpleMethod simpleMethod) throws MiniLangException {
         super(element, simpleMethod);
-        // NOTE: this is still supported, but is deprecated
-        this.mapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("map-name"));
-        this.fieldAcsr = new ContextAccessor<Object>(element.getAttribute("field"));
-        if (this.fieldAcsr.isEmpty()) {
-            // NOTE: this is still supported, but is deprecated
-            this.fieldAcsr = new ContextAccessor<Object>(element.getAttribute("field-name"));
-        }
+        this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
         this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
         Element elseElement = UtilXml.firstChildElement(element, "else");
         if (elseElement != null) {
             this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod));
+        } else {
+            this.elseSubOps = null;
         }
     }
 
     @Override
     public boolean exec(MethodContext methodContext) throws MiniLangException {
-        // if conditions fails, always return true; if a sub-op returns false
-        // return false and stop, otherwise return true
-        // return true;
-        Object fieldVal = null;
-        if (!mapAcsr.isEmpty()) {
-            Map<String, ? extends Object> fromMap = mapAcsr.get(methodContext);
-            if (fromMap == null) {
-                if (Debug.verboseOn())
-                    Debug.logVerbose("Map not found with name " + mapAcsr + ", not running operations", module);
-            } else {
-                fieldVal = fieldAcsr.get(fromMap, methodContext);
-            }
-        } else {
-            // no map name, try the env
-            fieldVal = fieldAcsr.get(methodContext);
-        }
-        if (fieldVal == null) {
-            if (Debug.verboseOn())
-                Debug.logVerbose("Field value not found with name " + fieldAcsr + " in Map with name " + mapAcsr + ", not running operations", module);
-        }
-        // only run subOps if element is not empty/null
-        boolean runSubOps = !ObjectType.isEmpty(fieldVal);
-        if (runSubOps) {
-            // if (Debug.verboseOn()) Debug.logVerbose("IfNotEmpty: Running if operations mapAcsr=" + mapAcsr + " fieldAcsr=" + fieldAcsr, module);
+        Object fieldVal = fieldFma.get(methodContext.getEnvMap());
+        if (!ObjectType.isEmpty(fieldVal)) {
             return SimpleMethod.runSubOps(subOps, methodContext);
         } else {
             if (elseSubOps != null) {
-                // if (Debug.verboseOn()) Debug.logVerbose("IfNotEmpty: Running else operations mapAcsr=" + mapAcsr + " fieldAcsr=" + fieldAcsr, module);
                 return SimpleMethod.runSubOps(elseSubOps, methodContext);
             } else {
-                // if (Debug.verboseOn()) Debug.logVerbose("IfNotEmpty: Not Running any operations mapAcsr=" + mapAcsr + " fieldAcsr=" + fieldAcsr, module);
                 return true;
             }
         }
@@ -102,8 +70,7 @@ public class IfNotEmpty extends MethodOp
 
     @Override
     public String expandedString(MethodContext methodContext) {
-        // TODO: something more than a stub/dummy
-        return this.rawString();
+        return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap());
     }
 
     public List<MethodOperation> getAllSubOps() {
@@ -116,15 +83,26 @@ public class IfNotEmpty extends MethodOp
 
     @Override
     public String rawString() {
-        // TODO: add all attributes and other info
-        return "<if-not-empty field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
+        return toString();
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("<if-instance-of ");
+        sb.append("field=\"").append(this.fieldFma).append("\"/>");
+        return sb.toString();
     }
 
+    /**
+     * A &lt;if-not-empty&gt; element factory. 
+     */
     public static final class IfNotEmptyFactory implements Factory<IfNotEmpty> {
+        @Override
         public IfNotEmpty createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
             return new IfNotEmpty(element, simpleMethod);
         }
 
+        @Override
         public String getName() {
             return "if-not-empty";
         }