001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.validators.datatype;
059:
060: import java.util.Hashtable;
061: import java.util.Vector;
062: import java.util.Enumeration;
063: import java.util.Locale;
064: import java.text.Collator;
065: import org.apache.xerces.validators.schema.SchemaSymbols;
066: import org.apache.xerces.utils.regex.RegularExpression;
067:
068: /**
069: * StringValidator validates that XML content is a W3C string type.
070: * @author Ted Leung
071: * @author Kito D. Mann, Virtua Communications Corp.
072: * @author Jeffrey Rodriguez
073: * @author Mark Swinkles - List Validation refactoring
074: * @version $Id: StringDatatypeValidator.java,v 1.27 2001/05/09 18:29:19 elena Exp $
075: */
076: public class StringDatatypeValidator extends AbstractStringValidator {
077:
078: private short fWhiteSpace;
079:
080: public StringDatatypeValidator()
081: throws InvalidDatatypeFacetException {
082: this (null, null, false); // Native, No Facets defined, Restriction
083:
084: }
085:
086: public StringDatatypeValidator(DatatypeValidator base,
087: Hashtable facets, boolean derivedByList)
088: throws InvalidDatatypeFacetException {
089:
090: super (base, facets, derivedByList);
091: }
092:
093: protected void assignAdditionalFacets(String key, Hashtable facets)
094: throws InvalidDatatypeFacetException {
095: fWhiteSpace = DatatypeValidator.PRESERVE;
096: if (key.equals(SchemaSymbols.ELT_WHITESPACE)) {
097: fFacetsDefined |= DatatypeValidator.FACET_WHITESPACE;
098: String ws = (String) facets.get(key);
099: // check 4.3.6.c0 must:
100: // whiteSpace = preserve || whiteSpace = replace || whiteSpace = collapse
101: if (ws.equals(SchemaSymbols.ATT_PRESERVE)) {
102: fWhiteSpace = DatatypeValidator.PRESERVE;
103: } else if (ws.equals(SchemaSymbols.ATT_REPLACE)) {
104: fWhiteSpace = DatatypeValidator.REPLACE;
105: } else if (ws.equals(SchemaSymbols.ATT_COLLAPSE)) {
106: fWhiteSpace = DatatypeValidator.COLLAPSE;
107: } else {
108: throw new InvalidDatatypeFacetException(
109: "whiteSpace value '"
110: + ws
111: + "' must be one of 'preserve', 'replace', 'collapse'.");
112: }
113: } else {
114: throw new InvalidDatatypeFacetException(getErrorString(
115: DatatypeMessageProvider.ILLEGAL_STRING_FACET,
116: DatatypeMessageProvider.MSG_NONE,
117: new Object[] { key }));
118: }
119: }
120:
121: protected void inheritAdditionalFacets() {
122: // inherit whiteSpace
123: if ((fFacetsDefined & DatatypeValidator.FACET_WHITESPACE) == 0
124: && (((StringDatatypeValidator) fBaseValidator).fFacetsDefined & DatatypeValidator.FACET_WHITESPACE) != 0) {
125: fFacetsDefined |= DatatypeValidator.FACET_WHITESPACE;
126: fWhiteSpace = ((StringDatatypeValidator) fBaseValidator).fWhiteSpace;
127: }
128: }
129:
130: //
131: // string needs to check constraints on whiteSpace
132: // check is done against fBaseValidator
133: //
134: protected void checkBaseFacetConstraints()
135: throws InvalidDatatypeFacetException {
136: // check 4.3.6.c1 error:
137: // (whiteSpace = preserve || whiteSpace = replace) && base.whiteSpace = collapese or
138: // whiteSpace = preserve && base.whiteSpace = replace
139:
140: if ((fFacetsDefined & DatatypeValidator.FACET_WHITESPACE) != 0
141: && (((StringDatatypeValidator) fBaseValidator).fFacetsDefined & DatatypeValidator.FACET_WHITESPACE) != 0) {
142: if ((((StringDatatypeValidator) fBaseValidator).fFlags & DatatypeValidator.FACET_WHITESPACE) != 0
143: && fWhiteSpace != ((StringDatatypeValidator) fBaseValidator).fWhiteSpace) {
144: throw new InvalidDatatypeFacetException(
145: "whiteSpace value = '"
146: + whiteSpaceValue(fWhiteSpace)
147: + "' must be equal to base.whiteSpace value = '"
148: + whiteSpaceValue(((StringDatatypeValidator) fBaseValidator).fWhiteSpace)
149: + "' with attribute {fixed} = true");
150: }
151: if ((fWhiteSpace == DatatypeValidator.PRESERVE || fWhiteSpace == DatatypeValidator.REPLACE)
152: && ((StringDatatypeValidator) fBaseValidator).fWhiteSpace == DatatypeValidator.COLLAPSE) {
153: throw new InvalidDatatypeFacetException(
154: "It is an error if whiteSpace = 'preserve' or 'replace' and base.whiteSpace = 'collapse'.");
155: }
156: if (fWhiteSpace == DatatypeValidator.PRESERVE
157: && ((StringDatatypeValidator) fBaseValidator).fWhiteSpace == DatatypeValidator.REPLACE)
158: throw new InvalidDatatypeFacetException(
159: "It is an error if whiteSpace = 'preserve' and base.whiteSpace = 'replace'.");
160: }
161: }
162:
163: /**
164: * return value of whiteSpace facet
165: */
166: public short getWSFacet() {
167: return fWhiteSpace;
168: }
169:
170: public int compare(String value1, String value2) {
171: Locale loc = Locale.getDefault();
172: Collator collator = Collator.getInstance(loc);
173: return collator.compare(value1, value2);
174: }
175:
176: private String whiteSpaceValue(short ws) {
177: return (ws != DatatypeValidator.PRESERVE) ? (ws == DatatypeValidator.REPLACE) ? "replace"
178: : "collapse"
179: : "preserve";
180: }
181:
182: /**
183: * Returns a copy of this object.
184: */
185: public Object clone() throws CloneNotSupportedException {
186: StringDatatypeValidator newObj = null;
187: try {
188: newObj = new StringDatatypeValidator();
189:
190: newObj.fLocale = this .fLocale;
191: newObj.fBaseValidator = this .fBaseValidator;
192: newObj.fLength = this .fLength;
193: newObj.fMaxLength = this .fMaxLength;
194: newObj.fMinLength = this .fMinLength;
195: newObj.fPattern = this .fPattern;
196: newObj.fWhiteSpace = this .fWhiteSpace;
197: newObj.fEnumeration = this .fEnumeration;
198: newObj.fFacetsDefined = this .fFacetsDefined;
199: } catch (InvalidDatatypeFacetException ex) {
200: ex.printStackTrace();
201: }
202: return newObj;
203: }
204:
205: }
|