KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > validators > datatype > AbstractNumericValidator


1
2 /*
3  * The Apache Software License, Version 1.1
4  *
5  *
6  * Copyright (c) 1999, 2000, 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Xerces" and "Apache Software Foundation" must
29  * not be used to endorse or promote products derived from this
30  * software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * nor may "Apache" appear in their name, without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation and was
53  * originally based on software copyright (c) 2001, International
54  * Business Machines, Inc., http://www.apache.org. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  */

58
59 package org.enhydra.apache.xerces.validators.datatype;
60
61
62 import java.util.Hashtable JavaDoc;
63 import java.util.Vector JavaDoc;
64
65 /**
66  * AbstractNumericValidator is a base class of decimal, double, and float
67  * schema datatypes
68  *
69  * @author Elena Litani
70  * @version $Id: AbstractNumericValidator.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
71  */

72
73 public abstract class AbstractNumericValidator extends AbstractNumericFacetValidator {
74
75     public AbstractNumericValidator () throws InvalidDatatypeFacetException {
76         super( null, null, false ); // Native, No Facets defined, Restriction
77
}
78
79     public AbstractNumericValidator ( DatatypeValidator base,
80                                       Hashtable JavaDoc facets,
81                                       boolean derivedByList) throws InvalidDatatypeFacetException {
82         super (base, facets, derivedByList);
83     }
84
85
86     /**
87      * Validate string against lexical space of datatype
88      *
89      * @param content A string containing the content to be validated
90      * @param state
91      * @return
92      * @exception throws InvalidDatatypeException if the content is
93      * is not a W3C decimal type
94      * @exception InvalidDatatypeValueException
95      */

96     public Object JavaDoc validate(String JavaDoc content, Object JavaDoc state) throws InvalidDatatypeValueException {
97         //REVISIT: should we pass state?
98
checkContent(content, state, null, false);
99         return null;
100     }
101
102     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
103         throw new CloneNotSupportedException JavaDoc("clone() is not supported in "+this.getClass().getName());
104     }
105
106     /**
107     * validate if the content is valid against base datatype and facets (if any)
108     * this function might be called directly from UnionDatatype or ListDatatype
109     *
110     * @param content A string containing the content to be validated
111     * @param enumeration A vector with enumeration strings
112     * @exception throws InvalidDatatypeException if the content is
113     * is not a W3C decimal type;
114     * @exception throws InvalidDatatypeFacetException if enumeration is not BigDecimal
115     */

116     protected void checkContentEnum(String JavaDoc content, Object JavaDoc state, Vector JavaDoc enumeration)
117     throws InvalidDatatypeValueException {
118         checkContent(content, state, enumeration, false);
119     }
120
121
122     //
123
// content - string value to be evaluated
124
//
125
abstract protected void checkContent( String JavaDoc content, Object JavaDoc State, Vector JavaDoc enumer, boolean asBase)
126                               throws InvalidDatatypeValueException;
127
128
129     /*
130      * check that a facet is in range, assumes that facets are compatible -- compatibility ensured by setFacets
131      */

132     protected void boundsCheck(Object JavaDoc d) throws InvalidDatatypeValueException {
133
134         boolean minOk = true;
135         boolean maxOk = true;
136         String JavaDoc upperBound="";
137
138         String JavaDoc lowerBound="";
139         String JavaDoc lowerBoundIndicator = "";
140         String JavaDoc upperBoundIndicator = "";
141         int compare;
142         if ( (fFacetsDefined & DatatypeValidator.FACET_MAXINCLUSIVE) != 0 ) {
143             compare = compareValues(d, fMaxInclusive);
144             maxOk=(compare==1)?false:true;
145             upperBound = getMaxInclusive(false);
146             if ( upperBound != null ) {
147                 upperBoundIndicator = "<=";
148             }
149             else {
150                 upperBound="";
151             }
152         }
153         if ( (fFacetsDefined & DatatypeValidator.FACET_MAXEXCLUSIVE) != 0 ) {
154             compare = compareValues(d, fMaxExclusive );
155             maxOk = (compare==-1)?true:false;
156             upperBound = getMaxExclusive (false);
157             if ( upperBound != null ) {
158                 upperBoundIndicator = "<";
159             }
160             else {
161                 upperBound = "";
162             }
163         }
164
165         if ( (fFacetsDefined & DatatypeValidator.FACET_MININCLUSIVE) != 0 ) {
166             compare = compareValues(d, fMinInclusive);
167             minOk = (compare==-1)?false:true;
168             lowerBound = getMinInclusive (false);
169             if ( lowerBound != null ) {
170                 lowerBoundIndicator = "<=";
171             }
172             else {
173                 lowerBound = "";
174             }
175         }
176         if ( (fFacetsDefined & DatatypeValidator.FACET_MINEXCLUSIVE) != 0 ) {
177             compare = compareValues(d, fMinExclusive);
178             minOk = (compare==1)?true:false;
179             lowerBound = getMinExclusive (false );
180             if ( lowerBound != null ) {
181                 lowerBoundIndicator = "<";
182             }
183             else {
184                 lowerBound = "";
185             }
186         }
187
188         if ( !(minOk && maxOk) )
189             throw new InvalidDatatypeValueException (
190                                                     getErrorString(DatatypeMessageProvider.OUT_OF_BOUNDS,
191                                                                    DatatypeMessageProvider.MSG_NONE,
192                                                                    new Object JavaDoc [] { d.toString() , lowerBound ,
193                                                                        upperBound, lowerBoundIndicator, upperBoundIndicator}));
194
195
196     }
197
198
199
200 }
201
Popular Tags