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: berkley $'
|
11
|
* '$Date: 2000-09-27 13:11:17 -0700 (Wed, 27 Sep 2000) $'
|
12
|
* '$Revision: 473 $'
|
13
|
*/
|
14
|
|
15
|
package edu.ucsb.nceas.metacat;
|
16
|
|
17
|
import java.net.MalformedURLException;
|
18
|
import java.util.Hashtable;
|
19
|
import java.util.Enumeration;
|
20
|
|
21
|
public class metacatURL
|
22
|
{
|
23
|
private String[][] params = new String[200][2];
|
24
|
private Hashtable paramsHash = new Hashtable();
|
25
|
private String urlType = null;
|
26
|
private String host = null;
|
27
|
private String url;
|
28
|
|
29
|
/**
|
30
|
* This constructor takes a string url and parses it according to the
|
31
|
* following rules.
|
32
|
* 1) The name of the url is the text before the "://" symbol.
|
33
|
* 2) Parameter names are written first and are terminated with the # symbol
|
34
|
* 3) Parameter values come 2nd and are terminated with an & except for the
|
35
|
* last value
|
36
|
* The form of the url looks like:
|
37
|
* urltype://name1#value1&name2#value2&nameN#valueN
|
38
|
* notice there is no & after the last param. If one is there it is ignored.
|
39
|
*
|
40
|
* @param url the string to parse
|
41
|
*/
|
42
|
public metacatURL(String url) throws MalformedURLException
|
43
|
{
|
44
|
this.url = url;
|
45
|
parseURL(url);
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* This method takes a string url and parses it according to the following
|
50
|
* rules.
|
51
|
* 1) The name of the url is the text before the "://" symbol.
|
52
|
* 2) Parameter names are written first and are terminated with the # symbol
|
53
|
* 3) Parameter values come 2nd and are terminated with an & except for the
|
54
|
* last value
|
55
|
* The form of the url looks like:
|
56
|
* urltype://name1#value1&name2#value2&nameN#valueN
|
57
|
* notice there is no & after the last param. If one is there it is ignored.
|
58
|
*/
|
59
|
private void parseURL(String url) throws MalformedURLException
|
60
|
{
|
61
|
String pname = null;
|
62
|
String param = null;
|
63
|
String temp = "";
|
64
|
boolean ampflag = true;
|
65
|
boolean poundflag = false;
|
66
|
int arrcount = 0;
|
67
|
|
68
|
int urlTypeIndex = url.indexOf("://"); //anything before this is the urltype
|
69
|
this.urlType = url.substring(0, urlTypeIndex);
|
70
|
paramsHash.put("urlType", this.urlType);
|
71
|
|
72
|
if(this.urlType.equals("http"))
|
73
|
{//if this is an http url
|
74
|
params[0][0] = "httpurl";
|
75
|
params[0][1] = url.substring(0, url.length());
|
76
|
paramsHash.put(params[0][0], params[0][1]);
|
77
|
for(int i=url.length()-1; i>0; i--)
|
78
|
{
|
79
|
if(url.charAt(i) == '/')
|
80
|
{
|
81
|
i=0;
|
82
|
}
|
83
|
else
|
84
|
{
|
85
|
String exchange = temp;
|
86
|
temp = "";
|
87
|
temp += url.charAt(i);
|
88
|
temp += exchange;
|
89
|
}
|
90
|
}
|
91
|
params[1][0] = "filename";
|
92
|
params[1][1] = temp;
|
93
|
paramsHash.put(params[1][0], params[1][1]);
|
94
|
}
|
95
|
else
|
96
|
{//other urls that meet the metacat type url structure.
|
97
|
int hostIndex = url.indexOf("?");
|
98
|
this.host = url.substring(urlTypeIndex + 3, hostIndex);
|
99
|
paramsHash.put("host", this.host);
|
100
|
for(int i=hostIndex + 1; i<url.length(); i++)
|
101
|
{ //go throught the remainder of the url one character at a time.
|
102
|
if(url.charAt(i) == '=')
|
103
|
{ //if the current char is a # then the preceding should be a parametet
|
104
|
//name
|
105
|
if(!poundflag && ampflag)
|
106
|
{
|
107
|
params[arrcount][0] = temp.trim();
|
108
|
temp = "";
|
109
|
}
|
110
|
else
|
111
|
{ //if there are two #s or &s in a row throw an exception.
|
112
|
throw new MalformedURLException("metacatURL: Two parameter names " +
|
113
|
"not allowed in sequence");
|
114
|
}
|
115
|
poundflag = true;
|
116
|
ampflag = false;
|
117
|
}
|
118
|
else if(url.charAt(i) == '&' || i == url.length()-1)
|
119
|
{ //the text preceding the & should be the param value.
|
120
|
if(i == url.length() - 1)
|
121
|
{ //if we are at the end of the string grab the last value and append it.
|
122
|
if(url.charAt(i) != '=')
|
123
|
{ //ignore an extra & on the end of the string
|
124
|
temp += url.charAt(i);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
if(!ampflag && poundflag)
|
129
|
{
|
130
|
params[arrcount][1] = temp.trim();
|
131
|
paramsHash.put(params[arrcount][0], params[arrcount][1]);
|
132
|
temp = "";
|
133
|
arrcount++; //increment the array to the next row.
|
134
|
}
|
135
|
else
|
136
|
{ //if there are two #s or &s in a row through an exception
|
137
|
throw new MalformedURLException("metacatURL: Two parameter values " +
|
138
|
"not allowed in sequence");
|
139
|
}
|
140
|
poundflag = false;
|
141
|
ampflag = true;
|
142
|
}
|
143
|
else
|
144
|
{ //get the next character in the string
|
145
|
temp += url.charAt(i);
|
146
|
}
|
147
|
}
|
148
|
}
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Returns the type of the url. This is defined by the text before the "://"
|
153
|
* symbol in the url.
|
154
|
*/
|
155
|
public String getURLType()
|
156
|
{
|
157
|
return this.urlType;
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* Returns the parameters as a 2D string array.
|
162
|
*/
|
163
|
public String[][] getParams()
|
164
|
{
|
165
|
return this.params;
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Returns the parameters in a hashtable.
|
170
|
*/
|
171
|
public Hashtable getHashParams()
|
172
|
{
|
173
|
return this.paramsHash;
|
174
|
}
|
175
|
|
176
|
/**
|
177
|
* returns a single parameter from the hash by name
|
178
|
* @param paramname the name of the parameter to return.
|
179
|
*/
|
180
|
public Object getHashParam(String paramname)
|
181
|
{
|
182
|
return this.paramsHash.get(paramname);
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* returns a string representation of this metacatURL
|
187
|
*/
|
188
|
public String toString()
|
189
|
{
|
190
|
return this.url;
|
191
|
}
|
192
|
|
193
|
public void printHashParams()
|
194
|
{
|
195
|
Enumeration e = this.paramsHash.keys();
|
196
|
System.out.println("name value");
|
197
|
System.out.println("-------------------");
|
198
|
while(e.hasMoreElements())
|
199
|
{
|
200
|
String key = (String)e.nextElement();
|
201
|
System.out.print(key);
|
202
|
System.out.print(" ");
|
203
|
System.out.println((String)this.paramsHash.get(key));
|
204
|
}
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Prints the parameters neatly to system.out
|
209
|
*/
|
210
|
public void printParams()
|
211
|
{
|
212
|
String[][] p = null;
|
213
|
System.out.println("url type: " + this.getURLType());
|
214
|
System.out.println("parameters: ");
|
215
|
p = this.getParams();
|
216
|
System.out.println("name value");
|
217
|
System.out.println("-------------------");
|
218
|
for(int i=0; i<p.length; i++)
|
219
|
{
|
220
|
if(p[i][0] != null)
|
221
|
{
|
222
|
System.out.print(p[i][0]);
|
223
|
System.out.print(" ");
|
224
|
System.out.print(p[i][1]);
|
225
|
System.out.println();
|
226
|
}
|
227
|
}
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Returns a single parameter and value as a 1D string array.
|
232
|
*
|
233
|
* @param index the index of the parameter, value array that you want.
|
234
|
*/
|
235
|
public String[] getParam(int index)
|
236
|
{
|
237
|
String[] s = new String[2];
|
238
|
s[0] = this.params[index][0].trim();
|
239
|
s[1] = this.params[index][1].trim();
|
240
|
//System.out.println("0: " + s[0]);
|
241
|
//System.out.println("1: " + s[1]);
|
242
|
return s;
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Test method for this class.
|
247
|
*/
|
248
|
public static void main(String args[])
|
249
|
{
|
250
|
String testurl = "metacat://dev.nceas.ucsb.edu?docid=NCEAS:10&username=chad&pasword=xyz";
|
251
|
String testurl2 = "http://dev.nceas.ucsb.edu/berkley/testdata.dat";
|
252
|
try
|
253
|
{
|
254
|
metacatURL murl = new metacatURL(testurl);
|
255
|
metacatURL murl2 = new metacatURL(testurl2);
|
256
|
String[][] p = null;
|
257
|
System.out.println("url type: " + murl.getURLType());
|
258
|
System.out.println("parameters: ");
|
259
|
p = murl.getParams();
|
260
|
Hashtable h = murl.getHashParams();
|
261
|
murl.printParams();
|
262
|
murl2.printParams();
|
263
|
System.out.println("hash params: " );
|
264
|
murl.printHashParams();
|
265
|
murl2.printHashParams();
|
266
|
}
|
267
|
catch(MalformedURLException murle)
|
268
|
{
|
269
|
System.out.println("bad url " + murle.getMessage());
|
270
|
}
|
271
|
}
|
272
|
|
273
|
}
|