KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ice > util > StringUtilities


1 /*
2 ** Tim Endres' utilities package.
3 ** Copyright (c) 1997 by Tim Endres
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package com.ice.util;
24
25 import java.util.*;
26
27
28 public class
29 StringUtilities
30     {
31
32     static public String JavaDoc[]
33     splitString( String JavaDoc splitStr, String JavaDoc delim )
34         {
35         int i, count;
36         String JavaDoc[] result;
37         StringTokenizer toker;
38
39         toker = new StringTokenizer( splitStr, delim );
40
41         count = toker.countTokens();
42
43         result = new String JavaDoc[ count ];
44
45         for ( i = 0 ; i < count ; ++i )
46             {
47             try { result[i] = toker.nextToken(); }
48             catch ( NoSuchElementException ex )
49                 {
50                 result = null;
51                 break;
52                 }
53             }
54
55         return result;
56         }
57
58     public static String JavaDoc[]
59     argumentSubstitution( String JavaDoc[] args, Hashtable vars )
60         {
61         StringBuffer JavaDoc argBuf = new StringBuffer JavaDoc();
62
63         String JavaDoc[] result = new String JavaDoc[ args.length ];
64
65         for ( int aIdx = 0 ; aIdx < args.length ; ++aIdx )
66             {
67             String JavaDoc argStr = args[ aIdx ];
68
69             int index = argStr.indexOf( '$' );
70
71             if ( index < 0 )
72                 {
73                 result[ aIdx ] = argStr;
74                 continue;
75                 }
76             else
77                 {
78                 result[ aIdx ] =
79                     StringUtilities.stringSubstitution( argStr, vars );
80                 }
81             }
82
83         return result;
84         }
85
86     public static String JavaDoc
87     stringSubstitution( String JavaDoc argStr, Hashtable vars )
88         {
89         StringBuffer JavaDoc argBuf = new StringBuffer JavaDoc();
90
91         for ( int cIdx = 0 ; cIdx < argStr.length() ; )
92             {
93             char ch = argStr.charAt( cIdx );
94
95             switch ( ch )
96                 {
97                 case '$':
98                     StringBuffer JavaDoc nameBuf = new StringBuffer JavaDoc();
99                     for ( ++cIdx ; cIdx < argStr.length() ; ++cIdx )
100                         {
101                         ch = argStr.charAt( cIdx );
102                         if ( ch == '_' || Character.isLetterOrDigit( ch ) )
103                             nameBuf.append( ch );
104                         else
105                             break;
106                         }
107
108                     if ( nameBuf.length() > 0 )
109                         {
110                         String JavaDoc value = (String JavaDoc)
111                             vars.get( nameBuf.toString() );
112
113                         if ( value != null )
114                             {
115                             argBuf.append( value );
116                             }
117                         }
118                     break;
119                 
120                 default:
121                     argBuf.append( ch );
122                     ++cIdx;
123                     break;
124                 }
125             }
126
127         return argBuf.toString();
128         }
129
130     public static String JavaDoc[]
131     parseArgumentString( String JavaDoc argStr )
132         {
133         String JavaDoc[] result = null;
134
135         Vector vector =
136             StringUtilities.parseArgumentVector( argStr );
137
138         if ( vector != null && vector.size() > 0 )
139             {
140             result = new String JavaDoc[ vector.size() ];
141
142             for ( int i = 0 ; i < result.length ; ++i )
143                 {
144                 result[i] = (String JavaDoc)vector.elementAt(i);
145                 }
146             }
147
148         return result;
149         }
150
151     public static Vector
152     parseArgumentVector( String JavaDoc argStr )
153         {
154         Vector result = new Vector();
155         StringBuffer JavaDoc argBuf = new StringBuffer JavaDoc();
156
157         boolean backSlash = false;
158         boolean matchSglQuote = false;
159         boolean matchDblQuote = false;
160
161         for ( int cIdx = 0 ; cIdx < argStr.length() ; ++cIdx )
162             {
163             char ch = argStr.charAt( cIdx );
164
165             switch ( ch )
166                 {
167                 //
168
// W H I T E S P A C E
169
//
170
case ' ':
171                 case '\t':
172                 case '\n':
173                 case '\r':
174                     if ( backSlash )
175                         {
176                         argBuf.append( ch );
177                         backSlash = false;
178                         }
179                     else if ( matchSglQuote || matchDblQuote )
180                         {
181                         argBuf.append( ch );
182                         }
183                     else if ( argBuf.length() > 0 )
184                         {
185                         result.addElement( argBuf.toString() );
186                         argBuf.setLength( 0 );
187                         }
188                     break;
189
190                 case '\\':
191                     if ( backSlash )
192                         {
193                         argBuf.append( "\\" );
194                         }
195                     backSlash = ! backSlash;
196                     break;
197
198                 case '\'':
199                     if ( backSlash )
200                         {
201                         argBuf.append( "'" );
202                         backSlash = false;
203                         }
204                     else if ( matchSglQuote )
205                         {
206                         result.addElement( argBuf.toString() );
207                         argBuf.setLength( 0 );
208                         matchSglQuote = false;
209                         }
210                     else if ( ! matchDblQuote )
211                         {
212                         matchSglQuote = true;
213                         }
214                     break;
215
216                 case '"':
217                     if ( backSlash )
218                         {
219                         argBuf.append( "\"" );
220                         backSlash = false;
221                         }
222                     else if ( matchDblQuote )
223                         {
224                         result.addElement( argBuf.toString() );
225                         argBuf.setLength( 0 );
226                         matchDblQuote = false;
227                         }
228                     else if ( ! matchSglQuote )
229                         {
230                         matchDblQuote = true;
231                         }
232                     break;
233
234                 default:
235                     if ( backSlash )
236                         {
237                         switch ( ch )
238                             {
239                             case 'b': argBuf.append( '\b' ); break;
240                             case 'f': argBuf.append( '\f' ); break;
241                             case 'n': argBuf.append( '\n' ); break;
242                             case 'r': argBuf.append( '\r' ); break;
243                             case 't': argBuf.append( '\t' ); break;
244
245                             default:
246                                 char ch2 = argStr.charAt( cIdx+1 );
247                                 char ch3 = argStr.charAt( cIdx+2 );
248                                 if ( (ch >= '0' && ch <= '7')
249                                         && (ch2 >= '0' && ch2 <= '7')
250                                         && (ch3 >= '0' && ch3 <= '7') )
251                                     {
252                                     int octal =
253                                         ( ( (ch - '0') * 64 )
254                                             + ( (ch2 - '0') * 8 )
255                                                 + (ch3 - '0') );
256                                     argBuf.append( (char) octal );
257                                     cIdx += 2;
258                                     }
259                                 else if ( ch == '0' )
260                                     {
261                                     argBuf.append( '\0' );
262                                     }
263                                 else
264                                     {
265                                     argBuf.append( ch );
266                                     }
267                                 break;
268                             }
269                         }
270                     else
271                         {
272                         argBuf.append( ch );
273                         }
274
275                     backSlash = false;
276                     break;
277                 }
278             }
279
280         if ( argBuf.length() > 0 )
281             {
282             result.addElement( argBuf.toString() );
283             }
284
285         return result;
286         }
287     
288     }
289
290
Popular Tags