1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements a URL for use in identifying metacat
|
4
|
* files. It also handles http urls
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Chad Berkley
|
8
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: jones $'
|
11
|
* '$Date: 2001-01-18 11:52:00 -0800 (Thu, 18 Jan 2001) $'
|
12
|
* '$Revision: 669 $'
|
13
|
*
|
14
|
* This program is free software; you can redistribute it and/or modify
|
15
|
* it under the terms of the GNU General Public License as published by
|
16
|
* the Free Software Foundation; either version 2 of the License, or
|
17
|
* (at your option) any later version.
|
18
|
*
|
19
|
* This program is distributed in the hope that it will be useful,
|
20
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22
|
* GNU General Public License for more details.
|
23
|
*
|
24
|
* You should have received a copy of the GNU General Public License
|
25
|
* along with this program; if not, write to the Free Software
|
26
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27
|
*/
|
28
|
|
29
|
package edu.ucsb.nceas.metacat;
|
30
|
|
31
|
import java.net.MalformedURLException;
|
32
|
import java.util.Hashtable;
|
33
|
import java.util.Enumeration;
|
34
|
|
35
|
public class MetacatURL
|
36
|
{
|
37
|
private String[][] params = new String[200][2];
|
38
|
private Hashtable paramsHash = new Hashtable();
|
39
|
private String protocol = null;
|
40
|
private String host = null;
|
41
|
private String url;
|
42
|
|
43
|
/**
|
44
|
* This constructor takes a string url and parses it according to the
|
45
|
* following rules.
|
46
|
* 1) The protocol of the url is the text before the "://" symbol.
|
47
|
* 2) Parameter names are written first and are terminated with the = symbol
|
48
|
* 3) Parameter values come 2nd and are terminated with an & except for the
|
49
|
* last value
|
50
|
* The form of the url looks like:
|
51
|
* protocol://server.domain.com/servlet/?name1=val1&name2=val2&nameN=valN
|
52
|
* notice there is no & after the last param. If one is there it is ignored.
|
53
|
*
|
54
|
* @param url the string to parse
|
55
|
*/
|
56
|
public MetacatURL(String url) throws MalformedURLException
|
57
|
{
|
58
|
this.url = url;
|
59
|
parseURL(url);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* This method takes a string url and parses it according to the following
|
64
|
* rules.
|
65
|
* 1) The protocol of the url is the text before the "://" symbol.
|
66
|
* 2) Parameter names are written first and are terminated with the = symbol
|
67
|
* 3) Parameter values come 2nd and are terminated with an & except for the
|
68
|
* last value
|
69
|
* The form of the url looks like:
|
70
|
* protocol://server.domain.com/servlet/?name1=val1&name2=val2&nameN=valN
|
71
|
* notice there is no & after the last param. If one is there it is ignored.
|
72
|
*/
|
73
|
private void parseURL(String url) throws MalformedURLException
|
74
|
{
|
75
|
String pname = null;
|
76
|
String param = null;
|
77
|
String temp = "";
|
78
|
boolean ampflag = true;
|
79
|
boolean poundflag = false;
|
80
|
int arrcount = 0;
|
81
|
|
82
|
//anything before this is the protocol
|
83
|
int protocolIndex = url.indexOf("://");
|
84
|
|
85
|
if (protocolIndex == -1) {
|
86
|
// URL badly formed, no protocol
|
87
|
throw new MalformedURLException("Invalid URL format: " +
|
88
|
"no protocol provided.");
|
89
|
}
|
90
|
this.protocol = url.substring(0, protocolIndex);
|
91
|
paramsHash.put("protocol", this.protocol);
|
92
|
|
93
|
if(this.protocol.equals("http"))
|
94
|
{//if this is an http url
|
95
|
params[0][0] = "httpurl";
|
96
|
params[0][1] = url.substring(0, url.length());
|
97
|
paramsHash.put(params[0][0], params[0][1]);
|
98
|
for(int i=url.length()-1; i>0; i--)
|
99
|
{
|
100
|
if(url.charAt(i) == '/')
|
101
|
{
|
102
|
i=0;
|
103
|
}
|
104
|
else
|
105
|
{
|
106
|
String exchange = temp;
|
107
|
temp = "";
|
108
|
temp += url.charAt(i);
|
109
|
temp += exchange;
|
110
|
}
|
111
|
}
|
112
|
params[1][0] = "filename";
|
113
|
params[1][1] = temp;
|
114
|
paramsHash.put(params[1][0], params[1][1]);
|
115
|
}
|
116
|
else
|
117
|
{//other urls that meet the metacat type url structure.
|
118
|
int hostIndex = url.indexOf("?");
|
119
|
this.host = url.substring(protocolIndex + 3, hostIndex);
|
120
|
paramsHash.put("host", this.host);
|
121
|
for(int i=hostIndex + 1; i<url.length(); i++)
|
122
|
{ //go throught the remainder of the url one character at a time.
|
123
|
if(url.charAt(i) == '=')
|
124
|
{ //if the current char is a # then the preceding should be a parametet
|
125
|
//name
|
126
|
if(!poundflag && ampflag)
|
127
|
{
|
128
|
params[arrcount][0] = temp.trim();
|
129
|
temp = "";
|
130
|
}
|
131
|
else
|
132
|
{ //if there are two #s or &s in a row throw an exception.
|
133
|
throw new MalformedURLException("metacatURL: Two parameter names " +
|
134
|
"not allowed in sequence");
|
135
|
}
|
136
|
poundflag = true;
|
137
|
ampflag = false;
|
138
|
}
|
139
|
else if(url.charAt(i) == '&' || i == url.length()-1)
|
140
|
{ //the text preceding the & should be the param value.
|
141
|
if(i == url.length() - 1)
|
142
|
{ //if we are at the end of the string grab the last value and append it.
|
143
|
if(url.charAt(i) != '=')
|
144
|
{ //ignore an extra & on the end of the string
|
145
|
temp += url.charAt(i);
|
146
|
}
|
147
|
}
|
148
|
|
149
|
if(!ampflag && poundflag)
|
150
|
{
|
151
|
params[arrcount][1] = temp.trim();
|
152
|
paramsHash.put(params[arrcount][0], params[arrcount][1]);
|
153
|
temp = "";
|
154
|
arrcount++; //increment the array to the next row.
|
155
|
}
|
156
|
else
|
157
|
{ //if there are two =s or &s in a row through an exception
|
158
|
throw new MalformedURLException("metacatURL: Two parameter values " +
|
159
|
"not allowed in sequence");
|
160
|
}
|
161
|
poundflag = false;
|
162
|
ampflag = true;
|
163
|
}
|
164
|
else
|
165
|
{ //get the next character in the string
|
166
|
temp += url.charAt(i);
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Returns the type of the url. This is defined by the text before the "://"
|
174
|
* symbol in the url.
|
175
|
*/
|
176
|
public String getProtocol()
|
177
|
{
|
178
|
return this.protocol;
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Returns the parameters as a 2D string array.
|
183
|
*/
|
184
|
public String[][] getParams()
|
185
|
{
|
186
|
return this.params;
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* Returns the parameters in a hashtable.
|
191
|
*/
|
192
|
public Hashtable getHashParams()
|
193
|
{
|
194
|
return this.paramsHash;
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* returns a single parameter from the hash by name
|
199
|
* @param paramname the name of the parameter to return.
|
200
|
*/
|
201
|
public Object getHashParam(String paramname)
|
202
|
{
|
203
|
return this.paramsHash.get(paramname);
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* returns a string representation of this metacatURL
|
208
|
*/
|
209
|
public String toString()
|
210
|
{
|
211
|
return this.url;
|
212
|
}
|
213
|
|
214
|
public void printHashParams()
|
215
|
{
|
216
|
Enumeration e = this.paramsHash.keys();
|
217
|
System.out.println("name value");
|
218
|
System.out.println("-------------------");
|
219
|
while(e.hasMoreElements())
|
220
|
{
|
221
|
String key = (String)e.nextElement();
|
222
|
System.out.print(key);
|
223
|
System.out.print(" ");
|
224
|
System.out.println((String)this.paramsHash.get(key));
|
225
|
}
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* Prints the parameters neatly to system.out
|
230
|
*/
|
231
|
public void printParams()
|
232
|
{
|
233
|
String[][] p = null;
|
234
|
System.out.println("protocol: " + this.getProtocol());
|
235
|
System.out.println("parameters: ");
|
236
|
p = this.getParams();
|
237
|
System.out.println("name value");
|
238
|
System.out.println("-------------------");
|
239
|
for(int i=0; i<p.length; i++)
|
240
|
{
|
241
|
if(p[i][0] != null)
|
242
|
{
|
243
|
System.out.print(p[i][0]);
|
244
|
System.out.print(" ");
|
245
|
System.out.print(p[i][1]);
|
246
|
System.out.println();
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
|
251
|
/**
|
252
|
* Returns a single parameter and value as a 1D string array.
|
253
|
*
|
254
|
* @param index the index of the parameter, value array that you want.
|
255
|
*/
|
256
|
public String[] getParam(int index)
|
257
|
{
|
258
|
String[] s = new String[2];
|
259
|
s[0] = this.params[index][0].trim();
|
260
|
s[1] = this.params[index][1].trim();
|
261
|
//System.out.println("0: " + s[0]);
|
262
|
//System.out.println("1: " + s[1]);
|
263
|
return s;
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* Test method for this class.
|
268
|
*/
|
269
|
public static void main(String args[])
|
270
|
{
|
271
|
String testurl = "metacat://dev.nceas.ucsb.edu?docid=NCEAS:10&username=chad&pasword=xyz";
|
272
|
String testurl2 = "http://dev.nceas.ucsb.edu/berkley/testdata.dat";
|
273
|
String testurl3 = "NCEAS.1287873498.32";
|
274
|
try
|
275
|
{
|
276
|
System.out.println("*********************************************");
|
277
|
MetacatURL murl = new MetacatURL(testurl);
|
278
|
//String[][] p = null;
|
279
|
System.out.println("protocol: " + murl.getProtocol());
|
280
|
System.out.println("parameters: ");
|
281
|
//p = murl.getParams();
|
282
|
//Hashtable h = murl.getHashParams();
|
283
|
murl.printParams();
|
284
|
murl.printHashParams();
|
285
|
System.out.println("*********************************************");
|
286
|
|
287
|
MetacatURL murl2 = new MetacatURL(testurl2);
|
288
|
System.out.println("protocol: " + murl2.getProtocol());
|
289
|
System.out.println("parameters: ");
|
290
|
murl2.printParams();
|
291
|
murl2.printHashParams();
|
292
|
System.out.println("*********************************************");
|
293
|
|
294
|
MetacatURL murl3 = new MetacatURL(testurl3);
|
295
|
System.out.println("protocol: " + murl3.getProtocol());
|
296
|
System.out.println("parameters: ");
|
297
|
murl3.printParams();
|
298
|
System.out.println("*********************************************");
|
299
|
}
|
300
|
catch(MalformedURLException murle)
|
301
|
{
|
302
|
System.out.println("bad url " + murle.getMessage());
|
303
|
}
|
304
|
}
|
305
|
|
306
|
}
|