Author: adrianc
Date: Mon May 7 08:28:47 2012 New Revision: 1334915 URL: http://svn.apache.org/viewvc?rev=1334915&view=rev Log: Overhauled a bunch of Mini-language conditional elements. Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CombinedCondition.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareCondition.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareFieldCondition.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Conditional.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ConditionalFactory.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ElseIf.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/EmptyCondition.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/HasPermissionCondition.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/MasterIf.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompare.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompareField.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=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original) +++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Mon May 7 08:28:47 2012 @@ -77,6 +77,17 @@ under the License. <xs:attribute type="xs:string" name="resource"/> <xs:attribute type="xs:string" name="property"/> </xs:attributeGroup> + <xs:attributeGroup name="attlist.optionalJavaType"> + <xs:attribute type="xs:string" name="type"> + <xs:annotation> + <xs:documentation> + The Java data type. The source object will be converted to this type. + <br/><br/> + Optional. Attribute type: constant. Attribute must contain a valid Java class name. + </xs:documentation> + </xs:annotation> + </xs:attribute> + </xs:attributeGroup> <xs:attributeGroup name="attlist.typeDefaultString"> <xs:attribute name="type" default="String"> <xs:simpleType> @@ -3719,23 +3730,8 @@ under the License. </xs:annotation> <xs:complexType> <xs:sequence> - <xs:element ref="condition"> - <xs:annotation> - <xs:documentation> - A simple element with no attributes that contains the condition that will be evaluated to determine which sub-operations to execute. - To combine the other if operations documented below the and, or, xor, and notelements can be used. - The and, or, and xor elements can contain as many general if operations and modifier/combination elements (ie and, or, xor, and not). - </xs:documentation> - </xs:annotation> - </xs:element> - <xs:element ref="then"> - <xs:annotation> - <xs:documentation> - The then element is used to contain operations that will run if the condition evaluate to true. - A then tag must be included, but can be empty. - </xs:documentation> - </xs:annotation> - </xs:element> + <xs:element ref="condition" /> + <xs:element ref="then" /> </xs:sequence> </xs:complexType> </xs:element> @@ -3907,54 +3903,40 @@ under the License. </xs:annotation> <xs:complexType> <xs:sequence> - <xs:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations"> - <xs:annotation> - <xs:documentation> - Any simple-method operation can be nested under an if-* tag. - </xs:documentation> - </xs:annotation> - </xs:group> - <xs:element minOccurs="0" ref="else"> - <xs:annotation> - <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:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations" /> + <xs:element minOccurs="0" ref="else" /> </xs:sequence> - <xs:attributeGroup ref="attlist.if-compare"/> - <xs:attributeGroup ref="attlist.operatorRequired"/> - <xs:attributeGroup ref="attlist.typeDefaultString"/> + <xs:attribute type="xs:string" name="field" use="required"> + <xs:annotation> + <xs:documentation> + The name of the environment field that will be compared. + <br/><br/> + Required. Attribute type: expression. + </xs:documentation> + </xs:annotation> + </xs:attribute> + <xs:attributeGroup ref="attlist.operatorRequired" /> + <xs:attribute type="xs:string" name="value" use="required"> + <xs:annotation> + <xs:documentation> + The value that the field will compared to. + <br/><br/> + Required. Attribute type: constant+expr. + </xs:documentation> + </xs:annotation> + </xs:attribute> + <xs:attributeGroup ref="attlist.optionalJavaType" /> + <xs:attribute type="xs:string" name="format"> + <xs:annotation> + <xs:documentation> + Format based on the type of the object (date,number, etc.). + <br/><br/> + Optional. Attribute type: constant+expr. + </xs:documentation> + </xs:annotation> + </xs:attribute> </xs:complexType> </xs:element> - <xs:attributeGroup name="attlist.if-compare"> - <xs:attribute type="xs:string" name="field" use="required"> - <xs:annotation> - <xs:documentation> - The name of the field in the context (environment) that will be compared. - </xs:documentation> - </xs:annotation> - </xs:attribute> - <xs:attribute type="xs:string" name="value" use="required"> - <xs:annotation> - <xs:documentation> - The value that the field will compared to. - Must be a String, but can be converted to other types. - </xs:documentation> - </xs:annotation> - </xs:attribute> - <xs:attribute type="xs:string" name="format"> - <xs:annotation> - <xs:documentation> - Format based on the type of the object (date,number, etc.). - </xs:documentation> - </xs:annotation> - </xs:attribute> - </xs:attributeGroup> <xs:element name="if-compare-field" substitutionGroup="IfBasicOperations"> <xs:annotation> <xs:documentation> @@ -3964,54 +3946,40 @@ under the License. </xs:annotation> <xs:complexType> <xs:sequence> - <xs:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations"> - <xs:annotation> - <xs:documentation> - Any simple-method operation can be nested under an if-* tag. - </xs:documentation> - </xs:annotation> - </xs:group> - <xs:element minOccurs="0" ref="else"> - <xs:annotation> - <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:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations" /> + <xs:element minOccurs="0" ref="else" /> </xs:sequence> - <xs:attributeGroup ref="attlist.if-compare-field"/> - <xs:attributeGroup ref="attlist.operatorRequired"/> - <xs:attributeGroup ref="attlist.typeDefaultString"/> + <xs:attribute type="xs:string" name="field" use="required"> + <xs:annotation> + <xs:documentation> + The name of the environment field that will be compared. + <br/><br/> + Required. Attribute type: expression. + </xs:documentation> + </xs:annotation> + </xs:attribute> + <xs:attributeGroup ref="attlist.operatorRequired" /> + <xs:attribute type="xs:string" name="to-field" use="required"> + <xs:annotation> + <xs:documentation> + The name of the environment field that will be compared to the field specified in the "field" attribute. + <br/><br/> + Required. Attribute type: expression. + </xs:documentation> + </xs:annotation> + </xs:attribute> + <xs:attributeGroup ref="attlist.optionalJavaType" /> + <xs:attribute type="xs:string" name="format"> + <xs:annotation> + <xs:documentation> + Format based on the type of the object (date,number, etc.). + <br/><br/> + Optional. Attribute type: constant+expr. + </xs:documentation> + </xs:annotation> + </xs:attribute> </xs:complexType> </xs:element> - <xs:attributeGroup name="attlist.if-compare-field"> - <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:attribute type="xs:string" name="to-field"> - <xs:annotation> - <xs:documentation> - The name of the context field that the main field will be compared to. - If left empty will default to the field attribute's value. - </xs:documentation> - </xs:annotation> - </xs:attribute> - <xs:attribute type="xs:string" name="format"> - <xs:annotation> - <xs:documentation> - Format based on the type of the object (date,number, etc.). - </xs:documentation> - </xs:annotation> - </xs:attribute> - </xs:attributeGroup> <xs:element name="if-regexp" substitutionGroup="IfBasicOperations"> <xs:annotation> <xs:documentation> @@ -4062,43 +4030,26 @@ under the License. <xs:element name="if-empty" substitutionGroup="IfBasicOperations"> <xs:annotation> <xs:documentation> - The operations contained by the if-empty tag will only be executed if the map field is empty. - This tag can contain any of the simple-method operations, including the conditional/if operations. + The operations contained by the if-empty element will be executed if the specified field is null or empty. + This element can contain any of the simple-method operations, including the conditional/if operations. </xs:documentation> </xs:annotation> <xs:complexType> <xs:sequence> - <xs:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations"> - <xs:annotation> - <xs:documentation> - Any simple-method operation can be nested under an if-* tag. - </xs:documentation> - </xs:annotation> - </xs:group> - <xs:element minOccurs="0" ref="else"> - <xs:annotation> - <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:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations" /> + <xs:element minOccurs="0" ref="else" /> </xs:sequence> - <xs:attributeGroup ref="attlist.if-empty"/> + <xs:attribute type="xs:string" name="field" use="required"> + <xs:annotation> + <xs:documentation> + The name of the context field that will be compared. + <br/><br/> + Required. Attribute type: expression. + </xs:documentation> + </xs:annotation> + </xs:attribute> </xs:complexType> </xs:element> - <xs:attributeGroup name="attlist.if-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-not-empty" substitutionGroup="IfOtherOperations"> <xs:annotation> <xs:documentation> @@ -4148,46 +4099,31 @@ under the License. </xs:annotation> <xs:complexType> <xs:sequence> - <xs:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations"> - <xs:annotation> - <xs:documentation> - Any simple-method operation can be nested under an if-* tag. - </xs:documentation> - </xs:annotation> - </xs:group> - <xs:element minOccurs="0" ref="else"> - <xs:annotation> - <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:group minOccurs="0" maxOccurs="unbounded" ref="AllOperations" /> + <xs:element minOccurs="0" ref="else" /> </xs:sequence> - <xs:attributeGroup ref="attlist.if-has-permission"/> + <xs:attribute type="xs:string" name="permission" use="required"> + <xs:annotation> + <xs:documentation> + The name of the permission. + The user must belong to a security group that includes this permission. + <br/><br/> + Required. Attribute type: constant+expr. + </xs:documentation> + </xs:annotation> + </xs:attribute> + <xs:attribute type="xs:string" name="action"> + <xs:annotation> + <xs:documentation> + If an action is specified the user can have one of two permissions: the permission + "_ADMIN" or permission + action. + Examples of actions include "_CREATE", "_VIEW", etc. + <br/><br/> + Optional. Attribute type: constant+expr. + </xs:documentation> + </xs:annotation> + </xs:attribute> </xs:complexType> </xs:element> - <xs:attributeGroup name="attlist.if-has-permission"> - <xs:attribute type="xs:string" name="permission" use="required"> - <xs:annotation> - <xs:documentation> - The name of the permission in the database. - The user must belong to a security group that has this permission. - </xs:documentation> - </xs:annotation> - </xs:attribute> - <xs:attribute type="xs:string" name="action"> - <xs:annotation> - <xs:documentation> - If an action is specified the user can have one of two permissions: the permission + "_ADMIN" or permission + action. - Examples of actions include "_CREATE", "_VIEW", etc. - </xs:documentation> - </xs:annotation> - </xs:attribute> - </xs:attributeGroup> <xs:element name="check-permission" substitutionGroup="IfOtherOperations"> <xs:annotation> <xs:documentation> Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CombinedCondition.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CombinedCondition.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CombinedCondition.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CombinedCondition.java Mon May 7 08:28:47 2012 @@ -18,12 +18,14 @@ *******************************************************************************/ package org.ofbiz.minilang.method.conditional; -import java.util.Iterator; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import javolution.util.FastList; - import org.ofbiz.base.util.UtilXml; +import org.ofbiz.minilang.MiniLangElement; +import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.MethodContext; import org.w3c.dom.Element; @@ -31,99 +33,52 @@ import org.w3c.dom.Element; /** * Implements generic combining conditions such as or, and, etc. */ -public class CombinedCondition implements Conditional { +public abstract class CombinedCondition extends MiniLangElement implements Conditional { - public static final int OR = 1; - public static final int XOR = 2; - public static final int AND = 3; - public static final int NOT = 4; - - int conditionType; - SimpleMethod simpleMethod; - List<Conditional> subConditions = FastList.newInstance(); - - public CombinedCondition(Element element, int conditionType, SimpleMethod simpleMethod) { - this.simpleMethod = simpleMethod; - this.conditionType = conditionType; - for (Element subElement : UtilXml.childElementList(element)) { - subConditions.add(ConditionalFactory.makeConditional(subElement, simpleMethod)); - } - } + protected final List<Conditional> subConditions; - public boolean checkCondition(MethodContext methodContext) { - if (subConditions.size() == 0) - return true; - Iterator<Conditional> subCondIter = subConditions.iterator(); - switch (this.conditionType) { - case OR: - while (subCondIter.hasNext()) { - Conditional subCond = subCondIter.next(); - if (subCond.checkCondition(methodContext)) { - return true; - } - } - return false; - case XOR: - boolean trueFound = false; - while (subCondIter.hasNext()) { - Conditional subCond = subCondIter.next(); - if (subCond.checkCondition(methodContext)) { - if (trueFound) { - return false; - } else { - trueFound = true; - } - } - } - return trueFound; - case AND: - while (subCondIter.hasNext()) { - Conditional subCond = subCondIter.next(); - if (!subCond.checkCondition(methodContext)) { - return false; - } - } - return true; - case NOT: - Conditional subCond = subCondIter.next(); - return !subCond.checkCondition(methodContext); - default: - return false; + public CombinedCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + List<? extends Element> childElements = UtilXml.childElementList(element); + if (MiniLangValidate.validationOn() && childElements.isEmpty()) { + MiniLangValidate.handleError("No conditional elements.", simpleMethod, element); + } + List<Conditional> conditionalList = new ArrayList<Conditional>(childElements.size()); + for (Element conditionalElement : UtilXml.childElementList(element)) { + conditionalList.add(ConditionalFactory.makeConditional(conditionalElement, simpleMethod)); } + this.subConditions = Collections.unmodifiableList(conditionalList); } - public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { + protected void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext, String combineText) { messageBuffer.append("("); - Iterator<Conditional> subCondIter = subConditions.iterator(); - while (subCondIter.hasNext()) { - Conditional subCond = subCondIter.next(); + for (Conditional subCond : subConditions) { subCond.prettyPrint(messageBuffer, methodContext); - if (subCondIter.hasNext()) { - switch (this.conditionType) { - case OR: - messageBuffer.append(" OR "); - break; - case XOR: - messageBuffer.append(" XOR "); - break; - case AND: - messageBuffer.append(" AND "); - break; - case NOT: - messageBuffer.append(" NOT "); - break; - default: - messageBuffer.append("?"); - } - } + messageBuffer.append(combineText); } messageBuffer.append(")"); } public static final class AndConditionFactory extends ConditionalFactory<CombinedCondition> { @Override - public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) { - return new CombinedCondition(element, AND, simpleMethod); + public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + return new CombinedCondition(element, simpleMethod) { + @Override + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { + if (subConditions.size() == 0) + return true; + for (Conditional subCond : subConditions) { + if (!subCond.checkCondition(methodContext)) { + return false; + } + } + return true; + } + @Override + public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { + prettyPrint(messageBuffer, methodContext, " AND "); + } + }; } @Override @@ -134,8 +89,25 @@ public class CombinedCondition implement public static final class NotConditionFactory extends ConditionalFactory<CombinedCondition> { @Override - public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) { - return new CombinedCondition(element, NOT, simpleMethod); + public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + return new CombinedCondition(element, simpleMethod) { + @Override + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { + if (subConditions.size() == 0) + return true; + Conditional subCond = subConditions.get(0); + return !subCond.checkCondition(methodContext); + } + @Override + public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { + messageBuffer.append("( NOT "); + if (subConditions.size() > 0) { + Conditional subCond = subConditions.get(0); + subCond.prettyPrint(messageBuffer, methodContext); + } + messageBuffer.append(")"); + } + }; } @Override @@ -146,8 +118,24 @@ public class CombinedCondition implement public static final class OrConditionFactory extends ConditionalFactory<CombinedCondition> { @Override - public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) { - return new CombinedCondition(element, OR, simpleMethod); + public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + return new CombinedCondition(element, simpleMethod) { + @Override + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { + if (subConditions.size() == 0) + return true; + for (Conditional subCond : subConditions) { + if (subCond.checkCondition(methodContext)) { + return true; + } + } + return false; + } + @Override + public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { + prettyPrint(messageBuffer, methodContext, " OR "); + } + }; } @Override @@ -158,8 +146,29 @@ public class CombinedCondition implement public static final class XorConditionFactory extends ConditionalFactory<CombinedCondition> { @Override - public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) { - return new CombinedCondition(element, XOR, simpleMethod); + public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + return new CombinedCondition(element, simpleMethod) { + @Override + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { + if (subConditions.size() == 0) + return true; + boolean trueFound = false; + for (Conditional subCond : subConditions) { + if (subCond.checkCondition(methodContext)) { + if (trueFound) { + return false; + } else { + trueFound = true; + } + } + } + return trueFound; + } + @Override + public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { + prettyPrint(messageBuffer, methodContext, " XOR "); + } + }; } @Override Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareCondition.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareCondition.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareCondition.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareCondition.java Mon May 7 08:28:47 2012 @@ -19,111 +19,75 @@ package org.ofbiz.minilang.method.conditional; 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.UtilValidate; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; +import org.ofbiz.minilang.MiniLangElement; +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.operation.BaseCompare; import org.w3c.dom.Element; /** * Implements compare to a constant condition. */ -public class CompareCondition implements Conditional { +public final class CompareCondition extends MiniLangElement implements Conditional { public static final String module = CompareCondition.class.getName(); - ContextAccessor<Object> fieldAcsr; - String format; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - String operator; - SimpleMethod simpleMethod; - String type; - String value; - - public CompareCondition(Element element, SimpleMethod simpleMethod) { - this.simpleMethod = 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")); + private final FlexibleMapAccessor<Object> fieldFma; + private final FlexibleStringExpander formatFse; + private final String operator; + private final String type; + private final FlexibleStringExpander valueFse; + + public CompareCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "field", "format", "operator", "type", "value"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "operator", "value"); + MiniLangValidate.constantAttributes(simpleMethod, element, "operator", "type"); + MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "value", "format"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "field"); + MiniLangValidate.noChildElements(simpleMethod, element); } - this.value = element.getAttribute("value"); + this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); + this.formatFse = FlexibleStringExpander.getInstance(element.getAttribute("format")); this.operator = element.getAttribute("operator"); - this.type = element.getAttribute("type"); - this.format = element.getAttribute("format"); + this.type = MiniLangValidate.checkAttribute(element.getAttribute("type"), "PlainString"); + this.valueFse = FlexibleStringExpander.getInstance(element.getAttribute("value")); } - public boolean checkCondition(MethodContext methodContext) { - String value = methodContext.expandString(this.value); - String operator = methodContext.expandString(this.operator); - String type = methodContext.expandString(this.type); - String format = methodContext.expandString(this.format); - Object fieldVal = getFieldVal(methodContext); - List<Object> messages = FastList.newInstance(); - Boolean resultBool = BaseCompare.doRealCompare(fieldVal, value, operator, type, format, messages, null, methodContext.getLoader(), true); - if (messages.size() > 0) { - messages.add(0, "Error with comparison in if-compare between field [" + mapAcsr.toString() + "." + fieldAcsr.toString() + "] with value [" + fieldVal + "] and value [" + value + "] with operator [" + operator + "] and type [" + type + "]: "); - if (methodContext.getMethodType() == MethodContext.EVENT) { - StringBuilder fullString = new StringBuilder(); - - for (Object message : messages) { - fullString.append(message); - } - Debug.logWarning(fullString.toString(), module); - methodContext.putEnv(simpleMethod.getEventErrorMessageName(), fullString.toString()); - methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); - } else if (methodContext.getMethodType() == MethodContext.SERVICE) { - methodContext.putEnv(simpleMethod.getServiceErrorMessageListName(), messages); - methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); - } - return false; - } - if (resultBool != null) - return resultBool.booleanValue(); - return false; - } - - protected Object getFieldVal(MethodContext methodContext) { - 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 + ", using empty string for comparison", module); - } else { - fieldVal = fieldAcsr.get(fromMap, methodContext); - } - } else { - // no map name, try the env - fieldVal = fieldAcsr.get(methodContext); - } - // always use an empty string by default + public boolean checkCondition(MethodContext methodContext) throws MiniLangRuntimeException { + Object fieldVal = fieldFma.get(methodContext.getEnvMap()); if (fieldVal == null) { fieldVal = ""; } - return fieldVal; + String value = valueFse.expandString(methodContext.getEnvMap()); + String format = formatFse.expandString(methodContext.getEnvMap()); + List<Object> errorMessages = FastList.newInstance(); + Boolean resultBool = ObjectType.doRealCompare(fieldVal, value, operator, type, format, errorMessages, methodContext.getLocale(), methodContext.getLoader(), true); + if (errorMessages.size() > 0 || resultBool == null) { + for (Object obj : errorMessages) { + simpleMethod.addErrorMessage(methodContext, (String) obj); + } + return false; + } + return resultBool.booleanValue(); } public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { - String value = methodContext.expandString(this.value); - String operator = methodContext.expandString(this.operator); - String type = methodContext.expandString(this.type); - String format = methodContext.expandString(this.format); - Object fieldVal = getFieldVal(methodContext); + String value = valueFse.expandString(methodContext.getEnvMap()); + String format = formatFse.expandString(methodContext.getEnvMap()); + Object fieldVal = fieldFma.get(methodContext.getEnvMap()); messageBuffer.append("["); - if (!this.mapAcsr.isEmpty()) { - messageBuffer.append(this.mapAcsr); - messageBuffer.append("."); - } - messageBuffer.append(this.fieldAcsr); + messageBuffer.append(this.fieldFma); messageBuffer.append("="); messageBuffer.append(fieldVal); messageBuffer.append("] "); @@ -140,7 +104,7 @@ public class CompareCondition implements public static final class CompareConditionFactory extends ConditionalFactory<CompareCondition> { @Override - public CompareCondition createCondition(Element element, SimpleMethod simpleMethod) { + public CompareCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new CompareCondition(element, simpleMethod); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareFieldCondition.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareFieldCondition.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareFieldCondition.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/CompareFieldCondition.java Mon May 7 08:28:47 2012 @@ -19,147 +19,95 @@ package org.ofbiz.minilang.method.conditional; 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.UtilValidate; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; +import org.ofbiz.minilang.MiniLangElement; +import org.ofbiz.minilang.MiniLangException; +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.operation.BaseCompare; import org.w3c.dom.Element; /** * Implements compare to a field condition. */ -public class CompareFieldCondition implements Conditional { +public final class CompareFieldCondition extends MiniLangElement implements Conditional { public static final String module = CompareFieldCondition.class.getName(); - ContextAccessor<Object> fieldAcsr; - String format; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - String operator; - SimpleMethod simpleMethod; - ContextAccessor<Object> toFieldAcsr; - ContextAccessor<Map<String, ? extends Object>> toMapAcsr; - String type; - - public CompareFieldCondition(Element element, SimpleMethod simpleMethod) { - this.simpleMethod = 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")); - } - // NOTE: this is still supported, but is deprecated - this.toMapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("to-map-name")); - // set fieldAcsr to their default value of fieldAcsr if empty - this.toFieldAcsr = new ContextAccessor<Object>(element.getAttribute("to-field"), element.getAttribute("field")); - if (this.toFieldAcsr.isEmpty()) { - // NOTE: this is still supported, but is deprecated - this.toFieldAcsr = new ContextAccessor<Object>(element.getAttribute("to-field-name"), element.getAttribute("field-name")); - } - // do NOT default the to-map-name to the map-name because that - // would make it impossible to compare from a map field to an - // environment field - this.operator = element.getAttribute("operator"); - this.type = element.getAttribute("type"); - this.format = element.getAttribute("format"); - } - - public boolean checkCondition(MethodContext methodContext) { - String operator = methodContext.expandString(this.operator); - String type = methodContext.expandString(this.type); - String format = methodContext.expandString(this.format); - Object fieldVal1 = getFieldVal1(methodContext); - Object fieldVal2 = getFieldVal2(methodContext); - List<Object> messages = FastList.newInstance(); - Boolean resultBool = BaseCompare.doRealCompare(fieldVal1, fieldVal2, operator, type, format, messages, null, methodContext.getLoader(), false); - if (messages.size() > 0) { - messages.add(0, "Error with comparison in if-compare-field between fields [" + mapAcsr.toString() + "." + fieldAcsr.toString() + "] with value [" + fieldVal1 + "] and [" + toMapAcsr.toString() + "." + toFieldAcsr.toString() + "] with value [" + fieldVal2 + "] with operator [" + operator - + "] and type [" + type + "]: "); - if (methodContext.getMethodType() == MethodContext.EVENT) { - StringBuilder fullString = new StringBuilder(); - for (Object message : messages) { - fullString.append(message); - } - Debug.logWarning(fullString.toString(), module); - methodContext.putEnv(simpleMethod.getEventErrorMessageName(), fullString.toString()); - methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); - } else if (methodContext.getMethodType() == MethodContext.SERVICE) { - methodContext.putEnv(simpleMethod.getServiceErrorMessageListName(), messages); - methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); - } - return false; + // This method is needed only during the v1 to v2 transition + private static boolean autoCorrect(Element element) { + // Correct missing to-field attribute + String toFieldAttr = element.getAttribute("to-field"); + if (toFieldAttr.isEmpty()) { + element.setAttribute("to-field", element.getAttribute("field")); + return true; } - if (resultBool != null) - return resultBool.booleanValue(); return false; } - protected Object getFieldVal1(MethodContext methodContext) { - Object fieldVal1 = 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 + ", using null for comparison", module); - } else { - fieldVal1 = fieldAcsr.get(fromMap, methodContext); - } - } else { - // no map name, try the env - fieldVal1 = fieldAcsr.get(methodContext); + private final FlexibleMapAccessor<Object> fieldFma; + private final FlexibleStringExpander formatFse; + private final String operator; + private final FlexibleMapAccessor<Object> toFieldFma; + private final String type; + + public CompareFieldCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "field", "format", "operator", "type", "to-field"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "operator", "to-field"); + MiniLangValidate.constantAttributes(simpleMethod, element, "operator", "type"); + MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "format"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "field", "to-field"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + boolean elementModified = autoCorrect(element); + if (elementModified && MiniLangUtil.autoCorrectOn()) { + MiniLangUtil.flagDocumentAsCorrected(element); } - return fieldVal1; + this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); + this.formatFse = FlexibleStringExpander.getInstance(element.getAttribute("format")); + this.operator = element.getAttribute("operator"); + this.toFieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("to-field")); + this.type = MiniLangValidate.checkAttribute(element.getAttribute("type"), "PlainString"); } - protected Object getFieldVal2(MethodContext methodContext) { - Object fieldVal2 = null; - if (!toMapAcsr.isEmpty()) { - Map<String, ? extends Object> toMap = toMapAcsr.get(methodContext); - if (toMap == null) { - if (Debug.infoOn()) - Debug.logInfo("To Map not found with name " + toMapAcsr + ", using null for comparison", module); - } else { - fieldVal2 = toFieldAcsr.get(toMap, methodContext); + public boolean checkCondition(MethodContext methodContext) { + Object fieldVal = fieldFma.get(methodContext.getEnvMap()); + Object toFieldVal = toFieldFma.get(methodContext.getEnvMap()); + String format = formatFse.expandString(methodContext.getEnvMap()); + List<Object> errorMessages = FastList.newInstance(); + Boolean resultBool = ObjectType.doRealCompare(fieldVal, toFieldVal, operator, type, format, errorMessages, methodContext.getLocale(), methodContext.getLoader(), true); + if (errorMessages.size() > 0 || resultBool == null) { + for (Object obj : errorMessages) { + simpleMethod.addErrorMessage(methodContext, (String) obj); } - } else { - // no map name, try the env - fieldVal2 = toFieldAcsr.get(methodContext); + return false; } - return fieldVal2; + return resultBool.booleanValue(); } public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { - String operator = methodContext.expandString(this.operator); - String type = methodContext.expandString(this.type); - String format = methodContext.expandString(this.format); - Object fieldVal1 = getFieldVal1(methodContext); - Object fieldVal2 = getFieldVal2(methodContext); + String format = formatFse.expandString(methodContext.getEnvMap()); + Object fieldVal = fieldFma.get(methodContext.getEnvMap()); + Object toFieldVal = toFieldFma.get(methodContext.getEnvMap()); messageBuffer.append("["); - if (!this.mapAcsr.isEmpty()) { - messageBuffer.append(this.mapAcsr); - messageBuffer.append("."); - } - messageBuffer.append(this.fieldAcsr); + messageBuffer.append(fieldFma); messageBuffer.append("="); - messageBuffer.append(fieldVal1); + messageBuffer.append(fieldVal); messageBuffer.append("] "); messageBuffer.append(operator); messageBuffer.append(" ["); - if (!this.toMapAcsr.isEmpty()) { - messageBuffer.append(this.toMapAcsr); - messageBuffer.append("."); - } - messageBuffer.append(this.toFieldAcsr); + messageBuffer.append(toFieldFma); messageBuffer.append("="); - messageBuffer.append(fieldVal2); + messageBuffer.append(toFieldVal); messageBuffer.append("] "); messageBuffer.append(" as "); messageBuffer.append(type); @@ -171,7 +119,7 @@ public class CompareFieldCondition imple public static final class CompareFieldConditionFactory extends ConditionalFactory<CompareFieldCondition> { @Override - public CompareFieldCondition createCondition(Element element, SimpleMethod simpleMethod) { + public CompareFieldCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new CompareFieldCondition(element, simpleMethod); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Conditional.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Conditional.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Conditional.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Conditional.java Mon May 7 08:28:47 2012 @@ -18,6 +18,7 @@ *******************************************************************************/ package org.ofbiz.minilang.method.conditional; +import org.ofbiz.minilang.MiniLangException; import org.ofbiz.minilang.method.MethodContext; /** @@ -25,7 +26,7 @@ import org.ofbiz.minilang.method.MethodC */ public interface Conditional { - public boolean checkCondition(MethodContext methodContext); + public boolean checkCondition(MethodContext methodContext) throws MiniLangException; public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ConditionalFactory.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ConditionalFactory.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ConditionalFactory.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ConditionalFactory.java Mon May 7 08:28:47 2012 @@ -26,6 +26,7 @@ import java.util.ServiceLoader; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilGenerics; +import org.ofbiz.minilang.MiniLangException; import org.ofbiz.minilang.SimpleMethod; import org.w3c.dom.Element; @@ -47,7 +48,7 @@ public abstract class ConditionalFactory conditionalFactories = Collections.unmodifiableMap(factories); } - public static Conditional makeConditional(Element element, SimpleMethod simpleMethod) { + public static Conditional makeConditional(Element element, SimpleMethod simpleMethod) throws MiniLangException { String tagName = element.getTagName(); ConditionalFactory<?> factory = conditionalFactories.get(tagName); if (factory != null) { @@ -58,7 +59,7 @@ public abstract class ConditionalFactory } } - public abstract C createCondition(Element element, SimpleMethod simpleMethod); + public abstract C createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException; public abstract String getName(); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ElseIf.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ElseIf.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ElseIf.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ElseIf.java Mon May 7 08:28:47 2012 @@ -22,7 +22,9 @@ import java.util.Collections; import java.util.List; import org.ofbiz.base.util.UtilXml; +import org.ofbiz.minilang.MiniLangElement; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; @@ -31,12 +33,17 @@ import org.w3c.dom.Element; /** * Implements the else-if alternate execution element. */ -public class ElseIf { +public final class ElseIf extends MiniLangElement { - protected Conditional condition; - protected List<MethodOperation> thenSubOps; + private final Conditional condition; + private final List<MethodOperation> thenSubOps; public ElseIf(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.childElements(simpleMethod, element, "condition", "then"); + MiniLangValidate.requiredChildElements(simpleMethod, element, "condition", "then"); + } Element conditionElement = UtilXml.firstChildElement(element, "condition"); Element conditionChildElement = UtilXml.firstChildElement(conditionElement); this.condition = ConditionalFactory.makeConditional(conditionChildElement, simpleMethod); @@ -44,7 +51,7 @@ public class ElseIf { this.thenSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(thenElement, simpleMethod)); } - public boolean checkCondition(MethodContext methodContext) { + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { return condition.checkCondition(methodContext); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/EmptyCondition.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/EmptyCondition.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/EmptyCondition.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/EmptyCondition.java Mon May 7 08:28:47 2012 @@ -18,98 +18,49 @@ *******************************************************************************/ package org.ofbiz.minilang.method.conditional; -import java.util.Collection; -import java.util.Map; - -import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.minilang.MiniLangElement; +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.w3c.dom.Element; /** * Implements compare to a constant condition. */ -public class EmptyCondition implements Conditional { +public final class EmptyCondition extends MiniLangElement implements Conditional { public static final String module = EmptyCondition.class.getName(); - ContextAccessor<Object> fieldAcsr; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - SimpleMethod simpleMethod; - - public EmptyCondition(Element element, SimpleMethod simpleMethod) { - this.simpleMethod = 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")); - } - } + private final FlexibleMapAccessor<Object> fieldFma; - public boolean checkCondition(MethodContext methodContext) { - // only run subOps if element is empty/null - boolean runSubOps = false; - Object fieldVal = getFieldVal(methodContext); - if (fieldVal == null) { - runSubOps = true; - } else { - if (fieldVal instanceof String) { - String fieldStr = (String) fieldVal; - if (fieldStr.length() == 0) { - runSubOps = true; - } - } else if (fieldVal instanceof Collection<?>) { - Collection<?> fieldCol = (Collection<?>) fieldVal; - if (fieldCol.size() == 0) { - runSubOps = true; - } - } else if (fieldVal instanceof Map<?, ?>) { - Map<?, ?> fieldMap = (Map<?, ?>) fieldVal; - if (fieldMap.size() == 0) { - runSubOps = true; - } - } + public EmptyCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "field"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "field"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "field"); } - return runSubOps; + this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); } - protected Object getFieldVal(MethodContext methodContext) { - 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); - } - return fieldVal; + public boolean checkCondition(MethodContext methodContext) { + return UtilValidate.isEmpty(fieldFma.get(methodContext.getEnvMap())); } public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { messageBuffer.append("empty["); - if (!this.mapAcsr.isEmpty()) { - messageBuffer.append(this.mapAcsr); - messageBuffer.append("."); - } - messageBuffer.append(this.fieldAcsr); - if (methodContext != null) { - messageBuffer.append("="); - messageBuffer.append(getFieldVal(methodContext)); - } + messageBuffer.append(fieldFma); + messageBuffer.append("="); + messageBuffer.append(fieldFma.get(methodContext.getEnvMap())); messageBuffer.append("]"); } public static final class EmptyConditionFactory extends ConditionalFactory<EmptyCondition> { @Override - public EmptyCondition createCondition(Element element, SimpleMethod simpleMethod) { + public EmptyCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new EmptyCondition(element, simpleMethod); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/HasPermissionCondition.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/HasPermissionCondition.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/HasPermissionCondition.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/HasPermissionCondition.java Mon May 7 08:28:47 2012 @@ -19,7 +19,11 @@ package org.ofbiz.minilang.method.conditional; import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.GenericValue; +import org.ofbiz.minilang.MiniLangElement; +import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.security.Security; @@ -29,58 +33,55 @@ import org.w3c.dom.Element; /** * Implements compare to a constant condition. */ -public class HasPermissionCondition implements Conditional { +public final class HasPermissionCondition extends MiniLangElement implements Conditional { - String action; - String permission; - SimpleMethod simpleMethod; - - public HasPermissionCondition(Element element, SimpleMethod simpleMethod) { - this.simpleMethod = simpleMethod; - this.permission = element.getAttribute("permission"); - this.action = element.getAttribute("action"); + private final FlexibleStringExpander actionFse; + private final FlexibleStringExpander permissionFse; + + public HasPermissionCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "permission", "action"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "permission"); + MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "permission", "action"); + } + this.permissionFse = FlexibleStringExpander.getInstance(element.getAttribute("permission")); + this.actionFse = FlexibleStringExpander.getInstance(element.getAttribute("action")); } public boolean checkCondition(MethodContext methodContext) { - // only run subOps if element is empty/null - boolean runSubOps = false; - // if no user is logged in, treat as if the user does not have permission: do not - // run subops GenericValue userLogin = methodContext.getUserLogin(); if (userLogin != null) { - String permission = methodContext.expandString(this.permission); - String action = methodContext.expandString(this.action); - - Authorization authz = methodContext.getAuthz(); - Security security = methodContext.getSecurity(); - if (UtilValidate.isNotEmpty(action)) { - // run hasEntityPermission + String permission = permissionFse.expandString(methodContext.getEnvMap()); + String action = actionFse.expandString(methodContext.getEnvMap()); + if (!action.isEmpty()) { + Security security = methodContext.getSecurity(); if (security.hasEntityPermission(permission, action, userLogin)) { - runSubOps = true; + return true; } } else { - // run hasPermission + Authorization authz = methodContext.getAuthz(); if (authz.hasPermission(userLogin.getString("userLoginId"), permission, methodContext.getEnvMap())) { - runSubOps = true; + return true; } } } - return runSubOps; + return false; } public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { messageBuffer.append("has-permission["); - messageBuffer.append(this.permission); - if (UtilValidate.isNotEmpty(this.action)) { + messageBuffer.append(this.permissionFse); + if (UtilValidate.isNotEmpty(this.actionFse)) { messageBuffer.append(":"); - messageBuffer.append(this.action); + messageBuffer.append(this.actionFse); } messageBuffer.append("]"); } public static final class HasPermissionConditionFactory extends ConditionalFactory<HasPermissionCondition> { @Override - public HasPermissionCondition createCondition(Element element, SimpleMethod simpleMethod) { + public HasPermissionCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new HasPermissionCondition(element, simpleMethod); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/MasterIf.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/MasterIf.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/MasterIf.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/MasterIf.java Mon May 7 08:28:47 2012 @@ -18,14 +18,15 @@ *******************************************************************************/ package org.ofbiz.minilang.method.conditional; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import javolution.util.FastList; -import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.UtilXml; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; @@ -34,29 +35,38 @@ import org.w3c.dom.Element; /** * Represents the top-level element and only mounted operation for the more flexible if structure. */ -public class MasterIf extends MethodOperation { +public final class MasterIf extends MethodOperation { - Conditional condition; - List<ElseIf> elseIfs = null; - List<MethodOperation> elseSubOps = null; - List<MethodOperation> thenSubOps; + private final Conditional condition; + private final List<ElseIf> elseIfs; + private final List<MethodOperation> elseSubOps; + private final List<MethodOperation> thenSubOps; public MasterIf(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.childElements(simpleMethod, element, "condition", "then", "else-if", "else"); + MiniLangValidate.requiredChildElements(simpleMethod, element, "condition", "then"); + } Element conditionElement = UtilXml.firstChildElement(element, "condition"); Element conditionChildElement = UtilXml.firstChildElement(conditionElement); this.condition = ConditionalFactory.makeConditional(conditionChildElement, simpleMethod); Element thenElement = UtilXml.firstChildElement(element, "then"); this.thenSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(thenElement, simpleMethod)); List<? extends Element> elseIfElements = UtilXml.childElementList(element, "else-if"); - if (UtilValidate.isNotEmpty(elseIfElements)) { - elseIfs = FastList.newInstance(); + if (elseIfElements.isEmpty()) { + this.elseIfs = null; + } else { + List<ElseIf> elseIfs = new ArrayList<ElseIf>(elseIfElements.size()); for (Element elseIfElement : elseIfElements) { elseIfs.add(new ElseIf(elseIfElement, simpleMethod)); } + this.elseIfs = Collections.unmodifiableList(elseIfs); } Element elseElement = UtilXml.firstChildElement(element, "else"); - if (elseElement != null) { + if (elseElement == null) { + this.elseSubOps = null; + } else { this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod)); } } @@ -72,7 +82,7 @@ public class MasterIf extends MethodOper return SimpleMethod.runSubOps(thenSubOps, methodContext); } else { // try the else-ifs - if (UtilValidate.isNotEmpty(elseIfs)) { + if (elseIfs != null) { for (ElseIf elseIf : elseIfs) { if (elseIf.checkCondition(methodContext)) { return elseIf.runSubOps(methodContext); Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompare.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompare.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompare.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompare.java Mon May 7 08:28:47 2012 @@ -27,6 +27,7 @@ import javolution.util.FastList; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilXml; 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; @@ -61,7 +62,7 @@ public class IfCompare extends MethodOpe } this.value = element.getAttribute("value"); this.operator = element.getAttribute("operator"); - this.type = element.getAttribute("type"); + this.type = MiniLangValidate.checkAttribute(element.getAttribute("type"), "PlainString"); this.format = element.getAttribute("format"); this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod)); Element elseElement = UtilXml.firstChildElement(element, "else"); Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompareField.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompareField.java?rev=1334915&r1=1334914&r2=1334915&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompareField.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfCompareField.java Mon May 7 08:28:47 2012 @@ -27,6 +27,7 @@ import javolution.util.FastList; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilXml; 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; @@ -72,7 +73,7 @@ public class IfCompareField extends Meth // would make it impossible to compare from a map field to an // environment field this.operator = element.getAttribute("operator"); - this.type = element.getAttribute("type"); + this.type = MiniLangValidate.checkAttribute(element.getAttribute("type"), "PlainString"); this.format = element.getAttribute("format"); this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod)); Element elseElement = UtilXml.firstChildElement(element, "else"); |
Free forum by Nabble | Edit this page |