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