dwinterfeldt 02/01/14 22:06:24
Modified: validator/src/share/org/apache/commons/validator Field.java
FormSet.java Validator.java ValidatorResources.java
ValidatorResourcesInitializer.java Var.java
validator/src/test/org/apache/commons/validator
ValidatorTest.java
Log:
Worked on adding braces to if statments and other sun standard coding. Removed
logger reference in ValidatorResourcesInitializer and made it so you can pass in the
ValidatorResources to be processed.
Revision Changes Path
1.2 +47 -45
jakarta-commons/validator/src/share/org/apache/commons/validator/Field.java
Index: Field.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Field.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Field.java 6 Jan 2002 05:08:43 -0000 1.1
+++ Field.java 15 Jan 2002 06:06:24 -0000 1.2
@@ -206,8 +206,9 @@
*/
public void addMsg(Msg msg) {
if (msg != null && msg.getKey() != null && msg.getKey().length() > 0 &&
- msg.getName() != null && msg.getName().length() > 0)
+ msg.getName() != null && msg.getName().length() > 0) {
hMsgs.put(msg.getName(), msg.getKey());
+ }
}
/**
@@ -222,10 +223,11 @@
*/
public void addArg0(Arg arg) {
if (arg != null && arg.getKey() != null && arg.getKey().length() > 0) {
- if (arg.getName() != null && arg.getName().length() > 0)
+ if (arg.getName() != null && arg.getName().length() > 0) {
hArg0.put(arg.getName(), arg);
- else
+ } else {
hArg0.put(ARG_DEFAULT, arg);
+ }
}
}
@@ -243,10 +245,7 @@
public Arg getArg0(String key) {
Object o = hArg0.get(key);
- if (o != null)
- return (Arg)o;
- else
- return getArg0();
+ return ((o != null) ? (Arg)o : getArg0());
}
/**
@@ -254,10 +253,11 @@
*/
public void addArg1(Arg arg) {
if (arg != null && arg.getKey() != null && arg.getKey().length() > 0) {
- if (arg.getName() != null && arg.getName().length() > 0)
+ if (arg.getName() != null && arg.getName().length() > 0) {
hArg1.put(arg.getName(), arg);
- else
+ } else {
hArg1.put(ARG_DEFAULT, arg);
+ }
}
}
@@ -275,10 +275,7 @@
public Arg getArg1(String key) {
Object o = hArg1.get(key);
- if (o != null)
- return (Arg)o;
- else
- return getArg1();
+ return ((o != null) ? (Arg)o : getArg1());
}
/**
@@ -286,10 +283,11 @@
*/
public void addArg2(Arg arg) {
if (arg != null && arg.getKey() != null && arg.getKey().length() > 0) {
- if (arg.getName() != null && arg.getName().length() > 0)
+ if (arg.getName() != null && arg.getName().length() > 0) {
hArg2.put(arg.getName(), arg);
- else
+ } else {
hArg2.put(ARG_DEFAULT, arg);
+ }
}
}
@@ -307,10 +305,7 @@
public Arg getArg2(String key) {
Object o = hArg2.get(key);
- if (o != null)
- return (Arg)o;
- else
- return getArg2();
+ return ((o != null) ? (Arg)o : getArg2());
}
/**
@@ -318,10 +313,11 @@
*/
public void addArg3(Arg arg) {
if (arg != null && arg.getKey() != null && arg.getKey().length() > 0) {
- if (arg.getName() != null && arg.getName().length() > 0)
+ if (arg.getName() != null && arg.getName().length() > 0) {
hArg3.put(arg.getName(), arg);
- else
+ } else {
hArg3.put(ARG_DEFAULT, arg);
+ }
}
}
@@ -339,33 +335,32 @@
public Arg getArg3(String key) {
Object o = hArg3.get(key);
- if (o != null)
- return (Arg)o;
- else
- return getArg3();
+ return ((o != null) ? (Arg)o : getArg3());
}
/**
* Add a <code>Var</code> to the <code>Field</code>.
*/
public void addVar(Var v) {
- if (v != null && v.getName() != null && v.getName().length() > 0 &&
v.getValue() != null)
+ if (v != null && v.getName() != null && v.getName().length() > 0 &&
v.getValue() != null) {
hVars.put(v.getName(), v);
+ }
}
/**
* Add a <code>Var</code>, based on the values passed in, to the
<code>Field</code>.
*/
public void addVarParam(String name, String value, String jsType) {
- if (name != null && name.length() > 0 && value != null)
+ if (name != null && name.length() > 0 && value != null) {
hVars.put(name, new Var(name, value, jsType));
+ }
}
/**
* Retrieve a variable.
*/
public Var getVar(String mainKey) {
- return (Var)hVars.get(mainKey);
+ return ((Var)hVars.get(mainKey));
}
/**
@@ -395,8 +390,9 @@
* Gets a unique key based on the property and indexedProperty fields.
*/
public String getKey() {
- if (key == null)
+ if (key == null) {
generateKey();
+ }
return key;
}
@@ -414,21 +410,19 @@
* <code>true</code> will be returned. Otherwise it will be <code>false</code>.
*/
public boolean isIndexed() {
- if ((indexedProperty != null && indexedProperty.length() > 0) &&
- (indexedListProperty != null && indexedListProperty.length() > 0))
- return true;
- else
- return false;
+ return ((indexedProperty != null && indexedProperty.length() > 0) &&
+ (indexedListProperty != null && indexedListProperty.length() > 0));
}
/**
* Generate correct <code>key</code> value.
*/
public void generateKey() {
- if (isIndexed())
+ if (isIndexed()) {
key = indexedProperty + TOKEN_INDEXED + "." + property;
- else
+ } else {
key = property;
+ }
}
/**
@@ -487,8 +481,9 @@
while (st.hasMoreTokens()) {
String depend = st.nextToken().trim();
- if (depend != null && depend.length() > 0)
+ if (depend != null && depend.length() > 0) {
hDependencies.put(depend, value);
+ }
}
@@ -540,8 +535,9 @@
for (Iterator i = hArgs.values().iterator(); i.hasNext(); ) {
Arg arg = (Arg)i.next();
- if (arg != null)
+ if (arg != null) {
arg.setKey(ValidatorUtil.replace(arg.getKey(), key, replaceValue));
+ }
}
}
@@ -550,10 +546,11 @@
* Checks if the key is listed as a dependency.
*/
public boolean isDependency(String key) {
- if (hDependencies != null)
+ if (hDependencies != null) {
return hDependencies.containsKey(key);
- else
+ } else {
return false;
+ }
}
/**
@@ -567,20 +564,25 @@
try {
Field field = (Field)super.clone();
- if (key != null)
+ if (key != null) {
field.setKey(new String(key));
+ }
- if (property != null)
+ if (property != null) {
field.setProperty(new String(property));
+ }
- if (indexedProperty != null)
+ if (indexedProperty != null) {
field.setIndexedProperty(new String(indexedProperty));
+ }
- if (indexedListProperty != null)
+ if (indexedListProperty != null) {
field.setIndexedListProperty(new String(indexedListProperty));
+ }
- if (depends != null)
+ if (depends != null) {
field.setDepends(new String(depends));
+ }
// page & fieldOrder should be taken care of by clone method
1.2 +2 -1
jakarta-commons/validator/src/share/org/apache/commons/validator/FormSet.java
Index: FormSet.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/FormSet.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FormSet.java 6 Jan 2002 05:08:43 -0000 1.1
+++ FormSet.java 15 Jan 2002 06:06:24 -0000 1.2
@@ -178,8 +178,9 @@
Form f = null;
Object o = hForms.get(key);
- if (o != null)
+ if (o != null) {
f = (Form)o;
+ }
return f;
}
1.2 +247 -201
jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java
Index: Validator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Validator.java 6 Jan 2002 05:08:43 -0000 1.1
+++ Validator.java 15 Jan 2002 06:06:24 -0000 1.2
@@ -111,207 +111,253 @@
public int getPage() {
return page;
}
-
- public void validate() throws ValidatorException {
- Locale locale = null;
-
- if (hResources.containsKey(LOCALE_KEY))
- locale = (Locale)hResources.get(LOCALE_KEY);
-
- if (locale == null)
- locale = Locale.getDefault();
-
- Form form = null;
- if ((form = resources.get(locale, formName)) != null) {
- Map hActions = resources.getValidatorActions();
- List lActions = new ArrayList();
- Map hActionsRun = new HashMap();
- boolean bMoreActions = true;
- boolean bErrors = false;
-
- for (Iterator actions = hActions.values().iterator(); actions.hasNext();
)
- lActions.add(actions.next());
-
- while (bMoreActions) {
- ValidatorAction va = null;
- int iErrorCount = 0;
-
- // FIX ME - These sorts will not work for all variations.
- // Sort by number dependencies
- Collections.sort(lActions, new DependencyComparator());
-
- // Sort by number of dependencies successfully run
- Collections.sort(lActions, new
DependencySuccessComparator(hActionsRun));
-
- if (lActions.size() > 0)
- va = (ValidatorAction)lActions.get(0);
-
- if (va != null && va.getDepends() != null && va.getDepends().length() >
0) {
- StringTokenizer st = new StringTokenizer(va.getDepends(), ",");
- while (st.hasMoreTokens()) {
- String depend = st.nextToken().trim();
- Object o = hActionsRun.get(depend);
-
- //if (logger.getDebug() > 10)
- // logger.info("***### Validator main - " + va.getName() + " - "
+ va.getDepends());
-
- if (o == null) {
- //if (logger.getDebug() > 10)
- // logger.info("***### Validator o==null - " +
va.getName() + " - " + va.getDepends());
-
- lActions.clear();
- va = null;
- bMoreActions = false;
- break;
- } else {
- boolean bContinue = ((Boolean)o).booleanValue();
-
- //if (logger.getDebug() > 10)
- // logger.info("***### Validator - " + va.getName() + "
depend=" + depend + " bContinue=" + bContinue);
-
- if (!bContinue) {
- lActions.clear();
- va = null;
- bMoreActions = false;
- break;
- }
- }
- }
-
-
-
- }
-
- // For debug
- /**if (logger.getDebug() > 10) {
- logger.info("***Order ******************************");
-
- for (Iterator actions = lActions.iterator(); actions.hasNext(); ) {
- ValidatorAction tmp = (ValidatorAction)actions.next();
- logger.info("***Order - " + tmp.getName() + " " +
tmp.getDepends());
- }
-
- logger.info("***Order End ******************************");
- }*/
-
- if (va != null) {
- for (Iterator i = form.getFields().iterator(); i.hasNext(); ) {
- Field field = (Field)i.next();
-
- if (field.getPage() <= page && (field.getDepends() != null &&
field.isDependency(va.getName()))) {
- try {
- // Add these two Objects to the resources since they
reference
- // the current validator action and field
- hResources.put(VALIDATOR_ACTION_KEY, va);
- hResources.put(FIELD_KEY, field);
-
- Class c = Class.forName(va.getClassname(), true,
this.getClass().getClassLoader());
-
- List lParams = va.getMethodParamsList();
- int size = lParams.size();
- int beanIndexPos = -1;
- int fieldIndexPos = -1;
- Class[] paramClass = new Class[size];
- Object[] paramValue = new Object[size];
-
- for (int x = 0; x < size; x++) {
- String paramKey = (String)lParams.get(x);
-
- if (BEAN_KEY.equals(paramKey))
- beanIndexPos = x;
-
- if (FIELD_KEY.equals(paramKey))
- fieldIndexPos = x;
-
- // There were problems calling getClass on paramValue[]
- paramClass[x] = Class.forName(paramKey, true,
this.getClass().getClassLoader());
- paramValue[x] = hResources.get(paramKey);
- }
-
- Method m = c.getMethod(va.getMethod(), paramClass);
-
- // If the method is static we don't need an instance of the
class
- // to call the method. If it isn't, we do.
- if (!Modifier.isStatic(m.getModifiers())) {
- try {
- if (va.getClassnameInstance() == null) {
- va.setClassnameInstance(c.newInstance());
- }
- } catch (Exception ex) {
- logger.log("Validator::validate - Couldn't load
instance " +
- "of class " + va.getClassname() + ". " +
ex.getMessage());
- }
- }
-
- Object result = null;
-
- if (field.isIndexed()) {
- Object oIndexed =
PropertyUtils.getProperty(hResources.get(BEAN_KEY), field.getIndexedListProperty());
- Object indexedList[] = new Object[0];
-
- if (oIndexed instanceof Collection)
- indexedList = ((Collection)oIndexed).toArray();
- else if(oIndexed.getClass().isArray())
- indexedList = (Object[])oIndexed;
-
- for (int pos = 0; pos < indexedList.length; pos++) {
- // Set current iteration object to the parameter
array
- paramValue[beanIndexPos] = indexedList[pos];
-
- // Set field clone with the key modified to
represent
- // the current field
- Field indexedField = (Field)field.clone();
-
indexedField.setKey(ValidatorUtil.replace(indexedField.getKey(), Field.TOKEN_INDEXED,
"[" + pos + "]"));
- paramValue[fieldIndexPos] = indexedField;
-
- result = m.invoke(va.getClassnameInstance(),
paramValue);
-
- if (result instanceof Boolean) {
- Boolean valid = (Boolean)result;
- if (!valid.booleanValue())
- iErrorCount++;
- }
- }
- } else {
- result = m.invoke(va.getClassnameInstance(),
paramValue);
-
- if (result instanceof Boolean) {
- Boolean valid = (Boolean)result;
- if (!valid.booleanValue())
- iErrorCount++;
- }
- }
- } catch (Exception e) {
- bErrors = true;
- logger.log("Validator::validate() reflection - " +
e.getMessage());
-
- if (e instanceof ValidatorException)
- throw ((ValidatorException)e);
- }
-
- }
- }
-
- if (iErrorCount == 0) {
- hActionsRun.put(va.getName(), new Boolean(true));
- } else {
- hActionsRun.put(va.getName(), new Boolean(false));
- }
-
- if (logger.getDebug() > 10)
- logger.info("*** Validator - " + va.getName() + " size=" +
lActions.size());
-
- if (lActions.size() > 0)
- lActions.remove(0);
-
- if (logger.getDebug() > 10)
- logger.info("*** Validator - after remove - " + va.getName() + "
size=" + lActions.size());
- }
-
- if (lActions.size() == 0)
- bMoreActions = false;
- }
- }
+
+ /**
+ * Performs validations based on the configured resources.
+ *
+ * @return The <code>Map</code> returned uses the property
+ * of the <code>Field</code> for the key and the value
+ * is the number of error the field had.
+ */
+ public Map validate() throws ValidatorException {
+ Map hResults = new HashMap();
+ Locale locale = null;
+
+ if (hResources.containsKey(LOCALE_KEY))
+ locale = (Locale)hResources.get(LOCALE_KEY);
+
+ if (locale == null)
+ locale = Locale.getDefault();
+
+ Form form = null;
+ if ((form = resources.get(locale, formName)) != null) {
+ Map hActions = resources.getValidatorActions();
+ List lActions = new ArrayList();
+ Map hActionsRun = new HashMap();
+ boolean bMoreActions = true;
+ boolean bErrors = false;
+
+ for (Iterator actions = hActions.values().iterator(); actions.hasNext(); )
+ lActions.add(actions.next());
+
+ while (bMoreActions) {
+ ValidatorAction va = null;
+ int iErrorCount = 0;
+
+ // FIX ME - These sorts will not work for all variations.
+ // Sort by number dependencies
+ Collections.sort(lActions, new DependencyComparator());
+
+ // Sort by number of dependencies successfully run
+ Collections.sort(lActions, new
DependencySuccessComparator(hActionsRun));
+
+ if (lActions.size() > 0)
+ va = (ValidatorAction)lActions.get(0);
+
+ if (va != null && va.getDepends() != null && va.getDepends().length() >
0) {
+ StringTokenizer st = new StringTokenizer(va.getDepends(), ",");
+ while (st.hasMoreTokens()) {
+ String depend = st.nextToken().trim();
+ Object o = hActionsRun.get(depend);
+
+ //if (logger.getDebug() > 10)
+ // logger.info("***### Validator main - " + va.getName() + " - "
+ va.getDepends());
+
+ if (o == null) {
+ //if (logger.getDebug() > 10)
+ // logger.info("***### Validator o==null - " + va.getName()
+ " - " + va.getDepends());
+
+ lActions.clear();
+ va = null;
+ bMoreActions = false;
+ break;
+ } else {
+ boolean bContinue = ((Boolean)o).booleanValue();
+
+ //if (logger.getDebug() > 10)
+ // logger.info("***### Validator - " + va.getName() + "
depend=" + depend + " bContinue=" + bContinue);
+
+ if (!bContinue) {
+ lActions.clear();
+ va = null;
+ bMoreActions = false;
+ break;
+ }
+ }
+ }
+
+
+
+ }
+
+ // For debug
+ /**if (logger.getDebug() > 10) {
+ logger.info("***Order ******************************");
+
+ for (Iterator actions = lActions.iterator(); actions.hasNext(); ) {
+ ValidatorAction tmp = (ValidatorAction)actions.next();
+ logger.info("***Order - " + tmp.getName() + " " +
tmp.getDepends());
+ }
+
+ logger.info("***Order End ******************************");
+ }*/
+
+ if (va != null) {
+ for (Iterator i = form.getFields().iterator(); i.hasNext(); ) {
+ Field field = (Field)i.next();
+
+ if (field.getPage() <= page && (field.getDepends() != null &&
field.isDependency(va.getName()))) {
+ try {
+ // Add these two Objects to the resources since they
reference
+ // the current validator action and field
+ hResources.put(VALIDATOR_ACTION_KEY, va);
+ hResources.put(FIELD_KEY, field);
+
+ Class c = Class.forName(va.getClassname(), true,
this.getClass().getClassLoader());
+
+ List lParams = va.getMethodParamsList();
+ int size = lParams.size();
+ int beanIndexPos = -1;
+ int fieldIndexPos = -1;
+ Class[] paramClass = new Class[size];
+ Object[] paramValue = new Object[size];
+
+ for (int x = 0; x < size; x++) {
+ String paramKey = (String)lParams.get(x);
+
+ if (BEAN_KEY.equals(paramKey))
+ beanIndexPos = x;
+
+ if (FIELD_KEY.equals(paramKey))
+ fieldIndexPos = x;
+
+ // There were problems calling getClass on
paramValue[]
+ paramClass[x] = Class.forName(paramKey, true,
this.getClass().getClassLoader());
+ paramValue[x] = hResources.get(paramKey);
+ }
+
+ Method m = c.getMethod(va.getMethod(), paramClass);
+
+ // If the method is static we don't need an instance of the
class
+ // to call the method. If it isn't, we do.
+ if (!Modifier.isStatic(m.getModifiers())) {
+ try {
+ if (va.getClassnameInstance() == null) {
+ va.setClassnameInstance(c.newInstance());
+ }
+ } catch (Exception ex) {
+ logger.log("Validator::validate - Couldn't load
instance " +
+ "of class " + va.getClassname() + ". " +
ex.getMessage());
+ }
+ }
+
+ Object result = null;
+
+ if (field.isIndexed()) {
+ Object oIndexed =
PropertyUtils.getProperty(hResources.get(BEAN_KEY), field.getIndexedListProperty());
+ Object indexedList[] = new Object[0];
+
+ if (oIndexed instanceof Collection)
+ indexedList = ((Collection)oIndexed).toArray();
+ else if(oIndexed.getClass().isArray())
+ indexedList = (Object[])oIndexed;
+
+ for (int pos = 0; pos < indexedList.length; pos++) {
+ // Set current iteration object to the parameter array
+ paramValue[beanIndexPos] = indexedList[pos];
+
+ // Set field clone with the key modified to represent
+ // the current field
+ Field indexedField = (Field)field.clone();
+
indexedField.setKey(ValidatorUtil.replace(indexedField.getKey(), Field.TOKEN_INDEXED,
"[" + pos + "]"));
+ paramValue[fieldIndexPos] = indexedField;
+
+ result = m.invoke(va.getClassnameInstance(),
paramValue);
+
+ int iCount = getErrorCount(result);
+
+ if (iCount != 0) {
+ iErrorCount += iCount;
+
+ if (hResults.containsKey(field.getKey())) {
+ Integer currentCount =
(Integer)hResults.get(field.getKey());
+ hResults.put(field.getKey(), new
Integer(currentCount.intValue() + iCount));
+ } else {
+ hResults.put(field.getKey(), new
Integer(iCount));
+ }
+ }
+ }
+ } else {
+ result = m.invoke(va.getClassnameInstance(), paramValue);
+
+ int iCount = getErrorCount(result);
+
+ if (iCount != 0) {
+ iErrorCount += iCount;
+
+ if (hResults.containsKey(field.getKey())) {
+ Integer currentCount =
(Integer)hResults.get(field.getKey());
+ hResults.put(field.getKey(), new
Integer(currentCount.intValue() + iCount));
+ } else {
+ hResults.put(field.getKey(), new Integer(iCount));
+ }
+ }
+ }
+ } catch (Exception e) {
+ bErrors = true;
+ logger.log("Validator::validate() reflection - " +
e.getMessage());
+
+ if (e instanceof ValidatorException)
+ throw ((ValidatorException)e);
+ }
+
+ }
+ }
+
+ if (iErrorCount == 0) {
+ hActionsRun.put(va.getName(), new Boolean(true));
+ } else {
+ hActionsRun.put(va.getName(), new Boolean(false));
+ }
+
+ if (logger.getDebug() > 10)
+ logger.info("*** Validator - " + va.getName() + " size=" +
lActions.size());
+
+ if (lActions.size() > 0)
+ lActions.remove(0);
+
+ if (logger.getDebug() > 10)
+ logger.info("*** Validator - after remove - " + va.getName() + "
size=" + lActions.size());
+ }
+
+ if (lActions.size() == 0)
+ bMoreActions = false;
+ }
+ }
+
+ return hResults;
+ }
+
+ /**
+ * Returns the error count for the validation loop. If the
+ * result object is <code>Boolean</code>, then it will increment
+ * the error count if the value is <code>false</code>. If the
+ * result object isn't <code>Boolean</code>, then it will increment
+ * the error count if the result object is <code>null</code>.
+ */
+ private int getErrorCount(Object result) {
+ int iResult = 0;
+
+ if (result instanceof Boolean) {
+ Boolean valid = (Boolean)result;
+ if (!valid.booleanValue())
+ iResult++;
+ } else {
+ if (result == null)
+ iResult++;
+ }
+
+ return iResult;
}
/**
1.2 +1 -5
jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResources.java
Index: ValidatorResources.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResources.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ValidatorResources.java 6 Jan 2002 05:08:43 -0000 1.1
+++ ValidatorResources.java 15 Jan 2002 06:06:24 -0000 1.2
@@ -79,7 +79,7 @@
/**
* Logging interface.
*/
- protected ValidatorLog logger = null;
+ protected ValidatorLog logger = new DefaultValidatorLog();
/**
* <code>FastHashMap</code> of <code>FormSet</code>s stored under
@@ -104,10 +104,6 @@
*/
protected static Locale defaultLocale = Locale.getDefault();
- public ValidatorResources(ValidatorLog logger) {
- this.logger = logger;
- }
-
/**
* Gets the logger.
*/
1.2 +30 -48
jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResourcesInitializer.java
Index: ValidatorResourcesInitializer.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResourcesInitializer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ValidatorResourcesInitializer.java 6 Jan 2002 05:08:43 -0000 1.1
+++ ValidatorResourcesInitializer.java 15 Jan 2002 06:06:24 -0000 1.2
@@ -74,76 +74,59 @@
/**
* Initializes a <code>ValidatorResources</code> based on a
- * file path.
+ * file path and automatically process the resources.
*
* @param fileName The file path for the xml resource.
*/
public static ValidatorResources initialize(String fileName)
throws IOException {
- return initialize(fileName, 0);
+ return initialize(new BufferedInputStream(new FileInputStream(fileName)));
}
/**
- * Initializes a <code>ValidatorResources</code> based on an
- * <code>InputStream</code>.
+ * Initializes a <code>ValidatorResources</code> based on the
<code>InputStream</code>
+ * and automatically process the resources.
*
* @param in <code>InputStream</code> for the xml resource.
*/
public static ValidatorResources initialize(InputStream in)
throws IOException {
-
- return initialize(in, 0);
-
- }
-
- /**
- * Initializes a <code>ValidatorResources</code> based on a
- * file path and the debug level.
- *
- * @param fileName The file path for the xml resource.
- * @param debug The debug level
- */
- public static ValidatorResources initialize(String fileName, int debug)
- throws IOException {
- BufferedInputStream in = new BufferedInputStream(new
FileInputStream(fileName));
+ ValidatorResources resources = new ValidatorResources();
+ initialize(resources, in);
- return initialize(new DefaultValidatorLog(), in, debug);
+ return resources;
+
}
+
/**
- * Initializes a <code>ValidatorResources</code> based on an
- * <code>InputStream</code> and the debug level.
+ * Initializes the <code>ValidatorResources</code> based on the
<code>InputStream</code>
+ * and automatically process the resources.
*
+ * @param resources Resources to initialize.
* @param in <code>InputStream</code> for the xml resource.
- * @param debug The debug level
- */
- public static ValidatorResources initialize(InputStream in, int debug)
+ */
+ public static void initialize(ValidatorResources resources, InputStream in)
throws IOException {
-
- return initialize(new DefaultValidatorLog(), in, debug);
-
+ initialize(resources, in, true);
}
-
+
/**
- * Initializes a <code>ValidatorResources</code> based on an
- * <code>ValidatorLog</code>, <code>InputStream</code>, and the debug level.
+ * Initializes a <code>ValidatorResources</code> based on the
<code>InputStream</code>
+ * and processes the resources based on the <code>boolean</code> passed in.
*
- * @param logger Used for logging.
+ * @param resources Resources to initialize.
* @param in <code>InputStream</code> for the xml resource.
- * @param debug The debug level
+ * @param process Whether or not to call process on
<code>ValidatorResources</code>.
*/
- public static ValidatorResources initialize(ValidatorLog logger, InputStream in,
int debug)
+ public static void initialize(ValidatorResources resources, InputStream in,
boolean process)
throws IOException {
- logger.setDebug(debug);
-
- ValidatorResources resources = new ValidatorResources(logger);
-
Digester digester = new Digester();
digester.push(resources);
- digester.setDebug(debug);
+ digester.setDebug(1);
digester.setNamespaceAware(true);
digester.setValidating(false);
@@ -161,10 +144,9 @@
"org.apache.commons.validator.ValidatorAction");
// Add the body of a javascript element to the Validatoraction
- digester.addCallMethod("form-validation/global/validator",
- "setJavascript", 1);
- digester.addCallParam("form-validation/global/validator/javascript", 0);
-
+ digester.addCallMethod("form-validation/global/validator/javascript",
+ "setJavascript", 0);
+
// Create FormSet objects
digester.addObjectCreate("form-validation/formset",
"org.apache.commons.validator.FormSet",
@@ -239,16 +221,16 @@
digester.parse(in);
in.close();
} catch (SAXException e) {
- logger.log("ValidatorResourcesInitializer::initialize - SAXException: " +
e.getMessage(), e);
+ System.out.println("ValidatorResourcesInitializer::initialize -
SAXException: " + e.getMessage());
} finally {
if (in != null)
try {in.close(); } catch (Exception e) {}
}
- resources.process();
-
- return resources;
-
+ if (process) {
+ resources.process();
+ }
+
}
}
1.2 +7 -4
jakarta-commons/validator/src/share/org/apache/commons/validator/Var.java
Index: Var.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Var.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Var.java 6 Jan 2002 05:08:43 -0000 1.1
+++ Var.java 15 Jan 2002 06:06:24 -0000 1.2
@@ -155,15 +155,18 @@
try {
Var var = (Var)super.clone();
- if (name != null)
+ if (name != null) {
var.name = new String(name);
+ }
- if (value != null)
+ if (value != null) {
var.value = new String(value);
+ }
- if (jsType != null)
+ if (jsType != null) {
var.jsType = new String(jsType);
-
+ }
+
return var;
} catch (CloneNotSupportedException e) {
throw new InternalError(e.toString());
1.2 +94 -11
jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTest.java
Index: ValidatorTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ValidatorTest.java 6 Jan 2002 05:06:21 -0000 1.1
+++ ValidatorTest.java 15 Jan 2002 06:06:24 -0000 1.2
@@ -58,15 +58,15 @@
package org.apache.commons.validator;
import java.util.ArrayList;
+import java.util.Date;
import java.util.List;
+import java.util.Locale;
+import java.text.DateFormat;
+import java.text.ParseException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.AssertionFailedError;
-//import java.io.BufferedReader;
-//import java.io.IOException;
-//import java.io.InputStreamReader;
-//import java.io.StringWriter;
/**
@@ -103,12 +103,64 @@
protected void tearDown() {
}
+
+ /**
+ * Verify that one value generates an error and the other passes. The
validation
+ * method being tested returns an object (<code>null</code> will be considered
an error).
+ */
+ public void testManualObject() {
+ ValidatorResources resources = new ValidatorResources();
+
+ ValidatorAction va = new ValidatorAction();
+ va.setName("date");
+ va.setClassname("org.apache.commons.validator.ValidatorTest");
+ va.setMethod("formatDate");
+ va.setMethodParams("java.lang.Object,org.apache.commons.validator.Field");
+
+ FormSet fs = new FormSet();
+ Form form = new Form();
+ form.setName("testForm");
+ Field field = new Field();
+ field.setProperty("date");
+ field.setDepends("date");
+ form.addField(field);
+ fs.addForm(form);
+
+ resources.addValidatorAction(va);
+ resources.put(fs);
+ resources.process();
+
+ List l = new ArrayList();
+
+ TestBean bean = new TestBean();
+ bean.setDate("2/3/1999");
+
+ Validator validator = new Validator(resources, "testForm");
+ validator.addResource(Validator.BEAN_KEY, bean);
+
+ try {
+ assertEquals("Validation of the date formatting has failed.", 0,
validator.validate().size());
+ } catch (Exception e) {
+ fail("An exception was thrown while calling Validator.validate()");
+ }
+
+ bean.setDate("2/30/1999");
+
+ try {
+ assertEquals("Validation of the date formatting has failed.", 1,
validator.validate().size());
+ } catch (Exception e) {
+ fail("An exception was thrown while calling Validator.validate()");
+ }
+
+
+ }
/**
- * Verify that one value generates and error and the other passes.
- */
- public void testManual() {
- ValidatorResources resources = new ValidatorResources(new
DefaultValidatorLog());
+ * Verify that one value generates an error and the other passes. The
validation
+ * method being tested returns a <code>boolean</code> value.
+ */
+ public void testManualBoolean() {
+ ValidatorResources resources = new ValidatorResources();
ValidatorAction va = new ValidatorAction();
va.setName("capLetter");
@@ -159,8 +211,8 @@
}
/**
- * Verify that two <code>String</code>s match using the <code>EqualTag</code>.
- */
+ * Checks if the field is one upper case letter between 'A' and 'Z'.
+ */
public static boolean isCapLetter(Object bean, Field field, List l) {
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
@@ -176,9 +228,33 @@
return false;
}
}
+
+ /**
+ * Formats a <code>String</code> to a <code>Date</code>.
+ * The <code>Validator</code> will interpret a <code>null</code>
+ * as validation having failed.
+ */
+ public static Date formatDate(Object bean, Field field) {
+ String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
+ Date date = null;
+
+ try {
+ DateFormat formatter = null;
+ formatter = DateFormat.getDateInstance(DateFormat.SHORT,
Locale.getDefault());
+
+ formatter.setLenient(false);
+
+ date = formatter.parse(value);
+ } catch (ParseException e) {
+ System.out.println("ValidatorTest::formatDate - " + e.getMessage());
+ }
+ return date;
+ }
+
public class TestBean {
private String letter = null;
+ private String date = null;
public String getLetter() {
return letter;
@@ -187,7 +263,14 @@
public void setLetter(String letter) {
this.letter = letter;
}
- }
+ public String getDate() {
+ return date;
+ }
+
+ public void setDate(String date) {
+ this.date = date;
+ }
+ }
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>