KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > util > ParsedURL


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.util;
13
14 import com.versant.core.common.BindingSupportImpl;
15
16 import java.net.MalformedURLException JavaDoc;
17
18 /**
19  * A URL parsed into its different parts. This differs from the URL class
20  * in that there does not have to be a handler for the protocol.
21  */

22 public class ParsedURL {
23
24     private String JavaDoc orginal;
25     private String JavaDoc protocol;
26     private String JavaDoc host;
27     private int port = -1;
28     private String JavaDoc path;
29     private String JavaDoc query;
30     private String JavaDoc ref;
31
32     /**
33      * Parse url and throw an user exception if invalid.
34      */

35     public ParsedURL(String JavaDoc url) {
36         this.orginal = url;
37         int i = url.indexOf(':');
38         if (i <= 0) {
39             throw BindingSupportImpl.getInstance().invalidOperation(
40                 "Missing protocol: " + orginal);
41         }
42         protocol = url.substring(0, i);
43         url = url.substring(i + 1);
44         if (url.startsWith("//")) {
45             url = url.substring(2);
46         }
47         i = url.indexOf(':');
48         if (i >= 0) {
49             host = url.substring(0, i);
50             url = url.substring(i + 1);
51             i = url.indexOf('/');
52             String JavaDoc s;
53             if (i >= 0) {
54                 s = url.substring(0, i);
55                 url = url.substring(i);
56             } else {
57                 s = url;
58                 url = null;
59             }
60             try {
61                 port = Integer.parseInt(s);
62             } catch (NumberFormatException JavaDoc e) {
63                 throw BindingSupportImpl.getInstance().invalidOperation(
64                         "Invalid port '" + s + "' in " + orginal);
65             }
66         } else {
67             i = url.indexOf('/');
68             if (i >= 0) {
69                 host = url.substring(0, i);
70                 url = url.substring(i);
71             } else {
72                 host = url;
73                 url = null;
74             }
75         }
76         if (url != null) {
77             int ind = url.indexOf('#');
78             ref = ind < 0 ? null: url.substring(ind + 1);
79             url = ind < 0 ? url: url.substring(0, ind);
80             int q = url.lastIndexOf('?');
81             if (q != -1) {
82                 query = url.substring(q + 1);
83                 path = url.substring(0, q);
84             } else {
85                 path = url;
86             }
87         }
88     }
89
90     /**
91      * Just extract the protocol from url.
92      */

93     public static String JavaDoc getProtocol(String JavaDoc url) {
94         int i = url.indexOf(':');
95         return i < 0 ? null : url.substring(0, i);
96     }
97
98     public String JavaDoc getUrl() {
99         return orginal;
100     }
101
102     public String JavaDoc getProtocol() {
103         return protocol;
104     }
105
106     public String JavaDoc getHost() {
107         return host;
108     }
109
110     /**
111      * Get the port or -1 if none set.
112      */

113     public int getPort() {
114         return port;
115     }
116
117     public String JavaDoc getPath() {
118         return path;
119     }
120
121     public String JavaDoc getQuery() {
122         return query;
123     }
124
125     public String JavaDoc getRef() {
126         return ref;
127     }
128
129 }
130
131
Popular Tags