001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.xs.facets;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: /**
023: * Captures the whitespace facet.
024: * <p>
025: * Constants and utility method for old fashion facet goodness. See BooleanXOHandler for an example.
026: * <p>
027: * Here is an example use:<pre><code>
028: * <simpleType name='token'>
029: * <restriction base='normalizedString'>
030: * <whiteSpace value='collapse'/>
031: * </restriction>
032: * </simpleType>
033: * </code></pre>
034: * @see a longing for Java 5 Enum construct
035: * @see <a href="http://www.w3.org/TR/xmlschema-2/#rf-whiteSpace">This web page</a>
036: * @author jgarnett
037: * @since 1.0.0
038: */
039: public abstract class Whitespace implements Comparable {
040: /**
041: * No normalization is done, the value is not changed
042: * <p>
043: * Simon the spec says: <i> No normalization is done, the value is not changed (this is the
044: * behavior required by [XML 1.0 (Second Edition)] for element content) </i>
045: * </p>
046: */
047: public static final Whitespace PRESERVE = new Whitespace(
048: "preserve", 0) {
049: public String preparse(String text) {
050: return text;
051: }
052: };
053:
054: /**
055: * All occurrences of tab, line feed and carriage return are replaced with space.
056: * <p>
057: * Simon the spec says: <i> All occurrences of #x9 (tab), #xA (line feed) and #xD (carriage
058: * return) are replaced with #x20 (space) </i>
059: * </p>
060: */
061: public static final Whitespace REPLACE = new Whitespace("replace",
062: 1) {
063: public String preparse(String text) {
064: StringBuffer replace = new StringBuffer(text);
065:
066: for (int i = 0; i < replace.length(); i++) {
067: char ch = replace.charAt(i);
068:
069: if (('\t' == ch) || ('\n' == ch) || ('\r' == ch)) {
070: replace.setCharAt(i, ' ');
071: }
072: }
073:
074: return replace.toString();
075: }
076: };
077:
078: /**
079: * All occurrences of tab, line feed and carriage return are replaced with space.
080: * <p>
081: * Simon the spec says: <i> All occurrences of #x9 (tab), #xA (line feed) and #xD (carriage
082: * return) are replaced with #x20 (space) </i>
083: * </p>
084: */
085: public static final Whitespace COLLAPSE = new Whitespace(
086: "collapse", 2) {
087: public String preparse(String text) {
088: text = REPLACE.preparse(text);
089: text = text.trim();
090:
091: StringBuffer collapse = new StringBuffer(text);
092: int i = 0;
093:
094: for (; i < collapse.length(); i++) {
095: if (' ' == collapse.charAt(i)) {
096: for (++i; (i < collapse.length())
097: && (' ' == collapse.charAt(i));) {
098: collapse.deleteCharAt(i);
099: }
100: }
101: }
102:
103: return collapse.toString();
104: }
105: };
106:
107: //
108: // Fake the ENUM thing for the Java 14 crowd
109: //
110: private static List values = new ArrayList();
111:
112: static {
113: values.add(PRESERVE);
114: values.add(REPLACE);
115: values.add(COLLAPSE);
116: }
117:
118: private int ordinal;
119: private String name;
120:
121: private Whitespace(String name, int number) {
122: this .ordinal = number;
123: this .name = name;
124: }
125:
126: /** Handle whitespace */
127: public abstract String preparse(String text);
128:
129: public String name() {
130: return name;
131: }
132:
133: public int ordinal() {
134: return ordinal;
135: }
136:
137: public int hashCode() {
138: return ordinal;
139: }
140:
141: protected Object clone() throws CloneNotSupportedException {
142: throw new CloneNotSupportedException("Ha Ha");
143: }
144:
145: public boolean equals(Object other) {
146: return (other != null) && other instanceof Whitespace
147: && (((Whitespace) other).ordinal == ordinal);
148: }
149:
150: public int compareTo(Object other) {
151: if ((other == null) || !(other instanceof Whitespace)) {
152: return -1;
153: }
154:
155: int ord = ((Whitespace) other).ordinal;
156:
157: if (ordinal == ord) {
158: return 0;
159: }
160:
161: if (ordinal < ord) {
162: return -1;
163: }
164:
165: return 1;
166: }
167:
168: /**
169: * Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
170: * @param whitespace
171: */
172: public static Whitespace valueOf(String whitespace) {
173: for (Iterator i = values.iterator(); i.hasNext();) {
174: Whitespace item = (Whitespace) i.next();
175:
176: if (whitespace.equals(item.name)) {
177: return item;
178: }
179: }
180:
181: return null;
182: }
183:
184: /**
185: * Returns the Class object corresponding to this enum constant's enum type.
186: *
187: * @return Whitespace.class
188: */
189: public Class getDeclaringClass() {
190: return Whitespace.class;
191: }
192:
193: public static List values() {
194: return values;
195: }
196: }
|