KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > utility > StringScanner


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * StringScanner.java
26  *
27  * Created on March 3, 2000
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.utility;
31
32 import java.util.*;
33
34 /**
35  * Helper class for breaking up a string.
36  * <P>
37  *<P>
38  */

39 //
40
public class StringScanner
41 {
42     private static String JavaDoc intStrTable[] = new String JavaDoc[2048];
43
44     static int skipWhite(String JavaDoc s, int offset)
45     {
46         int end = s.length();
47
48         if (offset < 0)
49             return end;
50         while (offset < end && Character.isWhitespace(s.charAt(offset)))
51             offset++;
52         return offset;
53     }
54
55     static int skipTo(String JavaDoc s, int offset, String JavaDoc delim)
56     {
57         int end = s.length();
58
59         if (offset < 0)
60             return end;
61         while (offset < end && delim.indexOf(s.charAt(offset)) == -1)
62             offset++;
63         return offset;
64     }
65
66     static int skipTo(String JavaDoc s, int offset, char delim)
67     {
68         int end = s.length();
69
70         if (offset < 0)
71             return end;
72         while (offset < end && delim != s.charAt(offset))
73             offset++;
74         return offset;
75     }
76
77     static char charAt(String JavaDoc s, int offset)
78     {
79         if (offset < 0 || offset >= s.length())
80             return '\0';
81         return s.charAt(offset);
82     }
83
84     static int skipInt(String JavaDoc s, int offset)
85     {
86         int end = s.length();
87
88         if (offset < 0 || !Character.isDigit(s.charAt(offset)))
89             return end;
90         //
91
// Unlike the others, this can return the offset past the end
92
// of the string. The idea is that the caller wants to
93
// do a substring to get the integer.
94
//
95
while (offset < end && Character.isDigit(s.charAt(offset)))
96             offset++;
97         return offset;
98     }
99
100     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt, Object JavaDoc o1)
101     {
102         return format(buf, fmt, 1, o1, null, null, null, null, null, null,
103                 null, null);
104     }
105
106     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt,
107                         Object JavaDoc o1,
108                         Object JavaDoc o2)
109     {
110         return format(buf, fmt, 2, o1, o2, null, null, null, null, null,
111                 null, null);
112     }
113
114     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt,
115                          Object JavaDoc o1,
116                          Object JavaDoc o2,
117                          Object JavaDoc o3)
118     {
119         return format(buf, fmt, 3, o1, o2, o3, null, null, null, null,
120                     null, null);
121     }
122
123     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt,
124                          Object JavaDoc o1,
125                          Object JavaDoc o2,
126                          Object JavaDoc o3,
127                          Object JavaDoc o4)
128     {
129         return format(buf, fmt, 4, o1, o2, o3, o4, null, null, null,
130                     null, null);
131     }
132
133     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt,
134                          Object JavaDoc o1,
135                          Object JavaDoc o2,
136                          Object JavaDoc o3,
137                          Object JavaDoc o4,
138                          Object JavaDoc o5)
139     {
140         return format(buf, fmt, 5, o1, o2, o3, o4, o5, null, null, null, null);
141     }
142
143     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt,
144                          Object JavaDoc o1,
145                          Object JavaDoc o2,
146                          Object JavaDoc o3,
147                          Object JavaDoc o4,
148                          Object JavaDoc o5,
149                          Object JavaDoc o6)
150     {
151         return format(buf, fmt, 6, o1, o2, o3, o4, o5, o6, null, null, null);
152     }
153
154     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt,
155                          Object JavaDoc o1,
156                          Object JavaDoc o2,
157                          Object JavaDoc o3,
158                          Object JavaDoc o4,
159                          Object JavaDoc o5,
160                          Object JavaDoc o6,
161                          Object JavaDoc o7)
162     {
163         return format(buf, fmt, 7, o1, o2, o3, o4, o5, o6, o7, null, null);
164     }
165
166     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt,
167                          Object JavaDoc o1,
168                          Object JavaDoc o2,
169                          Object JavaDoc o3,
170                          Object JavaDoc o4,
171                          Object JavaDoc o5,
172                          Object JavaDoc o6,
173                          Object JavaDoc o7,
174                          Object JavaDoc o8)
175     {
176         return format(buf, fmt, 8, o1, o2, o3, o4, o5, o6, o7, o8, null);
177     }
178
179     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt,
180                          Object JavaDoc o1,
181                          Object JavaDoc o2,
182                          Object JavaDoc o3,
183                          Object JavaDoc o4,
184                          Object JavaDoc o5,
185                          Object JavaDoc o6,
186                          Object JavaDoc o7,
187                          Object JavaDoc o8,
188                          Object JavaDoc o9)
189     {
190         return format(buf, fmt, 9, o1, o2, o3, o4, o5, o6, o7, o8, o9);
191     }
192
193     public static String JavaDoc format(StringBuffer JavaDoc buf, String JavaDoc fmt,
194                          int argcnt,
195                          Object JavaDoc o1,
196                          Object JavaDoc o2,
197                          Object JavaDoc o3,
198                          Object JavaDoc o4,
199                          Object JavaDoc o5,
200                          Object JavaDoc o6,
201                          Object JavaDoc o7,
202                          Object JavaDoc o8,
203                          Object JavaDoc o9)
204     {
205         Object JavaDoc[] params = {o1, o2, o3, o4, o5, o6, o7, o8, o9};
206
207         int i = 0;
208         int begSubstr = 0;
209         int percent;
210         int nextParam = 0;
211         StringBuffer JavaDoc msg;
212         int fmtLen;
213
214         if (buf == null)
215             msg = new StringBuffer JavaDoc();
216         else
217             msg = buf;
218
219         fmtLen = fmt.length();
220
221         while ((percent = fmt.indexOf('%', i)) >= 0 && percent < (fmtLen - 1))
222         {
223             char c = fmt.charAt(percent + 1);
224             boolean leftJustify = false;
225             boolean raw = false;
226             int nextChar = percent;
227
228             if (c == '-')
229             {
230                 nextChar++;
231                 leftJustify = true;
232                 if (nextChar + 1 < fmtLen)
233                     c = fmt.charAt(nextChar + 1);
234             }
235
236             if (Character.isDigit(c))
237             {
238                 int endInt = StringScanner.skipInt(fmt, nextChar + 1);
239                 int size = Integer.parseInt(fmt.substring(nextChar + 1,endInt));
240
241                 if (size == 0)
242                     raw = true;
243
244                 msg.append(fmt.substring(begSubstr, percent));
245                 if (nextParam < argcnt)
246                 {
247                     Object JavaDoc p = params[nextParam++];
248                     String JavaDoc val = p.toString();
249                     int len = val.length();
250                     
251                     if (!raw)
252                     {
253                         if (!leftJustify && len < size)
254                         {
255                             for (int j = 0; j < size - len; j++)
256                                 msg.append(" "); // NOI18N
257
}
258                         else if (len > size)
259                         {
260                             val = val.substring(0, size);
261                         }
262                     }
263                     msg.append(val);
264                     if (leftJustify && len < size)
265                     {
266                         for (int j = 0; j < size - len; j++)
267                             msg.append(" "); // NOI18N
268
}
269                 }
270                 else
271                 {
272                     /* throw (StringIndexOutOfBoundsException)
273                         ErrorManager.createFormatAdd(
274                             java.lang.StringIndexOutOfBoundsException.class,
275                             ErrorManager.USER,
276                             UtilMsgCat.SP_ERR_FMT_OUT_OF_RANGE,
277                             (new Integer(nextParam)),
278                             (new Integer(1)), (new Integer(argcnt)), fmt);
279                     */

280                 }
281                 i = endInt;
282                 begSubstr = i;
283             }
284             else
285             {
286                 i = nextChar + 1;
287             }
288         }
289         msg.append(fmt.substring(begSubstr, fmtLen));
290         return msg.toString();
291     }
292
293     static String JavaDoc createParamString(String JavaDoc message,
294                                     int argcnt,
295                                     Object JavaDoc param1,
296                                     Object JavaDoc param2,
297                                     Object JavaDoc param3,
298                                     Object JavaDoc param4,
299                                     Object JavaDoc param5,
300                                     Object JavaDoc param6,
301                                     Object JavaDoc param7,
302                                     Object JavaDoc param8,
303                                     Object JavaDoc param9)
304     {
305         if (argcnt < 1 || argcnt > 9)
306             return message;
307
308         Object JavaDoc[] p = null;
309         if (argcnt == 1)
310         {
311             Object JavaDoc[] params = {param1};
312             p = params;
313         }
314         else if (argcnt == 2)
315         {
316             Object JavaDoc[] params = {param1, param2};
317             p = params;
318         }
319         else if (argcnt == 3)
320         {
321             Object JavaDoc[] params = {param1, param2, param3};
322             p = params;
323         }
324         else if (argcnt == 4)
325         {
326             Object JavaDoc[] params = {param1, param2, param3, param4};
327             p = params;
328         }
329         else if (argcnt == 5)
330         {
331             Object JavaDoc[] params = {param1, param2, param3, param4, param5};
332             p = params;
333         }
334         else if (argcnt == 6)
335         {
336             Object JavaDoc[] params = {param1, param2, param3, param4, param5,
337                                    param6};
338             p = params;
339         }
340         else if (argcnt == 7)
341         {
342             Object JavaDoc[] params = {param1, param2, param3, param4, param5,
343                                    param6, param7};
344             p = params;
345         }
346         else if (argcnt == 8)
347         {
348             Object JavaDoc[] params = {param1, param2, param3, param4, param5,
349                                    param6, param7, param8};
350             p = params;
351         }
352         else if (argcnt == 9)
353         {
354             Object JavaDoc[] params = {param1, param2, param3, param4, param5,
355                                    param6, param7, param8, param9};
356             p = params;
357         }
358
359         //
360
// Decide if we should use our own formatting algorithm or
361
// call Java's MessageFormat.format(). Our own algorithm is
362
// about 20 times faster but cannot deal with templates.
363
// So, use our own if no template is involved. i.e. only
364
// positional parameters.
365
// e.g. {0} is OK but {0,ddd-mmm-yyyy} is not.
366
//
367
boolean useJava = false;
368         int lbrace, rbrace = 0;
369         int i = 0;
370
371         // look for the start of next positional parameter
372
while ((lbrace = message.indexOf('{', i)) >= 0)
373         {
374             // if the parameter doesn't end with a right brace, get
375
// out and use the Java formatter. Or if there are more
376
// than 1 character between the braces (e.g. {0,ddd-mmm-yyyy})
377
if ((rbrace = message.indexOf('}', lbrace + 1)) < 0 ||
378                 (rbrace - lbrace) > 2)
379             {
380                 useJava = true;
381                 break;
382             }
383
384             i = rbrace + 1;
385         }
386
387         if (useJava)
388         {
389             return java.text.MessageFormat.format(message, p);
390         }
391
392         // Since we are using our own algorithm, we need to get rid of
393
// the pesty single quote escape character
394
int esc;
395         boolean foundEsc = false;
396         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
397
398         i = 0;
399         while ((esc = message.indexOf('\'', i)) >= 0)
400         {
401             msg.append(message.substring(i, esc));
402             msg.append(message.substring(esc + 1, esc + 2));
403             i = esc + 2;
404             foundEsc = true;
405         }
406
407         if (foundEsc)
408         {
409             msg.append(message.substring(i, message.length()));
410             message = msg.toString();
411         }
412
413         char c;
414         i = 0;
415         int msglen = message.length();
416         msg = new StringBuffer JavaDoc();
417         while ( (lbrace = message.indexOf('{', i)) >= 0
418                && lbrace < msglen-1)
419         {
420             c = message.charAt(lbrace + 1);
421
422             if (Character.isDigit(c))
423             {
424                 int pnum = c - '0';
425
426                 if (0 <= pnum && pnum < argcnt)
427                 {
428                     Object JavaDoc pp = p[pnum];
429
430                     msg.append(message.substring(i, lbrace));
431
432                     if (pp == null)
433                     {
434                         msg.append("<null>"); // NOI18N
435
}
436                     else
437                     {
438                         msg.append(pp.toString());
439                     }
440                 }
441                 else
442                 {
443                     if (argcnt > 0)
444                     {
445                         /* throw (StringIndexOutOfBoundsException)
446                             ErrorManager.createFormatAdd(
447                                 java.lang.StringIndexOutOfBoundsException.class,
448                                 ErrorManager.USER,
449                                 UtilMsgCat.SP_ERR_FMT_OUT_OF_RANGE,
450                                 (new Integer(pnum)),
451                                 (new Integer(1)), (new Integer(argcnt)), msg);
452                         */

453                     }
454                     else
455                     {
456                         /* throw (StringIndexOutOfBoundsException)
457                             ErrorManager.createFormatAdd(
458                                 java.lang.StringIndexOutOfBoundsException.class,
459                                 ErrorManager.USER,
460                                 UtilMsgCat.SP_ERR_FMT_MISSING_PARAM,
461                                 (new Integer(pnum)), msg);
462                         */

463                     }
464                 }
465                 i = lbrace + 3;
466             }
467             else
468             {
469                 i = lbrace + 1;
470             }
471         }
472         msg.append(message.substring(i, msglen));
473         return msg.toString();
474     }
475
476     /**
477      * Return a string containing a "level" number of spaces.
478      * <P>
479      * Used for formatted print, this will return a string containing 4
480      * spaces for each specified level.
481      * @return The requested string.
482      * @param level The level to fill to.
483      */

484     //
485
// CHANGES
486
// 6-aug-1997
487
// Created (jak)
488
//
489
//
490
public static String JavaDoc levelString(int level)
491     {
492         int i;
493         String JavaDoc str;
494
495         if (level <= 0)
496             return ""; // NOI18N
497

498         switch (level)
499         {
500         case 1:
501             return " "; // NOI18N
502
case 2:
503             return " "; // NOI18N
504
case 3:
505             return " "; // NOI18N
506
case 4:
507             return " "; // NOI18N
508
case 5:
509             return " "; // NOI18N
510
case 6:
511             return " "; // NOI18N
512
default:
513             str = new String JavaDoc(" "); // NOI18N
514
for (i = 6; i < level; i++)
515                 str = str.concat(" "); // NOI18N
516
return str;
517         }
518     }
519
520     /**
521      * Split a string into sub-strings based on a given delimeter.
522      * <P>
523      * Split str based on the delimeter and return as an array of Strings.
524      * @return An array of sub-strings
525      * @param delimeter The string representing the delimeters.
526      * @param str The string to process.
527      */

528     //
529
// CHANGES
530
// 12-jun-1997
531
// Created (jak)
532
// 6-aug-1997
533
// Moved from CommandShell (jak)
534
//
535
//
536
public static Vector splitString(String JavaDoc str, String JavaDoc delimeter)
537     {
538         int mark;
539         int start;
540         int delLen;
541         Vector list;
542     
543         list = new Vector();
544
545         if (str != null)
546         {
547             mark = 0;
548             start = 0;
549
550             delLen = delimeter.length();
551             while ((start = str.indexOf(delimeter, mark)) != -1)
552             {
553                 if (start != 0)
554                     list.addElement((Object JavaDoc) str.substring(mark, start));
555                 mark = start + delLen;
556             }
557
558             // Put in the last bit.
559
if (mark < str.length())
560                 list.addElement((Object JavaDoc) str.substring(mark));
561         }
562         return list;
563     }
564
565     /**
566      * Fills a string with blanks to a given size.
567      * <P>
568      * Left or right fills the given string with spaces (' ').
569      * @return A new filled string.
570      * false.
571      * @param left true when the text should be left justified, otherwise
572      * @param len The desired length of the formatted string.
573      * @param str The string to process.
574      */

575     //
576
// CHANGES
577
// 19-aug-1997
578
// Created (jak)
579
//
580
//
581
public static String JavaDoc fill(String JavaDoc str, int len, boolean left)
582     {
583         String JavaDoc nstr;
584         int startLen;
585         int diff;
586
587         if (str == null)
588             str = "null"; // NOI18N
589

590         startLen = str.length();
591         nstr = str;
592         while (startLen < len)
593         {
594             diff = len - startLen;
595             if (diff >= 10)
596             {
597                 startLen += 10;
598                 if (left)
599                     nstr = nstr + " "; // NOI18N
600
else
601                     nstr = " " + nstr; // NOI18N
602
continue;
603             }
604             else if (diff >= 8)
605             {
606                 startLen += 8;
607                 if (left)
608                     nstr = nstr + " "; // NOI18N
609
else
610                     nstr = " " + nstr; // NOI18N
611
continue;
612             }
613             else if (diff >= 4)
614             {
615                 startLen += 4;
616                 if (left)
617                     nstr = nstr + " "; // NOI18N
618
else
619                     nstr = " " + nstr; // NOI18N
620
continue;
621             }
622             else if (diff >= 2)
623             {
624                 startLen += 2;
625                 if (left)
626                     nstr = nstr + " "; // NOI18N
627
else
628                     nstr = " " + nstr; // NOI18N
629
continue;
630             }
631             else
632             {
633                 startLen++;
634                 if (left)
635                     nstr = nstr + " "; // NOI18N
636
else
637                     nstr = " " + nstr; // NOI18N
638
continue;
639             }
640         }
641         return nstr;
642     }
643
644     public static String JavaDoc createParamString(String JavaDoc fmt)
645     {
646         return fmt;
647     }
648     public static String JavaDoc createParamString(String JavaDoc fmt, Object JavaDoc obj1)
649     {
650         return StringScanner.createParamString(fmt, 1, obj1, null, null, null,
651                                 null, null, null, null, null);
652     }
653     public static String JavaDoc createParamString(String JavaDoc fmt, Object JavaDoc obj1, Object JavaDoc obj2)
654     {
655         return StringScanner.createParamString(fmt, 2, obj1, obj2, null, null,
656                                 null, null, null, null, null);
657     }
658     public static String JavaDoc createParamString(String JavaDoc fmt, Object JavaDoc obj1, Object JavaDoc obj2,
659                                            Object JavaDoc obj3)
660     {
661         return StringScanner.createParamString(fmt, 3, obj1, obj2, obj3, null,
662                                 null, null, null, null, null);
663     }
664     public static String JavaDoc createParamString(String JavaDoc fmt, Object JavaDoc obj1, Object JavaDoc obj2,
665                                            Object JavaDoc obj3, Object JavaDoc obj4)
666     {
667         return StringScanner.createParamString(fmt, 4, obj1, obj2, obj3, obj4,
668                                 null, null, null, null, null);
669     }
670     public static String JavaDoc createParamString(String JavaDoc fmt, Object JavaDoc obj1, Object JavaDoc obj2,
671                                            Object JavaDoc obj3, Object JavaDoc obj4,
672                                            Object JavaDoc obj5)
673     {
674         return StringScanner.createParamString(fmt, 5, obj1, obj2, obj3, obj4,
675                                 obj5, null, null, null, null);
676     }
677     public static String JavaDoc createParamString(String JavaDoc fmt, Object JavaDoc obj1, Object JavaDoc obj2,
678                                            Object JavaDoc obj3, Object JavaDoc obj4,
679                                            Object JavaDoc obj5, Object JavaDoc obj6)
680     {
681         return StringScanner.createParamString(fmt, 6, obj1, obj2, obj3, obj4,
682                                 obj5, obj6, null, null, null);
683     }
684     public static String JavaDoc createParamString(String JavaDoc fmt, Object JavaDoc obj1, Object JavaDoc obj2,
685                                            Object JavaDoc obj3, Object JavaDoc obj4,
686                                            Object JavaDoc obj5, Object JavaDoc obj6,
687                                            Object JavaDoc obj7)
688     {
689         return StringScanner.createParamString(fmt, 7, obj1, obj2, obj3, obj4,
690                                 obj5, obj6, obj7, null, null);
691     }
692     public static String JavaDoc createParamString(String JavaDoc fmt, Object JavaDoc obj1, Object JavaDoc obj2,
693                                            Object JavaDoc obj3, Object JavaDoc obj4,
694                                            Object JavaDoc obj5, Object JavaDoc obj6,
695                                            Object JavaDoc obj7, Object JavaDoc obj8)
696     {
697         return StringScanner.createParamString(fmt, 8, obj1, obj2, obj3, obj4,
698                                 obj5, obj6, obj7, obj8, null);
699     }
700     public static String JavaDoc createParamString(String JavaDoc fmt, Object JavaDoc obj1, Object JavaDoc obj2,
701                                            Object JavaDoc obj3, Object JavaDoc obj4,
702                                            Object JavaDoc obj5, Object JavaDoc obj6,
703                                            Object JavaDoc obj7, Object JavaDoc obj8,
704                                            Object JavaDoc obj9)
705     {
706         return StringScanner.createParamString(fmt, 9, obj1, obj2, obj3, obj4,
707                                 obj5, obj6, obj7, obj8, obj9);
708     }
709
710     
711     public static String JavaDoc getIntStr(int num)
712     {
713         String JavaDoc str;
714         boolean big;
715
716         try
717         {
718             str = (String JavaDoc) StringScanner.intStrTable[num];
719             big = false;
720         }
721         catch (ArrayIndexOutOfBoundsException JavaDoc e)
722         {
723             str = null;
724             big = true;
725         }
726
727         if (str == null)
728         {
729             str = Integer.toString(num);
730             if (!big)
731                 StringScanner.intStrTable[num] = str;
732         }
733         return str;
734     }
735 }
736
Popular Tags