Commits

Anonymous committed 2006db6

WW-1380
- Validator defined in xml should be able to parse xml tag values against value stack

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1234e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 5bb4aa1
  • Branches xwork_1-2

Comments (0)

Files changed (2)

src/java/com/opensymphony/xwork/validator/validators/ValidatorSupport.java

  * Abstract implementation of the Validator interface suitable for subclassing.
  *
  * @author Jason Carreira
+ * @author tmjee
  */
 public abstract class ValidatorSupport implements Validator, ShortCircuitableValidator {
 
     protected String messageKey = null;
     private ValidatorContext validatorContext;
     private boolean shortCircuit;
+    private boolean parse = false;
     private String type;
 
 
+    public void setParse(boolean parse) { 
+    	this.parse = parse;
+    }
+    
+    public boolean getParse() {
+    	return parse;
+    }
+    
     public void setDefaultMessage(String message) {
         this.defaultMessage = message;
     }
     public String getValidatorType() {
         return type;
     }
+    
+    /**
+     * Parse <code>expression</code> passed in against value stack. Only parse
+     * when 'parse' param is set to true, else just returns the expression unparsed.
+     * 
+     * @param expression
+     * @return
+     */
+    protected Object conditionalParse(String expression) {
+    	if (parse) {
+    		OgnlValueStack stack = ActionContext.getContext().getValueStack();
+    		return TextParseUtil.translateVariables('$', expression, stack);
+    	}
+    	return expression;
+    }
 
+    /**
+     * Return the field value named <code>name</code> from <code>object</code>, 
+     * <code>object</code> should have the appropriate getter/setter.
+     * 
+     * @param name
+     * @param object
+     * @return
+     * @throws ValidationException
+     */
     protected Object getFieldValue(String name, Object object) throws ValidationException {
         OgnlValueStack stack = ActionContext.getContext().getValueStack();
 

src/test/com/opensymphony/xwork/validator/validators/ValidatorSupportTest.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.validator.validators;
+
+import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.util.OgnlValueStack;
+import com.opensymphony.xwork.validator.ValidationException;
+import com.opensymphony.xwork.validator.validators.ValidatorSupport;
+
+import junit.framework.TestCase;
+
+/**
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public class ValidatorSupportTest extends TestCase {
+
+	public void testConditionalParseExpression()  throws Exception {
+		OgnlValueStack oldStack = ActionContext.getContext().getValueStack();
+		try {
+			OgnlValueStack stack = new OgnlValueStack();
+			stack.getContext().put("something", "somevalue");
+			ActionContext.getContext().setValueStack(stack);
+			ValidatorSupport validator = new ValidatorSupport() {
+				public void validate(Object object) throws ValidationException {
+				}
+			};
+			
+			validator.setParse(true);
+			String result1 = validator.conditionalParse("${#something}").toString();
+			
+			validator.setParse(false);
+			String result2 = validator.conditionalParse("${#something}").toString();
+			
+			assertEquals(result1, "somevalue");
+			assertEquals(result2, "${#something}");
+		}
+		finally {
+			ActionContext.getContext().setValueStack(oldStack);
+		}
+	}
+	
+}