sidler 2002/07/31 06:24:49
Modified: tools/src/java/org/apache/velocity/tools/tools
ParameterParser.java
Log:
Commited lasted version of ParameterParser contributed by Nathan Bubna. Changes are:
- adds a get(String) convenience method per Claude's suggestion
- factors all number parsing into one protected method to allow subclasses to
easily override it and provide more advanced parsing as needed (this is a
need i have)
Revision Changes Path
1.5 +44 -18
jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/ParameterParser.java
Index: ParameterParser.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity-tools/tools/src/java/org/apache/velocity/tools/tools/ParameterParser.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ParameterParser.java 29 Jul 2002 09:06:15 -0000 1.4
+++ ParameterParser.java 31 Jul 2002 13:24:48 -0000 1.5
@@ -152,7 +152,23 @@
}
- // ----------------- parsing methods -----------------------------
+ // ----------------- public parsing methods --------------------------
+
+
+ /**
+ * Convenience method for use in Velocity templates.
+ * This allows for easy "dot" access to parameters.
+ *
+ * e.g. $params.foo instead of $params.getString('foo')
+ *
+ * @param key the parameter's key
+ * @return parameter matching the specified key or
+ * <code>null</code> if there is no matching
+ * parameter
+ */
+ public String get(String key) {
+ return getString(key);
+ }
/**
@@ -233,11 +249,7 @@
}
try
{
- if (s.indexOf('.') >= 0)
- {
- return new Double(s);
- }
- return new Long(s);
+ return parseNumber(s);
}
catch (Exception e)
{
@@ -319,14 +331,7 @@
{
if (strings[i] != null && strings[i].length() > 0)
{
- if (strings[i].indexOf('.') >= 0)
- {
- nums[i] = new Double(strings[i]);
- }
- else
- {
- nums[i] = new Long(strings[i]);
- }
+ nums[i] = parseNumber(strings[i]);
}
}
}
@@ -359,7 +364,7 @@
{
if (strings[i] != null && strings[i].length() > 0)
{
- ints[i] = Integer.parseInt(strings[i]);
+ ints[i] = parseNumber(strings[i]).intValue();
}
}
}
@@ -392,7 +397,7 @@
{
if (strings[i] != null && strings[i].length() > 0)
{
- doubles[i] = Double.parseDouble(strings[i]);
+ doubles[i] = parseNumber(strings[i]).doubleValue();
}
}
}
@@ -404,4 +409,25 @@
}
-}
\ No newline at end of file
+ // --------------------------- protected methods ------------------
+
+
+ /**
+ * Converts a parameter value into a {@link Number}
+ * This is used as the base for all numeric parsing methods. So,
+ * sub-classes can override to allow for customized number parsing.
+ * (e.g. to handle fractions, compound numbers, etc.)
+ *
+ * @param value the string to be parsed
+ * @return the value as a {@link Number}
+ */
+ protected Number parseNumber(String value) throws NumberFormatException {
+ if (value.indexOf('.') >= 0)
+ {
+ return new Double(value);
+ }
+ return new Long(value);
+ }
+
+
+}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>