KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > urls > QueryMapURL


1 package org.jahia.urls;
2
3 import java.util.Map JavaDoc;
4 import java.util.StringTokenizer JavaDoc;
5 import org.jahia.utils.*;
6
7 /**
8  * <p>Title: Basic class to generate URL in Jahia.</p>
9  * <p>Description: This class is used to offer tools to manipulate an URL in
10  * a fashion much more flexible than Java's URL class. We can here operate
11  * on individual query string parameters. You may also sub-class this class
12  * in order to build other URL types such as Jahia Engine URLs, etc...</p>
13  * <p>This class is really the first shot at something like this in Jahia so
14  * it might change in the future to accomodate more needs. But the basic idea
15  * is to sub-class this class rather than put everything including the kitchen
16  * sink in here.</p>
17  * <p>Copyright: Copyright (c) 2002</p>
18  * <p>Company: Jahia Ltd</p>
19  * @author Serge Huber
20  * @version 1.0
21  */

22
23 public class QueryMapURL extends URI {
24
25     protected InsertionSortedMap queryParameters = new InsertionSortedMap();
26
27     /**
28      * Empty constructor. This allows this class to be JavaBean compliant.
29      * If you use this constructor, don't forget to use one of the set*URL
30      * methods below of the object will not be initialized properly.
31      */

32     public QueryMapURL() {
33     }
34
35     /**
36      * Constructs an URL based on a string form of an URL. If the relative URL
37      * starts with a "/" character, this will be recorded in the internal state
38      * of the URL and when the URL will be converted back to String
39      * representation it will start at the path, not as a completely fully
40      * qualified URL.
41      *
42      * @param url a String containing a fully qualified URL
43      * @throws java.net.MalformedURLException if the URL was malformed or not
44      * fully qualified
45      */

46     public QueryMapURL(String JavaDoc url)
47         throws java.net.MalformedURLException JavaDoc {
48         setURI(url);
49         parseQueryParameters(super.getQueryString());
50     }
51
52     /**
53      * Constructs an URL based on an existing Java URL.
54      * @param javaURL an existing valid Java URL
55      */

56     public QueryMapURL(java.net.URL JavaDoc javaURL) {
57         setURI(javaURL);
58         parseQueryParameters(super.getQueryString());
59     }
60
61
62     /**
63      * Sets/resets a query parameter on this URL. Previous to calling this
64      * method, this object must be fully initialized using one of the
65      * constructors or one of the setters. The query parameters are pair
66      * of name=value strings that will be at the end of the URL after a
67      * "?" character and seperated by "&" characters. This method may be called
68      * more than once to set/reset the value of a query parameter. Note that
69      * the query parameters will be outputted in the order they have been
70      * set.
71      * @param name a String containing the name of the query parameter
72      * @param value a String containing the value of the query parameter
73      */

74     public void setQueryParameter(String JavaDoc name, String JavaDoc value) {
75         queryParameters.put(name, value);
76     }
77
78     /**
79      * Retrieves a query parameter on this URL. Previous to calling this
80      * method, this object must be fully initialized using one of the
81      * constructors or one of the setters. The query parameters are pair
82      * of name=value strings that will be at the end of the URL after a
83      * "?" character and seperated by "&" characters.
84      *
85      * @param name a String containing the name of the query parameter that
86      * we want to retrieve
87      *
88      * @return the string value of the parameter if it exists, null otherwise
89      */

90     public String JavaDoc getQueryParameter(String JavaDoc name) {
91         return (String JavaDoc) queryParameters.get(name);
92     }
93
94     /**
95      * Removes a query parameter and it's value
96      * @param name removes an existing query parameter and it's value. If the
97      * parameter didn't exist in the URL previously, this method does nothing.
98      */

99     public void removeQueryParameter(String JavaDoc name) {
100         queryParameters.remove(name);
101     }
102
103     /**
104      * @param name a String containing the name of the parameter to look for
105      * @return true if the query contains a parameter by the specified name
106      */

107     public boolean containsQueryParameter(String JavaDoc name) {
108         return queryParameters.containsKey(name);
109     }
110
111     private void parseQueryParameters(String JavaDoc queryString) {
112         if (queryString == null) {
113             return;
114         }
115         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(queryString, "&");
116         while (tokenizer.hasMoreTokens()) {
117             String JavaDoc curToken = tokenizer.nextToken();
118             int equalPos = curToken.indexOf("=");
119             if (equalPos == -1) {
120                 queryParameters.put(curToken, null);
121             } else {
122                 String JavaDoc curName = curToken.substring(0, equalPos);
123                 String JavaDoc curValue = curToken.substring(equalPos+1);
124                 queryParameters.put(curName, curValue);
125             }
126         }
127     }
128
129     public String JavaDoc getQueryString () {
130         if (queryParameters.size() == 0) {
131             return null;
132         }
133         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
134         java.util.Iterator JavaDoc parameterIter = queryParameters.entrySet().iterator();
135         while (parameterIter.hasNext()) {
136             Map.Entry JavaDoc curEntry = (Map.Entry JavaDoc) parameterIter.next();
137             result.append(curEntry.getKey());
138             result.append("=");
139             result.append(curEntry.getValue());
140             if (parameterIter.hasNext()) {
141                 result.append("&");
142             }
143         }
144         return result.toString();
145     }
146
147     public void setQueryString(String JavaDoc queryString) {
148         super.setQueryString(queryString);
149         parseQueryParameters(queryString);
150     }
151
152 }
Popular Tags