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-21 16:54:04 -0700 (Thu, 21 Jul 2005) $'
11
 * '$Revision: 2517 $'
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

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

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

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

    
265

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

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

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

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

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

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

    
410

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

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

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

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

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

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

    
499
        return docId;
500
    }//getDocIdFromString
501

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

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

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

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

    
542
        return version;
543
    }//getVersionFromString
544

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

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

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

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

    
584
        return versionString;
585
    }//getVersionFromString
586

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

    
604
    /**
605
     * This method will get inline data id without the revision number.
606
     * So if inlineData.1.2 is passed as input, inlineData.2 is returned.
607
     */
608
    public static String getInlineDataIdWithoutRev(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
        String version = accessionNumber.substring(indexOfLastSeperator,
615
                                                   accessionNumber.length());
616
        accessionNumber = accessionNumber.substring(0, indexOfLastSeperator);
617
        indexOfLastSeperator = accessionNumber.lastIndexOf(seperator);
618
        docid = accessionNumber.substring(0, indexOfLastSeperator) + version;
619
        MetaCatUtil.debugMessage("after parsing accessionnumber, docid is "
620
                                 + docid, 30);
621

    
622
        return docid;
623
    }
624

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

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

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

    
682
    }
683

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

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

    
713
    }
714

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

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

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

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

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

    
755
    }// replaceWhiteSpaceForUR
756

    
757
}
(43-43/63)