You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jt...@apache.org on 2002/04/16 21:56:41 UTC
cvs commit: jakarta-turbine-3/src/java/org/apache/turbine DynamicURI.java RelativeDynamicURI.java
jtaylor 02/04/16 12:56:41
Modified: src/java/org/apache/turbine DynamicURI.java
RelativeDynamicURI.java
Log:
Integrating more of the changes suggested by Stephane Bailliez with my own.
Also, removed use of the PATH_INFO/QUERY_DATA constants where possible -- that
can usually be determined at compile time, no need for all those case
statements.
Revision Changes Path
1.8 +190 -131 jakarta-turbine-3/src/java/org/apache/turbine/DynamicURI.java
Index: DynamicURI.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-3/src/java/org/apache/turbine/DynamicURI.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DynamicURI.java 16 Apr 2002 18:06:07 -0000 1.7
+++ DynamicURI.java 16 Apr 2002 19:56:41 -0000 1.8
@@ -54,9 +54,9 @@
* <http://www.apache.org/>.
*/
-import java.util.BitSet;
import java.util.Enumeration;
import java.util.Vector;
+import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -82,7 +82,7 @@
* @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
* @author <a href="mailto:jmcnally@collab.net">John McNally</a>
* @author <a href="mailto:james@jamestaylor.org">James Taylor</a>
- * @version $Id: DynamicURI.java,v 1.7 2002/04/16 18:06:07 jtaylor Exp $
+ * @version $Id: DynamicURI.java,v 1.8 2002/04/16 19:56:41 jtaylor Exp $
*/
public class DynamicURI
{
@@ -92,6 +92,10 @@
/** HTTPS protocol. */
public static final String HTTPS = "https";
+ /** Length of static part of an A tag */
+ public static final int ANCHOR_STATIC_PART_LENGTH
+ = "<a href=\"\"></a>".length();
+
/** The RunData object. */
protected RunData data = null;
@@ -99,22 +103,10 @@
public HttpServletResponse res = null;
/** A Vector that contains all the path info if any. */
- protected Vector pathInfo = null;
+ protected ArrayList pathInfo = new ArrayList();
/** A Vectory that contains all the query data if any. */
- protected Vector queryData = null;
-
- /**
- * Fast shortcut to determine if there is any data in the path
- * info.
- */
- protected boolean hasPathInfo = false;
-
- /**
- * Fast shortcut to determine if there is any data in the query
- * data.
- */
- protected boolean hasQueryData = false;
+ protected ArrayList queryData = new ArrayList();
/** Whether we want to redirect or not. */
protected boolean redirect = false;
@@ -253,41 +245,55 @@
String value)
{
Object[] tmp = new Object[2];
- tmp[0] = (Object) data.getParameters().convertAndTrim(name);
- tmp[1] = (Object) value;
- switch (type)
- {
- case PATH_INFO:
- this.pathInfo.addElement(tmp);
- this.hasPathInfo = true;
- break;
- case QUERY_DATA:
- this.queryData.addElement(tmp);
- this.hasQueryData = true;
- break;
+
+ tmp[0] = data.getParameters().convertAndTrim(name);
+ tmp[1] = value;
+
+ if ( type == PATH_INFO )
+ {
+ this.pathInfo.add(tmp);
+ }
+ else if ( type == QUERY_DATA )
+ {
+ this.queryData.add(tmp);
}
}
/**
- * Method for a quick way to add all the parameters in a
- * ParameterParser.
- *
- * <p>If the type is P (0), then add name/value to the pathInfo
- * hashtable.
+ * Add a key value pair (in the form of a 2 object array) to the provided
+ * list
*
- * <p>If the type is Q (1), then add name/value to the queryData
- * hashtable.
+ * @param list List to add to.
+ * @param name A String with the name to add.
+ * @param value A String with the value to add.
+ */
+ protected void addPair(ArrayList list,
+ String name,
+ String value)
+ {
+ Object[] tmp = new Object[2];
+
+ tmp[0] = data.getParameters().convertAndTrim(name);
+ tmp[1] = value;
+
+ list.add(tmp);
+ }
+
+ /**
+ * Method for a quick way to add all the parameters in a
+ * ParameterParser to a given List
*
- * @param type Type (P or Q) of insertion.
+ * @param list The list of pairs to add to
* @param pp A ParameterParser.
*/
- protected void add(int type,
+ protected void add(ArrayList list,
ParameterParser pp)
{
Enumeration e = pp.keys();
while (e.hasMoreElements())
{
String key = (String) e.nextElement();
+
if (!key.equalsIgnoreCase(Turbine.ACTION) &&
!key.equalsIgnoreCase(Turbine.SCREEN) &&
!key.equalsIgnoreCase(Turbine.TEMPLATE))
@@ -295,13 +301,39 @@
String[] values = pp.getStrings(key);
for (int i = 0; i < values.length; i++)
{
- add(type, key, values[i]);
+ addPair(list, key, values[i]);
}
}
}
}
/**
+ * Method for a quick way to add all the parameters in a
+ * ParameterParser.
+ *
+ * <p>If the type is P (0), then add name/value to the pathInfo
+ * hashtable.
+ *
+ * <p>If the type is Q (1), then add name/value to the queryData
+ * hashtable.
+ *
+ * @param type Type (P or Q) of insertion.
+ * @param pp A ParameterParser.
+ */
+ protected void add(int type,
+ ParameterParser pp)
+ {
+ if ( type == PATH_INFO )
+ {
+ add( pathInfo, pp);
+ }
+ else if ( type == QUERY_DATA )
+ {
+ add( queryData, pp);
+ }
+ }
+
+ /**
* Adds a name=value pair to the path_info string.
*
* @param name A String with the name to add.
@@ -309,7 +341,7 @@
*/
public DynamicURI addPathInfo(String name, Object value)
{
- add(PATH_INFO, name, value.toString());
+ addPathInfo(name, value.toString());
return this;
}
@@ -321,7 +353,7 @@
*/
public DynamicURI addPathInfo(String name, String value)
{
- add(PATH_INFO, name, value);
+ addPair( pathInfo, name, value);
return this;
}
@@ -333,7 +365,7 @@
*/
public DynamicURI addPathInfo(String name, double value)
{
- add(PATH_INFO, name, Double.toString(value));
+ addPathInfo(name, Double.toString(value));
return this;
}
@@ -345,7 +377,7 @@
*/
public DynamicURI addPathInfo(String name, int value)
{
- add(PATH_INFO, name, new Integer(value).toString());
+ addPathInfo(name, new Integer(value).toString());
return this;
}
@@ -357,7 +389,7 @@
*/
public DynamicURI addPathInfo(String name, long value)
{
- add(PATH_INFO, name, new Long(value).toString());
+ addPathInfo(name, new Long(value).toString());
return this;
}
@@ -369,7 +401,7 @@
*/
public DynamicURI addPathInfo(ParameterParser pp)
{
- add(PATH_INFO, pp);
+ add(pathInfo, pp);
return this;
}
@@ -381,7 +413,7 @@
*/
public DynamicURI addQueryData(String name, Object value)
{
- add(QUERY_DATA, name, value.toString());
+ addQueryData(name, value.toString());
return this;
}
@@ -393,7 +425,7 @@
*/
public DynamicURI addQueryData(String name, String value)
{
- add(QUERY_DATA, name, value);
+ addPair( queryData, name, value);
return this;
}
@@ -405,7 +437,7 @@
*/
public DynamicURI addQueryData(String name, double value)
{
- add(QUERY_DATA, name, Double.toString(value));
+ addQueryData(name, Double.toString(value));
return this;
}
@@ -417,7 +449,7 @@
*/
public DynamicURI addQueryData(String name, int value)
{
- add(QUERY_DATA, name, new Integer(value).toString());
+ addQueryData(name, new Integer(value).toString());
return this;
}
@@ -429,7 +461,7 @@
*/
public DynamicURI addQueryData(String name, long value)
{
- add(QUERY_DATA, name, new Long(value).toString());
+ addQueryData(name, new Long(value).toString());
return this;
}
@@ -441,7 +473,7 @@
*/
public DynamicURI addQueryData(ParameterParser pp)
{
- add(QUERY_DATA, pp);
+ add(queryData, pp);
return this;
}
@@ -463,13 +495,20 @@
*/
public String getA(String name)
{
- return new StringBuffer("<a href=\"")
- .append(this.toString())
- .append("\">")
- .append(name)
- .append("</a>")
- .toString();
- //return new A(this.toString(), name).toString();
+ final String s = this.toString();
+
+ // I'm being a pit picky about size here to avoid useless
+ // StringBuffer reallocation
+
+ final int size = ANCHOR_STATIC_PART_LENGTH + s.length() + name.length();
+
+ return new StringBuffer( size )
+ .append("<a href=\"")
+ .append(s)
+ .append("\">")
+ .append(name)
+ .append("</a>")
+ .toString();
}
/**
@@ -517,8 +556,8 @@
*/
protected void init()
{
- this.pathInfo = new Vector();
- this.queryData = new Vector();
+ this.pathInfo.clear();
+ this.queryData.clear();
}
/**
@@ -536,40 +575,13 @@
{
try
{
- switch (type)
+ if ( type == PATH_INFO )
+ {
+ removePathInfo( name );
+ }
+ else if ( type == QUERY_DATA )
{
- case PATH_INFO:
- for (Enumeration e = this.pathInfo.elements();
- e.hasMoreElements();)
- {
- Object[] tmp = (Object[]) e.nextElement();
- if (data.getParameters().convertAndTrim(name)
- .equals((String) tmp[0]))
- {
- this.pathInfo.removeElement(tmp);
- }
- }
- if (hasPathInfo && this.pathInfo.size() == 0)
- {
- this.hasPathInfo = false;
- }
- break;
- case QUERY_DATA:
- for (Enumeration e = this.queryData.elements();
- e.hasMoreElements();)
- {
- Object[] tmp = (Object[]) e.nextElement();
- if (data.getParameters().convertAndTrim(name)
- .equals((String) tmp[0]))
- {
- this.queryData.removeElement(tmp);
- }
- }
- if (hasQueryData && this.queryData.size() == 0)
- {
- this.hasQueryData = false;
- }
- break;
+ removeQueryData( name );
}
}
catch (Exception e)
@@ -578,12 +590,32 @@
}
/**
+ * Helper method to remove one or more pairs by its name (ie key).
+ * It is intended to be used with <tt>queryData</tt> and <tt>pathInfo</tt>.
+ * @param pairs the list of pairs to look over for removal.
+ * @param name the name of the pair(s) to remove.
+ */
+ protected void removePairByName(ArrayList pairs, String name)
+ {
+ name = data.getParameters().convertAndTrim(name);
+ // CAUTION: the dynamic evaluation of the size is on purpose because
+ // elements may be removed on the fly.
+ for (int i = 0; i < pairs.size(); i++)
+ {
+ Object[] pair = (Object[])pairs.get(i);
+ if ( name.equals( (String)pair[0] ) )
+ {
+ pairs.remove(i);
+ }
+ }
+ }
+
+ /**
* Removes all the path info elements.
*/
public void removePathInfo()
{
- this.pathInfo.removeAllElements();
- this.hasPathInfo = false;
+ this.pathInfo.clear();
}
/**
@@ -593,7 +625,7 @@
*/
public void removePathInfo(String name)
{
- remove(PATH_INFO, name);
+ removePairByName( pathInfo, name );;
}
/**
@@ -601,8 +633,7 @@
*/
public void removeQueryData()
{
- this.queryData.removeAllElements();
- this.hasQueryData = false;
+ this.queryData.clear();
}
/**
@@ -612,7 +643,7 @@
*/
public void removeQueryData(String name)
{
- remove(QUERY_DATA, name);
+ removePairByName( queryData, name );
}
/**
@@ -621,12 +652,13 @@
*
* @param data A Vector of key/value arrays.
* @return A String with the URL encoded data.
+ * @deprecated Prefer the ArrayList / StringBuffer form.
*/
protected String renderPathInfo(Vector data)
{
StringBuffer out = new StringBuffer();
- renderPathInfo(data, out);
+ renderPathInfo(new ArrayList(data), out);
return out.toString();
}
@@ -635,26 +667,12 @@
* This method takes a Vector of key/value arrays and writes it to the
* supplied StringBuffer as encoded path info.
*
- * @param data A Vector of key/value arrays.
+ * @param pairs A Vector of key/value arrays.
* @param out Buffer to which encoded path info is written
*/
- protected void renderPathInfo(Vector data, StringBuffer out)
+ protected void renderPathInfo(ArrayList pairs, StringBuffer out)
{
- Enumeration keys = data.elements();
-
- while (keys.hasMoreElements())
- {
- Object[] stuff = (Object[]) keys.nextElement();
-
- if (out.length() > 0)
- {
- out.append("/");
- }
-
- writeEncoded((String) stuff[0], out);
- out.append("/");
- writeEncoded((String) stuff[1], out);
- }
+ renderPairs( pairs, out, '/', '/' );
}
/**
@@ -663,39 +681,63 @@
*
* @param data A Vector of key/value arrays.
* @return A String with the URL encoded data.
+ * @deprecated Prefer the ArrayList / StringBuffer form.
*/
protected String renderQueryString(Vector data)
{
StringBuffer out = new StringBuffer();
- renderQueryString(data, out);
+ renderQueryString(new ArrayList(data), out);
return out.toString();
}
/**
- * This method takes a Vector of key/value arrays and writes it to the
+ * This method takes a List of key/value arrays and writes it to the
* provided StringBuffer in encoded query string format.
*
* @param data A Vector of key/value arrays.
* @param out Buffer to which encoded query string is written.
*/
- protected void renderQueryString(Vector data, StringBuffer out)
+ protected void renderQueryString(ArrayList data, StringBuffer out)
+ {
+ renderPairs( data, out, '&', '=' );
+ }
+
+ /**
+ * This method takes a List of key/value arrays and converts it
+ * into a URL encoded key/value pair format with the appropriate
+ * separator.
+ *
+ * @param out the buffer to write the pairs to.
+ * @param pairs A List of key/value arrays.
+ * @param pairSep the character to use as a separator between pairs.
+ * For example for a query-like rendering it would be '&'.
+ * @param keyValSep the character to use as a separator between
+ * key and value. For example for a query-like rendering, it would be '='.
+ */
+ protected void renderPairs(ArrayList pairs, StringBuffer out,
+ char pairSep, char keyValSep)
{
- Enumeration keys = data.elements();
+ boolean first = true;
- while (keys.hasMoreElements())
+ final int count = pairs.size();
+ for (int i = 0; i < count; i++)
{
- Object[] stuff = (Object[]) keys.nextElement();
+ Object[] pair = (Object[]) pairs.get(i);
- if (out.length() > 0)
+ if ( first )
+ {
+ first = false;
+ }
+ else
{
- out.append("&");
+ out.append(pairSep);
}
- writeEncoded((String) stuff[0], out);
- out.append("=");
- writeEncoded((String) stuff[1], out);
+ writeEncoded((String) pair[0], out);
+ out.append(keyValSep);
+ writeEncoded((String) pair[1], out);
}
}
@@ -710,7 +752,7 @@
*/
public DynamicURI setAction(String action)
{
- add(PATH_INFO, Turbine.ACTION, action);
+ addPathInfo(Turbine.ACTION, action);
return this;
}
@@ -725,7 +767,7 @@
*/
public DynamicURI setScreen(String screen)
{
- add(PATH_INFO, Turbine.SCREEN, screen);
+ addPathInfo(Turbine.SCREEN, screen);
return this;
}
@@ -844,12 +886,12 @@
output.append(getScriptName());
- if (this.hasPathInfo)
+ if (this.hasPathInfo())
{
output.append('/');
renderPathInfo(this.pathInfo, output);
}
- if (this.hasQueryData)
+ if (this.hasQueryData())
{
output.append('?');
renderQueryString(this.queryData, output);
@@ -979,6 +1021,23 @@
}
}
}
+
+ /**
+ * Does this URI have path info.
+ */
+ public boolean hasPathInfo()
+ {
+ return ! pathInfo.isEmpty();
+ }
+
+ /**
+ * Does this URI have query data.
+ */
+ public boolean hasQueryData()
+ {
+ return ! queryData.isEmpty();
+ }
+
// ------------------------------------- private constants for url encoding
1.5 +10 -8 jakarta-turbine-3/src/java/org/apache/turbine/RelativeDynamicURI.java
Index: RelativeDynamicURI.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-3/src/java/org/apache/turbine/RelativeDynamicURI.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- RelativeDynamicURI.java 5 Mar 2002 01:32:34 -0000 1.4
+++ RelativeDynamicURI.java 16 Apr 2002 19:56:41 -0000 1.5
@@ -54,9 +54,7 @@
* <http://www.apache.org/>.
*/
-import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
import org.apache.turbine.RunData;
/**
@@ -135,17 +133,17 @@
*/
public String toString()
{
- StringBuffer output = new StringBuffer();
+ StringBuffer output = new StringBuffer(128);
output.append ( getScriptName() );
- if ( this.hasPathInfo )
+ if ( this.hasPathInfo() )
{
output.append ( "/" );
- output.append ( renderPathInfo(this.pathInfo) );
+ renderPathInfo(this.pathInfo, output);
}
- if ( this.hasQueryData )
+ if ( this.hasQueryData() )
{
output.append ( "?" );
- output.append ( renderQueryString(this.queryData) );
+ renderQueryString(this.queryData, output);
}
// There seems to be a bug in Apache JServ 1.0 where the
@@ -154,9 +152,13 @@
if ( this.res != null )
{
if ( this.redirect )
+ {
return res.encodeRedirectURL (output.toString());
+ }
else
+ {
return res.encodeURL (output.toString());
+ }
}
else
{
@@ -176,7 +178,7 @@
*/
public static String toString(RunData data)
{
- StringBuffer output = new StringBuffer();
+ StringBuffer output = new StringBuffer(128);
HttpServletRequest request = data.getRequest();
output.append ( data.getScriptName() );
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>