KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > QueryString


1 package org.sapia.ubik.net;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7
8 /**
9  * This class models a query string. A query string has a format similar
10  * to the following:
11  *
12  * <pre>
13  * /some/path?name1=value1&name2=value2
14  * </pre>
15  *
16  * @author Yanick Duchesne
17  * <dl>
18  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class QueryString {
24   private String JavaDoc _path = "/";
25   private Map JavaDoc _properties = new HashMap JavaDoc();
26
27   /**
28    * Constructor for QueryString.
29    */

30   QueryString() {
31   }
32
33   /**
34    * Constructor for QueryString.
35    *
36    * This constructor takes the path of the query string.
37    *
38    * @param a path
39    */

40   public QueryString(String JavaDoc path) {
41     _path = path;
42   }
43
44   /**
45    * Returns this instance's path.
46    *
47    * @return a path.
48    */

49   public String JavaDoc getPath() {
50     return _path;
51   }
52
53   /**
54    * Returns this instance's parameters.
55    *
56    * @return a <code>Map</code> containing name/value pairs.
57    */

58   public Map JavaDoc getParameters() {
59     return _properties;
60   }
61
62   /**
63    * Adds the passed in name/value pair as parameter.
64    *
65    * @param name an object attribute name
66    * @param value an object attribute value.
67    */

68   public void addParameter(String JavaDoc name, String JavaDoc value) {
69     _properties.put(name, value);
70   }
71
72   /**
73    * Returns the value for the parameter with the passed
74    * in name.
75    *
76    * @param the name of the parameter whose value should be returned.
77    * @return the value of the given parameter, or <code>null</code>
78    * if no such value exists.
79    */

80   public String JavaDoc getParameter(String JavaDoc name) {
81     return (String JavaDoc) _properties.get(name);
82   }
83
84   /**
85    * Sets this instance's path.
86    *
87    * @param path a path.
88    */

89   void setPath(String JavaDoc path) {
90     _path = path;
91   }
92
93   /**
94    * Parses a query string and returns its object representation.
95    *
96    * @return a <code>QueryString</code>
97    */

98   public static QueryString parse(String JavaDoc queryStr) {
99     QueryStringParser p = new QueryStringParser();
100
101     return p.parseQueryString(queryStr);
102   }
103
104   public String JavaDoc toString() {
105     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(_path);
106
107     if (_properties.size() > 0) {
108       Map.Entry JavaDoc entry;
109       buf.append(QueryStringParser.QMARK);
110
111       Iterator JavaDoc itr = _properties.entrySet().iterator();
112       int count = 0;
113
114       while (itr.hasNext()) {
115         entry = (Map.Entry JavaDoc) itr.next();
116
117         if (count > 0) {
118           buf.append(QueryStringParser.AMP);
119         }
120
121         buf.append(entry.getKey().toString()).append(QueryStringParser.EQ)
122            .append(entry.getValue());
123         count++;
124       }
125     }
126
127     return buf.toString();
128   }
129 }
130
Popular Tags