1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements utility methods for a metadata catalog
|
4
|
* Copyright: 2009 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Michael Daigle
|
7
|
*
|
8
|
* '$Author: daigle $'
|
9
|
* '$Date: 2009-08-04 14:32:58 -0700 (Tue, 04 Aug 2009) $'
|
10
|
* '$Revision: 5015 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
package edu.ucsb.nceas.metacat.util;
|
28
|
|
29
|
import java.util.Stack;
|
30
|
import java.util.Vector;
|
31
|
|
32
|
import org.apache.log4j.Logger;
|
33
|
|
34
|
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
|
35
|
import edu.ucsb.nceas.metacat.DBSAXHandler;
|
36
|
import edu.ucsb.nceas.metacat.NodeRecord;
|
37
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
38
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
39
|
|
40
|
/**
|
41
|
* A suite of utility classes for the metadata catalog server
|
42
|
*/
|
43
|
public class DocumentUtil
|
44
|
{
|
45
|
|
46
|
public static AbstractDatabase dbAdapter;
|
47
|
|
48
|
private static Logger logMetacat = Logger.getLogger(DocumentUtil.class);
|
49
|
private static char separator = '.';
|
50
|
|
51
|
static {
|
52
|
try {
|
53
|
separator = PropertyService.getProperty("document.accNumSeparator").charAt(0);
|
54
|
} catch (PropertyNotFoundException pnfe) {
|
55
|
logMetacat.error("Could not retrieve account number separator. "
|
56
|
+ "Separator set to '.' : " + pnfe.getMessage());
|
57
|
}
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* Get docid from online/url string
|
62
|
*/
|
63
|
public static String getDocIdWithRevFromOnlineURL(String url)
|
64
|
{
|
65
|
String docid = null;
|
66
|
String DOCID = "docid";
|
67
|
boolean find = false;
|
68
|
char limited = '&';
|
69
|
int count = 0; //keep track how many & was found
|
70
|
Vector list = new Vector();// keep index number for &
|
71
|
if (url == null) {
|
72
|
logMetacat.info("url is null and null will be returned");
|
73
|
return docid;
|
74
|
}
|
75
|
// the first element in list is 0
|
76
|
list.add(new Integer(0));
|
77
|
for (int i = 0; i < url.length(); i++) {
|
78
|
if (url.charAt(i) == limited) {
|
79
|
// count plus 1
|
80
|
count++;
|
81
|
list.add(new Integer(i));
|
82
|
// get substring beween two &
|
83
|
String str = url.substring(
|
84
|
((Integer) list.elementAt(count - 1)).intValue(), i);
|
85
|
logMetacat.info("substring between two & is: " + str);
|
86
|
//if the subString contains docid, we got it
|
87
|
if (str.indexOf(DOCID) != -1) {
|
88
|
//get index of '="
|
89
|
int start = getIndexForGivenChar(str, '=') + 1;
|
90
|
int end = str.length();
|
91
|
docid = str.substring(start, end);
|
92
|
find = true;
|
93
|
}//if
|
94
|
}//if
|
95
|
}//for
|
96
|
//if not find, we need check the subtring between the index of last &
|
97
|
// and
|
98
|
// the end of string
|
99
|
if (!find) {
|
100
|
logMetacat.info("Checking the last substring");
|
101
|
String str = url.substring(((Integer) list.elementAt(count))
|
102
|
.intValue() + 1, url.length());
|
103
|
logMetacat.info("Last substring is: " + str);
|
104
|
if (str.indexOf(DOCID) != -1) {
|
105
|
//get index of '="
|
106
|
int start = getIndexForGivenChar(str, '=') + 1;
|
107
|
int end = str.length();
|
108
|
docid = str.substring(start, end);
|
109
|
find = true;
|
110
|
}//if
|
111
|
}//if
|
112
|
logMetacat.info("The docid from online url is:" + docid);
|
113
|
return docid.trim();
|
114
|
}
|
115
|
|
116
|
|
117
|
/**
|
118
|
* Eocgorid identifier will look like: ecogrid://knb/tao.1.1
|
119
|
* The AccessionNumber tao.1.1 will be returned. If the given doesn't
|
120
|
* contains ecogrid, null will be returned.
|
121
|
* @param identifier String
|
122
|
* @return String
|
123
|
*/
|
124
|
public static String getAccessionNumberFromEcogridIdentifier(String identifier)
|
125
|
{
|
126
|
String accessionNumber = null;
|
127
|
if (identifier != null && identifier.startsWith(DBSAXHandler.ECOGRID))
|
128
|
{
|
129
|
// find the last "/" in identifier
|
130
|
int indexOfLastSlash = identifier.lastIndexOf("/");
|
131
|
int start = indexOfLastSlash+1;
|
132
|
int end = identifier.length();
|
133
|
accessionNumber = identifier.substring(start, end);
|
134
|
}
|
135
|
logMetacat.warn("The accession number from url is " +
|
136
|
accessionNumber);
|
137
|
return accessionNumber;
|
138
|
}
|
139
|
|
140
|
private static int getIndexForGivenChar(String str, char character)
|
141
|
{
|
142
|
int index = -1;
|
143
|
// make sure str is not null
|
144
|
if (str == null) {
|
145
|
logMetacat.info(
|
146
|
"The given str is null and -1 will be returned");
|
147
|
return index;
|
148
|
}
|
149
|
// got though the string
|
150
|
for (int i = 0; i < str.length(); i++) {
|
151
|
// find the first one then break the loop
|
152
|
if (str.charAt(i) == character) {
|
153
|
index = i;
|
154
|
break;
|
155
|
}//if
|
156
|
}//for
|
157
|
logMetacat.info("the index for char " + character + " is: "
|
158
|
+ index);
|
159
|
return index;
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* Utility method to get docid from a given string
|
164
|
*
|
165
|
* @param string, the given string should be these two format: 1) str1.str2
|
166
|
* in this case docid= str1.str2 2) str1.str2.str3, in this case
|
167
|
* docid =str1.str2
|
168
|
* @param the sperator char
|
169
|
*/
|
170
|
public static String getDocIdFromString(String str)
|
171
|
{
|
172
|
String docId = null;
|
173
|
if (str == null) {
|
174
|
logMetacat.info(
|
175
|
"The given str is null and null will be returned"
|
176
|
+ " in getDocIdfromString");
|
177
|
return docId;
|
178
|
} //make sure docid is not null
|
179
|
int dotNumber = 0;//count how many dots in given string
|
180
|
int indexOfLastDot = 0;
|
181
|
|
182
|
for (int i = 0; i < str.length(); i++) {
|
183
|
if (str.charAt(i) == separator) {
|
184
|
dotNumber++;//count how many dots
|
185
|
indexOfLastDot = i;//keep the last dot postion
|
186
|
}
|
187
|
}//for
|
188
|
|
189
|
//The string formatt is wrong, because it has more than two or less
|
190
|
// than
|
191
|
//one seperator
|
192
|
if (dotNumber > 2 || dotNumber < 1) {
|
193
|
docId = null;
|
194
|
} else if (dotNumber == 2) //the case for str1.str2.str3
|
195
|
{
|
196
|
docId = str.substring(0, indexOfLastDot);
|
197
|
} else if (dotNumber == 1) //the case for str1.str2
|
198
|
{
|
199
|
docId = str;
|
200
|
}
|
201
|
|
202
|
return docId;
|
203
|
}//getDocIdFromString
|
204
|
|
205
|
/**
|
206
|
* Utility method to get version number from a given string
|
207
|
*
|
208
|
* @param string, the given string should be these two format: 1)
|
209
|
* str1.str2(no version) version =-1; 2) str1.str2.str3, in this
|
210
|
* case version = str3; 3) other, vresion =-2
|
211
|
*/
|
212
|
public static int getVersionFromString(String str)
|
213
|
throws NumberFormatException
|
214
|
{
|
215
|
int version = -1;
|
216
|
String versionString = null;
|
217
|
int dotNumber = 0;//count how many dots in given string
|
218
|
int indexOfLastDot = 0;
|
219
|
|
220
|
for (int i = 0; i < str.length(); i++) {
|
221
|
if (str.charAt(i) == separator) {
|
222
|
dotNumber++;//count how many dots
|
223
|
indexOfLastDot = i;//keep the last dot postion
|
224
|
}
|
225
|
}//for
|
226
|
|
227
|
//The string formatt is wrong, because it has more than two or less
|
228
|
// than
|
229
|
//one seperator
|
230
|
if (dotNumber > 2 || dotNumber < 1) {
|
231
|
version = -2;
|
232
|
} else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1)))
|
233
|
//the case for str1.str2.str3
|
234
|
{
|
235
|
versionString = str.substring((indexOfLastDot + 1), str.length());
|
236
|
version = Integer.parseInt(versionString);
|
237
|
} else if (dotNumber == 1) //the case for str1.str2
|
238
|
{
|
239
|
version = -1;
|
240
|
}
|
241
|
|
242
|
return version;
|
243
|
}//getVersionFromString
|
244
|
|
245
|
/**
|
246
|
* Utility method to get version string from a given string
|
247
|
*
|
248
|
* @param string, the given string should be these two format: 1)
|
249
|
* str1.str2(no version) version=null; 2) str1.str2.str3, in
|
250
|
* this case version = str3; 3) other, vresion =null;
|
251
|
*/
|
252
|
public static String getRevisionStringFromString(String str)
|
253
|
throws NumberFormatException
|
254
|
{
|
255
|
// String to store the version
|
256
|
String versionString = null;
|
257
|
int dotNumber = 0;//count how many dots in given string
|
258
|
int indexOfLastDot = 0;
|
259
|
|
260
|
for (int i = 0; i < str.length(); i++) {
|
261
|
if (str.charAt(i) == separator) {
|
262
|
dotNumber++;//count how many dots
|
263
|
indexOfLastDot = i;//keep the last dot postion
|
264
|
}
|
265
|
}//for
|
266
|
|
267
|
//The string formatt is wrong, because it has more than two or less
|
268
|
// than
|
269
|
//one seperator
|
270
|
if (dotNumber > 2 || dotNumber < 1) {
|
271
|
versionString = null;
|
272
|
} else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1))) {
|
273
|
//the case for str1.str2.str3
|
274
|
// indexOfLastDot != (str.length() -1) means get rid of str1.str2.
|
275
|
versionString = str.substring((indexOfLastDot + 1), str.length());
|
276
|
} else if (dotNumber == 1) //the case for str1.str2 or str1.str2.
|
277
|
{
|
278
|
versionString = null;
|
279
|
}
|
280
|
|
281
|
return versionString;
|
282
|
}//getVersionFromString
|
283
|
|
284
|
/**
|
285
|
* This method will get docid from an AccessionNumber. There is no
|
286
|
* assumption the accessnumber will be str1.str2.str3. It can be more. So
|
287
|
* we think the docid will be get rid of last part
|
288
|
*/
|
289
|
public static String getDocIdFromAccessionNumber(String accessionNumber)
|
290
|
{
|
291
|
String docid = null;
|
292
|
if (accessionNumber == null) { return docid; }
|
293
|
int indexOfLastSeperator = accessionNumber.lastIndexOf(separator);
|
294
|
docid = accessionNumber.substring(0, indexOfLastSeperator);
|
295
|
logMetacat.info("after parsing accessionnumber, docid is "
|
296
|
+ docid);
|
297
|
return docid;
|
298
|
}
|
299
|
|
300
|
/**
|
301
|
* This method will get inline data id without the revision number.
|
302
|
* So if inlineData.1.2 is passed as input, inlineData.2 is returned.
|
303
|
*/
|
304
|
public static String getInlineDataIdWithoutRev(String accessionNumber)
|
305
|
{
|
306
|
String docid = null;
|
307
|
if (accessionNumber == null) { return docid; }
|
308
|
int indexOfLastSeperator = accessionNumber.lastIndexOf(separator);
|
309
|
String version = accessionNumber.substring(indexOfLastSeperator,
|
310
|
accessionNumber.length());
|
311
|
accessionNumber = accessionNumber.substring(0, indexOfLastSeperator);
|
312
|
indexOfLastSeperator = accessionNumber.lastIndexOf(separator);
|
313
|
docid = accessionNumber.substring(0, indexOfLastSeperator) + version;
|
314
|
logMetacat.info("after parsing accessionnumber, docid is "
|
315
|
+ docid);
|
316
|
|
317
|
return docid;
|
318
|
}
|
319
|
|
320
|
/**
|
321
|
* This method will call both getDocIdFromString and
|
322
|
* getDocIdFromAccessionNumber. So first, if the string looks str1.str2,
|
323
|
* the docid will be str1.str2. If the string is str1.str2.str3, the docid
|
324
|
* will be str1.str2. If the string is str1.str2.str3.str4 or more, the
|
325
|
* docid will be str1.str2.str3. If the string look like str1, null will be
|
326
|
* returned
|
327
|
*
|
328
|
*/
|
329
|
public static String getSmartDocId(String str)
|
330
|
{
|
331
|
String docid = null;
|
332
|
//call geDocIdFromString first.
|
333
|
docid = getDocIdFromString(str);
|
334
|
// If docid is null, try to call getDocIdFromAccessionNumber
|
335
|
// it will handle the seperator more than2
|
336
|
if (docid == null) {
|
337
|
docid = getDocIdFromAccessionNumber(str);
|
338
|
}
|
339
|
logMetacat.info("The docid get from smart docid getor is "
|
340
|
+ docid);
|
341
|
return docid;
|
342
|
}
|
343
|
|
344
|
/**
|
345
|
* This method will get revision from an AccessionNumber. There is no
|
346
|
* assumption the accessnumber will be str1.str2.str3. It can be more. So
|
347
|
* we think the docid will be get rid of last part
|
348
|
*/
|
349
|
public static int getRevisionFromAccessionNumber(String accessionNumber)
|
350
|
throws NumberFormatException
|
351
|
{
|
352
|
String rev = null;
|
353
|
int revNumber = -1;
|
354
|
if (accessionNumber == null) { return revNumber; }
|
355
|
int indexOfLastSeperator = accessionNumber.lastIndexOf(separator);
|
356
|
rev = accessionNumber.substring(indexOfLastSeperator + 1,
|
357
|
accessionNumber.length());
|
358
|
revNumber = Integer.parseInt(rev);
|
359
|
logMetacat.info("after parsing accessionnumber, rev is "
|
360
|
+ revNumber);
|
361
|
return revNumber;
|
362
|
}
|
363
|
|
364
|
/**
|
365
|
* Method to get docidwithrev from eml2 inline data id The eml inline data
|
366
|
* id would look like eml.200.2.3
|
367
|
*/
|
368
|
public static String getDocIdWithoutRevFromInlineDataID(String inlineDataID)
|
369
|
{
|
370
|
String docidWithoutRev = null;
|
371
|
if (inlineDataID == null) { return docidWithoutRev; }
|
372
|
char charSeperator = separator;
|
373
|
int targetNumberOfSeperator = 2;// we want to know his index
|
374
|
int numberOfSeperator = 0;
|
375
|
for (int i = 0; i < inlineDataID.length(); i++) {
|
376
|
// meet seperator, increase number of seperator
|
377
|
if (inlineDataID.charAt(i) == charSeperator) {
|
378
|
numberOfSeperator++;
|
379
|
}
|
380
|
// if number of seperator reach the target one, record the index(i)
|
381
|
// and get substring and terminate the loop
|
382
|
if (numberOfSeperator == targetNumberOfSeperator) {
|
383
|
docidWithoutRev = inlineDataID.substring(0, i);
|
384
|
break;
|
385
|
}
|
386
|
}
|
387
|
|
388
|
logMetacat.info("Docid without rev from inlinedata id: "
|
389
|
+ docidWithoutRev);
|
390
|
return docidWithoutRev;
|
391
|
|
392
|
}
|
393
|
|
394
|
/**
|
395
|
* Revise stack change a stack to opposite order
|
396
|
*/
|
397
|
public static Stack<NodeRecord> reviseStack(Stack<NodeRecord> stack)
|
398
|
{
|
399
|
Stack result = new Stack();
|
400
|
// make sure the parameter is correct
|
401
|
if (stack == null || stack.isEmpty()) {
|
402
|
result = stack;
|
403
|
return result;
|
404
|
}
|
405
|
|
406
|
while (!stack.isEmpty()) {
|
407
|
Object obj = stack.pop();
|
408
|
result.push(obj);
|
409
|
}
|
410
|
return result;
|
411
|
}
|
412
|
|
413
|
}
|