Project

General

Profile

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: sgarg $'
10
 *     '$Date: 2005-04-04 16:45:17 -0700 (Mon, 04 Apr 2005) $'
11
 * '$Revision: 2449 $'
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.MalformedURLException;
32
import java.net.URL;
33
import java.util.Hashtable;
34
import java.util.Stack;
35
import java.util.Vector;
36

    
37
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
38
import edu.ucsb.nceas.utilities.Options;
39

    
40
/**
41
 * A suite of utility classes for the metadata catalog server
42
 */
43
public class MetaCatUtil
44
{
45

    
46
    public static AbstractDatabase dbAdapter;
47

    
48
    private static Options options = null;
49

    
50
    private static boolean debug = true;
51

    
52

    
53
    /**
54
     * Determine our db adapter class and create an instance of that class
55
     */
56
    static {
57
        try {
58
            dbAdapter = (AbstractDatabase) createObject(getOption("dbAdapter"));
59
        } catch (Exception e) {
60
            System.err.println("Error in MetaCatUtil static block:"
61
                    + e.getMessage());
62
        }
63
    }
64

    
65
    /**
66
     * Instantiate a class using the name of the class at runtime
67
     *
68
     * @param className the fully qualified name of the class to instantiate
69
     */
70
    public static Object createObject(String className) throws Exception
71
    {
72

    
73
        Object object = null;
74
        try {
75
            Class classDefinition = Class.forName(className);
76
            object = classDefinition.newInstance();
77
        } catch (InstantiationException e) {
78
            throw e;
79
        } catch (IllegalAccessException e) {
80
            throw e;
81
        } catch (ClassNotFoundException e) {
82
            throw e;
83
        }
84
        return object;
85
    }
86

    
87
    /**
88
     * Utility method to get an option value from the properties file
89
     *
90
     * @param optionName the name of the option requested
91
     * @return the String value for the option, or null if not set
92
     */
93
    public static String getOption(String optionName)
94
    {
95
        if (options == null) {
96
            options = Options.getInstance();
97
        }
98
        String value = options.getOption(optionName);
99
        return value;
100
    }
101

    
102
    /** Utility method to convert a file handle into a URL */
103
    public static URL fileToURL(File file)
104
    {
105
        String path = file.getAbsolutePath();
106
        String fSep = System.getProperty("file.separator");
107
        if (fSep != null && fSep.length() == 1)
108
                path = path.replace(fSep.charAt(0), '/');
109
        if (path.length() > 0 && path.charAt(0) != '/') path = '/' + path;
110
        try {
111
            return new URL("file", null, path);
112
        } catch (java.net.MalformedURLException e) {
113
            /*
114
             * According to the spec this could only happen if the file
115
             */
116
            throw new Error("unexpected MalformedURLException");
117
        }
118
    }
119

    
120
    /**
121
     * Utility method to parse the query part of a URL into parameters. This
122
     * method assumes the format of the query par tof the url is an ampersand
123
     * separated list of name/value pairs, with equal signs separating the name
124
     * from the value (e.g., name=tom&zip=99801 ). Returns a has of the name
125
     * value pairs, hashed on name.
126
     */
127
    public static Hashtable parseQuery(String query)
128
            throws MalformedURLException
129
    {
130
        String[][] params = new String[200][2];
131
        Hashtable parameters = new Hashtable();
132

    
133
        String temp = "";
134
        boolean ampflag = true;
135
        boolean poundflag = false;
136
        int arrcount = 0;
137

    
138
        if (query != null) {
139
            for (int i = 0; i < query.length(); i++) {
140

    
141
                // go throught the remainder of the query one character at a
142
                // time.
143
                if (query.charAt(i) == '=') {
144
                    // if the current char is a # then the preceding should be
145
                    // a name
146
                    if (!poundflag && ampflag) {
147
                        params[arrcount][0] = temp.trim();
148
                        temp = "";
149
                    } else {
150
                        //if there are two #s or &s in a row throw an
151
                        // exception.
152
                        throw new MalformedURLException(
153
                                "metacatURL: Two parameter names "
154
                                        + "not allowed in sequence");
155
                    }
156
                    poundflag = true;
157
                    ampflag = false;
158
                } else if (query.charAt(i) == '&' || i == query.length() - 1) {
159
                    //the text preceding the & should be the param value.
160
                    if (i == query.length() - 1) {
161
                        //if at the end of the string grab the last value and
162
                        // append it.
163
                        if (query.charAt(i) != '=') {
164
                            //ignore an extra & on the end of the string
165
                            temp += query.charAt(i);
166
                        }
167
                    }
168

    
169
                    if (!ampflag && poundflag) {
170
                        params[arrcount][1] = temp.trim();
171
                        parameters
172
                                .put(params[arrcount][0], params[arrcount][1]);
173
                        temp = "";
174
                        arrcount++; //increment the array to the next row.
175
                    } else {
176
                        //if there are two =s or &s in a row through an
177
                        // exception
178
                        throw new MalformedURLException(
179
                                "metacatURL: Two parameter values "
180
                                        + "not allowed in sequence");
181
                    }
182
                    poundflag = false;
183
                    ampflag = true;
184
                } else {
185
                    //get the next character in the string
186
                    temp += query.charAt(i);
187
                }
188
            }
189
        }
190
        return parameters;
191
    }
192

    
193
    /**
194
     * Utility method to print debugging messages. User can set debug level for
195
     * this message. The number is fewer, the message is more important
196
     *
197
     * @param msg, the content of the message
198
     * @param debugLevel, an integer indicating the message debug leve
199
     */
200
    public static void debugMessage(String msg, int debugLevel)
201
    {
202
        if (debug) {
203
            int limit = 1;
204
            try {
205
                limit = Integer.parseInt(getOption("debuglevel"));
206

    
207
            } catch (Exception e) {
208
                System.out.println(e.getMessage());
209
            }
210
            //don't allow the user set debugLevel less than or equals 0
211
            if (debugLevel <= 0) {
212
                debugLevel = 1;
213
            }
214

    
215
            if (debugLevel < limit) {
216
                System.err.println("@debugprefix@ " + msg);
217
            }
218
        }
219
    }
220

    
221
    public static Vector getOptionList(String optiontext)
222
    {
223
        Vector optionsVector = new Vector();
224
        if (optiontext.indexOf(",") == -1) {
225
            optionsVector.addElement(optiontext);
226
            return optionsVector;
227
        }
228

    
229
        while (optiontext.indexOf(",") != -1) {
230
            String s = optiontext.substring(0, optiontext.indexOf(","));
231
            optionsVector.addElement(s.trim());
232
            optiontext = optiontext.substring(optiontext.indexOf(",") + 1,
233
                    optiontext.length());
234
            if (optiontext.indexOf(",") == -1) { //catch the last list entry
235
                optionsVector.addElement(optiontext.trim());
236
            }
237
        }
238
        return optionsVector;
239
    }
240

    
241
    /** Normalizes the given string. Taken from configXML.java */
242
    public static String normalize(String s)
243
    {
244
        StringBuffer str = new StringBuffer();
245

    
246
             int len = (s != null) ? s.length() : 0;
247
             for (int i = 0; i < len; i++) {
248
                 char ch = s.charAt(i);
249
                 switch (ch) {
250
                     case '<': {
251
                         str.append("&lt;");
252
                         break;
253
                     }
254
                     case '>': {
255
                         str.append("&gt;");
256
                         break;
257
                     }
258
                     case '&': {
259
                         /*
260
                          * patch provided by Johnoel Ancheta from U of Hawaii
261
                          */
262
                         // check if & is for a character reference &#xnnnn;
263
                         if (i + 1 < len - 1 && s.charAt(i + 1) == '#') {
264
                             str.append("&#");
265
                             i += 2;
266

    
267
                             ch = s.charAt(i);
268
                             while (i < len && ch != ';') {
269
                                 str.append(ch);
270
                                 i++;
271
                                 ch = s.charAt(i);
272
                             }
273
                             str.append(';');
274
                         }
275
                         else
276
                             str.append("&amp;");
277
                         /////////
278
                         break;
279
                     }
280
                    default: {
281
                         if ( (ch<128) && (ch>31) ) {
282
                             str.append(ch);
283
                         }
284
                         else if (ch<32) {
285
                             if (ch == 10) { // new line
286
                                 str.append(ch);
287
                             }
288
                             if (ch == 13) { // carriage return
289
                                 str.append(ch);
290
                             }
291
                             if (ch == 9) {  // tab
292
                                 str.append(ch);
293
                             }
294
                             // otherwise skip
295
                         }
296
                         else {
297
                             str.append("&#");
298
                             str.append(Integer.toString(ch));
299
                             str.append(';');
300
                         }
301
                     }
302
                 }
303
             }
304
             return str.toString();
305
    }
306

    
307
    /**
308
     * Get docid from online/url string
309
     */
310
    public static String getDocIdWithRevFromOnlineURL(String url)
311
    {
312
        String docid = null;
313
        String DOCID = "docid";
314
        boolean find = false;
315
        char limited = '&';
316
        int count = 0; //keep track how many & was found
317
        Vector list = new Vector();// keep index number for &
318
        if (url == null) {
319
            MetaCatUtil.debugMessage("url is null and null will be returned",
320
                    30);
321
            return docid;
322
        }
323
        // the first element in list is 0
324
        list.add(new Integer(0));
325
        for (int i = 0; i < url.length(); i++) {
326
            if (url.charAt(i) == limited) {
327
                // count plus 1
328
                count++;
329
                list.add(new Integer(i));
330
                // get substring beween two &
331
                String str = url.substring(
332
                        ((Integer) list.elementAt(count - 1)).intValue(), i);
333
                MetaCatUtil.debugMessage("substring between two & is: " + str,
334
                        30);
335
                //if the subString contains docid, we got it
336
                if (str.indexOf(DOCID) != -1) {
337
                    //get index of '="
338
                    int start = getIndexForGivenChar(str, '=') + 1;
339
                    int end = str.length();
340
                    docid = str.substring(start, end);
341
                    find = true;
342
                }//if
343
            }//if
344
        }//for
345
        //if not find, we need check the subtring between the index of last &
346
        // and
347
        // the end of string
348
        if (!find) {
349
            MetaCatUtil.debugMessage("Checking the last substring", 35);
350
            String str = url.substring(((Integer) list.elementAt(count))
351
                    .intValue() + 1, url.length());
352
            MetaCatUtil.debugMessage("Last substring is: " + str, 30);
353
            if (str.indexOf(DOCID) != -1) {
354
                //get index of '="
355
                int start = getIndexForGivenChar(str, '=') + 1;
356
                int end = str.length();
357
                docid = str.substring(start, end);
358
                find = true;
359
            }//if
360
        }//if
361
        MetaCatUtil.debugMessage("The docid from online url is:" + docid, 30);
362
        return docid.trim();
363
    }
364

    
365

    
366
    /**
367
     * Eocgorid identifier will look like: ecogrid://knb/tao.1.1
368
     * The AccessionNumber tao.1.1 will be returned. If the given doesn't
369
     * contains ecogrid, null will be returned.
370
     * @param identifier String
371
     * @return String
372
     */
373
    public static String getAccessionNumberFromEcogridIdentifier(String identifier)
374
    {
375
      String accessionNumber = null;
376
      if (identifier != null && identifier.startsWith(DBSAXHandler.ECOGRID))
377
      {
378
        // find the last "/" in identifier
379
        int indexOfLastSlash = identifier.lastIndexOf("/");
380
        int start = indexOfLastSlash+1;
381
        int end   = identifier.length();
382
        accessionNumber = identifier.substring(start, end);
383
      }
384
      MetaCatUtil.debugMessage("The accession number from url is " +
385
                                 accessionNumber, 10);
386
      return accessionNumber;
387
    }
388

    
389
    private static int getIndexForGivenChar(String str, char character)
390
    {
391
        int index = -1;
392
        // make sure str is not null
393
        if (str == null) {
394
            MetaCatUtil.debugMessage(
395
                    "The given str is null and -1 will be returned", 30);
396
            return index;
397
        }
398
        // got though the string
399
        for (int i = 0; i < str.length(); i++) {
400
            // find the first one then break the loop
401
            if (str.charAt(i) == character) {
402
                index = i;
403
                break;
404
            }//if
405
        }//for
406
        MetaCatUtil.debugMessage("the index for char " + character + " is: "
407
                + index, 30);
408
        return index;
409
    }
410

    
411
    /**
412
     * Utility method to get docid from a given string
413
     *
414
     * @param string, the given string should be these two format: 1) str1.str2
415
     *            in this case docid= str1.str2 2) str1.str2.str3, in this case
416
     *            docid =str1.str2
417
     * @param the sperator char
418
     */
419
    public static String getDocIdFromString(String str)
420
    {
421
        String docId = null;
422
        if (str == null) {
423
            MetaCatUtil.debugMessage(
424
                    "The given str is null and null will be returned"
425
                            + " in getDocIdfromString", 30);
426
            return docId;
427
        } //make sure docid is not null
428
        int dotNumber = 0;//count how many dots in given string
429
        int indexOfLastDot = 0;
430

    
431
        //assume that seperator is one charactor string
432
        char seperator = getOption("accNumSeparator").charAt(0);
433

    
434
        for (int i = 0; i < str.length(); i++) {
435
            if (str.charAt(i) == seperator) {
436
                dotNumber++;//count how many dots
437
                indexOfLastDot = i;//keep the last dot postion
438
            }
439
        }//for
440

    
441
        //The string formatt is wrong, because it has more than two or less
442
        // than
443
        //one seperator
444
        if (dotNumber > 2 || dotNumber < 1) {
445
            docId = null;
446
        } else if (dotNumber == 2) //the case for str1.str2.str3
447
        {
448
            docId = str.substring(0, indexOfLastDot);
449
        } else if (dotNumber == 1) //the case for str1.str2
450
        {
451
            docId = str;
452
        }
453

    
454
        return docId;
455
    }//getDocIdFromString
456

    
457
    /**
458
     * Utility method to get version number from a given string
459
     *
460
     * @param string, the given string should be these two format: 1)
461
     *            str1.str2(no version) version =-1; 2) str1.str2.str3, in this
462
     *            case version = str3; 3) other, vresion =-2
463
     */
464
    public static int getVersionFromString(String str)
465
            throws NumberFormatException
466
    {
467
        int version = -1;
468
        String versionString = null;
469
        int dotNumber = 0;//count how many dots in given string
470
        int indexOfLastDot = 0;
471

    
472
        //assume that seperator is one charactor string
473
        char seperator = getOption("accNumSeparator").charAt(0);
474

    
475
        for (int i = 0; i < str.length(); i++) {
476
            if (str.charAt(i) == seperator) {
477
                dotNumber++;//count how many dots
478
                indexOfLastDot = i;//keep the last dot postion
479
            }
480
        }//for
481

    
482
        //The string formatt is wrong, because it has more than two or less
483
        // than
484
        //one seperator
485
        if (dotNumber > 2 || dotNumber < 1) {
486
            version = -2;
487
        } else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1)))
488
        //the case for str1.str2.str3
489
        {
490
            versionString = str.substring((indexOfLastDot + 1), str.length());
491
            version = Integer.parseInt(versionString);
492
        } else if (dotNumber == 1) //the case for str1.str2
493
        {
494
            version = -1;
495
        }
496

    
497
        return version;
498
    }//getVersionFromString
499

    
500
    /**
501
     * Utility method to get version string from a given string
502
     *
503
     * @param string, the given string should be these two format: 1)
504
     *            str1.str2(no version) version=null; 2) str1.str2.str3, in
505
     *            this case version = str3; 3) other, vresion =null;
506
     */
507
    public static String getRevisionStringFromString(String str)
508
            throws NumberFormatException
509
    {
510
        // String to store the version
511
        String versionString = null;
512
        int dotNumber = 0;//count how many dots in given string
513
        int indexOfLastDot = 0;
514

    
515
        //assume that seperator is one charactor string
516
        char seperator = getOption("accNumSeparator").charAt(0);
517

    
518
        for (int i = 0; i < str.length(); i++) {
519
            if (str.charAt(i) == seperator) {
520
                dotNumber++;//count how many dots
521
                indexOfLastDot = i;//keep the last dot postion
522
            }
523
        }//for
524

    
525
        //The string formatt is wrong, because it has more than two or less
526
        // than
527
        //one seperator
528
        if (dotNumber > 2 || dotNumber < 1) {
529
            versionString = null;
530
        } else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1))) {
531
            //the case for str1.str2.str3
532
            // indexOfLastDot != (str.length() -1) means get rid of str1.str2.
533
            versionString = str.substring((indexOfLastDot + 1), str.length());
534
        } else if (dotNumber == 1) //the case for str1.str2 or str1.str2.
535
        {
536
            versionString = null;
537
        }
538

    
539
        return versionString;
540
    }//getVersionFromString
541

    
542
    /**
543
     * This method will get docid from an AccessionNumber. There is no
544
     * assumption the accessnumber will be str1.str2.str3. It can be more. So
545
     * we think the docid will be get rid of last part
546
     */
547
    public static String getDocIdFromAccessionNumber(String accessionNumber)
548
    {
549
        String docid = null;
550
        if (accessionNumber == null) { return docid; }
551
        String seperator = getOption("accNumSeparator");
552
        int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
553
        docid = accessionNumber.substring(0, indexOfLastSeperator);
554
        MetaCatUtil.debugMessage("after parsing accessionnumber, docid is "
555
                + docid, 30);
556
        return docid;
557
    }
558

    
559
    /**
560
     * This method will get inline data id without the revision number.
561
     * So if inlineData.1.2 is passed as input, inlineData.2 is returned.
562
     */
563
    public static String getInlineDataIdWithoutRev(String accessionNumber)
564
    {
565
        String docid = null;
566
        if (accessionNumber == null) { return docid; }
567
        String seperator = getOption("accNumSeparator");
568
        int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
569
        String version = accessionNumber.substring(indexOfLastSeperator,
570
                                                   accessionNumber.length());
571
        accessionNumber = accessionNumber.substring(0, indexOfLastSeperator);
572
        indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
573
        docid = accessionNumber.substring(0, indexOfLastSeperator) + version;
574
        MetaCatUtil.debugMessage("after parsing accessionnumber, docid is "
575
                                 + docid, 30);
576

    
577
        return docid;
578
    }
579

    
580
    /**
581
     * This method will call both getDocIdFromString and
582
     * getDocIdFromAccessionNumber. So first, if the string looks str1.str2,
583
     * the docid will be str1.str2. If the string is str1.str2.str3, the docid
584
     * will be str1.str2. If the string is str1.str2.str3.str4 or more, the
585
     * docid will be str1.str2.str3. If the string look like str1, null will be
586
     * returned
587
     *
588
     */
589
    public static String getSmartDocId(String str)
590
    {
591
        String docid = null;
592
        //call geDocIdFromString first.
593
        docid = getDocIdFromString(str);
594
        // If docid is null, try to call getDocIdFromAccessionNumber
595
        // it will handle the seperator more than2
596
        if (docid == null) {
597
            docid = getDocIdFromAccessionNumber(str);
598
        }
599
        MetaCatUtil.debugMessage("The docid get from smart docid getor is "
600
                + docid, 30);
601
        return docid;
602
    }
603

    
604
    /**
605
     * This method will get revision from an AccessionNumber. There is no
606
     * assumption the accessnumber will be str1.str2.str3. It can be more. So
607
     * we think the docid will be get rid of last part
608
     */
609
    public static int getRevisionFromAccessionNumber(String accessionNumber)
610
            throws NumberFormatException
611
    {
612
        String rev = null;
613
        int revNumber = -1;
614
        if (accessionNumber == null) { return revNumber; }
615
        String seperator = getOption("accNumSeparator");
616
        int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
617
        rev = accessionNumber.substring(indexOfLastSeperator + 1,
618
                accessionNumber.length());
619
        revNumber = Integer.parseInt(rev);
620
        MetaCatUtil.debugMessage("after parsing accessionnumber, rev is "
621
                + revNumber, 30);
622
        return revNumber;
623
    }
624

    
625
    /**
626
     * Method to get the name of local replication server
627
     */
628
    public static String getLocalReplicationServerName()
629
    {
630
        String replicationServerName = null;
631
        String serverHost = null;
632
        serverHost = getOption("server");
633
        // append "context/servelet/replication" to the host name
634
        replicationServerName = serverHost + getOption("replicationpath");
635
        return replicationServerName;
636

    
637
    }
638

    
639
    /**
640
     * Method to get docidwithrev from eml2 inline data id The eml inline data
641
     * id would look like eml.200.2.3
642
     */
643
    public static String getDocIdWithoutRevFromInlineDataID(String inlineDataID)
644
    {
645
        String docidWithoutRev = null;
646
        if (inlineDataID == null) { return docidWithoutRev; }
647
        String seperator = MetaCatUtil.getOption("accNumSeparator");
648
        char charSeperator = seperator.charAt(0);
649
        int targetNumberOfSeperator = 2;// we want to know his index
650
        int numberOfSeperator = 0;
651
        for (int i = 0; i < inlineDataID.length(); i++) {
652
            // meet seperator, increase number of seperator
653
            if (inlineDataID.charAt(i) == charSeperator) {
654
                numberOfSeperator++;
655
            }
656
            // if number of seperator reach the target one, record the index(i)
657
            // and get substring and terminate the loop
658
            if (numberOfSeperator == targetNumberOfSeperator) {
659
                docidWithoutRev = inlineDataID.substring(0, i);
660
                break;
661
            }
662
        }
663

    
664
        MetaCatUtil.debugMessage("Docid without rev from inlinedata id: "
665
                + docidWithoutRev, 35);
666
        return docidWithoutRev;
667

    
668
    }
669

    
670
    /**
671
     * Revise stack change a stack to opposite order
672
     */
673
    public static Stack reviseStack(Stack stack)
674
    {
675
        Stack result = new Stack();
676
        // make sure the parameter is correct
677
        if (stack == null || stack.isEmpty()) {
678
            result = stack;
679
            return result;
680
        }
681

    
682
        while (!stack.isEmpty()) {
683
            Object obj = stack.pop();
684
            result.push(obj);
685
        }
686
        return result;
687
    }
688

    
689
    /** A method to replace whitespace in url */
690
    public static String replaceWhiteSpaceForURL(String urlHasWhiteSpace)
691
    {
692
        StringBuffer newUrl = new StringBuffer();
693
        String whiteSpaceReplace = "%20";
694
        if (urlHasWhiteSpace == null || urlHasWhiteSpace.trim().equals("")) { return null; }
695

    
696
        for (int i = 0; i < urlHasWhiteSpace.length(); i++) {
697
            char ch = urlHasWhiteSpace.charAt(i);
698
            if (!Character.isWhitespace(ch)) {
699
                newUrl.append(ch);
700
            } else {
701
                //it is white sapce, replace it by %20
702
                newUrl = newUrl.append(whiteSpaceReplace);
703
            }
704

    
705
        }//for
706
        MetaCatUtil.debugMessage("The new string without space is:"
707
                + newUrl.toString(), 35);
708
        return newUrl.toString();
709

    
710
    }// replaceWhiteSpaceForUR
711

    
712
}
(43-43/63)