KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > StringSplitter


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.util;
16
17 /**
18  * Used to split a string into substrings based on a single character
19  * delimiter. A fast, simple version of
20  * {@link java.util.StringTokenizer}.
21  *
22  * @author Howard Lewis Ship
23  *
24  **/

25
26 public class StringSplitter
27 {
28     private char delimiter;
29
30     public StringSplitter(char delimiter)
31     {
32         this.delimiter = delimiter;
33     }
34
35     public char getDelimiter()
36     {
37         return delimiter;
38     }
39
40     /**
41      * Splits a string on the delimter into an array of String
42      * tokens. The delimiters are not included in the tokens. Null
43      * tokens (caused by two consecutive delimiter) are reduced to an
44      * empty string. Leading delimiters are ignored.
45      *
46      **/

47
48     public String JavaDoc[] splitToArray(String JavaDoc value)
49     {
50         char[] buffer;
51         int i;
52         String JavaDoc[] result;
53         int resultCount = 0;
54         int start;
55         int length;
56         String JavaDoc token;
57         String JavaDoc[] newResult;
58         boolean first = true;
59
60         buffer = value.toCharArray();
61
62         result = new String JavaDoc[3];
63
64         start = 0;
65         length = 0;
66
67         for (i = 0; i < buffer.length; i++)
68         {
69             if (buffer[i] != delimiter)
70             {
71                 length++;
72                 continue;
73             }
74
75             // This is used to ignore leading delimiter(s).
76

77             if (length > 0 || !first)
78             {
79                 token = new String JavaDoc(buffer, start, length);
80
81                 if (resultCount == result.length)
82                 {
83                     newResult = new String JavaDoc[result.length * 2];
84
85                     System.arraycopy(result, 0, newResult, 0, result.length);
86
87                     result = newResult;
88                 }
89
90                 result[resultCount++] = token;
91
92                 first = false;
93             }
94
95             start = i + 1;
96             length = 0;
97         }
98
99         // Special case: if the string contains no delimiters
100
// then it isn't really split. Wrap the input string
101
// in an array and return. This is a little optimization
102
// to prevent a new String instance from being
103
// created unnecessarily.
104

105         if (start == 0 && length == buffer.length)
106         {
107             result = new String JavaDoc[1];
108             result[0] = value;
109             return result;
110         }
111
112         // If the string is all delimiters, then this
113
// will result in a single empty token.
114

115         token = new String JavaDoc(buffer, start, length);
116
117         newResult = new String JavaDoc[resultCount + 1];
118         System.arraycopy(result, 0, newResult, 0, resultCount);
119         newResult[resultCount] = token;
120
121         return newResult;
122     }
123 }
Popular Tags