Author: adrianc
Date: Mon May 7 12:26:01 2012 New Revision: 1334971 URL: http://svn.apache.org/viewvc?rev=1334971&view=rev Log: More work on Mini-language: API fixes, overhauled <if-regexp>, <if-validate-method>, and <check-id> elements. Added: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/CheckId.java - copied, changed from r1334327, ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckId.java Removed: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckId.java Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory 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/EmptyCondition.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/HasPermissionCondition.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/RegexpCondition.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ValidateMethodCondition.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfValidateMethod.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=1334971&r1=1334970&r2=1334971&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original) +++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Mon May 7 12:26:01 2012 @@ -73,10 +73,21 @@ under the License. </xs:simpleType> </xs:attribute> + <xs:attribute type="xs:string" name="error-list-name"> + <xs:annotation> + <xs:documentation> + The name of the error message list. Defaults to "error_list". + <br/><br/> + Optional. Attribute type: constant + </xs:documentation> + </xs:annotation> + </xs:attribute> + <xs:attributeGroup name="attlist.propertyInfoOptional"> <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> @@ -88,6 +99,7 @@ under the License. </xs:annotation> </xs:attribute> </xs:attributeGroup> + <xs:attributeGroup name="attlist.typeDefaultString"> <xs:attribute name="type" default="String"> <xs:simpleType> @@ -108,6 +120,7 @@ under the License. </xs:simpleType> </xs:attribute> </xs:attributeGroup> + <xs:attributeGroup name="attlist.operatorRequired"> <xs:attribute name="operator" use="required"> <xs:simpleType> @@ -3556,15 +3569,7 @@ under the License. <xs:element ref="fail-message" /> <xs:element ref="fail-property" /> </xs:choice> - <xs:attribute type="xs:string" name="error-list-name"> - <xs:annotation> - <xs:documentation> - The name of the error message list. Defaults to "error_list". - <br/><br/> - Optional. Attribute type: constant - </xs:documentation> - </xs:annotation> - </xs:attribute> + <xs:attribute ref="error-list-name"/> </xs:complexType> </xs:element> <xs:element name="break" substitutionGroup="ControlOperations"> @@ -3617,16 +3622,7 @@ under the License. </xs:documentation> </xs:annotation> </xs:attribute> - <xs:attribute type="xs:string" name="error-list-name"> - <xs:annotation> - <xs:documentation> - The name of the list in the method environment to check for error messages. - Defaults to "error_list". - <br/><br/> - Optional. Attribute type: constant - </xs:documentation> - </xs:annotation> - </xs:attribute> + <xs:attribute ref="error-list-name"/> </xs:complexType> </xs:element> @@ -3797,57 +3793,45 @@ under the License. <xs:documentation> The operations contained by the if-validate-method tag will only be executed if the validate method returns true. If-validate-method calls a static Java method that takes a String and returns a boolean. - This tag can contain any of the simple-method operations, including the conditional/if operations. + This tag 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-validate-method"/> + <xs:attribute type="xs:string" name="field" use="required"> + <xs:annotation> + <xs:documentation> + The name of the field (variable) that will be validated. + <br/><br/> + Required. Attribute type: expression + </xs:documentation> + </xs:annotation> + </xs:attribute> + <xs:attribute type="xs:string" name="method" use="required"> + <xs:annotation> + <xs:documentation> + The name of the method that will be called to validate the field. + It must be a static method that takes a single String parameter and return a boolean. + <br/><br/> + Required. Attribute type: constant + </xs:documentation> + </xs:annotation> + </xs:attribute> + <xs:attribute type="xs:string" name="class"> + <xs:annotation> + <xs:documentation> + The name of the class that contains the validation method. Defaults to "org.ofbiz.base.util.UtilValidate". + <br/><br/> + Optional. Attribute type: constant + </xs:documentation> + </xs:annotation> + </xs:attribute> </xs:complexType> </xs:element> - <xs:attributeGroup name="attlist.if-validate-method"> - <xs:attribute type="xs:string" name="field" use="required"> - <xs:annotation> - <xs:documentation> - The name of the field (variable) that will be validated. - </xs:documentation> - </xs:annotation> - </xs:attribute> - <xs:attribute type="xs:string" name="method" use="required"> - <xs:annotation> - <xs:documentation> - The name of the method that will be called to validate the field. - It must be a static method that takes a single String parameter and return a boolean. - </xs:documentation> - </xs:annotation> - </xs:attribute> - <xs:attribute type="xs:string" name="class" default="org.ofbiz.base.util.UtilValidate"> - <xs:annotation> - <xs:documentation> - The name of the class that contains the validation method. If not specified defaults to "org.ofbiz.base.util.UtilValidate". - </xs:documentation> - </xs:annotation> - </xs:attribute> - </xs:attributeGroup> <xs:element name="if-instance-of" substitutionGroup="IfBasicOperations"> <xs:annotation> <xs:documentation> @@ -4225,39 +4209,32 @@ under the License. </xs:annotation> </xs:attribute> </xs:attributeGroup> - <xs:element name="check-id" substitutionGroup="IfOtherOperations"> + <xs:element name="check-id" substitutionGroup="EnvOperations"> <xs:annotation> <xs:documentation> - <![CDATA[The check-id tag checks to see if the ID value in the given field is a valid ID string. + The check-id tag checks to see if the ID value in the given field is a valid database ID string. + An invalid ID field will add an error message to the error message list. + <br/><br/> Valid IDs can be any sequence of characters or digits but must not containt the following characters: - space [ ], doublequote ["], single quote ['], ampersand [&], question mark [?], less-than sign [<], greater-than sign [>].]]> + space [ ], double quote ["], single quote ['], ampersand [&], question + mark [?], less-than sign [<], greater-than sign [>]. </xs:documentation> </xs:annotation> <xs:complexType> <xs:choice minOccurs="0"> - <xs:element ref="fail-message"/> - <xs:element ref="fail-property"/> + <xs:element ref="fail-message" /> + <xs:element ref="fail-property" /> </xs:choice> - <xs:attributeGroup ref="attlist.check-id"/> + <xs:attribute type="xs:string" name="field" use="required"> + <xs:annotation> + <xs:documentation> + The name of the field that contains the ID value to check. + </xs:documentation> + </xs:annotation> + </xs:attribute> + <xs:attribute ref="error-list-name"/> </xs:complexType> </xs:element> - <xs:attributeGroup name="attlist.check-id"> - <xs:attribute type="xs:string" name="field" use="required"> - <xs:annotation> - <xs:documentation> - The name of the field that contains the ID value to check. - </xs:documentation> - </xs:annotation> - </xs:attribute> - <xs:attribute type="xs:string" name="error-list-name" default="error_list"> - <xs:annotation> - <xs:documentation> - The name of a list in the method environment that the error messages will be added to. - Will be created if does not exist. Defaults to "error_list". - </xs:documentation> - </xs:annotation> - </xs:attribute> - </xs:attributeGroup> <!-- the "Other" Operations --> <xs:element name="log" substitutionGroup="OtherOperations"> <xs:annotation> Modified: ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation%24Factory?rev=1334971&r1=1334970&r2=1334971&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory (original) +++ ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory Mon May 7 12:26:01 2012 @@ -67,6 +67,7 @@ org.ofbiz.minilang.method.entityops.Tran org.ofbiz.minilang.method.entityops.TransactionCommit$TransactionCommitFactory org.ofbiz.minilang.method.entityops.TransactionRollback$TransactionRollbackFactory org.ofbiz.minilang.method.envops.Break$BreakFactory +org.ofbiz.minilang.method.envops.CheckId$CheckIdFactory org.ofbiz.minilang.method.envops.ClearField$ClearFieldFactory org.ofbiz.minilang.method.envops.Continue$ContinueFactory org.ofbiz.minilang.method.envops.FieldToList$FieldToListFactory @@ -90,7 +91,6 @@ org.ofbiz.minilang.method.eventops.Reque org.ofbiz.minilang.method.eventops.RequestToField$RequestToFieldFactory org.ofbiz.minilang.method.eventops.SessionToField$SessionToFieldFactory org.ofbiz.minilang.method.eventops.WebappPropertyToField$WebappPropertyToFieldFactory -org.ofbiz.minilang.method.ifops.CheckId$CheckIdFactory org.ofbiz.minilang.method.ifops.CheckPermission$CheckPermissionFactory org.ofbiz.minilang.method.ifops.IfCompare$IfCompareFactory org.ofbiz.minilang.method.ifops.IfCompareField$IfCompareFieldFactory 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=1334971&r1=1334970&r2=1334971&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 12:26:01 2012 @@ -64,6 +64,7 @@ public final class CompareCondition exte this.valueFse = FlexibleStringExpander.getInstance(element.getAttribute("value")); } + @Override public boolean checkCondition(MethodContext methodContext) throws MiniLangRuntimeException { Object fieldVal = fieldFma.get(methodContext.getEnvMap()); if (fieldVal == null) { 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=1334971&r1=1334970&r2=1334971&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 12:26:01 2012 @@ -66,7 +66,6 @@ public final class CompareFieldCondition 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()) { @@ -79,7 +78,8 @@ public final class CompareFieldCondition this.type = MiniLangValidate.checkAttribute(element.getAttribute("type"), "PlainString"); } - public boolean checkCondition(MethodContext methodContext) { + @Override + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { Object fieldVal = fieldFma.get(methodContext.getEnvMap()); Object toFieldVal = toFieldFma.get(methodContext.getEnvMap()); String format = formatFse.expandString(methodContext.getEnvMap()); 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=1334971&r1=1334970&r2=1334971&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 12:26:01 2012 @@ -46,7 +46,8 @@ public final class EmptyCondition extend this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); } - public boolean checkCondition(MethodContext methodContext) { + @Override + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { return UtilValidate.isEmpty(fieldFma.get(methodContext.getEnvMap())); } 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=1334971&r1=1334970&r2=1334971&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 12:26:01 2012 @@ -49,7 +49,8 @@ public final class HasPermissionConditio this.actionFse = FlexibleStringExpander.getInstance(element.getAttribute("action")); } - public boolean checkCondition(MethodContext methodContext) { + @Override + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { GenericValue userLogin = methodContext.getUserLogin(); if (userLogin != null) { String permission = permissionFse.expandString(methodContext.getEnvMap()); Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/RegexpCondition.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/RegexpCondition.java?rev=1334971&r1=1334970&r2=1334971&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/RegexpCondition.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/RegexpCondition.java Mon May 7 12:26:01 2012 @@ -18,110 +18,77 @@ *******************************************************************************/ package org.ofbiz.minilang.method.conditional; -import java.util.List; -import java.util.Map; - -import javolution.util.FastList; - import org.apache.oro.text.regex.MalformedPatternException; import org.ofbiz.base.util.CompilerMatcher; -import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.ObjectType; +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.w3c.dom.Element; /** * Implements compare to a constant condition. */ -public class RegexpCondition implements Conditional { +public class RegexpCondition extends MiniLangElement implements Conditional { public static final String module = RegexpCondition.class.getName(); private transient static ThreadLocal<CompilerMatcher> compilerMatcher = CompilerMatcher.getThreadLocal(); - List<?> elseSubOps = null; - FlexibleStringExpander exprExdr; - ContextAccessor<Object> fieldAcsr; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - SimpleMethod simpleMethod; - List<?> subOps = FastList.newInstance(); - - public RegexpCondition(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")); - } - this.exprExdr = FlexibleStringExpander.getInstance(element.getAttribute("expr")); - } + private final FlexibleMapAccessor<Object> fieldFma; + private final FlexibleStringExpander exprFse; - public boolean checkCondition(MethodContext methodContext) { - String fieldString = getFieldString(methodContext); - boolean matches = false; - try { - matches = compilerMatcher.get().matches(fieldString, methodContext.expandString(this.exprExdr)); - } catch (MalformedPatternException e) { - Debug.logError(e, "Regular Expression [" + this.exprExdr + "] is mal-formed: " + e.toString(), module); - } - if (matches) { - return true; - } else { - return false; + public RegexpCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "field", "expr"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "expr"); + MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "expr"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "field"); } + this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); + this.exprFse = FlexibleStringExpander.getInstance(element.getAttribute("expr")); } - protected String getFieldString(MethodContext methodContext) { - String fieldString = null; - 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); - } - if (fieldVal != null) { + @Override + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { + Object fieldVal = fieldFma.get(methodContext.getEnvMap()); + if (fieldVal == null) { + fieldVal = ""; + } else if (!(fieldVal instanceof String)) { try { - fieldString = (String) ObjectType.simpleTypeConvert(fieldVal, "String", null, methodContext.getTimeZone(), methodContext.getLocale(), true); + fieldVal = ObjectType.simpleTypeConvert(fieldVal, "String", null, methodContext.getTimeZone(), methodContext.getLocale(), true); } catch (GeneralException e) { - Debug.logError(e, "Could not convert object to String, using empty String", module); + throw new MiniLangRuntimeException(e, this); } } - // always use an empty string by default - if (fieldString == null) - fieldString = ""; - return fieldString; + String regExp = exprFse.expandString(methodContext.getEnvMap()); + try { + return compilerMatcher.get().matches((String) fieldVal, regExp); + } catch (MalformedPatternException e) { + throw new MiniLangRuntimeException(e, this); + } } public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { messageBuffer.append("regexp["); messageBuffer.append("["); - if (!this.mapAcsr.isEmpty()) { - messageBuffer.append(this.mapAcsr); - messageBuffer.append("."); - } - messageBuffer.append(this.fieldAcsr); + messageBuffer.append(this.fieldFma); messageBuffer.append("="); - messageBuffer.append(getFieldString(methodContext)); + messageBuffer.append(fieldFma.get(methodContext.getEnvMap())); messageBuffer.append("] matches "); - messageBuffer.append(methodContext.expandString(this.exprExdr)); + messageBuffer.append(exprFse.expandString(methodContext.getEnvMap())); messageBuffer.append("]"); } public static final class RegexpConditionFactory extends ConditionalFactory<RegexpCondition> { @Override - public RegexpCondition createCondition(Element element, SimpleMethod simpleMethod) { + public RegexpCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new RegexpCondition(element, simpleMethod); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ValidateMethodCondition.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ValidateMethodCondition.java?rev=1334971&r1=1334970&r2=1334971&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ValidateMethodCondition.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/ValidateMethodCondition.java Mon May 7 12:26:01 2012 @@ -19,92 +19,64 @@ package org.ofbiz.minilang.method.conditional; import java.lang.reflect.Method; -import java.util.Map; -import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.ObjectType; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +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.w3c.dom.Element; /** * Implements validate method condition. */ -public class ValidateMethodCondition implements Conditional { +public final class ValidateMethodCondition extends MiniLangElement implements Conditional { public static final String module = ValidateMethodCondition.class.getName(); + private static final Class<?>[] paramTypes = new Class<?>[] { String.class }; - String className; - ContextAccessor<Object> fieldAcsr; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - String methodName; - - public ValidateMethodCondition(Element element) { - this.mapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("map-name")); - this.fieldAcsr = new ContextAccessor<Object>(element.getAttribute("field-name")); - this.methodName = element.getAttribute("method"); - this.className = element.getAttribute("class"); - } - - public boolean checkCondition(MethodContext methodContext) { - String methodName = methodContext.expandString(this.methodName); - String className = methodContext.expandString(this.className); - String fieldString = getFieldString(methodContext); - Class<?>[] paramTypes = new Class<?>[] { String.class }; - Object[] params = new Object[] { fieldString }; - Class<?> valClass; - try { - valClass = methodContext.getLoader().loadClass(className); - } catch (ClassNotFoundException cnfe) { - Debug.logError("Could not find validation class: " + className, module); - return false; - } - Method valMethod; - try { - valMethod = valClass.getMethod(methodName, paramTypes); - } catch (NoSuchMethodException cnfe) { - Debug.logError("Could not find validation method: " + methodName + " of class " + className, module); - return false; - } - Boolean resultBool = Boolean.FALSE; - try { - resultBool = (Boolean) valMethod.invoke(null, params); - } catch (Exception e) { - Debug.logError(e, "Error in IfValidationMethod " + methodName + " of class " + className + ", not processing sub-ops ", module); + private final String className; + private final FlexibleMapAccessor<Object> fieldFma; + private final String methodName; + + public ValidateMethodCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "field", "method", "class"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "method"); + MiniLangValidate.constantAttributes(simpleMethod, element, "method", "class"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "field"); } - if (resultBool != null) - return resultBool.booleanValue(); - return false; + this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); + this.methodName = element.getAttribute("method"); + this.className = MiniLangValidate.checkAttribute(element.getAttribute("class"), "org.ofbiz.base.util.UtilValidate"); } - protected String getFieldString(MethodContext methodContext) { - String fieldString = null; - 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); - } - if (fieldVal != null) { + @Override + public boolean checkCondition(MethodContext methodContext) throws MiniLangException { + Object fieldVal = fieldFma.get(methodContext.getEnvMap()); + if (fieldVal == null) { + fieldVal = ""; + } else if (!(fieldVal instanceof String)) { try { - fieldString = (String) ObjectType.simpleTypeConvert(fieldVal, "String", null, methodContext.getTimeZone(), methodContext.getLocale(), true); + fieldVal = ObjectType.simpleTypeConvert(fieldVal, "String", null, methodContext.getTimeZone(), methodContext.getLocale(), true); } catch (GeneralException e) { - Debug.logError(e, "Could not convert object to String, using empty String", module); + throw new MiniLangRuntimeException(e, this); } } - // always use an empty string by default - if (fieldString == null) - fieldString = ""; - return fieldString; + Object[] params = new Object[] { fieldVal }; + try { + Class<?> valClass = methodContext.getLoader().loadClass(className); + Method valMethod = valClass.getMethod(methodName, paramTypes); + Boolean resultBool = (Boolean) valMethod.invoke(null, params); + return resultBool.booleanValue(); + } catch (Exception e) { + throw new MiniLangRuntimeException(e, this); + } } public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) { @@ -116,22 +88,18 @@ public class ValidateMethodCondition imp messageBuffer.append("."); messageBuffer.append(methodName); messageBuffer.append("("); - if (!this.mapAcsr.isEmpty()) { - messageBuffer.append(this.mapAcsr); - messageBuffer.append("."); - } - messageBuffer.append(this.fieldAcsr); + messageBuffer.append(this.fieldFma); if (methodContext != null) { messageBuffer.append("="); - messageBuffer.append(getFieldString(methodContext)); + messageBuffer.append(fieldFma.get(methodContext.getEnvMap())); } messageBuffer.append(")]"); } public static final class ValidateMethodConditionFactory extends ConditionalFactory<ValidateMethodCondition> { @Override - public ValidateMethodCondition createCondition(Element element, SimpleMethod simpleMethod) { - return new ValidateMethodCondition(element); + public ValidateMethodCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException { + return new ValidateMethodCondition(element, simpleMethod); } @Override Copied: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/CheckId.java (from r1334327, ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckId.java) URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/CheckId.java?p2=ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/CheckId.java&p1=ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckId.java&r1=1334327&r2=1334971&rev=1334971&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/CheckId.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/CheckId.java Mon May 7 12:26:01 2012 @@ -16,20 +16,20 @@ * specific language governing permissions and limitations * under the License. *******************************************************************************/ -package org.ofbiz.minilang.method.ifops; +package org.ofbiz.minilang.method.envops; import java.util.List; -import java.util.Map; import javolution.util.FastList; -import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.UtilXml; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; @@ -37,95 +37,111 @@ import org.w3c.dom.Element; /** * If the given ID field is not valid the fail-message or fail-property sub-elements are used to add a message to the error-list. */ -public class CheckId extends MethodOperation { +public final class CheckId extends MethodOperation { public static final String module = CheckId.class.getName(); - ContextAccessor<List<Object>> errorListAcsr; - ContextAccessor<Object> fieldAcsr; - boolean isProperty = false; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - String message = null; - String propertyResource = null; + private final FlexibleMapAccessor<List<String>> errorListFma; + private final FlexibleMapAccessor<Object> fieldFma; + private final FlexibleStringExpander messageFse; + private final String propertykey; + private final String propertyResource; public CheckId(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")); - this.errorListAcsr = new ContextAccessor<List<Object>>(element.getAttribute("error-list-name"), "error_list"); - // note: if no fail-message or fail-property then message will be null - Element failMessage = UtilXml.firstChildElement(element, "fail-message"); - Element failProperty = UtilXml.firstChildElement(element, "fail-property"); - if (failMessage != null) { - this.message = failMessage.getAttribute("message"); - this.isProperty = false; - } else if (failProperty != null) { - this.propertyResource = failProperty.getAttribute("resource"); - this.message = failProperty.getAttribute("property"); - this.isProperty = true; - } - } - - public void addMessage(List<Object> messages, MethodContext methodContext, String defaultMessage, String errorDetails) { - String message = methodContext.expandString(this.message); - String propertyResource = methodContext.expandString(this.propertyResource); - if (!isProperty && message != null) { - messages.add(message + errorDetails); - } else if (isProperty && propertyResource != null && message != null) { - // String propMsg = UtilProperties.getPropertyValue(UtilURL.fromResource(propertyResource, loader), message); - String propMsg = UtilProperties.getMessage(propertyResource, message, methodContext.getEnvMap(), methodContext.getLocale()); - if (UtilValidate.isEmpty(propMsg)) { - messages.add(defaultMessage + errorDetails); - } else { - messages.add(methodContext.expandString(propMsg) + errorDetails); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "error-list-name"); + MiniLangValidate.constantAttributes(simpleMethod, element, "error-list-name"); + MiniLangValidate.childElements(simpleMethod, element, "fail-message", "fail-property"); + MiniLangValidate.requireAnyChildElement(simpleMethod, element, "fail-message", "fail-property"); + } + this.errorListFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list")); + this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); + Element childElement = UtilXml.firstChildElement(element, "fail-message"); + if (childElement != null) { + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, childElement, "message"); + MiniLangValidate.requiredAttributes(simpleMethod, childElement, "message"); + MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, childElement, "message"); } + this.messageFse = FlexibleStringExpander.getInstance(childElement.getAttribute("message")); + this.propertykey = null; + this.propertyResource = null; } else { - messages.add(defaultMessage + errorDetails); + childElement = UtilXml.firstChildElement(element, "fail-property"); + if (childElement != null) { + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, childElement, "property", "resource"); + MiniLangValidate.requiredAttributes(simpleMethod, childElement, "property", "resource"); + MiniLangValidate.constantAttributes(simpleMethod, childElement, "property", "resource"); + } + this.messageFse = FlexibleStringExpander.getInstance(null); + this.propertykey = childElement.getAttribute("property"); + this.propertyResource = childElement.getAttribute("resource"); + } else { + this.messageFse = FlexibleStringExpander.getInstance(null); + this.propertykey = null; + this.propertyResource = null; + } } } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - boolean isValid = true; - List<Object> messages = errorListAcsr.get(methodContext); - if (messages == null) { - messages = FastList.newInstance(); - errorListAcsr.put(methodContext, messages); - } - 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); - } + String errorMsg = null; + Object fieldVal = fieldFma.get(methodContext.getEnvMap()); + if (fieldVal == null) { + errorMsg = "field \"" + fieldFma + "\" is null"; } else { - // no map name, try the env - fieldVal = fieldAcsr.get(methodContext); + errorMsg = UtilValidate.checkValidDatabaseId(fieldVal.toString()); } - String fieldStr = fieldVal.toString(); - StringBuilder errorDetails = new StringBuilder(); - // check various illegal characters, etc for ids - isValid = UtilValidate.isValidDatabaseId(fieldStr, errorDetails); - if (!isValid) { - this.addMessage(messages, methodContext, "The ID value in the field [" + fieldAcsr + "] was not valid", ": " + errorDetails.toString()); + if (errorMsg != null) { + String message = null; + if (!this.messageFse.isEmpty()) { + message = this.messageFse.expandString(methodContext.getEnvMap()); + } else if (this.propertyResource != null) { + message = UtilProperties.getMessage(this.propertyResource, this.propertykey, methodContext.getEnvMap(), methodContext.getLocale()); + } + if (message != null) { + List<String> messages = errorListFma.get(methodContext.getEnvMap()); + if (messages == null) { + messages = FastList.newInstance(); + } + errorListFma.put(methodContext.getEnvMap(), messages); + messages.add(message.concat(": ").concat(errorMsg)); + } } 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: add all attributes and other info - return "<check-id field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<check-id "); + if (!this.fieldFma.isEmpty()) { + sb.append("field=\"").append(this.fieldFma).append("\" "); + } + if (!"error_list".equals(this.errorListFma.getOriginalName())) { + sb.append("error-list-name=\"").append(this.errorListFma).append("\" "); + } + sb.append(">"); + if (!this.messageFse.isEmpty()) { + sb.append("<fail-message message=\"").append(this.messageFse).append("\" />"); + } + if (this.propertykey != null) { + sb.append("<fail-property property=\"").append(this.propertykey).append(" resource=\"").append(this.propertyResource).append("\" />"); + } + sb.append("</check-id>"); + return sb.toString(); } public static final class CheckIdFactory implements Factory<CheckId> { Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfValidateMethod.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfValidateMethod.java?rev=1334971&r1=1334970&r2=1334971&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfValidateMethod.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/ifops/IfValidateMethod.java Mon May 7 12:26:01 2012 @@ -30,6 +30,7 @@ import org.ofbiz.base.util.GeneralExcept import org.ofbiz.base.util.ObjectType; 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; @@ -56,7 +57,7 @@ public class IfValidateMethod extends Me this.fieldAcsr = new ContextAccessor<Object>(element.getAttribute("field"), element.getAttribute("field-name")); this.mapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("map-name")); this.methodName = element.getAttribute("method"); - this.className = element.getAttribute("class"); + this.className = MiniLangValidate.checkAttribute(element.getAttribute("class"), "org.ofbiz.base.util.UtilValidate"); this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod)); Element elseElement = UtilXml.firstChildElement(element, "else"); if (elseElement != null) { |
Free forum by Nabble | Edit this page |