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-07-25 11:49:12 -0700 (Mon, 25 Jul 2005) $'
11
 * '$Revision: 2521 $'
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
    /** Utility method to convert a file handle into a URL */
105
    public static URL fileToURL(File file)
106
    {
107
        String path = file.getAbsolutePath();
108
        String fSep = System.getProperty("file.separator");
109
        if (fSep != null && fSep.length() == 1)
110
                path = path.replace(fSep.charAt(0), '/');
111
        if (path.length() > 0 && path.charAt(0) != '/') path = '/' + path;
112
        try {
113
            return new URL("file", null, path);
114
        } catch (java.net.MalformedURLException e) {
115
            /*
116
             * According to the spec this could only happen if the file
117
             */
118
            throw new Error("unexpected MalformedURLException");
119
        }
120
    }
121

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

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

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

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

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

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

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

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

    
223

    
224
    /**
225
     * Utility method to print debugging messages. User can set debug level for
226
     * this message. The number is fewer, the message is more important. Can be
227
     * used to control if the carriage return and debugPrefix are printed
228
     *
229
     * @param msg, the content of the message
230
     * @param debugLevel, an integer indicating the message debug level
231
     * @param debugPrefix, a boolean indicating whether debugprefix
232
     *                    should be printed or not
233
     */
234
    public static void formattedDebugMessage(String msg, int debugLevel,
235
                                      boolean carriageReturn,
236
                                      boolean debugPrefix)
237
    {
238
        if (debug) {
239
            int limit = 1;
240
            try {
241
                limit = Integer.parseInt(getOption("debuglevel"));
242

    
243
            } catch (Exception e) {
244
                System.out.println(e.getMessage());
245
            }
246
            //don't allow the user set debugLevel less than or equals 0
247
            if (debugLevel <= 0) {
248
                debugLevel = 1;
249
            }
250

    
251
            if (debugLevel < limit) {
252
                if(debugPrefix) {
253
                    if(carriageReturn)
254
                        System.err.println("@debugprefix@ " + msg);
255
                    else
256
                        System.err.print("@debugprefix@ " + msg);
257
                } else {
258
                    if(carriageReturn)
259
                        System.err.println(msg);
260
                    else
261
                        System.err.print(msg);
262
                }
263
            }
264
        }
265
    }
266

    
267

    
268
    public static Vector getOptionList(String optiontext)
269
    {
270
        Vector optionsVector = new Vector();
271
        if (optiontext.indexOf(",") == -1) {
272
            optionsVector.addElement(optiontext);
273
            return optionsVector;
274
        }
275

    
276
        while (optiontext.indexOf(",") != -1) {
277
            String s = optiontext.substring(0, optiontext.indexOf(","));
278
            optionsVector.addElement(s.trim());
279
            optiontext = optiontext.substring(optiontext.indexOf(",") + 1,
280
                    optiontext.length());
281
            if (optiontext.indexOf(",") == -1) { //catch the last list entry
282
                optionsVector.addElement(optiontext.trim());
283
            }
284
        }
285
        return optionsVector;
286
    }
287

    
288
    /** Normalizes the given string. Taken from configXML.java */
289
    public static String normalize(String s)
290
    {
291
        StringBuffer str = new StringBuffer();
292

    
293
             int len = (s != null) ? s.length() : 0;
294
             for (int i = 0; i < len; i++) {
295
                 char ch = s.charAt(i);
296
                 switch (ch) {
297
                     case '<': {
298
                         str.append("&lt;");
299
                         break;
300
                     }
301
                     case '>': {
302
                         str.append("&gt;");
303
                         break;
304
                     }
305
                     case '&': {
306
                         /*
307
                          * patch provided by Johnoel Ancheta from U of Hawaii
308
                          */
309
                         // check if & is for a character reference &#xnnnn;
310
                         if (i + 1 < len - 1 && s.charAt(i + 1) == '#') {
311
                             str.append("&#");
312
                             i += 2;
313

    
314
                             ch = s.charAt(i);
315
                             while (i < len && ch != ';') {
316
                                 str.append(ch);
317
                                 i++;
318
                                 ch = s.charAt(i);
319
                             }
320
                             str.append(';');
321
                         }
322
                         else
323
                             str.append("&amp;");
324
                         /////////
325
                         break;
326
                     }
327
                    default: {
328
                         if ( (ch<128) && (ch>31) ) {
329
                             str.append(ch);
330
                         }
331
                         else if (ch<32) {
332
                             if (ch == 10) { // new line
333
                                 str.append(ch);
334
                             }
335
                             if (ch == 13) { // carriage return
336
                                 str.append(ch);
337
                             }
338
                             if (ch == 9) {  // tab
339
                                 str.append(ch);
340
                             }
341
                             // otherwise skip
342
                         }
343
                         else {
344
                             str.append("&#");
345
                             str.append(Integer.toString(ch));
346
                             str.append(';');
347
                         }
348
                     }
349
                 }
350
             }
351
             return str.toString();
352
    }
353

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

    
412

    
413
    /**
414
     * Eocgorid identifier will look like: ecogrid://knb/tao.1.1
415
     * The AccessionNumber tao.1.1 will be returned. If the given doesn't
416
     * contains ecogrid, null will be returned.
417
     * @param identifier String
418
     * @return String
419
     */
420
    public static String getAccessionNumberFromEcogridIdentifier(String identifier)
421
    {
422
      String accessionNumber = null;
423
      if (identifier != null && identifier.startsWith(DBSAXHandler.ECOGRID))
424
      {
425
        // find the last "/" in identifier
426
        int indexOfLastSlash = identifier.lastIndexOf("/");
427
        int start = indexOfLastSlash+1;
428
        int end   = identifier.length();
429
        accessionNumber = identifier.substring(start, end);
430
      }
431
      MetaCatUtil.debugMessage("The accession number from url is " +
432
                                 accessionNumber, 10);
433
      return accessionNumber;
434
    }
435

    
436
    private static int getIndexForGivenChar(String str, char character)
437
    {
438
        int index = -1;
439
        // make sure str is not null
440
        if (str == null) {
441
            MetaCatUtil.debugMessage(
442
                    "The given str is null and -1 will be returned", 30);
443
            return index;
444
        }
445
        // got though the string
446
        for (int i = 0; i < str.length(); i++) {
447
            // find the first one then break the loop
448
            if (str.charAt(i) == character) {
449
                index = i;
450
                break;
451
            }//if
452
        }//for
453
        MetaCatUtil.debugMessage("the index for char " + character + " is: "
454
                + index, 30);
455
        return index;
456
    }
457

    
458
    /**
459
     * Utility method to get docid from a given string
460
     *
461
     * @param string, the given string should be these two format: 1) str1.str2
462
     *            in this case docid= str1.str2 2) str1.str2.str3, in this case
463
     *            docid =str1.str2
464
     * @param the sperator char
465
     */
466
    public static String getDocIdFromString(String str)
467
    {
468
        String docId = null;
469
        if (str == null) {
470
            MetaCatUtil.debugMessage(
471
                    "The given str is null and null will be returned"
472
                            + " in getDocIdfromString", 30);
473
            return docId;
474
        } //make sure docid is not null
475
        int dotNumber = 0;//count how many dots in given string
476
        int indexOfLastDot = 0;
477

    
478
        //assume that seperator is one charactor string
479
        char seperator = getOption("accNumSeparator").charAt(0);
480

    
481
        for (int i = 0; i < str.length(); i++) {
482
            if (str.charAt(i) == seperator) {
483
                dotNumber++;//count how many dots
484
                indexOfLastDot = i;//keep the last dot postion
485
            }
486
        }//for
487

    
488
        //The string formatt is wrong, because it has more than two or less
489
        // than
490
        //one seperator
491
        if (dotNumber > 2 || dotNumber < 1) {
492
            docId = null;
493
        } else if (dotNumber == 2) //the case for str1.str2.str3
494
        {
495
            docId = str.substring(0, indexOfLastDot);
496
        } else if (dotNumber == 1) //the case for str1.str2
497
        {
498
            docId = str;
499
        }
500

    
501
        return docId;
502
    }//getDocIdFromString
503

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

    
519
        //assume that seperator is one charactor string
520
        char seperator = getOption("accNumSeparator").charAt(0);
521

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

    
529
        //The string formatt is wrong, because it has more than two or less
530
        // than
531
        //one seperator
532
        if (dotNumber > 2 || dotNumber < 1) {
533
            version = -2;
534
        } else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1)))
535
        //the case for str1.str2.str3
536
        {
537
            versionString = str.substring((indexOfLastDot + 1), str.length());
538
            version = Integer.parseInt(versionString);
539
        } else if (dotNumber == 1) //the case for str1.str2
540
        {
541
            version = -1;
542
        }
543

    
544
        return version;
545
    }//getVersionFromString
546

    
547
    /**
548
     * Utility method to get version string from a given string
549
     *
550
     * @param string, the given string should be these two format: 1)
551
     *            str1.str2(no version) version=null; 2) str1.str2.str3, in
552
     *            this case version = str3; 3) other, vresion =null;
553
     */
554
    public static String getRevisionStringFromString(String str)
555
            throws NumberFormatException
556
    {
557
        // String to store the version
558
        String versionString = null;
559
        int dotNumber = 0;//count how many dots in given string
560
        int indexOfLastDot = 0;
561

    
562
        //assume that seperator is one charactor string
563
        char seperator = getOption("accNumSeparator").charAt(0);
564

    
565
        for (int i = 0; i < str.length(); i++) {
566
            if (str.charAt(i) == seperator) {
567
                dotNumber++;//count how many dots
568
                indexOfLastDot = i;//keep the last dot postion
569
            }
570
        }//for
571

    
572
        //The string formatt is wrong, because it has more than two or less
573
        // than
574
        //one seperator
575
        if (dotNumber > 2 || dotNumber < 1) {
576
            versionString = null;
577
        } else if (dotNumber == 2 && (indexOfLastDot != (str.length() - 1))) {
578
            //the case for str1.str2.str3
579
            // indexOfLastDot != (str.length() -1) means get rid of str1.str2.
580
            versionString = str.substring((indexOfLastDot + 1), str.length());
581
        } else if (dotNumber == 1) //the case for str1.str2 or str1.str2.
582
        {
583
            versionString = null;
584
        }
585

    
586
        return versionString;
587
    }//getVersionFromString
588

    
589
    /**
590
     * This method will get docid from an AccessionNumber. There is no
591
     * assumption the accessnumber will be str1.str2.str3. It can be more. So
592
     * we think the docid will be get rid of last part
593
     */
594
    public static String getDocIdFromAccessionNumber(String accessionNumber)
595
    {
596
        String docid = null;
597
        if (accessionNumber == null) { return docid; }
598
        String seperator = getOption("accNumSeparator");
599
        int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
600
        docid = accessionNumber.substring(0, indexOfLastSeperator);
601
        MetaCatUtil.debugMessage("after parsing accessionnumber, docid is "
602
                + docid, 30);
603
        return docid;
604
    }
605

    
606
    /**
607
     * This method will get inline data id without the revision number.
608
     * So if inlineData.1.2 is passed as input, inlineData.2 is returned.
609
     */
610
    public static String getInlineDataIdWithoutRev(String accessionNumber)
611
    {
612
        String docid = null;
613
        if (accessionNumber == null) { return docid; }
614
        String seperator = getOption("accNumSeparator");
615
        int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
616
        String version = accessionNumber.substring(indexOfLastSeperator,
617
                                                   accessionNumber.length());
618
        accessionNumber = accessionNumber.substring(0, indexOfLastSeperator);
619
        indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
620
        docid = accessionNumber.substring(0, indexOfLastSeperator) + version;
621
        MetaCatUtil.debugMessage("after parsing accessionnumber, docid is "
622
                                 + docid, 30);
623

    
624
        return docid;
625
    }
626

    
627
    /**
628
     * This method will call both getDocIdFromString and
629
     * getDocIdFromAccessionNumber. So first, if the string looks str1.str2,
630
     * the docid will be str1.str2. If the string is str1.str2.str3, the docid
631
     * will be str1.str2. If the string is str1.str2.str3.str4 or more, the
632
     * docid will be str1.str2.str3. If the string look like str1, null will be
633
     * returned
634
     *
635
     */
636
    public static String getSmartDocId(String str)
637
    {
638
        String docid = null;
639
        //call geDocIdFromString first.
640
        docid = getDocIdFromString(str);
641
        // If docid is null, try to call getDocIdFromAccessionNumber
642
        // it will handle the seperator more than2
643
        if (docid == null) {
644
            docid = getDocIdFromAccessionNumber(str);
645
        }
646
        MetaCatUtil.debugMessage("The docid get from smart docid getor is "
647
                + docid, 30);
648
        return docid;
649
    }
650

    
651
    /**
652
     * This method will get revision from an AccessionNumber. There is no
653
     * assumption the accessnumber will be str1.str2.str3. It can be more. So
654
     * we think the docid will be get rid of last part
655
     */
656
    public static int getRevisionFromAccessionNumber(String accessionNumber)
657
            throws NumberFormatException
658
    {
659
        String rev = null;
660
        int revNumber = -1;
661
        if (accessionNumber == null) { return revNumber; }
662
        String seperator = getOption("accNumSeparator");
663
        int indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
664
        rev = accessionNumber.substring(indexOfLastSeperator + 1,
665
                accessionNumber.length());
666
        revNumber = Integer.parseInt(rev);
667
        MetaCatUtil.debugMessage("after parsing accessionnumber, rev is "
668
                + revNumber, 30);
669
        return revNumber;
670
    }
671

    
672
    /**
673
     * Method to get the name of local replication server
674
     */
675
    public static String getLocalReplicationServerName()
676
    {
677
        String replicationServerName = null;
678
        String serverHost = null;
679
        serverHost = getOption("server");
680
        // append "context/servelet/replication" to the host name
681
        replicationServerName = serverHost + getOption("replicationpath");
682
        return replicationServerName;
683

    
684
    }
685

    
686
    /**
687
     * Method to get docidwithrev from eml2 inline data id The eml inline data
688
     * id would look like eml.200.2.3
689
     */
690
    public static String getDocIdWithoutRevFromInlineDataID(String inlineDataID)
691
    {
692
        String docidWithoutRev = null;
693
        if (inlineDataID == null) { return docidWithoutRev; }
694
        String seperator = MetaCatUtil.getOption("accNumSeparator");
695
        char charSeperator = seperator.charAt(0);
696
        int targetNumberOfSeperator = 2;// we want to know his index
697
        int numberOfSeperator = 0;
698
        for (int i = 0; i < inlineDataID.length(); i++) {
699
            // meet seperator, increase number of seperator
700
            if (inlineDataID.charAt(i) == charSeperator) {
701
                numberOfSeperator++;
702
            }
703
            // if number of seperator reach the target one, record the index(i)
704
            // and get substring and terminate the loop
705
            if (numberOfSeperator == targetNumberOfSeperator) {
706
                docidWithoutRev = inlineDataID.substring(0, i);
707
                break;
708
            }
709
        }
710

    
711
        MetaCatUtil.debugMessage("Docid without rev from inlinedata id: "
712
                + docidWithoutRev, 35);
713
        return docidWithoutRev;
714

    
715
    }
716

    
717
    /**
718
     * Revise stack change a stack to opposite order
719
     */
720
    public static Stack reviseStack(Stack stack)
721
    {
722
        Stack result = new Stack();
723
        // make sure the parameter is correct
724
        if (stack == null || stack.isEmpty()) {
725
            result = stack;
726
            return result;
727
        }
728

    
729
        while (!stack.isEmpty()) {
730
            Object obj = stack.pop();
731
            result.push(obj);
732
        }
733
        return result;
734
    }
735

    
736
    /** A method to replace whitespace in url */
737
    public static String replaceWhiteSpaceForURL(String urlHasWhiteSpace)
738
    {
739
        StringBuffer newUrl = new StringBuffer();
740
        String whiteSpaceReplace = "%20";
741
        if (urlHasWhiteSpace == null || urlHasWhiteSpace.trim().equals("")) { return null; }
742

    
743
        for (int i = 0; i < urlHasWhiteSpace.length(); i++) {
744
            char ch = urlHasWhiteSpace.charAt(i);
745
            if (!Character.isWhitespace(ch)) {
746
                newUrl.append(ch);
747
            } else {
748
                //it is white sapce, replace it by %20
749
                newUrl = newUrl.append(whiteSpaceReplace);
750
            }
751

    
752
        }//for
753
        MetaCatUtil.debugMessage("The new string without space is:"
754
                + newUrl.toString(), 35);
755
        return newUrl.toString();
756

    
757
    }// replaceWhiteSpaceForUR
758

    
759
}
(43-43/63)