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: tao $'
10
 *     '$Date: 2005-08-30 17:03:57 -0700 (Tue, 30 Aug 2005) $'
11
 * '$Revision: 2554 $'
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
    public static Vector pathsForIndexing;
49

    
50
    private static Options options = null;
51

    
52
    private static boolean debug = true;
53

    
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:"
63
                    + e.getMessage());
64
        }
65
    }
66

    
67
    /**
68
     * Instantiate a class using the name of the class at runtime
69
     *
70
     * @param className the fully qualified name of the class to instantiate
71
     */
72
    public static Object createObject(String className) throws Exception
73
    {
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
     * @return the String value for the option, or null if not set
94
     */
95
    public static String getOption(String optionName)
96
    {
97
        if (options == null) {
98
            options = Options.getInstance();
99
        }
100
        String value = options.getOption(optionName);
101
        return value;
102
    }
103
    
104
    /**
105
     * Utility method to set an option value from the properties file
106
     *
107
     * @param optionName the name of the option requested
108
     */
109
    public static void setOption(String optionName, String newValue)
110
    {
111
        if (options == null) {
112
            options = Options.getInstance();
113
        }
114
        options.setOption(optionName, newValue);
115
        
116
    }
117

    
118
    /** Utility method to convert a file handle into a URL */
119
    public static URL fileToURL(File file)
120
    {
121
        String path = file.getAbsolutePath();
122
        String fSep = System.getProperty("file.separator");
123
        if (fSep != null && fSep.length() == 1)
124
                path = path.replace(fSep.charAt(0), '/');
125
        if (path.length() > 0 && path.charAt(0) != '/') path = '/' + path;
126
        try {
127
            return new URL("file", null, path);
128
        } catch (java.net.MalformedURLException e) {
129
            /*
130
             * According to the spec this could only happen if the file
131
             */
132
            throw new Error("unexpected MalformedURLException");
133
        }
134
    }
135

    
136
    /**
137
     * Utility method to parse the query part of a URL into parameters. This
138
     * method assumes the format of the query par tof the url is an ampersand
139
     * separated list of name/value pairs, with equal signs separating the name
140
     * from the value (e.g., name=tom&zip=99801 ). Returns a has of the name
141
     * value pairs, hashed on name.
142
     */
143
    public static Hashtable parseQuery(String query)
144
            throws MalformedURLException
145
    {
146
        String[][] params = new String[200][2];
147
        Hashtable parameters = new Hashtable();
148

    
149
        String temp = "";
150
        boolean ampflag = true;
151
        boolean poundflag = false;
152
        int arrcount = 0;
153

    
154
        if (query != null) {
155
            for (int i = 0; i < query.length(); i++) {
156

    
157
                // go throught the remainder of the query one character at a
158
                // time.
159
                if (query.charAt(i) == '=') {
160
                    // if the current char is a # then the preceding should be
161
                    // a name
162
                    if (!poundflag && ampflag) {
163
                        params[arrcount][0] = temp.trim();
164
                        temp = "";
165
                    } else {
166
                        //if there are two #s or &s in a row throw an
167
                        // exception.
168
                        throw new MalformedURLException(
169
                                "metacatURL: Two parameter names "
170
                                        + "not allowed in sequence");
171
                    }
172
                    poundflag = true;
173
                    ampflag = false;
174
                } else if (query.charAt(i) == '&' || i == query.length() - 1) {
175
                    //the text preceding the & should be the param value.
176
                    if (i == query.length() - 1) {
177
                        //if at the end of the string grab the last value and
178
                        // append it.
179
                        if (query.charAt(i) != '=') {
180
                            //ignore an extra & on the end of the string
181
                            temp += query.charAt(i);
182
                        }
183
                    }
184

    
185
                    if (!ampflag && poundflag) {
186
                        params[arrcount][1] = temp.trim();
187
                        parameters
188
                                .put(params[arrcount][0], params[arrcount][1]);
189
                        temp = "";
190
                        arrcount++; //increment the array to the next row.
191
                    } else {
192
                        //if there are two =s or &s in a row through an
193
                        // exception
194
                        throw new MalformedURLException(
195
                                "metacatURL: Two parameter values "
196
                                        + "not allowed in sequence");
197
                    }
198
                    poundflag = false;
199
                    ampflag = true;
200
                } else {
201
                    //get the next character in the string
202
                    temp += query.charAt(i);
203
                }
204
            }
205
        }
206
        return parameters;
207
    }
208

    
209
    /**
210
     * Utility method to print debugging messages. User can set debug level for
211
     * this message. The number is fewer, the message is more important
212
     *
213
     * @param msg, the content of the message
214
     * @param debugLevel, an integer indicating the message debug leve
215
     */
216
    public static void debugMessage(String msg, int debugLevel)
217
    {
218
        if (debug) {
219
            int limit = 1;
220
            try {
221
                limit = Integer.parseInt(getOption("debuglevel"));
222

    
223
            } catch (Exception e) {
224
                System.out.println(e.getMessage());
225
            }
226
            //don't allow the user set debugLevel less than or equals 0
227
            if (debugLevel <= 0) {
228
                debugLevel = 1;
229
            }
230

    
231
            if (debugLevel < limit) {
232
                System.err.println("@debugprefix@ " + msg);
233
            }
234
        }
235
    }
236

    
237

    
238
    /**
239
     * Utility method to print debugging messages. User can set debug level for
240
     * this message. The number is fewer, the message is more important. Can be
241
     * used to control if the carriage return and debugPrefix are printed
242
     *
243
     * @param msg, the content of the message
244
     * @param debugLevel, an integer indicating the message debug level
245
     * @param debugPrefix, a boolean indicating whether debugprefix
246
     *                    should be printed or not
247
     */
248
    public static void formattedDebugMessage(String msg, int debugLevel,
249
                                      boolean carriageReturn,
250
                                      boolean debugPrefix)
251
    {
252
        if (debug) {
253
            int limit = 1;
254
            try {
255
                limit = Integer.parseInt(getOption("debuglevel"));
256

    
257
            } catch (Exception e) {
258
                System.out.println(e.getMessage());
259
            }
260
            //don't allow the user set debugLevel less than or equals 0
261
            if (debugLevel <= 0) {
262
                debugLevel = 1;
263
            }
264

    
265
            if (debugLevel < limit) {
266
                if(debugPrefix) {
267
                    if(carriageReturn)
268
                        System.err.println("@debugprefix@ " + msg);
269
                    else
270
                        System.err.print("@debugprefix@ " + msg);
271
                } else {
272
                    if(carriageReturn)
273
                        System.err.println(msg);
274
                    else
275
                        System.err.print(msg);
276
                }
277
            }
278
        }
279
    }
280

    
281

    
282
    public static Vector getOptionList(String optiontext)
283
    {
284
        Vector optionsVector = new Vector();
285
        if (optiontext.indexOf(",") == -1) {
286
            optionsVector.addElement(optiontext);
287
            return optionsVector;
288
        }
289

    
290
        while (optiontext.indexOf(",") != -1) {
291
            String s = optiontext.substring(0, optiontext.indexOf(","));
292
            optionsVector.addElement(s.trim());
293
            optiontext = optiontext.substring(optiontext.indexOf(",") + 1,
294
                    optiontext.length());
295
            if (optiontext.indexOf(",") == -1) { //catch the last list entry
296
                optionsVector.addElement(optiontext.trim());
297
            }
298
        }
299
        return optionsVector;
300
    }
301

    
302
    /** Normalizes the given string. Taken from configXML.java */
303
    public static String normalize(String s)
304
    {
305
        StringBuffer str = new StringBuffer();
306

    
307
             int len = (s != null) ? s.length() : 0;
308
             for (int i = 0; i < len; i++) {
309
                 char ch = s.charAt(i);
310
                 switch (ch) {
311
                     case '<': {
312
                         str.append("&lt;");
313
                         break;
314
                     }
315
                     case '>': {
316
                         str.append("&gt;");
317
                         break;
318
                     }
319
                     case '&': {
320
                         /*
321
                          * patch provided by Johnoel Ancheta from U of Hawaii
322
                          */
323
                         // check if & is for a character reference &#xnnnn;
324
                         if (i + 1 < len - 1 && s.charAt(i + 1) == '#') {
325
                             str.append("&#");
326
                             i += 2;
327

    
328
                             ch = s.charAt(i);
329
                             while (i < len && ch != ';') {
330
                                 str.append(ch);
331
                                 i++;
332
                                 ch = s.charAt(i);
333
                             }
334
                             str.append(';');
335
                         }
336
                         else
337
                             str.append("&amp;");
338
                         /////////
339
                         break;
340
                     }
341
                    default: {
342
                         if ( (ch<128) && (ch>31) ) {
343
                             str.append(ch);
344
                         }
345
                         else if (ch<32) {
346
                             if (ch == 10) { // new line
347
                                 str.append(ch);
348
                             }
349
                             if (ch == 13) { // carriage return
350
                                 str.append(ch);
351
                             }
352
                             if (ch == 9) {  // tab
353
                                 str.append(ch);
354
                             }
355
                             // otherwise skip
356
                         }
357
                         else {
358
                             str.append("&#");
359
                             str.append(Integer.toString(ch));
360
                             str.append(';');
361
                         }
362
                     }
363
                 }
364
             }
365
             return str.toString();
366
    }
367

    
368
    /**
369
     * Get docid from online/url string
370
     */
371
    public static String getDocIdWithRevFromOnlineURL(String url)
372
    {
373
        String docid = null;
374
        String DOCID = "docid";
375
        boolean find = false;
376
        char limited = '&';
377
        int count = 0; //keep track how many & was found
378
        Vector list = new Vector();// keep index number for &
379
        if (url == null) {
380
            MetaCatUtil.debugMessage("url is null and null will be returned",
381
                    30);
382
            return docid;
383
        }
384
        // the first element in list is 0
385
        list.add(new Integer(0));
386
        for (int i = 0; i < url.length(); i++) {
387
            if (url.charAt(i) == limited) {
388
                // count plus 1
389
                count++;
390
                list.add(new Integer(i));
391
                // get substring beween two &
392
                String str = url.substring(
393
                        ((Integer) list.elementAt(count - 1)).intValue(), i);
394
                MetaCatUtil.debugMessage("substring between two & is: " + str,
395
                        30);
396
                //if the subString contains docid, we got it
397
                if (str.indexOf(DOCID) != -1) {
398
                    //get index of '="
399
                    int start = getIndexForGivenChar(str, '=') + 1;
400
                    int end = str.length();
401
                    docid = str.substring(start, end);
402
                    find = true;
403
                }//if
404
            }//if
405
        }//for
406
        //if not find, we need check the subtring between the index of last &
407
        // and
408
        // the end of string
409
        if (!find) {
410
            MetaCatUtil.debugMessage("Checking the last substring", 35);
411
            String str = url.substring(((Integer) list.elementAt(count))
412
                    .intValue() + 1, url.length());
413
            MetaCatUtil.debugMessage("Last substring is: " + str, 30);
414
            if (str.indexOf(DOCID) != -1) {
415
                //get index of '="
416
                int start = getIndexForGivenChar(str, '=') + 1;
417
                int end = str.length();
418
                docid = str.substring(start, end);
419
                find = true;
420
            }//if
421
        }//if
422
        MetaCatUtil.debugMessage("The docid from online url is:" + docid, 30);
423
        return docid.trim();
424
    }
425

    
426

    
427
    /**
428
     * Eocgorid identifier will look like: ecogrid://knb/tao.1.1
429
     * The AccessionNumber tao.1.1 will be returned. If the given doesn't
430
     * contains ecogrid, null will be returned.
431
     * @param identifier String
432
     * @return String
433
     */
434
    public static String getAccessionNumberFromEcogridIdentifier(String identifier)
435
    {
436
      String accessionNumber = null;
437
      if (identifier != null && identifier.startsWith(DBSAXHandler.ECOGRID))
438
      {
439
        // find the last "/" in identifier
440
        int indexOfLastSlash = identifier.lastIndexOf("/");
441
        int start = indexOfLastSlash+1;
442
        int end   = identifier.length();
443
        accessionNumber = identifier.substring(start, end);
444
      }
445
      MetaCatUtil.debugMessage("The accession number from url is " +
446
                                 accessionNumber, 10);
447
      return accessionNumber;
448
    }
449

    
450
    private static int getIndexForGivenChar(String str, char character)
451
    {
452
        int index = -1;
453
        // make sure str is not null
454
        if (str == null) {
455
            MetaCatUtil.debugMessage(
456
                    "The given str is null and -1 will be returned", 30);
457
            return index;
458
        }
459
        // got though the string
460
        for (int i = 0; i < str.length(); i++) {
461
            // find the first one then break the loop
462
            if (str.charAt(i) == character) {
463
                index = i;
464
                break;
465
            }//if
466
        }//for
467
        MetaCatUtil.debugMessage("the index for char " + character + " is: "
468
                + index, 30);
469
        return index;
470
    }
471

    
472
    /**
473
     * Utility method to get docid from a given string
474
     *
475
     * @param string, the given string should be these two format: 1) str1.str2
476
     *            in this case docid= str1.str2 2) str1.str2.str3, in this case
477
     *            docid =str1.str2
478
     * @param the sperator char
479
     */
480
    public static String getDocIdFromString(String str)
481
    {
482
        String docId = null;
483
        if (str == null) {
484
            MetaCatUtil.debugMessage(
485
                    "The given str is null and null will be returned"
486
                            + " in getDocIdfromString", 30);
487
            return docId;
488
        } //make sure docid is not null
489
        int dotNumber = 0;//count how many dots in given string
490
        int indexOfLastDot = 0;
491

    
492
        //assume that seperator is one charactor string
493
        char seperator = getOption("accNumSeparator").charAt(0);
494

    
495
        for (int i = 0; i < str.length(); i++) {
496
            if (str.charAt(i) == seperator) {
497
                dotNumber++;//count how many dots
498
                indexOfLastDot = i;//keep the last dot postion
499
            }
500
        }//for
501

    
502
        //The string formatt is wrong, because it has more than two or less
503
        // than
504
        //one seperator
505
        if (dotNumber > 2 || dotNumber < 1) {
506
            docId = null;
507
        } else if (dotNumber == 2) //the case for str1.str2.str3
508
        {
509
            docId = str.substring(0, indexOfLastDot);
510
        } else if (dotNumber == 1) //the case for str1.str2
511
        {
512
            docId = str;
513
        }
514

    
515
        return docId;
516
    }//getDocIdFromString
517

    
518
    /**
519
     * Utility method to get version number from a given string
520
     *
521
     * @param string, the given string should be these two format: 1)
522
     *            str1.str2(no version) version =-1; 2) str1.str2.str3, in this
523
     *            case version = str3; 3) other, vresion =-2
524
     */
525
    public static int getVersionFromString(String str)
526
            throws NumberFormatException
527
    {
528
        int version = -1;
529
        String versionString = null;
530
        int dotNumber = 0;//count how many dots in given string
531
        int indexOfLastDot = 0;
532

    
533
        //assume that seperator is one charactor string
534
        char seperator = getOption("accNumSeparator").charAt(0);
535

    
536
        for (int i = 0; i < str.length(); i++) {
537
            if (str.charAt(i) == seperator) {
538
                dotNumber++;//count how many dots
539
                indexOfLastDot = i;//keep the last dot postion
540
            }
541
        }//for
542

    
543
        //The string formatt is wrong, because it has more than two or less
544
        // than
545
        //one seperator
546
        if (dotNumber > 2 || dotNumber < 1) {
547
            version = -2;
548
        } else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1)))
549
        //the case for str1.str2.str3
550
        {
551
            versionString = str.substring((indexOfLastDot + 1), str.length());
552
            version = Integer.parseInt(versionString);
553
        } else if (dotNumber == 1) //the case for str1.str2
554
        {
555
            version = -1;
556
        }
557

    
558
        return version;
559
    }//getVersionFromString
560

    
561
    /**
562
     * Utility method to get version string from a given string
563
     *
564
     * @param string, the given string should be these two format: 1)
565
     *            str1.str2(no version) version=null; 2) str1.str2.str3, in
566
     *            this case version = str3; 3) other, vresion =null;
567
     */
568
    public static String getRevisionStringFromString(String str)
569
            throws NumberFormatException
570
    {
571
        // String to store the version
572
        String versionString = null;
573
        int dotNumber = 0;//count how many dots in given string
574
        int indexOfLastDot = 0;
575

    
576
        //assume that seperator is one charactor string
577
        char seperator = getOption("accNumSeparator").charAt(0);
578

    
579
        for (int i = 0; i < str.length(); i++) {
580
            if (str.charAt(i) == seperator) {
581
                dotNumber++;//count how many dots
582
                indexOfLastDot = i;//keep the last dot postion
583
            }
584
        }//for
585

    
586
        //The string formatt is wrong, because it has more than two or less
587
        // than
588
        //one seperator
589
        if (dotNumber > 2 || dotNumber < 1) {
590
            versionString = null;
591
        } else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1))) {
592
            //the case for str1.str2.str3
593
            // indexOfLastDot != (str.length() -1) means get rid of str1.str2.
594
            versionString = str.substring((indexOfLastDot + 1), str.length());
595
        } else if (dotNumber == 1) //the case for str1.str2 or str1.str2.
596
        {
597
            versionString = null;
598
        }
599

    
600
        return versionString;
601
    }//getVersionFromString
602

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

    
620
    /**
621
     * This method will get inline data id without the revision number.
622
     * So if inlineData.1.2 is passed as input, inlineData.2 is returned.
623
     */
624
    public static String getInlineDataIdWithoutRev(String accessionNumber)
625
    {
626
        String docid = null;
627
        if (accessionNumber == null) { return docid; }
628
        String seperator = getOption("accNumSeparator");
629
        int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
630
        String version = accessionNumber.substring(indexOfLastSeperator,
631
                                                   accessionNumber.length());
632
        accessionNumber = accessionNumber.substring(0, indexOfLastSeperator);
633
        indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
634
        docid = accessionNumber.substring(0, indexOfLastSeperator) + version;
635
        MetaCatUtil.debugMessage("after parsing accessionnumber, docid is "
636
                                 + docid, 30);
637

    
638
        return docid;
639
    }
640

    
641
    /**
642
     * This method will call both getDocIdFromString and
643
     * getDocIdFromAccessionNumber. So first, if the string looks str1.str2,
644
     * the docid will be str1.str2. If the string is str1.str2.str3, the docid
645
     * will be str1.str2. If the string is str1.str2.str3.str4 or more, the
646
     * docid will be str1.str2.str3. If the string look like str1, null will be
647
     * returned
648
     *
649
     */
650
    public static String getSmartDocId(String str)
651
    {
652
        String docid = null;
653
        //call geDocIdFromString first.
654
        docid = getDocIdFromString(str);
655
        // If docid is null, try to call getDocIdFromAccessionNumber
656
        // it will handle the seperator more than2
657
        if (docid == null) {
658
            docid = getDocIdFromAccessionNumber(str);
659
        }
660
        MetaCatUtil.debugMessage("The docid get from smart docid getor is "
661
                + docid, 30);
662
        return docid;
663
    }
664

    
665
    /**
666
     * This method will get revision from an AccessionNumber. There is no
667
     * assumption the accessnumber will be str1.str2.str3. It can be more. So
668
     * we think the docid will be get rid of last part
669
     */
670
    public static int getRevisionFromAccessionNumber(String accessionNumber)
671
            throws NumberFormatException
672
    {
673
        String rev = null;
674
        int revNumber = -1;
675
        if (accessionNumber == null) { return revNumber; }
676
        String seperator = getOption("accNumSeparator");
677
        int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
678
        rev = accessionNumber.substring(indexOfLastSeperator + 1,
679
                accessionNumber.length());
680
        revNumber = Integer.parseInt(rev);
681
        MetaCatUtil.debugMessage("after parsing accessionnumber, rev is "
682
                + revNumber, 30);
683
        return revNumber;
684
    }
685

    
686
    /**
687
     * Method to get the name of local replication server
688
     */
689
    public static String getLocalReplicationServerName()
690
    {
691
        String replicationServerName = null;
692
        String serverHost = null;
693
        serverHost = getOption("server");
694
        // append "context/servelet/replication" to the host name
695
        replicationServerName = serverHost + getOption("replicationpath");
696
        return replicationServerName;
697

    
698
    }
699

    
700
    /**
701
     * Method to get docidwithrev from eml2 inline data id The eml inline data
702
     * id would look like eml.200.2.3
703
     */
704
    public static String getDocIdWithoutRevFromInlineDataID(String inlineDataID)
705
    {
706
        String docidWithoutRev = null;
707
        if (inlineDataID == null) { return docidWithoutRev; }
708
        String seperator = MetaCatUtil.getOption("accNumSeparator");
709
        char charSeperator = seperator.charAt(0);
710
        int targetNumberOfSeperator = 2;// we want to know his index
711
        int numberOfSeperator = 0;
712
        for (int i = 0; i < inlineDataID.length(); i++) {
713
            // meet seperator, increase number of seperator
714
            if (inlineDataID.charAt(i) == charSeperator) {
715
                numberOfSeperator++;
716
            }
717
            // if number of seperator reach the target one, record the index(i)
718
            // and get substring and terminate the loop
719
            if (numberOfSeperator == targetNumberOfSeperator) {
720
                docidWithoutRev = inlineDataID.substring(0, i);
721
                break;
722
            }
723
        }
724

    
725
        MetaCatUtil.debugMessage("Docid without rev from inlinedata id: "
726
                + docidWithoutRev, 35);
727
        return docidWithoutRev;
728

    
729
    }
730

    
731
    /**
732
     * Revise stack change a stack to opposite order
733
     */
734
    public static Stack reviseStack(Stack stack)
735
    {
736
        Stack result = new Stack();
737
        // make sure the parameter is correct
738
        if (stack == null || stack.isEmpty()) {
739
            result = stack;
740
            return result;
741
        }
742

    
743
        while (!stack.isEmpty()) {
744
            Object obj = stack.pop();
745
            result.push(obj);
746
        }
747
        return result;
748
    }
749

    
750
    /** A method to replace whitespace in url */
751
    public static String replaceWhiteSpaceForURL(String urlHasWhiteSpace)
752
    {
753
        StringBuffer newUrl = new StringBuffer();
754
        String whiteSpaceReplace = "%20";
755
        if (urlHasWhiteSpace == null || urlHasWhiteSpace.trim().equals("")) { return null; }
756

    
757
        for (int i = 0; i < urlHasWhiteSpace.length(); i++) {
758
            char ch = urlHasWhiteSpace.charAt(i);
759
            if (!Character.isWhitespace(ch)) {
760
                newUrl.append(ch);
761
            } else {
762
                //it is white sapce, replace it by %20
763
                newUrl = newUrl.append(whiteSpaceReplace);
764
            }
765

    
766
        }//for
767
        MetaCatUtil.debugMessage("The new string without space is:"
768
                + newUrl.toString(), 35);
769
        return newUrl.toString();
770

    
771
    }// replaceWhiteSpaceForUR
772

    
773
}
(43-43/63)