KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > WhiteSpaceProcessor


1 package javax.xml.bind;
2
3 /**
4  * Processes white space normalization.
5  *
6  * @since 1.0
7  */

8 abstract class WhiteSpaceProcessor {
9
10 // benchmarking (see test/src/ReplaceTest.java in the CVS Attic)
11
// showed that this code is slower than the current code.
12
//
13
// public static String replace(String text) {
14
// final int len = text.length();
15
// StringBuffer result = new StringBuffer(len);
16
//
17
// for (int i = 0; i < len; i++) {
18
// char ch = text.charAt(i);
19
// if (isWhiteSpace(ch))
20
// result.append(' ');
21
// else
22
// result.append(ch);
23
// }
24
//
25
// return result.toString();
26
// }
27

28     public static String JavaDoc replace(String JavaDoc text) {
29         return replace( (CharSequence JavaDoc)text ).toString();
30     }
31
32     /**
33      * @since 2.0
34      */

35     public static CharSequence JavaDoc replace(CharSequence JavaDoc text) {
36         int i=text.length()-1;
37
38         // look for the first whitespace char.
39
while( i>=0 && !isWhiteSpaceExceptSpace(text.charAt(i)) )
40             i--;
41
42         if( i<0 )
43             // no such whitespace. replace(text)==text.
44
return text;
45
46         // we now know that we need to modify the text.
47
// allocate a char array to do it.
48
StringBuilder JavaDoc buf = new StringBuilder JavaDoc(text);
49
50         buf.setCharAt(i--,' ');
51         for( ; i>=0; i-- )
52             if( isWhiteSpaceExceptSpace(buf.charAt(i)))
53                 buf.setCharAt(i,' ');
54
55         return new String JavaDoc(buf);
56     }
57
58     /**
59      * Equivalent of {@link String#trim()}.
60      * @since 2.0
61      */

62     public static CharSequence JavaDoc trim(CharSequence JavaDoc text) {
63         int len = text.length();
64         int start = 0;
65
66         while( start<len && isWhiteSpace(text.charAt(start)) )
67             start++;
68
69         int end = len-1;
70
71         while( end>start && isWhiteSpace(text.charAt(end)) )
72             end--;
73
74         if(start==0 && end==len-1)
75             return text; // no change
76
else
77             return text.subSequence(start,end+1);
78     }
79
80     public static String JavaDoc collapse(String JavaDoc text) {
81         return collapse( (CharSequence JavaDoc)text ).toString();
82     }
83
84     /**
85      * This is usually the biggest processing bottleneck.
86      *
87      * @since 2.0
88      */

89     public static CharSequence JavaDoc collapse(CharSequence JavaDoc text) {
90         int len = text.length();
91
92         // most of the texts are already in the collapsed form.
93
// so look for the first whitespace in the hope that we will
94
// never see it.
95
int s=0;
96         while(s<len) {
97             if(isWhiteSpace(text.charAt(s)))
98                 break;
99             s++;
100         }
101         if(s==len)
102             // the input happens to be already collapsed.
103
return text;
104
105         // we now know that the input contains spaces.
106
// let's sit down and do the collapsing normally.
107

108         StringBuilder JavaDoc result = new StringBuilder JavaDoc(len /*allocate enough size to avoid re-allocation*/ );
109
110         if(s!=0) {
111             for( int i=0; i<s; i++ )
112                 result.append(text.charAt(i));
113             result.append(' ');
114         }
115
116         boolean inStripMode = true;
117         for (int i = s+1; i < len; i++) {
118             char ch = text.charAt(i);
119             boolean b = isWhiteSpace(ch);
120             if (inStripMode && b)
121                 continue; // skip this character
122

123             inStripMode = b;
124             if (inStripMode)
125                 result.append(' ');
126             else
127                 result.append(ch);
128         }
129
130         // remove trailing whitespaces
131
len = result.length();
132         if (len > 0 && result.charAt(len - 1) == ' ')
133             result.setLength(len - 1);
134         // whitespaces are already collapsed,
135
// so all we have to do is to remove the last one character
136
// if it's a whitespace.
137

138         return result;
139     }
140
141     /**
142      * Returns true if the specified string is all whitespace.
143      */

144     public static final boolean isWhiteSpace(CharSequence JavaDoc s) {
145         for( int i=s.length()-1; i>=0; i-- )
146             if(!isWhiteSpace(s.charAt(i)))
147                 return false;
148         return true;
149     }
150
151     /** returns true if the specified char is a white space character. */
152     public static final boolean isWhiteSpace(char ch) {
153         // most of the characters are non-control characters.
154
// so check that first to quickly return false for most of the cases.
155
if( ch>0x20 ) return false;
156
157         // other than we have to do four comparisons.
158
return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20;
159     }
160
161     /**
162      * Returns true if the specified char is a white space character
163      * but not 0x20.
164      */

165     protected static final boolean isWhiteSpaceExceptSpace(char ch) {
166         // most of the characters are non-control characters.
167
// so check that first to quickly return false for most of the cases.
168
if( ch>=0x20 ) return false;
169
170         // other than we have to do four comparisons.
171
return ch == 0x9 || ch == 0xA || ch == 0xD;
172     }
173 }
174
Popular Tags