You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by nb...@apache.org on 2005/10/15 00:00:48 UTC
svn commit: r321229 - in
/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools:
generic/ValueParser.java view/tools/ParameterParser.java
Author: nbubna
Date: Fri Oct 14 15:00:43 2005
New Revision: 321229
URL: http://svn.apache.org/viewcvs?rev=321229&view=rev
Log:
pull generic ParameterParser code into new ValueParser tool (and enhance it a bit) to allow use of the parsing code in non-servlet environments
Added:
jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/ValueParser.java (with props)
Modified:
jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ParameterParser.java
Added: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/ValueParser.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/ValueParser.java?rev=321229&view=auto
==============================================================================
--- jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/ValueParser.java (added)
+++ jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/ValueParser.java Fri Oct 14 15:00:43 2005
@@ -0,0 +1,433 @@
+/*
+ * Copyright 2003-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.velocity.tools.generic;
+
+import java.lang.reflect.Array;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * <p>Utility class for easy parsing of String values held in a Map.</p>
+ * <p><pre>
+ * Template example(s):
+ * $parser.foo -> bar
+ * $parser.getNumber('baz') -> 12.6
+ * $parser.getInt('baz') -> 12
+ * $parser.getNumbers('foo') -> [12.6]
+ *
+ * Toolbox configuration:
+ * <tool>
+ * <key>parser</key>
+ * <class>org.apache.velocity.generic.Parser</class>
+ * </tool>
+ * </pre></p>
+ *
+ * <p>This comes in very handy when parsing parameters.</p>
+ *
+ * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
+ * @version $Revision$ $Date$
+ * @since VelocityTools 1.2
+ */
+public class ValueParser
+{
+ private Map source = null;
+
+ public ValueParser() {}
+
+ public ValueParser(Map source)
+ {
+ setSource(source);
+ }
+
+ protected void setSource(Map source)
+ {
+ this.source = source;
+ }
+
+ protected Map getSource()
+ {
+ if (source == null)
+ {
+ throw new NullPointerException("You must set a Map source for values to be parsed.");
+ }
+ return this.source;
+ }
+
+ // ----------------- public parsing methods --------------------------
+
+ /**
+ * Convenience method for checking whether a certain parameter exists.
+ *
+ * @param key the parameter's key
+ * @return <code>true</code> if a parameter exists for the specified
+ * key; otherwise, returns <code>false</code>.
+ */
+ public boolean exists(String key)
+ {
+ return (getString(key) != null);
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * @param key the parameter's key
+ * @return parameter matching the specified key or
+ * <code>null</code> if there is no matching
+ * parameter
+ */
+ public String getString(String key)
+ {
+ Object value = getSource().get(key);
+ if (value == null)
+ {
+ return null;
+ }
+
+ if (value instanceof Collection)
+ {
+ Collection values = (Collection)value;
+ if (!values.isEmpty())
+ {
+ // take the next available value
+ value = values.iterator().next();
+ }
+ }
+ else if (value.getClass().isArray())
+ {
+ if (Array.getLength(value) > 0)
+ {
+ // take the first value
+ value = Array.get(value, 0);
+ }
+ }
+ return String.valueOf(value);
+ }
+
+ /**
+ * @param key the desired parameter's key
+ * @param alternate The alternate value
+ * @return parameter matching the specified key or the
+ * specified alternate String if there is no matching
+ * parameter
+ */
+ public String getString(String key, String alternate)
+ {
+ String s = getString(key);
+ return (s != null) ? s : alternate;
+ }
+
+ /**
+ * @param key the desired parameter's key
+ * @return a {@link Boolean} object for the specified key or
+ * <code>null</code> if no matching parameter is found
+ */
+ public Boolean getBoolean(String key)
+ {
+ String s = getString(key);
+ return (s != null) ? parseBoolean(s) : null;
+ }
+
+ /**
+ * @param key the desired parameter's key
+ * @param alternate The alternate boolean value
+ * @return boolean value for the specified key or the
+ * alternate boolean is no value is found
+ */
+ public boolean getBoolean(String key, boolean alternate)
+ {
+ Boolean bool = getBoolean(key);
+ return (bool != null) ? bool.booleanValue() : alternate;
+ }
+
+ /**
+ * @param key the desired parameter's key
+ * @param alternate the alternate {@link Boolean}
+ * @return a {@link Boolean} for the specified key or the specified
+ * alternate if no matching parameter is found
+ */
+ public Boolean getBoolean(String key, Boolean alternate)
+ {
+ Boolean bool = getBoolean(key);
+ return (bool != null) ? bool : alternate;
+ }
+
+ /**
+ * @param key the desired parameter's key
+ * @return a {@link Number} for the specified key or
+ * <code>null</code> if no matching parameter is found
+ */
+ public Number getNumber(String key)
+ {
+ String s = getString(key);
+ if (s == null || s.length() == 0)
+ {
+ return null;
+ }
+ try
+ {
+ return parseNumber(s);
+ }
+ catch (Exception e)
+ {
+ //there is no Number with that key
+ return null;
+ }
+ }
+
+ /**
+ * @param key the desired parameter's key
+ * @param alternate The alternate Number
+ * @return a Number for the specified key or the specified
+ * alternate if no matching parameter is found
+ */
+ public Number getNumber(String key, Number alternate)
+ {
+ Number n = getNumber(key);
+ return (n != null) ? n : alternate;
+ }
+
+ /**
+ * @param key the desired parameter's key
+ * @param alternate The alternate int value
+ * @return the int value for the specified key or the specified
+ * alternate value if no matching parameter is found
+ */
+ public int getInt(String key, int alternate)
+ {
+ Number n = getNumber(key);
+ return (n != null) ? n.intValue() : alternate;
+ }
+
+ /**
+ * @param key the desired parameter's key
+ * @param alternate The alternate double value
+ * @return the double value for the specified key or the specified
+ * alternate value if no matching parameter is found
+ */
+ public double getDouble(String key, double alternate)
+ {
+ Number n = getNumber(key);
+ return (n != null) ? n.doubleValue() : alternate;
+ }
+
+
+ /**
+ * @param key the key for the desired parameter
+ * @return an array of String objects containing all of the values
+ * associated with the given key, or <code>null</code>
+ * if the no values are associated with the given key
+ */
+ public String[] getStrings(String key)
+ {
+ Object value = getSource().get(key);
+ if (value == null)
+ {
+ return null;
+ }
+
+ String[] strings = null;
+ if (value instanceof Collection)
+ {
+ Collection values = (Collection)value;
+ if (!values.isEmpty())
+ {
+ strings = new String[values.size()];
+ int index = 0;
+ for (Iterator i = values.iterator(); i.hasNext(); )
+ {
+ strings[index++] = String.valueOf(i.next());
+ }
+ }
+ }
+ else if (value.getClass().isArray())
+ {
+ strings = new String[Array.getLength(value)];
+ for (int i=0; i < strings.length; i++)
+ {
+ strings[i] = String.valueOf(Array.get(value, i));
+ }
+ }
+ else
+ {
+ strings = new String[] { String.valueOf(value) };
+ }
+ return strings;
+ }
+
+
+ public Boolean[] getBooleans(String key)
+ {
+ String[] strings = getStrings(key);
+ if (strings == null)
+ {
+ return null;
+ }
+
+ Boolean[] bools = new Boolean[strings.length];
+ for (int i=0; i<strings.length; i++)
+ {
+ if (strings[i] != null && strings[i].length() > 0)
+ {
+ bools[i] = parseBoolean(strings[i]);
+ }
+ }
+ return bools;
+ }
+
+ /**
+ * @param key the key for the desired parameter
+ * @return an array of Number objects associated with the given key,
+ * or <code>null</code> if Numbers are not associated with it.
+ */
+ public Number[] getNumbers(String key)
+ {
+ String[] strings = getStrings(key);
+ if (strings == null)
+ {
+ return null;
+ }
+
+ Number[] nums = new Number[strings.length];
+ try
+ {
+ for (int i=0; i<nums.length; i++)
+ {
+ if (strings[i] != null && strings[i].length() > 0)
+ {
+ nums[i] = parseNumber(strings[i]);
+ }
+ }
+ }
+ catch (NumberFormatException nfe)
+ {
+ return null;
+ }
+ return nums;
+ }
+
+ /**
+ * @param key the key for the desired parameter
+ * @return an array of int values associated with the given key,
+ * or <code>null</code> if numbers are not associated with it.
+ */
+ public int[] getInts(String key)
+ {
+ String[] strings = getStrings(key);
+ if (strings == null)
+ {
+ return null;
+ }
+
+ int[] ints = new int[strings.length];
+ try
+ {
+ for (int i=0; i<ints.length; i++)
+ {
+ if (strings[i] != null && strings[i].length() > 0)
+ {
+ ints[i] = parseNumber(strings[i]).intValue();
+ }
+ }
+ }
+ catch (NumberFormatException nfe)
+ {
+ return null;
+ }
+ return ints;
+ }
+
+ /**
+ * @param key the key for the desired parameter
+ * @return an array of double values associated with the given key,
+ * or <code>null</code> if numbers are not associated with it.
+ */
+ public double[] getDoubles(String key)
+ {
+ String[] strings = getStrings(key);
+ if (strings == null)
+ {
+ return null;
+ }
+
+ double[] doubles = new double[strings.length];
+ try
+ {
+ for (int i=0; i<doubles.length; i++)
+ {
+ if (strings[i] != null && strings[i].length() > 0)
+ {
+ doubles[i] = parseNumber(strings[i]).doubleValue();
+ }
+ }
+ }
+ catch (NumberFormatException nfe)
+ {
+ return null;
+ }
+ return doubles;
+ }
+
+
+ // --------------------------- 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);
+ }
+
+ /**
+ * Converts a parameter value into a {@link Boolean}
+ * Sub-classes can override to allow for customized boolean parsing.
+ * (e.g. to handle "Yes/No" or "T/F")
+ *
+ * @param value the string to be parsed
+ * @return the value as a {@link Boolean}
+ */
+ protected Boolean parseBoolean(String value)
+ {
+ return Boolean.valueOf(value);
+ }
+
+}
Propchange: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/ValueParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/ValueParser.java
------------------------------------------------------------------------------
svn:keywords = Id Author Date Revision
Modified: jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ParameterParser.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ParameterParser.java?rev=321229&r1=321228&r2=321229&view=diff
==============================================================================
--- jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ParameterParser.java (original)
+++ jakarta/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/ParameterParser.java Fri Oct 14 15:00:43 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2004 The Apache Software Foundation.
+ * Copyright 2003-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.
@@ -16,8 +16,11 @@
package org.apache.velocity.tools.view.tools;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Map;
import javax.servlet.ServletRequest;
-
+import org.apache.velocity.tools.generic.ValueParser;
import org.apache.velocity.tools.view.context.ViewContext;
import org.apache.velocity.tools.view.tools.ViewTool;
@@ -43,21 +46,18 @@
* or action code as well as in templates.</p>
*
* @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
- * @version $Revision: 1.9 $ $Date: 2004/03/12 20:30:32 $
+ * @version $Revision$ $Date$
*/
-public class ParameterParser implements ViewTool
+public class ParameterParser extends ValueParser implements ViewTool
{
-
private ServletRequest request;
-
/**
* Constructs a new instance
*/
public ParameterParser()
{}
-
/**
* Constructs a new instance using the specified request.
*
@@ -67,8 +67,7 @@
{
setRequest(request);
}
-
-
+
/**
* Initializes this instance.
*
@@ -93,7 +92,6 @@
}
}
-
/**
* Sets the current {@link ServletRequest}
*
@@ -104,7 +102,6 @@
this.request = request;
}
-
/**
* Returns the current {@link ServletRequest} for this instance.
*
@@ -120,41 +117,11 @@
return request;
}
-
- // ----------------- public parsing methods --------------------------
-
/**
- * Convenience method for checking whether a certain parameter exists.
+ * Overrides ValueParser.getString(String key) to retrieve the
+ * String from the ServletRequest instead of an arbitrary Map.
*
* @param key the parameter's key
- * @return <code>true</code> if a parameter exists for the specified
- * key; otherwise, returns <code>false</code>.
- */
- public boolean exists(String key)
- {
- return (getString(key) != null);
- }
-
-
- /**
- * 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);
- }
-
-
- /**
- * @param key the parameter's key
* @return parameter matching the specified key or
* <code>null</code> if there is no matching
* parameter
@@ -166,121 +133,9 @@
/**
- * @param key the desired parameter's key
- * @param alternate The alternate value
- * @return parameter matching the specified key or the
- * specified alternate String if there is no matching
- * parameter
- */
- public String getString(String key, String alternate)
- {
- String s = getString(key);
- return (s != null) ? s : alternate;
- }
-
-
- /**
- * @param key the desired parameter's key
- * @return a {@link Boolean} object for the specified key or
- * <code>null</code> if no matching parameter is found
- */
- public Boolean getBoolean(String key)
- {
- String s = getString(key);
- return (s != null) ? Boolean.valueOf(s) : null;
- }
-
-
- /**
- * @param key the desired parameter's key
- * @param alternate The alternate boolean value
- * @return boolean value for the specified key or the
- * alternate boolean is no value is found
- */
- public boolean getBoolean(String key, boolean alternate)
- {
- Boolean bool = getBoolean(key);
- return (bool != null) ? bool.booleanValue() : alternate;
- }
-
-
- /**
- * @param key the desired parameter's key
- * @param alternate the alternate {@link Boolean}
- * @return a {@link Boolean} for the specified key or the specified
- * alternate if no matching parameter is found
- */
- public Boolean getBoolean(String key, Boolean alternate)
- {
- Boolean bool = getBoolean(key);
- return (bool != null) ? bool : alternate;
- }
-
-
- /**
- * @param key the desired parameter's key
- * @return a {@link Number} for the specified key or
- * <code>null</code> if no matching parameter is found
- */
- public Number getNumber(String key)
- {
- String s = getString(key);
- if (s == null || s.length() == 0)
- {
- return null;
- }
- try
- {
- return parseNumber(s);
- }
- catch (Exception e)
- {
- //there is no Number with that key
- return null;
- }
- }
-
-
- /**
- * @param key the desired parameter's key
- * @param alternate The alternate Number
- * @return a Number for the specified key or the specified
- * alternate if no matching parameter is found
- */
- public Number getNumber(String key, Number alternate)
- {
- Number n = getNumber(key);
- return (n != null) ? n : alternate;
- }
-
-
- /**
- * @param key the desired parameter's key
- * @param alternate The alternate int value
- * @return the int value for the specified key or the specified
- * alternate value if no matching parameter is found
- */
- public int getInt(String key, int alternate)
- {
- Number n = getNumber(key);
- return (n != null) ? n.intValue() : alternate;
- }
-
-
- /**
- * @param key the desired parameter's key
- * @param alternate The alternate double value
- * @return the double value for the specified key or the specified
- * alternate value if no matching parameter is found
- */
- public double getDouble(String key, double alternate)
- {
- Number n = getNumber(key);
- return (n != null) ? n.doubleValue() : alternate;
- }
-
-
- /**
+ * Overrides ValueParser.getString(String key) to retrieve
+ * Strings from the ServletRequest instead of an arbitrary Map.
+ *
* @param key the key for the desired parameter
* @return an array of String objects containing all of the values
* the given request parameter has, or <code>null</code>
@@ -291,124 +146,43 @@
return getRequest().getParameterValues(key);
}
-
/**
- * @param key the key for the desired parameter
- * @return an array of Number objects for the specified key or
- * <code>null</code> if the parameter does not exist or the
- * parameter does not contain Numbers.
+ * Overrides ValueParser.setSource(Map source) to throw an
+ * UnsupportedOperationException, because this class uses
+ * a servlet request as its source, not a Map.
*/
- public Number[] getNumbers(String key)
+ protected void setSource(Map source)
{
- String[] strings = getStrings(key);
- if (strings == null)
- {
- return null;
- }
-
- Number[] nums = new Number[strings.length];
- try
- {
- for(int i=0; i<nums.length; i++)
- {
- if (strings[i] != null && strings[i].length() > 0)
- {
- nums[i] = parseNumber(strings[i]);
- }
- }
- }
- catch (NumberFormatException nfe)
- {
- return null;
- }
- return nums;
+ throw new UnsupportedOperationException();
}
-
/**
- * @param key the key for the desired parameter
- * @return an array of int values for the specified key or
- * <code>null</code> if the parameter does not exist or the
- * parameter does not contain ints.
+ * Overrides ValueParser.getSource() to return the result
+ * of getRequest().getParameterMap() if Servlet 2.3 or above
+ * is being used. Otherwise, this throws an
+ * UnsupportedOperationException, because the class uses a
+ * servlet request as its source, not a Map.
*/
- public int[] getInts(String key)
+ protected Map getSource()
{
- String[] strings = getStrings(key);
- if (strings == null)
- {
- return null;
- }
-
- int[] ints = new int[strings.length];
try
{
- for(int i=0; i<ints.length; i++)
- {
- if (strings[i] != null && strings[i].length() > 0)
- {
- ints[i] = parseNumber(strings[i]).intValue();
- }
- }
+ // use reflection so we can compile against Servlet 2.2
+ Method getmap = ServletRequest.class.getMethod("getParameterMap", null);
+ return (Map)getmap.invoke(getRequest(), null);
}
- catch (NumberFormatException nfe)
+ catch (NoSuchMethodException nsme)
{
- return null;
+ throw new UnsupportedOperationException("This method is only supported with Servlet 2.3 and higher.");
}
- return ints;
- }
-
-
- /**
- * @param key the key for the desired parameter
- * @return an array of double values for the specified key or
- * <code>null</code> if the parameter does not exist or the
- * parameter does not contain doubles.
- */
- public double[] getDoubles(String key)
- {
- String[] strings = getStrings(key);
- if (strings == null)
+ catch (IllegalAccessException iae)
{
- return null;
+ throw new UnsupportedOperationException("ServletRequest.getParameterMap() is restricted - " + iae);
}
-
- double[] doubles = new double[strings.length];
- try
+ catch (InvocationTargetException ite)
{
- for(int i=0; i<doubles.length; i++)
- {
- if (strings[i] != null && strings[i].length() > 0)
- {
- doubles[i] = parseNumber(strings[i]).doubleValue();
- }
- }
+ throw new UnsupportedOperationException("ServletRequest.getParameterMap() threw an exception - " + ite);
}
- catch (NumberFormatException nfe)
- {
- return null;
- }
- return doubles;
}
-
- // --------------------------- 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: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org