KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > idltree > Util


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.idltree;
26
27 import java.util.*;
28
29 /**
30  * Contains utility functions, mostly for string manipulation.
31  *
32  * @author Harold Batteram
33  */

34 public class Util {
35     public static String JavaDoc unscopeName(String JavaDoc name) {
36         if (name == null) {
37             return name;
38         }
39         int idx = name.lastIndexOf("::");
40         if (idx >= 0) {
41             return name.substring(idx + 2);
42         }
43         return name;
44     }
45         
46     /**
47      * Splits a string like xxx.yyy.zzz into a string array with elements xxx, yyy and zzz
48      *
49      * @param name the string that needs to be split.
50      * @return the list of elements resulting from the split.
51      */

52     public static String JavaDoc[] splitName(String JavaDoc name) {
53         return splitName(name, ".");
54     }
55     
56     /**
57      * Splits a string like xxx.yyy.zzz into a string array with elements xxx, yyy and zzz
58      *
59      * @param name the string that needs to be split.
60      * @param separator the separator to be used in splitting.
61      * @return the list of elements resulting from the split.
62      */

63     public static String JavaDoc[] splitName(String JavaDoc name, String JavaDoc separator) {
64         if (name == null) {
65             return new String JavaDoc[0];
66         }
67         StringTokenizer st = new StringTokenizer(name, separator);
68         Vector v = new Vector();
69         if (name.startsWith(separator)) {
70             v.addElement("");
71         }
72         while (st.hasMoreTokens()) {
73             v.addElement(st.nextToken());
74         }
75         String JavaDoc[] s = new String JavaDoc[v.size()];
76         v.copyInto(s);
77         return s;
78     }
79     
80     /**
81      * Concatenates a list of strings into one string where the separator between the elements is a ".".
82      *
83      * @param split the list of elements to be concatenated.
84      * @return the concatenated string.
85      */

86     public static String JavaDoc combineName(String JavaDoc[] split) {
87         return combineName(split, ".");
88     }
89       
90     /**
91      * Concatenates a list of strings into one string with a specified separator between the elements.
92      *
93      * @param split the list of elements to be concatenated.
94      * @param separator the string to be used as a separator between the elements.
95      * @return the concatenated string.
96      */

97     public static String JavaDoc combineName(String JavaDoc[] split, String JavaDoc separator) {
98         String JavaDoc s = "";
99         for (int i = 0; i < split.length; i++) {
100             if (i != 0) {
101                 s += separator;
102             }
103             s += split[i];
104         }
105         return s;
106     }
107
108     public static String JavaDoc replaceAll(String JavaDoc source, String JavaDoc what, String JavaDoc into) {
109         int index = source.indexOf(what);
110         if (index == -1) return source;
111         String JavaDoc s1 = source.substring(0, index);
112         String JavaDoc s2 = source.substring(index + what.length(), source.length());
113         return s1 + into + replaceAll(s2, what, into);
114     }
115 }
116
Popular Tags