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.Enumeration;
062: import org.apache.xerces.validators.schema.SchemaSymbols;
063: import org.apache.xerces.utils.regex.RegularExpression;
064: import org.apache.xerces.validators.schema.SchemaSymbols;
065: import org.apache.xerces.validators.datatype.InvalidDatatypeFacetException;
066:
067: /**
068: *
069: * BooleanValidator validates that content satisfies the W3C XML Datatype for Boolean
070: *
071: * @author Ted Leung
072: * @author Jeffrey Rodriguez
073: * @author Mark Swinkles - List Validation refactoring
074: * @version $Id: BooleanDatatypeValidator.java,v 1.10 2001/04/25 20:41:54 elena Exp $
075: */
076:
077: public class BooleanDatatypeValidator extends AbstractDatatypeValidator {
078:
079: private static final String fValueSpace[] = { "false", "true", "0",
080: "1" };
081:
082: public BooleanDatatypeValidator()
083: throws InvalidDatatypeFacetException {
084: this (null, null, false); // Native, No Facets defined, Restriction
085: }
086:
087: public BooleanDatatypeValidator(DatatypeValidator base,
088: Hashtable facets, boolean derivedByList)
089: throws InvalidDatatypeFacetException {
090: fBaseValidator = base; // Set base type
091:
092: // list types are handled by ListDatatypeValidator, we do nothing here.
093: if (derivedByList)
094: return;
095:
096: // Set Facets if any defined
097: if (facets != null) {
098: for (Enumeration e = facets.keys(); e.hasMoreElements();) {
099: String key = (String) e.nextElement();
100:
101: if (key.equals(SchemaSymbols.ELT_PATTERN)) {
102: fFacetsDefined |= DatatypeValidator.FACET_PATTERN;
103: fPattern = (String) facets.get(key);
104: if (fPattern != null)
105: fRegex = new RegularExpression(fPattern, "X");
106: } else {
107: throw new InvalidDatatypeFacetException(
108: getErrorString(
109: DatatypeMessageProvider.ILLEGAL_BOOLEAN_FACET,
110: DatatypeMessageProvider.MSG_NONE,
111: new Object[] { key }));
112: }
113: }
114: }// End of facet setting
115: }
116:
117: /**
118: * validate that a string matches the boolean datatype
119: * @param content A string containing the content to be validated
120: *
121: * @exception throws InvalidDatatypeException if the content is
122: * is not valid.
123: */
124:
125: public Object validate(String content, Object state)
126: throws InvalidDatatypeValueException {
127: checkContent(content, false);
128: return null;
129: }
130:
131: /**
132: * Compare two boolean data types
133: *
134: * @param content1
135: * @param content2
136: * @return 0 if equal, 1 if not equal
137: */
138: public int compare(String content1, String content2) {
139: if (content1.equals(content2)) {
140: return 0;
141: }
142: Boolean b1 = Boolean.valueOf(content1);
143: Boolean b2 = Boolean.valueOf(content2);
144:
145: return (b1.equals(b2)) ? 0 : 1;
146: }
147:
148: /**
149: * Return a Hashtable that contains facet information
150: *
151: * @return Hashtable
152: */
153:
154: public Hashtable getFacets() {
155: return null;
156: }
157:
158: //Begin private method definitions
159:
160: /**
161: * Returns a copy of this object.
162: */
163: public Object clone() throws CloneNotSupportedException {
164: throw new CloneNotSupportedException(
165: "clone() is not supported in "
166: + this .getClass().getName());
167: }
168:
169: /**
170: * Checks content for validity.
171: *
172: * @param content
173: * @exception InvalidDatatypeValueException
174: */
175: private void checkContent(String content, boolean asBase)
176: throws InvalidDatatypeValueException {
177: // validate against parent type if any
178: if (this .fBaseValidator != null) {
179: // validate content as a base type
180: ((BooleanDatatypeValidator) fBaseValidator).checkContent(
181: content, true);
182: }
183:
184: // we check pattern first
185: if ((fFacetsDefined & DatatypeValidator.FACET_PATTERN) != 0) {
186: if (fRegex == null || fRegex.matches(content) == false)
187: throw new InvalidDatatypeValueException("Value'"
188: + content
189: + "does not match regular expression facet"
190: + fPattern);
191: }
192:
193: // if this is a base validator, we only need to check pattern facet
194: // all other facet were inherited by the derived type
195: if (asBase)
196: return;
197:
198: boolean isContentInDomain = false;
199: for (int i = 0; i < fValueSpace.length; i++) {
200: if (content.equals(fValueSpace[i]))
201: isContentInDomain = true;
202: }
203: if (isContentInDomain == false)
204: throw new InvalidDatatypeValueException(getErrorString(
205: DatatypeMessageProvider.NOT_BOOLEAN,
206: DatatypeMessageProvider.MSG_NONE,
207: new Object[] { content }));
208: }
209: }
|