KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.net;
2
3
4 /**
5  * This class models a URI.
6  * <p>
7  * Usage:
8  *
9  * <pre>
10  * Uri httpURI = Uri.parse("http://www.sapia-oss.org:80/index.html");
11  *
12  * // will print: http
13  * System.out.println(httpURI.getScheme());
14  *
15  * // will print: www.sapia-oss.org
16  * System.out.println(httpURI.getHost());
17  *
18  * // will print: 80
19  * System.out.println(httpURI.getPort());
20  *
21  * // will print: /index.html
22  * System.out.println(httpURI.getPath());
23  *
24  * Uri fileURI = Uri.parse("file:/some/directory/foo.txt");
25  *
26  * // will print: file
27  * System.out.println(fileURI.getScheme());
28  *
29  * // these calls don't make sense:
30  * System.out.println(fileURI.getHost());
31  * System.out.println(fileURI.getHost());
32  *
33  * // will print: /some/directory/foo.txt
34  * System.out.println(fileURI.getPath());
35  *
36  * </pre>
37  *
38  *
39  *
40  * @author Yanick Duchesne
41  * <dl>
42  * <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>
43  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
44  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
45  * </dl>
46  */

47 public class Uri {
48   public static final int UNDEFINED_PORT = -1;
49   public static final String JavaDoc UNDEFINED_HOST = "";
50   private static final QueryStringParser PARSER = new QueryStringParser();
51   private static final String JavaDoc PROTO = "://";
52   private static final char COLON = ':';
53   private static final char SLASH = '/';
54   private String JavaDoc _scheme;
55   private String JavaDoc _host;
56   private QueryString _query;
57   private int _port = UNDEFINED_PORT;
58
59   /**
60    * Constructor for UrlParser.
61    */

62   private Uri() {
63   }
64
65   public Uri(String JavaDoc scheme, String JavaDoc host, int port, String JavaDoc path) {
66     _scheme = scheme;
67     _host = host;
68     _port = port;
69     _query = new QueryString("");
70   }
71
72   /**
73    * Returns the path of this URI.
74    *
75    * @return a path.
76    */

77   public String JavaDoc getPath() {
78     return _query.getPath();
79   }
80
81   /**
82    * Returns the scheme of this URI.
83    *
84    * @return a scheme.
85    */

86   public String JavaDoc getScheme() {
87     return _scheme;
88   }
89
90   /**
91    * Returns the host of this URI.
92    *
93    * @return a host - if no host was specified, the returned value
94    * corresponds to the UNDEFINED_HOST constant of this class.
95    */

96   public String JavaDoc getHost() {
97     return _host;
98   }
99
100   /**
101    * Returns the port of this URI.
102    *
103    * @return a port - if no port was specified, the returned value
104    * corresponds to the UNDEFINED_PORT constant of this class.
105    */

106   public int getPort() {
107     return _port;
108   }
109
110   /**
111    * Sets this instance's scheme.
112    *
113    * @param a scheme.
114    */

115   public void setScheme(String JavaDoc scheme) {
116     _scheme = scheme;
117   }
118
119   /**
120    * Sets this instance's host.
121    *
122    * @param a host identifier.
123    */

124   public void setHost(String JavaDoc host) {
125     _host = host;
126   }
127
128   /**
129    * Sets this instance's port.
130    *
131    * @param a port.
132    */

133   public void setPort(int port) {
134     _port = port;
135   }
136
137   /**
138    * Returns this instance's query string.
139    *
140    * @return a <code>QueryString</code>, or <code>null</code>
141    * if this instance has no query string.
142    */

143   public QueryString getQueryString() {
144     return _query;
145   }
146
147   /**
148    * Returns this instance's string format.
149    *
150    * @return a <code>String</code>.
151    */

152   public String JavaDoc toString() {
153     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(_scheme);
154
155     if ((_host != null) && !_host.equals(UNDEFINED_HOST)) {
156       buf.append(PROTO).append(_host);
157
158       if (_port != UNDEFINED_PORT) {
159         buf.append(COLON).append(_port);
160       }
161     } else {
162       buf.append(COLON);
163     }
164
165     buf.append(_query.toString());
166
167     return buf.toString();
168   }
169
170   /**
171    * Parses the given URI string and returns its object
172    * representation.
173    *
174    * @return a <code>Uri</code>.
175    */

176   public static Uri parse(String JavaDoc uriStr) throws UriSyntaxException {
177     Uri uri = new Uri();
178
179     parseUrl(uri, uriStr);
180
181     return uri;
182   }
183
184   void setQueryString(QueryString str) {
185     _query = str;
186   }
187
188   private static void parseUrl(Uri url, String JavaDoc str) throws UriSyntaxException {
189     int idx = str.indexOf(PROTO);
190
191     if (idx < 0) {
192       url.setHost(UNDEFINED_HOST);
193       idx = str.indexOf(COLON);
194
195       if (idx < 0) {
196         url.setScheme(str);
197         url.setQueryString(new QueryString(""));
198       } else {
199         url.setScheme(str.substring(0, idx));
200         url.setQueryString(PARSER.parseQueryString(str.substring(idx + 1)));
201       }
202     } else {
203       String JavaDoc proto = str.substring(0, idx);
204
205       if (proto.length() == 0) {
206         throw new UriSyntaxException("Empty protocol in URI: " + str);
207       }
208
209       url.setScheme(proto);
210
211       parseHost(url, str.substring(idx + PROTO.length()));
212     }
213   }
214
215   /* expects: host:port/name?prop1=value1... */
216   private static void parseHost(Uri url, String JavaDoc str) throws UriSyntaxException {
217     int idx = str.indexOf(COLON);
218
219     if (idx >= 0) {
220       String JavaDoc host = str.substring(0, idx);
221
222       if (host.length() == 0) {
223         url.setHost(UNDEFINED_HOST);
224
225         //throw new UriSyntaxException("Host empty in URI: " + str);
226
} else {
227         url.setHost(host);
228         parsePort(url, str.substring(idx + 1));
229       }
230     } else {
231       idx = str.indexOf(SLASH);
232
233       String JavaDoc host;
234
235       if (idx < 0) {
236         host = str; //str.substring(0);
237

238         // throw new UriSyntaxException("Expected '/' after host in URI " + str);
239
} else {
240         host = str.substring(0, idx);
241       }
242
243       if (host.length() == 0) {
244         url.setHost(UNDEFINED_HOST);
245
246         //throw new UriSyntaxException("Host empty in URI: " + str);
247
} else {
248         url.setHost(host);
249       }
250
251       if (idx > 0) {
252         String JavaDoc qString = str.substring(idx);
253         url.setQueryString(PARSER.parseQueryString(qString));
254       } else {
255         url.setQueryString(new QueryString(""));
256       }
257     }
258   }
259
260   private static void parsePort(Uri url, String JavaDoc str) throws UriSyntaxException {
261     int idx = str.indexOf(SLASH);
262
263     String JavaDoc port;
264
265     if (idx < 0) {
266       port = str.substring(0);
267     } else {
268       port = str.substring(0, idx);
269     }
270
271     if (port.length() == 0) {
272       throw new UriSyntaxException("Port expected but not specified in URI: " +
273         str);
274     }
275
276     try {
277       url.setPort(Integer.parseInt(port));
278     } catch (NumberFormatException JavaDoc e) {
279       throw new UriSyntaxException("Port is not a valid number: " + str);
280     }
281
282     if (idx > 0) {
283       String JavaDoc qString = str.substring(idx);
284       url.setQueryString(PARSER.parseQueryString(qString));
285     } else {
286       url.setQueryString(new QueryString(""));
287     }
288   }
289 }
290
Popular Tags