1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements utility methods for a metadata catalog
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Matt Jones, Jivka Bojilova
|
7
|
* Release: @release@
|
8
|
*
|
9
|
* '$Author: berkley $'
|
10
|
* '$Date: 2003-04-10 10:00:31 -0700 (Thu, 10 Apr 2003) $'
|
11
|
* '$Revision: 1538 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacat;
|
29
|
|
30
|
import java.io.File;
|
31
|
import java.net.URL;
|
32
|
import java.net.MalformedURLException;
|
33
|
import java.sql.Connection;
|
34
|
import java.sql.DriverManager;
|
35
|
import java.sql.SQLException;
|
36
|
import java.util.PropertyResourceBundle;
|
37
|
import java.util.Hashtable;
|
38
|
import java.util.Enumeration;
|
39
|
import java.util.Vector;
|
40
|
|
41
|
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
|
42
|
|
43
|
/**
|
44
|
* A suite of utility classes for the metadata catalog server
|
45
|
*/
|
46
|
public class MetaCatUtil {
|
47
|
|
48
|
public static AbstractDatabase dbAdapter;
|
49
|
private static PropertyResourceBundle options = null;
|
50
|
private static String propertiesFile = "edu.ucsb.nceas.metacat.metacat";
|
51
|
private static boolean debug = true;
|
52
|
|
53
|
//private Hashtable connectionPool = new Hashtable();
|
54
|
|
55
|
/**
|
56
|
* Determine our db adapter class and create an instance of that class
|
57
|
*/
|
58
|
static {
|
59
|
try {
|
60
|
dbAdapter = (AbstractDatabase)createObject(getOption("dbAdapter"));
|
61
|
} catch (Exception e) {
|
62
|
System.err.println("Error in MetaCatUtil static block:" + e.getMessage());
|
63
|
}
|
64
|
}
|
65
|
|
66
|
|
67
|
|
68
|
/**
|
69
|
* Instantiate a class using the name of the class at runtime
|
70
|
*
|
71
|
* @param className the fully qualified name of the class to instantiate
|
72
|
*/
|
73
|
public static Object createObject(String className) throws Exception {
|
74
|
|
75
|
Object object = null;
|
76
|
try {
|
77
|
Class classDefinition = Class.forName(className);
|
78
|
object = classDefinition.newInstance();
|
79
|
} catch (InstantiationException e) {
|
80
|
throw e;
|
81
|
} catch (IllegalAccessException e) {
|
82
|
throw e;
|
83
|
} catch (ClassNotFoundException e) {
|
84
|
throw e;
|
85
|
}
|
86
|
return object;
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Utility method to get an option value from the properties file
|
91
|
*
|
92
|
* @param optionName the name of the option requested
|
93
|
*/
|
94
|
public static String getOption(String optionName) {
|
95
|
// Get the configuration file information
|
96
|
if (options == null) {
|
97
|
options = (PropertyResourceBundle)
|
98
|
PropertyResourceBundle.getBundle(propertiesFile);
|
99
|
}
|
100
|
String value = (String)options.handleGetObject(optionName);
|
101
|
return value;
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Utility method to get an option value from a properties file
|
106
|
*
|
107
|
* @param optionName the name of the option requested
|
108
|
* @param propFile the name of the file where to get the properties from
|
109
|
*/
|
110
|
public String getOption(String optionName, String propFile) {
|
111
|
// Get the configuration file information
|
112
|
PropertyResourceBundle options = (PropertyResourceBundle)
|
113
|
PropertyResourceBundle.getBundle(propFile);
|
114
|
String value = (String)options.handleGetObject(optionName);
|
115
|
return value;
|
116
|
}
|
117
|
|
118
|
|
119
|
|
120
|
|
121
|
/** Utility method to convert a file handle into a URL */
|
122
|
public static URL fileToURL(File file)
|
123
|
{
|
124
|
String path = file.getAbsolutePath();
|
125
|
String fSep = System.getProperty("file.separator");
|
126
|
if (fSep != null && fSep.length() == 1)
|
127
|
path = path.replace(fSep.charAt(0), '/');
|
128
|
if (path.length() > 0 && path.charAt(0) != '/')
|
129
|
path = '/' + path;
|
130
|
try {
|
131
|
return new URL("file", null, path);
|
132
|
}
|
133
|
catch (java.net.MalformedURLException e) {
|
134
|
/* According to the spec this could only happen if the file
|
135
|
protocol were not recognized. */
|
136
|
throw new Error("unexpected MalformedURLException");
|
137
|
}
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* Utility method to parse the query part of a URL into parameters. This
|
142
|
* method assumes the format of the query par tof the url is an
|
143
|
* ampersand separated list of name/value pairs, with equal signs separating
|
144
|
* the name from the value (e.g., name=tom&zip=99801 ). Returns a
|
145
|
* has of the name value pairs, hashed on name.
|
146
|
*/
|
147
|
public static Hashtable parseQuery(String query) throws MalformedURLException
|
148
|
{
|
149
|
String[][] params = new String[200][2];
|
150
|
Hashtable parameters = new Hashtable();
|
151
|
|
152
|
String temp = "";
|
153
|
boolean ampflag = true;
|
154
|
boolean poundflag = false;
|
155
|
int arrcount = 0;
|
156
|
|
157
|
if ( query != null ) {
|
158
|
for (int i=0; i < query.length(); i++) {
|
159
|
|
160
|
// go throught the remainder of the query one character at a time.
|
161
|
if (query.charAt(i) == '=') {
|
162
|
// if the current char is a # then the preceding should be a name
|
163
|
if (!poundflag && ampflag) {
|
164
|
params[arrcount][0] = temp.trim();
|
165
|
temp = "";
|
166
|
} else {
|
167
|
//if there are two #s or &s in a row throw an exception.
|
168
|
throw new MalformedURLException("metacatURL: Two parameter names "+
|
169
|
"not allowed in sequence");
|
170
|
}
|
171
|
poundflag = true;
|
172
|
ampflag = false;
|
173
|
} else if (query.charAt(i) == '&' || i == query.length()-1) {
|
174
|
//the text preceding the & should be the param value.
|
175
|
if (i == query.length() - 1) {
|
176
|
//if at the end of the string grab the last value and append it.
|
177
|
if (query.charAt(i) != '=') {
|
178
|
//ignore an extra & on the end of the string
|
179
|
temp += query.charAt(i);
|
180
|
}
|
181
|
}
|
182
|
|
183
|
if (!ampflag && poundflag) {
|
184
|
params[arrcount][1] = temp.trim();
|
185
|
parameters.put(params[arrcount][0], params[arrcount][1]);
|
186
|
temp = "";
|
187
|
arrcount++; //increment the array to the next row.
|
188
|
} else {
|
189
|
//if there are two =s or &s in a row through an exception
|
190
|
throw new MalformedURLException("metacatURL: Two parameter values "+
|
191
|
"not allowed in sequence");
|
192
|
}
|
193
|
poundflag = false;
|
194
|
ampflag = true;
|
195
|
} else {
|
196
|
//get the next character in the string
|
197
|
temp += query.charAt(i);
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
return parameters;
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Utility method to print debugging messages. User can set debug level
|
206
|
* for this message. The number is fewer, the message is more important
|
207
|
* @param msg, the content of the message
|
208
|
* @param debugLevel, an integer indicating the message debug leve
|
209
|
*/
|
210
|
public static void debugMessage(String msg, int debugLevel)
|
211
|
{
|
212
|
if (debug)
|
213
|
{
|
214
|
int limit = 1;
|
215
|
try
|
216
|
{
|
217
|
limit=Integer.parseInt(getOption("debuglevel"));
|
218
|
|
219
|
}
|
220
|
catch (Exception e)
|
221
|
{
|
222
|
System.out.println(e.getMessage());
|
223
|
}
|
224
|
//don't allow the user set debugLevel less than or equals 0
|
225
|
if (debugLevel<=0)
|
226
|
{
|
227
|
debugLevel=1;
|
228
|
}
|
229
|
|
230
|
if (debugLevel < limit)
|
231
|
{
|
232
|
System.err.println("@debugprefix@ " +msg);
|
233
|
}
|
234
|
}
|
235
|
}
|
236
|
|
237
|
|
238
|
public static Vector getOptionList(String optiontext)
|
239
|
{
|
240
|
Vector options = new Vector();
|
241
|
if(optiontext.indexOf(",") == -1)
|
242
|
{
|
243
|
options.addElement(optiontext);
|
244
|
return options;
|
245
|
}
|
246
|
|
247
|
while(optiontext.indexOf(",") != -1)
|
248
|
{
|
249
|
String s = optiontext.substring(0, optiontext.indexOf(","));
|
250
|
options.addElement(s.trim());
|
251
|
optiontext = optiontext.substring(optiontext.indexOf(",") + 1,
|
252
|
optiontext.length());
|
253
|
if(optiontext.indexOf(",") == -1)
|
254
|
{ //catch the last list entry
|
255
|
options.addElement(optiontext.trim());
|
256
|
}
|
257
|
}
|
258
|
return options;
|
259
|
}
|
260
|
|
261
|
/** Normalizes the given string. Taken from configXML.java*/
|
262
|
public static String normalize(String s)
|
263
|
{
|
264
|
StringBuffer str = new StringBuffer();
|
265
|
|
266
|
int len = (s != null) ? s.length() : 0;
|
267
|
for (int i = 0; i < len; i++)
|
268
|
{
|
269
|
char ch = s.charAt(i);
|
270
|
switch (ch)
|
271
|
{
|
272
|
case '<':
|
273
|
{
|
274
|
str.append("<");
|
275
|
break;
|
276
|
}
|
277
|
case '>':
|
278
|
{
|
279
|
str.append(">");
|
280
|
break;
|
281
|
}
|
282
|
case '&':
|
283
|
{
|
284
|
str.append("&");
|
285
|
break;
|
286
|
}
|
287
|
case '"':
|
288
|
{
|
289
|
str.append(""");
|
290
|
break;
|
291
|
}
|
292
|
case '\r':
|
293
|
case '\n':
|
294
|
{
|
295
|
// else, default append char
|
296
|
}
|
297
|
default:
|
298
|
{
|
299
|
str.append(ch);
|
300
|
}
|
301
|
}
|
302
|
}
|
303
|
return (str.toString());
|
304
|
}
|
305
|
|
306
|
/**
|
307
|
* Utility method to get docid from a given string
|
308
|
* @param string, the given string should be these two format:
|
309
|
* 1) str1.str2 in this case docid= str1.str2
|
310
|
* 2) str1.str2.str3, in this case docid =str1.str2
|
311
|
* @param the sperator char
|
312
|
*/
|
313
|
public static String getDocIdFromString(String str)
|
314
|
{
|
315
|
String docId = null;
|
316
|
int dotNumber = 0;//count how many dots in given string
|
317
|
int indexOfLastDot = 0;
|
318
|
|
319
|
//assume that seperator is one charactor string
|
320
|
char seperator=getOption("accNumSeparator").charAt(0);
|
321
|
|
322
|
for (int i=0; i<str.length(); i++)
|
323
|
{
|
324
|
if ( str.charAt(i)==seperator)
|
325
|
{
|
326
|
dotNumber++;//count how many dots
|
327
|
indexOfLastDot=i;//keep the last dot postion
|
328
|
}
|
329
|
}//for
|
330
|
|
331
|
//The string formatt is wrong, because it has more than two or less than
|
332
|
//one seperator
|
333
|
if ( dotNumber>2 || dotNumber < 1)
|
334
|
{
|
335
|
docId=null;
|
336
|
}
|
337
|
else if (dotNumber == 2) //the case for str1.str2.str3
|
338
|
{
|
339
|
docId=str.substring(0, indexOfLastDot);
|
340
|
}
|
341
|
else if (dotNumber == 1) //the case for str1.str2
|
342
|
{
|
343
|
docId=str;
|
344
|
}
|
345
|
|
346
|
return docId;
|
347
|
}//getDocIdFromString
|
348
|
|
349
|
/**
|
350
|
* Utility method to get version number from a given string
|
351
|
* @param string, the given string should be these two format:
|
352
|
* 1) str1.str2(no version) version =-1;
|
353
|
* 2) str1.str2.str3, in this case version = str3;
|
354
|
* 3) other, vresion =-2
|
355
|
*/
|
356
|
public static int getVersionFromString(String str)
|
357
|
throws NumberFormatException
|
358
|
{
|
359
|
int version=-1;
|
360
|
String versionString=null;
|
361
|
int dotNumber = 0;//count how many dots in given string
|
362
|
int indexOfLastDot = 0;
|
363
|
|
364
|
//assume that seperator is one charactor string
|
365
|
char seperator=getOption("accNumSeparator").charAt(0);
|
366
|
|
367
|
for (int i=0; i<str.length(); i++)
|
368
|
{
|
369
|
if ( str.charAt(i)==seperator)
|
370
|
{
|
371
|
dotNumber++;//count how many dots
|
372
|
indexOfLastDot=i;//keep the last dot postion
|
373
|
}
|
374
|
}//for
|
375
|
|
376
|
//The string formatt is wrong, because it has more than two or less than
|
377
|
//one seperator
|
378
|
if ( dotNumber>2 || dotNumber < 1)
|
379
|
{
|
380
|
version=-2;
|
381
|
}
|
382
|
else if (dotNumber == 2 && (indexOfLastDot != (str.length() -1)))
|
383
|
//the case for str1.str2.str3
|
384
|
{
|
385
|
versionString=str.substring((indexOfLastDot+1), str.length());
|
386
|
version=Integer.parseInt(versionString);
|
387
|
}
|
388
|
else if (dotNumber == 1) //the case for str1.str2
|
389
|
{
|
390
|
version=-1;
|
391
|
}
|
392
|
|
393
|
return version;
|
394
|
}//getVersionFromString
|
395
|
|
396
|
|
397
|
/**
|
398
|
* Utility method to get version string from a given string
|
399
|
* @param string, the given string should be these two format:
|
400
|
* 1) str1.str2(no version) version=null;
|
401
|
* 2) str1.str2.str3, in this case version = str3;
|
402
|
* 3) other, vresion =null;
|
403
|
*/
|
404
|
public static String getRevisionStringFromString(String str)
|
405
|
throws NumberFormatException
|
406
|
{
|
407
|
// String to store the version
|
408
|
String versionString=null;
|
409
|
int dotNumber = 0;//count how many dots in given string
|
410
|
int indexOfLastDot = 0;
|
411
|
|
412
|
//assume that seperator is one charactor string
|
413
|
char seperator=getOption("accNumSeparator").charAt(0);
|
414
|
|
415
|
for (int i=0; i<str.length(); i++)
|
416
|
{
|
417
|
if ( str.charAt(i)==seperator)
|
418
|
{
|
419
|
dotNumber++;//count how many dots
|
420
|
indexOfLastDot=i;//keep the last dot postion
|
421
|
}
|
422
|
}//for
|
423
|
|
424
|
//The string formatt is wrong, because it has more than two or less than
|
425
|
//one seperator
|
426
|
if ( dotNumber>2 || dotNumber < 1)
|
427
|
{
|
428
|
versionString = null;
|
429
|
}
|
430
|
else if (dotNumber == 2 && (indexOfLastDot != (str.length() -1)))
|
431
|
{
|
432
|
//the case for str1.str2.str3
|
433
|
// indexOfLastDot != (str.length() -1) means get rid of str1.str2.
|
434
|
versionString=str.substring((indexOfLastDot+1), str.length());
|
435
|
}
|
436
|
else if (dotNumber == 1) //the case for str1.str2 or str1.str2.
|
437
|
{
|
438
|
versionString=null;
|
439
|
}
|
440
|
|
441
|
return versionString;
|
442
|
}//getVersionFromString
|
443
|
|
444
|
/**
|
445
|
* Method to get the name of local replication server
|
446
|
*/
|
447
|
public static String getLocalReplicationServerName()
|
448
|
{
|
449
|
String replicationServerName=null;
|
450
|
String serverHost=null;
|
451
|
serverHost=getOption("server");
|
452
|
// append "context/servelet/replication" to the host name
|
453
|
replicationServerName=serverHost+getOption("replicationpath");
|
454
|
return replicationServerName;
|
455
|
|
456
|
}
|
457
|
|
458
|
/**
|
459
|
* Method to get docidwithrev from eml2 inline data id
|
460
|
* The eml inline data id would look like eml.200.2.3
|
461
|
*/
|
462
|
public static String getDocIdWithoutRevFromInlineDataID(String inlineDataID)
|
463
|
{
|
464
|
String docidWithoutRev = null;
|
465
|
if ( inlineDataID == null)
|
466
|
{
|
467
|
return docidWithoutRev;
|
468
|
}
|
469
|
String seperator = MetaCatUtil.getOption("accNumSeparator");
|
470
|
char charSeperator = seperator.charAt(0);
|
471
|
int targetNumberOfSeperator = 2;// we want to know his index
|
472
|
int numberOfSeperator = 0;
|
473
|
for (int i = 0; i< inlineDataID.length(); i++)
|
474
|
{
|
475
|
// meet seperator, increase number of seperator
|
476
|
if (inlineDataID.charAt(i) == charSeperator)
|
477
|
{
|
478
|
numberOfSeperator++;
|
479
|
}
|
480
|
// if number of seperator reach the target one, record the index(i)
|
481
|
// and get substring and terminate the loop
|
482
|
if ( numberOfSeperator == targetNumberOfSeperator)
|
483
|
{
|
484
|
docidWithoutRev = inlineDataID.substring(0, i);
|
485
|
break;
|
486
|
}
|
487
|
}
|
488
|
|
489
|
MetaCatUtil.debugMessage("Docid without rev from inlinedata id: " +
|
490
|
docidWithoutRev, 35);
|
491
|
return docidWithoutRev;
|
492
|
|
493
|
}
|
494
|
|
495
|
}
|