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