Debian Codegraph @

net/sf/saxon/value/Whitespace.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.value;
9
10 import net.sf.saxon.tree.iter.UnfailingIterator;
11 import net.sf.saxon.tree.tiny.CharSlice;
12 import net.sf.saxon.tree.tiny.CompressedWhitespace;
13 import net.sf.saxon.tree.util.FastStringBuffer;
14
15 /**
16 * This class provides helper methods and constants for handling whitespace
17 */
18 public class Whitespace {
19
20 private Whitespace() {
21 }
22
23
24 /**
25 * The values PRESERVE, REPLACE, and COLLAPSE represent the three options for whitespace
26 * normalization. They are deliberately chosen in ascending strength order; given a number
27 * of whitespace facets, only the strongest needs to be carried out. The option TRIM is
28 * used instead of COLLAPSE when all valid values have no interior whitespace; trimming
29 * leading and trailing whitespace is then equivalent to the action of COLLAPSE, but faster.
30 */
31
32 public static final int PRESERVE = 0;
33 public static final int REPLACE = 1;
34 public static final int COLLAPSE = 2;
35 public static final int TRIM = 3;
36
37 /**
38 * The values NONE, IGNORABLE, and ALL identify which kinds of whitespace text node
39 * should be stripped when building a source tree. UNSPECIFIED indicates that no
40 * particular request has been made. XSLT indicates that whitespace should be stripped
41 * as defined by the xsl:strip-space and xsl:preserve-space declarations in the stylesheet
42 */
43
44 public static final int NONE = 0;
45 public static final int IGNORABLE = 1;
46 public static final int ALL = 2;
47 public static final int UNSPECIFIED = 3;
48 public static final int XSLT = 4;
49
50 /**
51 * Test whether a character is whitespace
52 *
53 * @param ch the character (Unicode codepoint) to be tested
54 * @return true if the character is one of tab, newline, carriage return, or space
55 */
56
57 public static boolean isWhitespace(int ch) {
58 switch (ch) {
59 case 9:
60 case 10:
61 case 13:
62 case 32:
63 return true;
64 default:
65 return false;
66 }
67 }
68
69 /**
70 * Apply schema-defined whitespace normalization to a string
71 *
72 * @param action the action to be applied: one of PRESERVE, REPLACE, or COLLAPSE
73 * @param value the value to be normalized
74 * @return the value after normalization
75 */
76
77 public static CharSequence applyWhitespaceNormalization(int action, /*@NotNull*/ CharSequence value) {
78 switch (action) {
79 case PRESERVE:
80 return value;
81 case REPLACE:
82 FastStringBuffer sb = new FastStringBuffer(value.length());
83 for (int i = 0; i < value.length(); i++) {
84 char c = value.charAt(i);
85 switch (c) {
86 case '\n':
87 case '\r':
88 case '\t':
89 sb.append(' ');
90 break;
91 default:
92 sb.append(c);
93 }
94 }
95 return sb;
96 case COLLAPSE:
97 return collapseWhitespace(value);
98 case TRIM:
99 return trimWhitespace(value);
100 default:
101 throw new IllegalArgumentException("Unknown whitespace facet value");
102 }
103 }
104
105 /**
106 * Remove all whitespace characters from a string
107 *
108 * @param value the string from which whitespace is to be removed
109 * @return the string without its whitespace. This may be the original value
110 * if it contained no whitespace
111 */
112
113 /*@NotNull*/
114 public static CharSequence removeAllWhitespace(/*@NotNull*/ CharSequence value) {
115 if (containsWhitespace(value)) {
116 FastStringBuffer sb = new FastStringBuffer(value.length());
117 for (int i = 0; i < value.length(); i++) {
118 char c = value.charAt(i);
119 if (c > 32 || !C0WHITE[c]) {
120 sb.append(c);
121 }
122 }
123 return sb;
124 } else {
125 return value;
126 }
127 }
128
129 /**
130 * Remove leading whitespace characters from a string
131 *
132 * @param value the string whose leading whitespace is to be removed
133 * @return the string with leading whitespace removed. This may be the
134 * original string if there was no leading whitespace
135 */
136
137 public static CharSequence removeLeadingWhitespace(/*@NotNull*/ CharSequence value) {
138 final int len = value.length();
139 // quick exit for common cases...
140 if (len == 0 || value.charAt(0) > 32) {
141 return value;
142 }
143 int start = -1;
144 for (int i = 0; i < len; i++) {
145 char c = value.charAt(i);
146 if (c > 32 || !C0WHITE[c]) {
147 start = i;
148 break;
149 }
150 }
151 if (start == 0) {
152 return value;
153 } else if (start < 0 || start == len) {
154 return "";
155 } else {
156 return value.subSequence(start, len);
157 }
158 }
159
160 /**
161 * Determine if a string contains any whitespace
162 *
163 * @param value the string to be tested
164 * @return true if the string contains a character that is XML whitespace, that is
165 * tab, newline, carriage return, or space
166 */
167
168 public static boolean containsWhitespace(CharSequence value) {
169 for (int i = value.length() - 1; i >= 0; ) {
170 char c = value.charAt(i--);
171 if (c <= 32 && C0WHITE[c]) {
172 return true;
173 }
174 }
175 return false;
176 }
177
178 /**
179 * Determine if a string is all-whitespace
180 *
181 * @param content the string to be tested
182 * @return true if the supplied string contains no non-whitespace
183 * characters
184 */
185
186 public static boolean isWhite(/*@NotNull*/ CharSequence content) {
187 if (content instanceof CompressedWhitespace) {
188 return true;
189 }
190 final int len = content.length();
191 for (int i = 0; i < len; ) {
192 // all valid XML 1.0 whitespace characters, and only whitespace characters, are <= 0x20
193 // But XML 1.1 allows non-white characters that are also < 0x20, so we need a specific test for these
194 char c = content.charAt(i++);
195 if (c > 32 || !C0WHITE[c]) {
196 return false;
197 }
198 }
199 return true;
200 }
201
202 /*@NotNull*/ private static boolean[] C0WHITE = {
203 false, false, false, false, false, false, false, false, // 0-7
204 false, true, true, false, false, true, false, false, // 8-15
205 false, false, false, false, false, false, false, false, // 16-23
206 false, false, false, false, false, false, false, false, // 24-31
207 true // 32
208 };
209
210 /**
211 * Determine if a character is whitespace
212 *
213 * @param c the character to be tested
214 * @return true if the character is a whitespace character
215 */
216
217 public static boolean isWhite(char c) {
218 return c <= 32 && C0WHITE[c];
219 }
220
221 /**
222 * Normalize whitespace as defined in XML Schema. Note that this is not the same
223 * as the XPath normalize-space() function, which is supported by the
224 * {@link #collapseWhitespace} method
225 *
226 * @param in the string to be normalized
227 * @return a copy of the string in which any whitespace character is replaced by
228 * a single space character
229 */
230
231 /*@NotNull*/
232 public static CharSequence normalizeWhitespace(/*@NotNull*/ CharSequence in) {
233 FastStringBuffer sb = new FastStringBuffer(in.length());
234 for (int i = 0; i < in.length(); i++) {
235 char c = in.charAt(i);
236 switch (c) {
237 case '\n':
238 case '\r':
239 case '\t':
240 sb.append(' ');
241 break;
242 default:
243 sb.append(c);
244 break;
245 }
246 }
247 return sb;
248 }
249
250 /**
251 * Collapse whitespace as defined in XML Schema. This is equivalent to the
252 * XPath normalize-space() function
253 *
254 * @param in the string whose whitespace is to be collapsed
255 * @return the string with any leading or trailing whitespace removed, and any
256 * internal sequence of whitespace characters replaced with a single space character.
257 */
258
259 /*@NotNull*/
260 public static CharSequence collapseWhitespace(/*@NotNull*/ CharSequence in) {
261 if (!containsWhitespace(in)) {
262 return in;
263 }
264 int len = in.length();
265 FastStringBuffer sb = new FastStringBuffer(len);
266 boolean inWhitespace = true;
267 int i = 0;
268 for (; i < len; i++) {
269 char c = in.charAt(i);
270 switch (c) {
271 case '\n':
272 case '\r':
273 case '\t':
274 case ' ':
275 if (inWhitespace) {
276 // remove the whitespace
277 } else {
278 sb.append(' ');
279 inWhitespace = true;
280 }
281 break;
282 default:
283 sb.append(c);
284 inWhitespace = false;
285 break;
286 }
287 }
288 int nlen = sb.length();
289 if (nlen > 0 && sb.charAt(nlen - 1) == ' ') {
290 sb.setLength(nlen - 1);
291 }
292 return sb;
293 }
294
295 /**
296 * Remove leading and trailing whitespace. This has the same effect as collapseWhitespace,
297 * but is cheaper, for use by data types that do not allow internal whitespace.
298 *
299 * @param in the input string whose whitespace is to be removed
300 * @return the result of removing excess whitespace
301 */
302 public static CharSequence trimWhitespace(/*@NotNull*/ CharSequence in) {
303 if (in.length() == 0) {
304 return in;
305 }
306 int first = 0;
307 int last = in.length() - 1;
308 while (true) {
309 final char x = in.charAt(first);
310 if (x > 32 || !C0WHITE[x]) {
311 break;
312 }
313 if (first++ >= last) {
314 return "";
315 }
316 }
317 while (true) {
318 final char x = in.charAt(last);
319 if (x > 32 || !C0WHITE[x]) {
320 break;
321 }
322 last--;
323 }
324 if (first == 0 && last == in.length() - 1) {
325 return in;
326 } else {
327 return in.subSequence(first, last + 1);
328 }
329 }
330
331 /**
332 * Trim leading and trailing whitespace from a string, returning a string.
333 * This differs from the Java trim() method in that the only characters treated as
334 * whitespace are space, \n, \r, and \t. The String#trim() method removes all C0
335 * control characters (which is not the same thing under XML 1.1).
336 *
337 * @param s the string to be trimmed. If null is supplied, null is returned.
338 * @return the string with leading and trailing whitespace removed.
339 * Returns null if and only if the supplied argument is null.
340 */
341
342 /*@Nullable*/
343 public static String trim(/*@Nullable*/ CharSequence s) {
344 if (s == null) {
345 return null;
346 }
347 return trimWhitespace(s).toString();
348 }
349
350 /**
351 * An iterator that splits a string on whitespace boundaries, corresponding to the XPath 3.1 function tokenize#1
352 */
353
354 public static class Tokenizer implements UnfailingIterator<StringValue> {
355
356 private char[] input;
357 private int position;
358
359 public Tokenizer(char[] input) {
360 this.input = input;
361 this.position = 0;
362 }
363
364 public Tokenizer(CharSequence input) {
365 this.input = CharSlice.toCharArray(input);
366 this.position = 0;
367 }
368
369 @Override
370 public StringValue next() {
371 int start = position;
372 int eol = input.length;
373 while (start < eol && isWhite(input[start])) {
374 start++;
375 }
376 if (start >= eol) {
377 return null;
378 }
379 int end = start;
380 while (end < eol && !isWhite(input[end])) {
381 end++;
382 }
383 position = end;
384 return StringValue.makeStringValue(new CharSlice(input, start, end-start));
385 }
386
387 @Override
388 public void close() {
389 // no action
390 }
391
392 @Override
393 public int getProperties() {
394 return 0;
395 }
396 }
397 }