KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > StringUtils


1 // $Id: StringUtils.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // String processing functions.
4

5 /*
6  * Copyright 1999 by John D. Ramsdell
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package edu.neu.ccs.jmk;
24
25 import java.io.File JavaDoc;
26
27 /**
28  * String processing functions.
29  * These functions are used by the makefile loader to implement
30  * its string processing functions.
31  * The results computed by a string processing function is appended
32  * to its list argument to produce the final result.
33  * @see edu.neu.ccs.jmk.GlobalTable
34  * @version May 1999
35  * @author John D. Ramsdell
36  */

37 public class StringUtils
38 {
39
40   // No instances of this class should be constructed.
41
private StringUtils() { }
42
43   private final static char genericSeparatorChar = '/';
44   private final static char genericPathSeparatorChar = ';';
45
46   /**
47    * Localizes file related strings by translating the
48    * the generic separator character '/' to the separator
49    * character used on this platform, and the generic
50    * path separator ':' to the path separator character
51    * used on this platform.
52    */

53   public static String JavaDoc localizePaths(String JavaDoc paths) {
54     paths = paths.replace(genericPathSeparatorChar,
55               File.pathSeparatorChar);
56     paths = paths.replace(genericSeparatorChar,
57               File.separatorChar);
58     return paths;
59   }
60
61   /**
62    * Generalizes file related strings by performing the inverse
63    * translation given by localizePaths.
64    */

65   public static String JavaDoc generalizePaths(String JavaDoc paths) {
66     paths = paths.replace(File.pathSeparatorChar,
67               genericPathSeparatorChar);
68     paths = paths.replace(File.separatorChar,
69               genericSeparatorChar);
70     return paths;
71   }
72
73   /**
74    * The patsubst function matches the pattern with the strings
75    * in the input. A string that does not match is copied unchanged
76    * to the output. A string that does match is replaced by a string
77    * generated by replacing wild cards in the replacement with the match.
78    * When the replacement does not contain a wild card, the match is
79    * concatenated with the replacement.
80    * @see edu.neu.ccs.jmk.Matcher
81    */

82   static StringList patsubst(String JavaDoc pattern,
83                  String JavaDoc replacement,
84                  StringList input,
85                  StringList list) {
86     if (input == null)
87       return list;
88     else {
89       StringList head = new StringList("");
90       StringList last = head;
91       Matcher matcher = new Matcher(pattern);
92       for (; input != null; input = input.getRest()) {
93     String JavaDoc string = input.getString();
94     String JavaDoc match = matcher.match(string);
95     if (match != null) {
96       if (Matcher.isPattern(replacement))
97         string = Matcher.subst(match, replacement);
98       else
99         string = match + replacement;
100     }
101     StringList temp = new StringList(string);
102     last.setRest(temp);
103     last = temp;
104       }
105       last.setRest(list);
106       return head.getRest();
107     }
108   }
109
110   /**
111    * Applies stringSubst to each element of the input.
112    */

113   static StringList subst(String JavaDoc pattern,
114               String JavaDoc replacement,
115               StringList input,
116               StringList list) {
117     if (input == null)
118       return list;
119     else {
120       StringList head = new StringList("");
121       StringList last = head;
122       for (; input != null; input = input.getRest()) {
123     String JavaDoc string = input.getString();
124     string = stringSubst(pattern, replacement, string);
125     StringList temp = new StringList(string);
126     last.setRest(temp);
127     last = temp;
128       }
129       last.setRest(list);
130       return head.getRest();
131     }
132   }
133
134   /**
135    * Substitutes the replacement for every non-overlapping occurrence
136    * of the pattern in the string.
137    */

138   static String JavaDoc stringSubst(String JavaDoc pattern,
139                 String JavaDoc replacement,
140                 String JavaDoc string) {
141     int i = string.indexOf(pattern);
142     if (i >= 0) { // Pattern found.
143
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
144       do {
145     sb.append(string.substring(0, i));
146     sb.append(replacement);
147     string = string.substring(i + pattern.length());
148     i = string.indexOf(pattern);
149       }
150       while (i >= 0);
151       sb.append(string);
152       string = sb.toString();
153     }
154     return string;
155   }
156
157   /**
158    * Concatenates the strings in the input into one string which
159    * is added to the output. The null string is added to the output
160    * when there are no strings in the input.
161    */

162   static StringList cat(StringList input, StringList list) {
163     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
164     for (; input != null; input = input.getRest())
165       sb.append(input.getString());
166     return new StringList(sb.toString(), list);
167   }
168
169   /**
170    * Expands file name that contain the wild card '*' to a list
171    * of file names. The inputs and outputs use generic separators.
172    * @see edu.neu.ccs.jmk.FileOperator#glob(String)
173    * @exception CommandFailedException if wild card is misused
174    */

175   static StringList glob(StringList input, StringList list)
176        throws CommandFailedException
177   {
178     if (input == null)
179       return list;
180     else {
181       StringList head = new StringList("");
182       StringList last = head;
183       for (; input != null; input = input.getRest()) {
184     String JavaDoc string = input.getString();
185     String JavaDoc[] globbed = FileOperator.glob(localizePaths(string));
186     for (int i = 0; i < globbed.length; i++) {
187       StringList temp = new StringList(generalizePaths(globbed[i]));
188       last.setRest(temp);
189       last = temp;
190     }
191       }
192       last.setRest(list);
193       return head.getRest();
194     }
195   }
196
197   /**
198    * This method expands a list of file names into a list of all
199    * directories in those files.
200    * The inputs and outputs use generic separators.
201    * @see edu.neu.ccs.jmk.FileOperator#dirs(String)
202    */

203   static StringList dirs(StringList input, StringList list) {
204     if (input == null)
205       return list;
206     else {
207       StringList head = new StringList("");
208       StringList last = head;
209       for (; input != null; input = input.getRest()) {
210     String JavaDoc string = input.getString();
211     String JavaDoc[] dirList = FileOperator.dirs(localizePaths(string));
212     for (int i = 0; i < dirList.length; i++) {
213       StringList temp = new StringList(generalizePaths(dirList[i]));
214       last.setRest(temp);
215       last = temp;
216     }
217       }
218       last.setRest(list);
219       return head.getRest();
220     }
221   }
222
223   /**
224    * Each input is used as a key to retrieve data from the system's
225    * properties. The results use generic separators.
226    */

227   static StringList getprop(StringList input, StringList list) {
228     if (input == null)
229       return list;
230     else {
231       StringList head = new StringList("");
232       StringList last = head;
233       for (; input != null; input = input.getRest()) {
234     String JavaDoc string = input.getString();
235     String JavaDoc prop = System.getProperty(string);
236     if (prop != null) {
237       StringList temp = new StringList(generalizePaths(prop));
238       last.setRest(temp);
239       last = temp;
240     }
241       }
242       last.setRest(list);
243       return head.getRest();
244     }
245   }
246
247   /**
248    * Concatenates each string in prefixes with every string in suffixes.
249    */

250   static StringList join(StringList prefixes,
251              StringList suffixes,
252              StringList list) {
253     StringList head = new StringList("");
254     StringList last = head;
255     for (StringList pres = prefixes; pres != null; pres = pres.getRest()) {
256       String JavaDoc prefix = pres.getString();
257       for (StringList sufs = suffixes; sufs != null; sufs = sufs.getRest()) {
258     String JavaDoc suffix = sufs.getString();
259     StringList temp = new StringList(prefix + suffix);
260     last.setRest(temp);
261     last = temp;
262       }
263     }
264     last.setRest(list);
265     return head.getRest();
266   }
267
268   /**
269    * Returns true if the two input lists are equal.
270    * True is a list containing the string "true".
271    * False is the empty list.
272    */

273   static StringList equal(StringList sl1, StringList sl2, StringList list) {
274     for (;;) {
275       if (sl1 == null)
276     return sl2 == null ? new StringList("true", list) : list;
277       else if (sl2 == null)
278     return list;
279       else if (!sl1.getString().equals(sl2.getString()))
280     return list;
281       else {
282     sl1 = sl1.getRest();
283     sl2 = sl2.getRest();
284       }
285     }
286   }
287 }
288
Popular Tags