KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > value > Whitespace


1 package net.sf.saxon.value;
2
3 import net.sf.saxon.functions.NormalizeSpace;
4 import net.sf.saxon.om.FastStringBuffer;
5
6 /**
7  * This class provides helper methods and constants for handling whitespace
8  */

9 public class Whitespace {
10
11     private Whitespace() {}
12
13
14     /**
15      * The values PRESERVE, REPLACE, and COLLAPSE represent the three options for whitespace
16      * normalization. They are deliberately chosen in ascending strength order; given a number
17      * of whitespace facets, only the strongest needs to be carried out.
18      */

19
20     public static final int PRESERVE = 0;
21     public static final int REPLACE = 1;
22     public static final int COLLAPSE = 2;
23
24     /**
25      * Apply schema-defined whitespace normalization to a string
26      * @param action the action to be applied: one of PRESERVE, REPLACE, or COLLAPSE
27      * @param value the value to be normalized
28      * @return the value after normalization
29      */

30
31     public static CharSequence JavaDoc applyWhitespaceNormalization(int action, CharSequence JavaDoc value) {
32         switch (action) {
33             case PRESERVE:
34                 return value;
35             case REPLACE:
36                 FastStringBuffer sb = new FastStringBuffer(value.length());
37                 for (int i=0; i<value.length(); i++) {
38                     char c = value.charAt(i);
39                     switch (c) {
40                         case '\n':
41                         case '\r':
42                         case '\t':
43                             sb.append(' ');
44                         default:
45                             sb.append(c);
46                     }
47                 }
48                 return sb;
49             case COLLAPSE:
50                 return NormalizeSpace.normalize(value.toString());
51             default:
52                 throw new IllegalArgumentException JavaDoc("Unknown whitespace facet value");
53         }
54     }
55
56     /**
57      * Determine if a string is all-whitespace
58      *
59      * @param content the string to be tested
60      * @return true if the supplied string contains no non-whitespace
61      * characters
62      */

63
64     public static final boolean isWhite(CharSequence JavaDoc content) {
65         int len = content.length();
66         for (int i=0; i<len;) {
67             // all valid XML 1.0 whitespace characters, and only whitespace characters, are <= 0x20
68
// But XML 1.1 allows non-white characters that are also < 0x20, so we need a specific test for these
69
char c = content.charAt(i++);
70             if (c > 32 || !C0WHITE[c]) {
71                 return false;
72             }
73         }
74         return true;
75     }
76
77     private static boolean[] C0WHITE = {
78         false, false, false, false, false, false, false, false, // 0-7
79
false, true, true, false, false, true, false, false, // 8-15
80
false, false, false, false, false, false, false, false, // 16-23
81
false, false, false, false, false, false, false, false, // 24-31
82
true // 32
83
};
84 }
85 //
86
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
87
// you may not use this file except in compliance with the License. You may obtain a copy of the
88
// License at http://www.mozilla.org/MPL/
89
//
90
// Software distributed under the License is distributed on an "AS IS" basis,
91
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
92
// See the License for the specific language governing rights and limitations under the License.
93
//
94
// The Original Code is: all this file
95
//
96
// The Initial Developer of the Original Code is Michael H. Kay.
97
//
98
// Contributor(s):
99
//
100

101
Popular Tags