1 package org.sapia.ubik.net; 2 3 4 47 public class Uri { 48 public static final int UNDEFINED_PORT = -1; 49 public static final String UNDEFINED_HOST = ""; 50 private static final QueryStringParser PARSER = new QueryStringParser(); 51 private static final String PROTO = "://"; 52 private static final char COLON = ':'; 53 private static final char SLASH = '/'; 54 private String _scheme; 55 private String _host; 56 private QueryString _query; 57 private int _port = UNDEFINED_PORT; 58 59 62 private Uri() { 63 } 64 65 public Uri(String scheme, String host, int port, String path) { 66 _scheme = scheme; 67 _host = host; 68 _port = port; 69 _query = new QueryString(""); 70 } 71 72 77 public String getPath() { 78 return _query.getPath(); 79 } 80 81 86 public String getScheme() { 87 return _scheme; 88 } 89 90 96 public String getHost() { 97 return _host; 98 } 99 100 106 public int getPort() { 107 return _port; 108 } 109 110 115 public void setScheme(String scheme) { 116 _scheme = scheme; 117 } 118 119 124 public void setHost(String host) { 125 _host = host; 126 } 127 128 133 public void setPort(int port) { 134 _port = port; 135 } 136 137 143 public QueryString getQueryString() { 144 return _query; 145 } 146 147 152 public String toString() { 153 StringBuffer buf = new StringBuffer (_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 176 public static Uri parse(String 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 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 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 216 private static void parseHost(Uri url, String str) throws UriSyntaxException { 217 int idx = str.indexOf(COLON); 218 219 if (idx >= 0) { 220 String host = str.substring(0, idx); 221 222 if (host.length() == 0) { 223 url.setHost(UNDEFINED_HOST); 224 225 } else { 227 url.setHost(host); 228 parsePort(url, str.substring(idx + 1)); 229 } 230 } else { 231 idx = str.indexOf(SLASH); 232 233 String host; 234 235 if (idx < 0) { 236 host = str; 238 } else { 240 host = str.substring(0, idx); 241 } 242 243 if (host.length() == 0) { 244 url.setHost(UNDEFINED_HOST); 245 246 } else { 248 url.setHost(host); 249 } 250 251 if (idx > 0) { 252 String 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 str) throws UriSyntaxException { 261 int idx = str.indexOf(SLASH); 262 263 String 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 e) { 279 throw new UriSyntaxException("Port is not a valid number: " + str); 280 } 281 282 if (idx > 0) { 283 String qString = str.substring(idx); 284 url.setQueryString(PARSER.parseQueryString(qString)); 285 } else { 286 url.setQueryString(new QueryString("")); 287 } 288 } 289 } 290 | Popular Tags |