Repository navigation
Expand file tree
/
Copy pathURLHelper.java
More file actions
283 lines (244 loc) · 7.46 KB
/
Copy pathURLHelper.java
File metadata and controls
283 lines (244 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package flash.util;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class URLHelper
{
private static Pattern URL_PATTERN = Pattern.compile("^(.*?)(\\?.*?)?(#.*)?$"); //$NON-NLS-1$
/**
* Everything before the "query" part of the URL. E.g. for
* "http://www.example.com/file?firstname=Bob&lastname=Smith#foo"
* this would be "http://www.example.com/file".
*/
private String m_everythingBeforeQuery;
/**
* The "query" in a URL is the "?firstname=Bob&lastname=Smith" part.
* m_query contains the query (including "?"), or contains "" if the
* URL has no query. Never null.
*/
private String m_query;
/**
* The "fragment" in a URL is the "#foo" part at the end of a URL.
* m_fragment contains the fragment (including "#"), or contains "" if the
* URL has no fragment. Never null.
*/
private String m_fragment;
public URLHelper(String url)
{
Matcher matcher = URL_PATTERN.matcher(url);
if (!matcher.matches())
throw new IllegalArgumentException(url);
if (matcher.matches())
{
m_everythingBeforeQuery = matcher.group(1);
m_query = matcher.group(2);
if (m_query == null) m_query = ""; //$NON-NLS-1$
m_fragment = matcher.group(3);
if (m_fragment == null) m_fragment = ""; //$NON-NLS-1$
}
}
/**
* Everything before the "query" part of the URL. E.g. for
* "http://www.example.com/file?firstname=Bob&lastname=Smith#foo"
* this would be "http://www.example.com/file".
*/
public String getEverythingBeforeQuery()
{
return m_everythingBeforeQuery;
}
public void setEverythingBeforeQuery(String everythingBeforeQuery)
{
assertValidArguments(everythingBeforeQuery, getQuery(), getFragment());
m_everythingBeforeQuery = everythingBeforeQuery;
}
/**
* Rturns the "query" portion of the URL, e.g. the
* "?firstname=Bob&lastname=Smith" part. m_query contains the query
* (including "?"), or "" if the URL has no query. Never null.
*/
public String getQuery()
{
return m_query;
}
/**
* Sets the "query" portion of the URL. This must be either the
* empty string or a string that begins with "?".
*/
public void setQuery(String query)
{
// if there is a query, make sure it starts with "?"
if (query.length() > 0 && query.charAt(0) != '?')
query = "?" + query; //$NON-NLS-1$
assertValidArguments(getEverythingBeforeQuery(), query, getFragment());
m_query = query;
}
/**
* Returns the "fragment" portion of the URL, e.g. the "#foo" part, or
* "" if the URL has no fragment. Never null.
*/
public String getFragment()
{
return m_fragment;
}
/**
* Sets the "fragment" portion of the URL. This must be either the
* empty string or a string that begins with "#".
* @param fragment
*/
public void setFragment(String fragment)
{
// if there is a fragment, make sure it starts with "#"
if (fragment.length() > 0 && fragment.charAt(0) != '#')
fragment = "#" + fragment; //$NON-NLS-1$
assertValidArguments(getEverythingBeforeQuery(), getQuery(), fragment);
m_fragment = fragment;
}
private static void assertValidArguments(String everythingBeforeQuery, String query, String fragment)
{
assert areArgumentsValid(everythingBeforeQuery, query, fragment);
}
/**
* This will test for various error conditions, e.g. a query string that
* contains "#" or has incorrect contents.
*/
private static boolean areArgumentsValid(String everythingBeforeQuery, String query, String fragment)
{
if (everythingBeforeQuery == null || query == null || fragment == null)
return false;
URLHelper newHelper = new URLHelper(everythingBeforeQuery + query + fragment);
if (!newHelper.getEverythingBeforeQuery().equals(everythingBeforeQuery) ||
!newHelper.getQuery().equals(query) ||
!newHelper.getFragment().equals(fragment))
{
return false;
}
return true;
}
/**
* Returns the entire URL.
*/
public String getURL()
{
return m_everythingBeforeQuery + m_query + m_fragment;
}
/**
* Returns the query portion of the URL, broken up into individual key/value
* pairs. Does NOT unescape the keys and values.
*/
public LinkedHashMap<String, String> getParameterMap()
{
LinkedHashMap<String, String> map;
StringTokenizer tokens = new StringTokenizer(getQuery(), "?&"); //$NON-NLS-1$
// multiply by 2 to create a sufficiently large HashMap
map = new LinkedHashMap<String, String>(tokens.countTokens() * 2);
while (tokens.hasMoreElements())
{
String nameValuePair = tokens.nextToken();
String name = nameValuePair;
String value = ""; //$NON-NLS-1$
int equalsIndex = nameValuePair.indexOf('=');
if (equalsIndex != -1)
{
name = nameValuePair.substring(0, equalsIndex);
if (name.length() > 0)
{
value = nameValuePair.substring(equalsIndex + 1);
}
}
map.put(name, value);
}
return map;
}
/**
* Sets the query portion of the URL.
*
* @param parameterMap
* a key/value mapping; these must already be escaped!
*/
public void setParameterMap(Map<String,String> parameterMap)
{
if ((parameterMap != null) && (!parameterMap.isEmpty()))
{
StringBuilder queryString = new StringBuilder();
Iterator<Map.Entry<String,String>> it = parameterMap.entrySet().iterator();
while (it.hasNext())
{
Map.Entry<String,String> entry = it.next();
String name = (String) entry.getKey();
String value = String.valueOf(entry.getValue());
queryString.append(name);
if ((value != null) && (!value.equals(""))) //$NON-NLS-1$
{
queryString.append('=');
queryString.append(value);
}
if (it.hasNext())
{
queryString.append('&');
}
}
setQuery(queryString.toString());
} else
{
setQuery(""); //$NON-NLS-1$
}
}
// shortcut for converting spaces to %20 in URIs
public static String escapeSpace(String uri)
{
return escapeCharacter(uri, ' ', "%20"); //$NON-NLS-1$
}
/**
* Locates characters 'c' in the scheme specific portion of a URI and
* translates them into 'to'
*/
public static String escapeCharacter(String uri, char c, String to)
{
StringBuilder sb = new StringBuilder();
int size = uri.length();
int at = uri.indexOf(':');
int lastAt = 0;
// skip the scheme
if (at > -1)
{
for(int i=0; i<=at; i++)
sb.append(uri.charAt(i));
lastAt = ++at;
}
// while we have 'c's in uri
while( (at = uri.indexOf(c, at)) > -1)
{
// original portion
for(int i=lastAt; i<at; i++)
sb.append(uri.charAt(i));
// conversion
sb.append(to);
lastAt = ++at; // advance to char after conversion
}
if (lastAt < size)
{
for(int i=lastAt; i<size; i++)
sb.append(uri.charAt(i));
}
return sb.toString();
}
}