Commits

Anonymous committed 68edbc3

porting over contitional parse from XWork 1.2.2-dev

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1242e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (2)

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

 
     protected final Log log = LogFactory.getLog(this.getClass());
     protected String defaultMessage = "";
-    protected String messageKey = null;
+    protected String messageKey;
     private ValidatorContext validatorContext;
     private boolean shortCircuit;
+    private boolean parse;
     private String type;
 
 
         return defaultMessage;
     }
 
+    public void setParse(boolean parse) {
+    	this.parse = parse;
+    }
+
+    public boolean getParse() {
+    	return parse;
+    }
+    
     public String getMessage(Object object) {
         String message;
         ValueStack stack = ActionContext.getContext().getValueStack();
         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 Object
+     */
+    protected Object conditionalParse(String expression) {
+        if (parse) {
+            ValueStack 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 Object as field value
+     * @throws ValidationException
+     */
     protected Object getFieldValue(String name, Object object) throws ValidationException {
         ValueStack stack = ActionContext.getContext().getValueStack();
 

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

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork2.validator.validators;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.util.OgnlValueStack;
+import com.opensymphony.xwork2.util.ValueStack;
+import com.opensymphony.xwork2.validator.ValidationException;
+import com.opensymphony.xwork2.validator.validators.ValidatorSupport;
+
+import junit.framework.TestCase;
+
+/**
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public class ValidatorSupportTest extends TestCase {
+
+	public void testConditionalParseExpression()  throws Exception {
+		ValueStack 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);
+		}
+	}
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.