KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.urls;
2
3 import org.apache.regexp.RE;
4 import org.apache.regexp.RESyntaxException;
5 import java.util.Map JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.io.UnsupportedEncodingException JavaDoc;
8
9 /**
10  * <p>Title: </p>
11  * <p>Description: </p>
12  * <p>Copyright: Copyright (c) 2002</p>
13  * <p>Company: Jahia Ltd</p>
14  * @author Serge Huber
15  * @version 1.0
16  */

17
18 public class URI {
19
20     private static org.apache.log4j.Logger logger =
21             org.apache.log4j.Logger.getLogger (URI.class);
22
23     private static RE uriRegexp;
24     static {
25         try {
26             uriRegexp = new RE(
27                 "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
28         } catch (RESyntaxException rese) {
29             logger.error("Error in regexp syntax", rese);
30         }
31     }
32
33     static private final Object JavaDoc[][] defaultSchemePortArray = {
34         { "http", new Integer JavaDoc(80) },
35         { "https", new Integer JavaDoc(443) }
36     };
37
38     private boolean uriStartingAtPath = false;
39     private boolean relative = false;
40
41     private String JavaDoc scheme = null;
42     private String JavaDoc authority = null;
43     private String JavaDoc userInfo = null;
44     private String JavaDoc hostName = null;
45     private int port = -1;
46     private String JavaDoc path = null;
47     private String JavaDoc queryString = null;
48     private String JavaDoc fragmentString = null;
49
50     private Map JavaDoc defaultSchemePorts;
51
52     private static String JavaDoc defaultEncoding = URICodec.getDefaultEncoding();
53     private String JavaDoc encoding = defaultEncoding;
54
55     static public String JavaDoc getDefaultEncoding() {
56         return defaultEncoding;
57     }
58
59     static public void setDefaultEncoding(String JavaDoc encoding) {
60         defaultEncoding = encoding;
61     }
62
63     public URI() {
64         init();
65     }
66
67     public URI(String JavaDoc uri) {
68         this();
69         setURI(uri);
70     }
71
72     /**
73      * Allows to set/reset the uri. This makes this class mutable, but this
74      * method should only be used if you are using this class as a JavaBean.
75      * If the URI starts with a "/" character, this will be recorded in the
76      * internal state of the URI and when the URI will be converted back to
77      * String representation it will start at the path, not as a completely
78      * fully qualified URI.
79      * @param uri a String containing a fully qualified URI or a relative URI
80      * starting with a "/" character
81      */

82     public void setURI(String JavaDoc uri) {
83         if (uri == null) {
84             return;
85         }
86         if (uri.indexOf("/") == 0) {
87             uriStartingAtPath = true;
88             parseURI(uri);
89         } else {
90             parseURI(uri);
91         }
92     }
93
94     /**
95      * Sets/resets the URI using an existing Java URL.
96      * @param javaURL an existing valid Java URL
97      */

98     public void setURI(java.net.URL JavaDoc javaURL) {
99         if (javaURL == null) {
100             return;
101         }
102         parseURI(javaURL.toString());
103     }
104
105     /**
106      * Converts the URI to a String form. This is done by remembering whether
107      * the URI is a relative or absolute URI, and by outputing all the URI,
108      * including the query parameters, in the order they were set.
109      * Note that if you need to output URIs that contain the session
110      * tracking part, you should use the toString(response) method.
111      * @return a String containing the converted URI
112      */

113     public String JavaDoc toString(String JavaDoc anEncoding) {
114         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
115         try {
116         if (!uriStartingAtPath) {
117             if (getScheme() != null) {
118                     result.append(URICodec.encode(getScheme(), anEncoding,
119                                                   URICodec.ALPHANUM_CHARS));
120                 result.append(":");
121             }
122             if (getAuthority() != null) {
123                 result.append("//");
124                     result.append(URICodec.encode(getAuthority(), anEncoding,
125                                                   URICodec.
126                                                   AUTHORITY_AUTHORIZEDCHARS));
127             }
128         }
129             if (getPath() != null) {
130                 result.append(URICodec.encode(getPath(), anEncoding,
131                                               URICodec.PATH_AUTHORIZEDCHARS));
132             }
133
134         // now let's append the paramters if there are some.
135
if (getQueryString() != null) {
136             result.append("?");
137                 result.append(URICodec.encode(getQueryString(), anEncoding, URICodec.QUERY_AUTHORIZEDCHARS));
138         }
139
140         if (getFragmentString() != null) {
141             result.append("#");
142                 result.append(URICodec.encode(getFragmentString(), anEncoding, URICodec.FRAGMENT_AUTHORIZEDCHARS));
143         }
144         } catch (UnsupportedEncodingException JavaDoc uee) {
145             logger.error("Unsupported encoding: " + anEncoding, uee);
146         }
147         if (result.toString().length() == 0) {
148             return null;
149         }
150         return result.toString();
151     }
152
153     /**
154      * Same as the toString() method, except that it feeds the output through
155      * the response.encodeURL() method to properly output session tracking
156      * part.
157      * @param response the HttpServletResponse object to use to encode the
158      * URL with the current session ID.
159      * @return a String containing the converted and encoded URL.
160      */

161     public String JavaDoc toString(javax.servlet.http.HttpServletResponse JavaDoc response) {
162         if (response == null) {
163             return null;
164         }
165         return response.encodeURL(toString());
166     }
167
168     public String JavaDoc toString(javax.servlet.http.HttpServletResponse JavaDoc response, String JavaDoc anEncoding) {
169         if (response == null) {
170             return null;
171         }
172         return response.encodeURL(toString(anEncoding));
173     }
174
175     public String JavaDoc toString() {
176         return toString(encoding);
177     }
178
179     /**
180      * Basically allows to decide if we want to generate a fully qualified
181      * URI using the toString() methods, or a relative URI. A relative URI
182      * means that the scheme :// hostname : port / will be omitted
183      * @param uriStartingAtPath set to true if we want to generate this
184      * URI as a relative URI.
185      */

186     public void setURIStartingAtPath(boolean anUriStartingAtPath) {
187         this.uriStartingAtPath = anUriStartingAtPath;
188     }
189
190     /**
191      * @return true if the URI will be generated as a relative URI, false
192      * otherwise.
193      */

194     public boolean isURIStartingAtPath() {
195         return uriStartingAtPath;
196     }
197
198     public String JavaDoc getScheme() {
199         return scheme;
200     }
201
202     public void setScheme(String JavaDoc aScheme) {
203         this.scheme = aScheme;
204     }
205
206     public String JavaDoc getAuthority() {
207         return authority;
208     }
209
210     public void setAuthority(String JavaDoc anAuthority) {
211         this.authority = anAuthority;
212         if (anAuthority != null) {
213             String JavaDoc curSubString = anAuthority;
214             int userInfoSepPos = anAuthority.indexOf("@");
215             if (userInfoSepPos > 0) {
216                 userInfo = anAuthority.substring(0, userInfoSepPos);
217                 curSubString = anAuthority.substring(userInfoSepPos+1);
218             }
219             int portSepPos = curSubString.indexOf(":");
220             if (portSepPos > 0) {
221                 hostName = curSubString.substring(0, portSepPos);
222                 port = Integer.parseInt(curSubString.substring(portSepPos+1));
223             } else {
224                 hostName = curSubString;
225                 port = getSchemeDefaultPort(scheme);
226             }
227         }
228     }
229
230     public String JavaDoc getUserInfo() {
231         return userInfo;
232     }
233
234     public void setUserInfo(String JavaDoc anUserInfo) {
235         this.userInfo = anUserInfo;
236         setAuthority(anUserInfo, hostName, port);
237     }
238
239     public String JavaDoc getHostName() {
240         return hostName;
241     }
242
243     public void setHostName(String JavaDoc aHostName) {
244         this.hostName = aHostName;
245         setAuthority(userInfo, aHostName, port);
246     }
247
248     public int getPort() {
249         return port;
250     }
251
252     public void setPort(int aPort) {
253         this.port = aPort;
254         setAuthority(userInfo, hostName, aPort);
255     }
256
257     public String JavaDoc getPath() {
258         return path;
259     }
260
261     public void setPath(String JavaDoc aPath) {
262         this.path = aPath;
263     }
264
265     public String JavaDoc getQueryString() {
266         return queryString;
267     }
268
269     public void setQueryString(String JavaDoc aQueryString) {
270         this.queryString = aQueryString;
271     }
272
273     public String JavaDoc getFragmentString() {
274         return fragmentString;
275     }
276
277     public void setFragmentString(String JavaDoc aFragmentString) {
278         this.fragmentString = aFragmentString;
279     }
280
281     public String JavaDoc getEncoding() {
282         return encoding;
283     }
284
285     public void setEncoding(String JavaDoc anEncoding) {
286         this.encoding = anEncoding;
287     }
288
289     private void parseURI(String JavaDoc uri) {
290         synchronized (uriRegexp) {
291             if (uriRegexp.match(uri)) {
292                 // this was taken from
293
scheme = uriRegexp.getParen(2);
294                 setAuthority(uriRegexp.getParen(4));
295                 path = uriRegexp.getParen(5);
296                 queryString = uriRegexp.getParen(7);
297                 fragmentString = uriRegexp.getParen(9);
298
299                 if (authority == null) {
300                     if (path.startsWith("/")) {
301                         uriStartingAtPath = true;
302                     } else {
303                         uriStartingAtPath = false;
304                         relative = true;
305                     }
306                 } else {
307                     uriStartingAtPath = false;
308                 }
309             }
310         }
311     }
312
313     private void setAuthority(String JavaDoc anUserInfo, String JavaDoc aHostName, int aPort) {
314         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
315         if (anUserInfo != null) {
316             result.append(anUserInfo);
317             result.append("@");
318         }
319         if (aHostName != null) {
320             result.append(aHostName);
321         }
322         if (aPort != getSchemeDefaultPort(scheme) && aPort > 0) {
323             result.append(":");
324             result.append(aPort);
325         }
326         if (result.toString().length() > 0) {
327             authority = result.toString();
328         } else {
329             authority = null;
330         }
331     }
332
333     private int getSchemeDefaultPort(String JavaDoc aScheme) {
334         if (aScheme == null) {
335             return -1;
336         }
337         Integer JavaDoc defaultPortInt = (Integer JavaDoc) defaultSchemePorts.get(aScheme);
338         if (defaultPortInt != null) {
339             return defaultPortInt.intValue();
340         } else {
341             return -1;
342         }
343     }
344
345     private void init() {
346         defaultSchemePorts = new HashMap JavaDoc();
347         for (int i = 0; i < defaultSchemePortArray.length; i++) {
348             defaultSchemePorts.put(defaultSchemePortArray[i][0], defaultSchemePortArray[i][1]);
349         }
350     }
351     public boolean isRelative() {
352         return relative;
353     }
354     public void setRelative(boolean isRelative) {
355         this.relative = isRelative;
356     }
357
358
359
360 }
361
Popular Tags