Author: hrabago
Date: Tue Feb 28 17:24:08 2006
New Revision: 381860
URL: http://svn.apache.org/viewcvs?rev=381860&view=rev
Log:
Add support for specifying validator parameters using nested s:validatorVar
tags.
Added:
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/ValidatorVarTag.java
(with props)
Modified:
struts/shale/trunk/core-library/src/conf/taglib.tld
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/CommonsValidatorTag.java
struts/shale/trunk/core-library/src/java/org/apache/shale/util/Tags.java
struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java
Modified: struts/shale/trunk/core-library/src/conf/taglib.tld
URL:
http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/conf/taglib.tld?rev=381860&r1=381859&r2=381860&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/conf/taglib.tld (original)
+++ struts/shale/trunk/core-library/src/conf/taglib.tld Tue Feb 28 17:24:08 2006
@@ -138,7 +138,7 @@
<name>commonsValidator</name>
<tag-class>org.apache.shale.taglib.CommonsValidatorTag</tag-class>
- <body-content>empty</body-content>
+ <body-content>JSP</body-content>
<display-name>Commons Validator</display-name>
<description>
Provides access to Commons Validators.
@@ -266,6 +266,37 @@
The name of the function to generate.
</description>
</attribute>
+ </tag>
+
+ <tag>
+
+ <name>validatorVar</name>
+ <tag-class>org.apache.shale.taglib.ValidatorVarTag</tag-class>
+ <body-content>empty</body-content>
+ <display-name>Validator Var</display-name>
+ <description>
+ Parameters for specific validators.
+ </description>
+
+ <!-- Custom -->
+
+ <attribute>
+ <name>name</name>
+ <required>true</required>
+ <rtexprvalue>false</rtexprvalue>
+ <description>
+ The name of the parameter.
+ </description>
+ </attribute>
+
+ <attribute>
+ <name>value</name>
+ <required>true</required>
+ <rtexprvalue>false</rtexprvalue>
+ <description>
+ The value of the parameter.
+ </description>
+ </attribute>
</tag>
</taglib>
Modified:
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/CommonsValidatorTag.java
URL:
http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/CommonsValidatorTag.java?rev=381860&r1=381859&r2=381860&view=diff
==============================================================================
---
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/CommonsValidatorTag.java
(original)
+++
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/CommonsValidatorTag.java
Tue Feb 28 17:24:08 2006
@@ -16,6 +16,9 @@
package org.apache.shale.taglib;
+import java.util.LinkedHashMap;
+import java.util.Iterator;
+
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.webapp.ValidatorTag;
@@ -33,81 +36,87 @@
/**
* <p>The tag utility object. This tag class uses the
- * utility object to store tag attribute values as
- * component attributes.
+ * utility object to store tag attribute values as
+ * component attributes.
*/
private org.apache.shale.util.Tags tagUtils = null;
-
+
/**
* <p>The <code>type</code> attribute.
*/
private String type;
-
+
/**
* <p>The <code>min</code> attribute.
*/
private String min;
-
+
/**
* <p>The <code>max</code> attribute.
*/
private String max;
-
+
/**
* <p>The <code>minlength</code> attribute.
*/
private String minlength;
-
+
/**
* <p>The <code>maxlength</code> attribute.
*/
private String maxlength;
-
+
/**
* <p>The <code>datePatternStrict</code> attribute.
*/
private String datePatternStrict;
-
+
/**
* <p>The <code>mask</code> attribute.
*/
private String mask;
-
+
/**
* <p>The <code>message</code> attribute.
*/
private String message;
-
+
/**
* <p>The <code>arg</code> attribute.
*/
private String arg;
-
+
/**
* <p>The <code>client</code> attribute.
*/
private String client;
-
+
/**
* <p>The <code>server</code> attribute.
*/
private String server;
-
+
+
+ /**
+ * <p>The parameters to pass to the validator.</p>
+ */
+ private LinkedHashMap params;
+
/**
* <p>This constructor obtains a reference to the
- * tag utility object, which is a JSF managed bean.
+ * tag utility object, which is a JSF managed bean.
*/
public CommonsValidatorTag() {
setValidatorId("org.apache.shale.CommonsValidator");
@@ -117,127 +126,176 @@
.getVariableResolver()
.resolveVariable(context,
ShaleConstants.TAG_UTILITY_BEAN);
}
-
+
/**
* <p>Setter method for the <code>type</code> attribute.
*/
- public void setType(String newValue) {
+ public void setType(String newValue) {
type = newValue;
- }
-
+ }
+
/**
* <p>Setter method for the <code>min</code> attribute.
*/
- public void setMin(String newValue) {
+ public void setMin(String newValue) {
min = newValue;
- }
-
+ }
+
/**
* <p>Setter method for the <code>max</code> attribute.
*/
- public void setMax(String newValue) {
+ public void setMax(String newValue) {
max = newValue;
- }
-
+ }
+
/**
* <p>Setter method for the <code>minlength</code> attribute.
*/
- public void setMinlength(String newValue) {
+ public void setMinlength(String newValue) {
minlength = newValue;
- }
+ }
/**
* <p>Setter method for the <code>maxlength</code> attribute.
*/
- public void setMaxlength(String newValue) {
+ public void setMaxlength(String newValue) {
maxlength = newValue;
- }
+ }
/**
* <p>Setter method for the <code>setDatePatternStrict</code> attribute.
*/
- public void setDatePatternStrict(String newValue) {
+ public void setDatePatternStrict(String newValue) {
datePatternStrict = newValue;
- }
+ }
/**
* <p>Setter method for the <code>mask</code> attribute.
*/
- public void setMask(String newValue) {
+ public void setMask(String newValue) {
mask = newValue;
- }
-
+ }
+
/**
* <p>Setter method for the <code>message</code> attribute.
*/
- public void setMessage(String newValue) {
+ public void setMessage(String newValue) {
message = newValue;
- }
-
+ }
+
/**
* <p>Setter method for the <code>arg</code> attribute.
*/
- public void setArg(String newValue) {
+ public void setArg(String newValue) {
arg = newValue;
- }
-
+ }
+
/**
* <p>Setter method for the <code>client</code> attribute.
*/
- public void setClient(String newValue) {
+ public void setClient(String newValue) {
client = newValue;
- }
-
+ }
+
/**
* <p>Setter method for the <code>server</code> attribute.
*/
- public void setServer(String newValue) {
+ public void setServer(String newValue) {
server = newValue;
- }
-
+ }
- /**
+
+ /**
+ * <p>Adds a parameter to pass to the Commons Validator that will be
+ * used.</p>
+ *
+ * @param name the name of the parameter to pass to the validator.
+ * @param value the value of the parameter to pass to the validator.
+ */
+ public void addParam(String name, String value) {
+ if (params == null) {
+ params = new LinkedHashMap();
+ }
+ params.put(name, value);
+ }
+
+ /**
+ * <p>Overridden to prevent the call to [EMAIL PROTECTED] #createValidator}
from
+ * occurring here.</p>
+ *
+ * @return EVAL_BODY_INCLUDE
+ */
+ public int doStartTag() throws JspException {
+ return EVAL_BODY_INCLUDE;
+ }
+
+
+ /**
+ * <p>Executes the code that was skippsed in the <code>doStartTag()</code>
+ * override.</p>
+ */
+ public int doEndTag() throws JspException {
+ super.doStartTag();
+ return super.doEndTag();
+ }
+
+
+ /**
* <p>Create a validaotor by calling <code>super.createValidator()</code>.
- * This method initializes that validator with the tag's attribute
- * values.
+ * This method initializes that validator with the tag's attribute
+ * values.
*/
public Validator createValidator() throws JspException {
CommonsValidator validator = (CommonsValidator) super.createValidator();
- validator.setType(tagUtils.eval(type));
+ validator.setType(tagUtils.evalString(type));
+
+ // parameters for specific validators
validator.setMin(tagUtils.evalDouble(min));
validator.setMax(tagUtils.evalDouble(max));
validator.setMinlength(tagUtils.evalInteger(minlength));
validator.setMaxlength(tagUtils.evalInteger(maxlength));
- validator.setDatePatternStrict(tagUtils.eval(
+ validator.setDatePatternStrict(tagUtils.evalString(
datePatternStrict));
- validator.setMask(tagUtils.eval(mask));
- validator.setMessage(tagUtils.eval(message));
- validator.setArg(tagUtils.eval(arg));
+ validator.setMask(tagUtils.evalString(mask));
+
+ // pass the parameters specified through <s:validatorVar>
+ if (params != null) {
+ for (Iterator iterator = params.keySet().iterator();
+ iterator.hasNext();) {
+ Object key = iterator.next();
+ Object value = tagUtils.eval((String) params.get(key));
+ validator.addParam(key, value);
+ }
+ }
+
+ validator.setMessage(tagUtils.evalString(message));
+ validator.setArg(tagUtils.evalString(arg));
validator.setClient(tagUtils.evalBoolean(client));
validator.setServer(tagUtils.evalBoolean(server));
-
+
return validator;
}
-
+
/**
* <p>Sets all instance objects representing tag attribute values
- * to <code>null</code>.
+ * to <code>null</code>.
*/
public void release() {
type = null;
+ params = null;
min = null;
max = null;
minlength = null;
@@ -249,4 +307,5 @@
client = null;
server = null;
}
+
}
Added:
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/ValidatorVarTag.java
URL:
http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/ValidatorVarTag.java?rev=381860&view=auto
==============================================================================
---
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/ValidatorVarTag.java
(added)
+++
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/ValidatorVarTag.java
Tue Feb 28 17:24:08 2006
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shale.taglib;
+
+import javax.servlet.jsp.tagext.TagSupport;
+import javax.servlet.jsp.tagext.Tag;
+import javax.servlet.jsp.JspException;
+
+/**
+ * The tag class for the <code>s:validatorVar</code> tag, used to specify
+ * parameters to specific validators of Commons Validator.
+ * <p/>
+ * $Id$
+ */
+public class ValidatorVarTag
+ extends TagSupport {
+
+ /**
+ * The parent validator tag.
+ */
+ CommonsValidatorTag commonsValidatorTag = null;
+
+
+ /**
+ * The name of the var to pass to the validator.
+ */
+ private String name;
+
+
+ /**
+ * The value of the var to pass to the validator.
+ */
+ private String value;
+
+
+ /**
+ * Keeps track of the <s:commonsValidator> tag.
+ * <p/>
+ *
+ * @param tag the parent tag of this tag instance.
+ */
+ public void setParent(Tag tag) {
+ super.setParent(tag);
+ if (tag instanceof CommonsValidatorTag) {
+ commonsValidatorTag = (CommonsValidatorTag) tag;
+ } else {
+ throw new IllegalStateException(
+ "This tag must be used inside a <s:commonsValidator> tag");
+ }
+ }
+
+
+ /**
+ * Pass the validation parameter to the commonsValidatorTag that immediately
+ * surrounds this tag.
+ */
+ public int doEndTag() throws JspException {
+ commonsValidatorTag.addParam(name, value);
+ return EVAL_PAGE;
+ }
+
+
+ //*************************************************************** Accessors
+
+
+ /**
+ * The var name.
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+
+ /**
+ * The var value.
+ */
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+
+}
Propchange:
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/ValidatorVarTag.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
struts/shale/trunk/core-library/src/java/org/apache/shale/taglib/ValidatorVarTag.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified:
struts/shale/trunk/core-library/src/java/org/apache/shale/util/Tags.java
URL:
http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/util/Tags.java?rev=381860&r1=381859&r2=381860&view=diff
==============================================================================
--- struts/shale/trunk/core-library/src/java/org/apache/shale/util/Tags.java
(original)
+++ struts/shale/trunk/core-library/src/java/org/apache/shale/util/Tags.java
Tue Feb 28 17:24:08 2006
@@ -262,19 +262,38 @@
/**
* <p>Evaluate the <code>expression</code>. If it's a value reference,
- * get the reference's value. Otherwise, return the
<code>expression</code>.
+ * get the reference's value.
+ * Otherwise, return the <code>expression</code>.
*
* @param expression The expression
*/
- public String eval(String expression) {
+ public Object eval(String expression) {
if (expression == null) return null;
if (UIComponentTag.isValueReference(expression)) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
- return "" + app.createValueBinding(expression).getValue(context);
+ return app.createValueBinding(expression).getValue(context);
}
else return expression;
}
+
+
+ /**
+ * <p>Evaluate the <code>expression</code>. If it's a value reference,
+ * get the reference's value as a String.
+ * Otherwise, return the <code>expression</code>.
+ *
+ * @param expression The expression
+ */
+ public String evalString(String expression) {
+ if (expression == null) return null;
+ if (UIComponentTag.isValueReference(expression)) {
+ FacesContext context = FacesContext.getCurrentInstance();
+ Application app = context.getApplication();
+ return "" + app.createValueBinding(expression).getValue(context);
+ }
+ else return expression;
+ }
/**
Modified:
struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java
URL:
http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java?rev=381860&r1=381859&r2=381860&view=diff
==============================================================================
---
struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java
(original)
+++
struts/shale/trunk/core-library/src/java/org/apache/shale/validator/CommonsValidator.java
Tue Feb 28 17:24:08 2006
@@ -31,6 +31,7 @@
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
+import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -157,6 +158,11 @@
private String datePatternStrict;
+ /**
+ * <p>Parameters for the specific Commons Validator to be used.</p>
+ */
+ private LinkedHashMap params;
+
// -------------------------------------------------------- Transient
Variables
@@ -315,44 +321,65 @@
public void setDatePatternStrict(String newValue) {
datePatternStrict = newValue;
- }
-
+ }
+
+
+ /**
+ * <p>Add a parameter for the specific validator being used.</p>
+ *
+ * @param key The name of the validator parameter to add.
+ * @param value The value of the validator parameter.
+ */
+ public void addParam(Object key, Object value) {
+ if (params == null) {
+ params = new LinkedHashMap();
+ }
+ params.put(key, value);
+ }
+
/**
* <p>The <code>Object[]</code> returned from this method represents
* parameters that were explicitly set for this validator.
- * Typically, that happens in the <code>s:commonsValidator</code>
- * tag. See the <code>CommonsValidatorTag</code>
- * class for more information about those parameters.
- */
- public Object[] getParams() {
- ArrayList r = new ArrayList();
- if (min != null) r.add(min);
- if (max != null) r.add(max);
- if (minlength != null) r.add(minlength);
- if (maxlength != null) r.add(maxlength);
- if (mask != null) r.add(mask);
- if (datePatternStrict != null) r.add(datePatternStrict);
- return r.toArray();
+ * Typically, that happens in the <code>s:validatorVar</code>
+ * tag or the <code>s:commonsValidator</code> tag.
+ */
+ public Object[] getParams() {
+ // if the params map is empty, try the individual properties
+ if ((params == null) || (params.size() == 0)) {
+ ArrayList r = new ArrayList();
+ if (min != null) r.add(min);
+ if (max != null) r.add(max);
+ if (minlength != null) r.add(minlength);
+ if (maxlength != null) r.add(maxlength);
+ if (mask != null) r.add(mask);
+ if (datePatternStrict != null) r.add(datePatternStrict);
+ return r.toArray();
+ }
+ return params.values().toArray();
}
/**
* <p>The <code>String[]</code> returned from this method represents
* the names of parameters that were explicitly set for this validator.
- * Typically, that happens in the <code>s:commonsValidator</code>
- * tag. See the <code>CommonsValidatorTag</code>
- * class for more information about those parameters.
- */
- public String[] getParamNames() {
- ArrayList r = new ArrayList();
- if (min != null) r.add("min");
- if (max != null) r.add("max");
- if (minlength != null) r.add("minlength");
- if (maxlength != null) r.add("maxlength");
- if (mask != null) r.add("mask");
- if (datePatternStrict != null) r.add("datePatternStrict");
- return (String[]) r.toArray(new String[r.size()]);
+ * Typically, that happens in the <code>s:validatorVar</code>
+ * tag or the <code>s:commonsValidator</code> tag.
+ */
+ public String[] getParamNames() {
+ // if the params map is empty, try the individual properties
+ if ((params == null) || (params.size() == 0)) {
+ ArrayList r = new ArrayList();
+ if (min != null) r.add("min");
+ if (max != null) r.add("max");
+ if (minlength != null) r.add("minlength");
+ if (maxlength != null) r.add("maxlength");
+ if (mask != null) r.add("mask");
+ if (datePatternStrict != null) r.add("datePatternStrict");
+ return (String[]) r.toArray(new String[r.size()]);
+ }
+ return (String[]) params.keySet()
+ .toArray(new String[params.keySet().size()]);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]