KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.net;
2
3 import java.util.*;
4
5 import javax.naming.CompositeName JavaDoc;
6 import javax.naming.Name JavaDoc;
7 import javax.naming.NameParser JavaDoc;
8 import javax.naming.NamingException JavaDoc;
9
10
11 /**
12  * Parses query strings to object representations. It can be used to
13  * create <code>QueryString</code> or <code>Name</code> instances.
14  * <p>
15  * From this class' point of view, a query string/name is made of paths
16  * delimited by '/'; it can optionally be followed by a list of name-value
17  * pairs. The following is a valid query string:
18  * <p>
19  * <code>some/object?prop1=value1&prop2=value2</code>...
20  *
21  * @see org.sapia.ubik.net.QueryString
22  *
23  * @author Yanick Duchesne
24  * <dl>
25  * <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>
26  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
27  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
28  * </dl>
29  */

30 public class QueryStringParser implements NameParser JavaDoc {
31   static final char QMARK = '?';
32   static final String JavaDoc AMP = "&";
33   static final char EQ = '=';
34   static final String JavaDoc SLASH = "/";
35
36   /**
37    * Constructor for QueryStringParser.
38    */

39   public QueryStringParser() {
40     super();
41   }
42
43   /**
44    * Parses the given name string into a <code>QueryString</code> instance.
45    *
46    * @return a <code>QueryString</code>.
47    */

48   public QueryString parseQueryString(String JavaDoc string) {
49     QueryString qs = new QueryString();
50
51     if ((string == null) || (string.length() == 0)) {
52       return qs;
53     }
54
55     parseName(qs, string);
56
57     return qs;
58   }
59
60   /**
61    * Parses the given string into an enumeration of paths.
62    *
63    * @param queryString a query string.
64    *
65    * @return an <code>Enumeration</code> containing the separate paths
66    * that make the given query string; if the given name has trailing
67    * name-value pairs has attributes, the latter are concatenated to the
68    * last path. Thus, the following string:
69    * <code>some/object?prop1=value1&prop2=value2</code> would have
70    * <code>object?prop1=value1&prop2=value2</code> as its last path string.
71    */

72   public Enumeration parseNameTokens(String JavaDoc queryString) {
73     Vector tokens = new Vector();
74     QueryString qs = new QueryString();
75
76     parseName(qs, queryString);
77
78     StringTokenizer st = new StringTokenizer(qs.getPath(), SLASH);
79
80     while (st.hasMoreTokens()) {
81       tokens.add(st.nextToken());
82     }
83
84     if (qs.getParameters().size() > 0) {
85       StringBuffer JavaDoc buf = new StringBuffer JavaDoc((String JavaDoc) tokens.lastElement());
86       Map.Entry entry;
87       buf.append(QueryStringParser.QMARK);
88
89       Iterator itr = qs.getParameters().entrySet().iterator();
90       int count = 0;
91
92       while (itr.hasNext()) {
93         entry = (Map.Entry) itr.next();
94
95         if (count > 0) {
96           buf.append(QueryStringParser.AMP);
97         }
98
99         buf.append(entry.getKey().toString()).append(QueryStringParser.EQ)
100            .append(entry.getValue());
101         count++;
102       }
103
104       tokens.set(tokens.size() - 1, buf.toString());
105     }
106
107     return tokens.elements();
108   }
109
110   /**
111    * Parses the given query string into a <code>javax.naming.Name</code> instance.
112    * The query string's parameters are part of the last element in the returned
113    * name object.
114    *
115    * @return a <code>Name</code>.
116    * @see #parseNameTokens(String)
117    * @see javax.naming.NameParser#parse(String)
118    */

119   public Name JavaDoc parse(String JavaDoc queryString) throws NamingException JavaDoc {
120     return new CompositeNameEx(parseNameTokens(queryString));
121   }
122
123   /**
124    * Returns the string form of a <code>Name</code>.
125    *
126    * @return a name - as a string.
127    */

128   public String JavaDoc nameToString(Name JavaDoc n) {
129     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
130
131     for (int i = 0; i < n.size(); i++) {
132       buf.append(n.get(i));
133
134       if (i < (n.size() - 1)) {
135         buf.append(SLASH);
136       }
137     }
138
139     return buf.toString();
140   }
141
142   /* takes name?prop1=val1&prop2=val2&prop3=val3 */
143   private void parseName(QueryString qs, String JavaDoc name) {
144     int idx = name.indexOf(QMARK);
145
146     if (idx < 0) {
147       qs.setPath(name);
148
149       return;
150     }
151
152     qs.setPath(name.substring(0, idx));
153
154     if (idx == (name.length() - 1)) {
155       return;
156     }
157
158     parseProperties(qs, name.substring(idx + 1));
159   }
160
161   /* takes prop1=val1&prop2=val2&prop3=val3 */
162   private void parseProperties(QueryString tn, String JavaDoc props) {
163     StringTokenizer st = new StringTokenizer(props, AMP);
164     String JavaDoc token;
165
166     while (st.hasMoreTokens()) {
167       token = st.nextToken();
168       parseProperty(tn, token);
169     }
170   }
171
172   /* takes prop1=val1 */
173   private void parseProperty(QueryString qs, String JavaDoc prop) {
174     String JavaDoc name = null;
175     String JavaDoc value = null;
176
177     int idx = prop.indexOf(EQ);
178
179     if (idx < 0) {
180       if (prop.length() > 0) {
181         name = prop;
182       }
183     } else {
184       name = prop.substring(0, idx);
185
186       if (idx != (prop.length() - 1)) {
187         value = prop.substring(idx + 1);
188       }
189     }
190
191     if ((name != null) && (value != null)) {
192       qs.addParameter(name, value);
193     }
194   }
195
196   /*//////////////////////////////////////////////////
197                      INNER CLASSES
198   //////////////////////////////////////////////////*/

199   static final class CompositeNameEx extends CompositeName JavaDoc {
200     protected CompositeNameEx(Enumeration en) {
201       super(en);
202     }
203   }
204 }
205
Popular Tags