56
|
1 |
/*
|
87
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
56
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.microedition.m2g.connection;
|
|
19 |
|
|
20 |
import java.io.IOException;
|
|
21 |
import java.lang.NullPointerException;
|
|
22 |
import java.lang.IllegalArgumentException;
|
|
23 |
import com.nokia.mj.impl.utils.Logger;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* Connection policy
|
|
27 |
*/
|
|
28 |
public class M2GUrlTokenizer
|
|
29 |
{
|
|
30 |
//--------------------------------------------------
|
|
31 |
// STATIC CONSTANTS
|
|
32 |
//--------------------------------------------------
|
|
33 |
public static final String PROTOCOL_SEPARATOR =
|
|
34 |
"://";
|
|
35 |
public static final char PARAMETER_SEPARATOR =
|
|
36 |
'?';
|
|
37 |
public static final char SEPARATOR =
|
|
38 |
'/';
|
|
39 |
|
|
40 |
//--------------------------------------------------
|
|
41 |
// VARIABLES
|
|
42 |
//--------------------------------------------------
|
|
43 |
private String iUrl = null;
|
|
44 |
private String iBaseUrl = null;
|
|
45 |
|
|
46 |
//--------------------------------------------------
|
|
47 |
// METHODS
|
|
48 |
//--------------------------------------------------
|
|
49 |
/**
|
|
50 |
* Ctor
|
|
51 |
*/
|
|
52 |
public M2GUrlTokenizer()
|
|
53 |
{
|
|
54 |
}
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Ctor
|
|
58 |
* @param aUrl Locator
|
|
59 |
* @throws NullPointerException if the locator is null.
|
|
60 |
* @throws IllegalArgumentException if the locator is of unsupported type.
|
|
61 |
*/
|
|
62 |
public M2GUrlTokenizer(String aUrl)
|
|
63 |
{
|
|
64 |
setUrl(aUrl);
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Check if the url begin with slash ('/')
|
|
69 |
* @return true is the url begin with slash
|
|
70 |
*/
|
|
71 |
public boolean beginWithSlash()
|
|
72 |
{
|
|
73 |
if ((iUrl != null) &&
|
|
74 |
(iUrl.indexOf(SEPARATOR) == 0))
|
|
75 |
{
|
|
76 |
return true;
|
|
77 |
}
|
|
78 |
return false;
|
|
79 |
}
|
|
80 |
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Parses the base part of the locator
|
|
84 |
* @return parsed base part
|
|
85 |
*/
|
|
86 |
public String getBasePart()
|
|
87 |
{
|
|
88 |
if (iUrl == null)
|
|
89 |
{
|
|
90 |
return null;
|
|
91 |
}
|
|
92 |
int pos = iUrl.indexOf(PARAMETER_SEPARATOR);
|
|
93 |
if (pos == -1)
|
|
94 |
{
|
|
95 |
pos = iUrl.length();
|
|
96 |
}
|
|
97 |
return iUrl.substring(0, pos);
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Parses the base url
|
|
102 |
* @return parsed base url
|
|
103 |
*/
|
|
104 |
public String getBaseUrl()
|
|
105 |
{
|
|
106 |
return iBaseUrl;
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Parses the end part of the locator
|
|
111 |
* @return parsed end part
|
|
112 |
* @throws IllegalArgumentException if base part cannot
|
|
113 |
* be parsed from the locator
|
|
114 |
*/
|
|
115 |
public String getEndPart()
|
|
116 |
{
|
|
117 |
if (iUrl == null)
|
|
118 |
{
|
|
119 |
return null;
|
|
120 |
}
|
|
121 |
int pos = iUrl.lastIndexOf(SEPARATOR);
|
|
122 |
if (pos == -1)
|
|
123 |
{
|
|
124 |
return iUrl;
|
|
125 |
}
|
|
126 |
return iUrl.substring(pos, iUrl.length());
|
|
127 |
}
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Parses the middle part of the locator
|
|
131 |
* @return parsed middle part
|
|
132 |
* @throws IllegalArgumentException if middle part cannot
|
|
133 |
* be parsed from the locator
|
|
134 |
*/
|
|
135 |
public String getMiddlePart()
|
|
136 |
{
|
|
137 |
if (iUrl == null)
|
|
138 |
{
|
|
139 |
return null;
|
|
140 |
}
|
|
141 |
int pos = iUrl.indexOf(PARAMETER_SEPARATOR);
|
|
142 |
if (pos == -1)
|
|
143 |
{
|
|
144 |
pos = iUrl.length();
|
|
145 |
}
|
|
146 |
return iUrl.substring(
|
|
147 |
getProtocol().length() + PROTOCOL_SEPARATOR.length(), pos);
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Parses possible parameters of the locator
|
|
152 |
* @return Parsed parameters or null if there are not parameters
|
|
153 |
*/
|
|
154 |
public String getParameters()
|
|
155 |
{
|
|
156 |
if (iUrl == null)
|
|
157 |
{
|
|
158 |
return null;
|
|
159 |
}
|
|
160 |
int pos = iUrl.indexOf(PARAMETER_SEPARATOR);
|
|
161 |
String parameters = null;
|
|
162 |
if (pos != -1)
|
|
163 |
{
|
|
164 |
parameters = iUrl.substring(pos + 1);
|
|
165 |
}
|
|
166 |
return parameters;
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Parses the protocol part of the locator.
|
|
171 |
* @see M2GUrlTokenizer#isAbsolutURL()
|
|
172 |
* @return Protocol
|
|
173 |
* @throws IllegalArgumentException If the locator is of
|
|
174 |
* unsupported type.
|
|
175 |
*/
|
|
176 |
public String getProtocol()
|
|
177 |
{
|
|
178 |
int pos = -1;
|
|
179 |
if ((iUrl == null) || ((pos = iUrl.indexOf(PROTOCOL_SEPARATOR)) == -1))
|
|
180 |
{
|
|
181 |
throw new IllegalArgumentException("Unable to parse protocol");
|
|
182 |
}
|
|
183 |
return iUrl.substring(0, pos);
|
|
184 |
}
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Returns a whole locator string
|
|
188 |
* @return Locator used to create this instance
|
|
189 |
*/
|
|
190 |
public String getUrl()
|
|
191 |
{
|
|
192 |
return iUrl;
|
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* Resolve URL according to existing base url and
|
|
197 |
* the given relative url
|
87
|
198 |
* Should be resolved by using RFC 2396
|
56
|
199 |
* @aRelativeUrl Relative URL.
|
|
200 |
* @return Resolved URL
|
|
201 |
*/
|
|
202 |
public String resolveUrl(String aRelativeUrl)
|
|
203 |
{
|
|
204 |
check(aRelativeUrl);
|
|
205 |
if (iBaseUrl != null)
|
|
206 |
{
|
|
207 |
return (iBaseUrl + aRelativeUrl);
|
|
208 |
}
|
|
209 |
else
|
|
210 |
{
|
|
211 |
return aRelativeUrl;
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
|
|
216 |
/**
|
|
217 |
* Set a locator string
|
|
218 |
* @param aUrl Locator
|
|
219 |
* @throws NullPointerException if the locator is null.
|
|
220 |
* @throws IllegalArgumentException if the locator is of unsupported type.
|
|
221 |
*/
|
|
222 |
public void setUrl(String aUrl)
|
|
223 |
{
|
|
224 |
check(aUrl);
|
|
225 |
iUrl = aUrl;
|
|
226 |
int pos = iUrl.lastIndexOf(SEPARATOR);
|
|
227 |
if (pos == -1)
|
|
228 |
{
|
|
229 |
iBaseUrl = null;
|
|
230 |
}
|
|
231 |
else
|
|
232 |
{
|
|
233 |
iBaseUrl = iUrl.substring(0, (pos + 1));
|
|
234 |
}
|
|
235 |
}
|
|
236 |
|
|
237 |
/**
|
|
238 |
* Check url
|
|
239 |
* @param aUrl Locator
|
|
240 |
* @throws NullPointerException if the locator is null.
|
|
241 |
* @throws IllegalArgumentException if the locator is of unsupported type.
|
|
242 |
*/
|
|
243 |
private void check(String aUrl)
|
|
244 |
{
|
|
245 |
if (aUrl == null)
|
|
246 |
{
|
|
247 |
Logger.ELOG(Logger.EJavaUI, "setUrl() - url is null");
|
|
248 |
throw new NullPointerException();
|
|
249 |
}
|
|
250 |
if (aUrl.length() == 0)
|
|
251 |
{
|
|
252 |
Logger.ELOG(Logger.EJavaUI, "setUrl() - url is illegal");
|
|
253 |
throw new IllegalArgumentException();
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
//--------------------------------------------------
|
|
258 |
// STATIC METHODS
|
|
259 |
//--------------------------------------------------
|
|
260 |
/**
|
|
261 |
* Check if contains protocol.
|
|
262 |
* An absolute URL contains the name of the scheme being used (<scheme>)
|
|
263 |
* followed by a colon (":") and then a string (the <scheme-specific-
|
|
264 |
* // part>) whose interpretation depends on the scheme.
|
|
265 |
* Also if the URL begins with a slash character "/" then the URL is treated
|
|
266 |
* as absolute.
|
|
267 |
* @param aUrl URL
|
|
268 |
* @return true if URL contains a protocol
|
|
269 |
*/
|
|
270 |
static public boolean isAbsolutURL(String aUrl)
|
|
271 |
{
|
|
272 |
if ((aUrl == null) ||
|
|
273 |
((aUrl.indexOf(PROTOCOL_SEPARATOR) == -1) &&
|
|
274 |
(aUrl.indexOf(SEPARATOR) != 0)))
|
|
275 |
{
|
|
276 |
return false;
|
|
277 |
}
|
|
278 |
return true;
|
|
279 |
}
|
|
280 |
}
|