KickJava   Java API By Example, From Geeks To Geeks.

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


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

57
58 package org.enhydra.apache.xerces.validators.datatype;
59
60 import java.util.Enumeration JavaDoc;
61 import java.util.Hashtable JavaDoc;
62 import java.util.Vector JavaDoc;
63
64 import org.enhydra.apache.xerces.utils.regex.RegularExpression;
65 import org.enhydra.apache.xerces.validators.schema.SchemaSymbols;
66
67
68
69 /**
70  * @author Jeffrey Rodriguez
71  * @author Elena Litani
72  * UnionValidator validates that XML content is a W3C string type.
73  * Implements the September 22 XML Schema datatype Union Datatype type
74  */

75 public class UnionDatatypeValidator extends AbstractDatatypeValidator {
76     
77     private Vector JavaDoc fBaseValidators = null; // union collection of validators
78
private int fValidatorsSize = 0;
79     private Vector JavaDoc fEnumeration = null;
80     private StringBuffer JavaDoc errorMsg = null;
81
82
83     public UnionDatatypeValidator () throws InvalidDatatypeFacetException{
84         this( null, null, false ); // Native, No Facets defined, Restriction
85

86     }
87
88
89     public UnionDatatypeValidator ( DatatypeValidator base, Hashtable JavaDoc facets, boolean derivedBy ) throws InvalidDatatypeFacetException {
90         fBaseValidator = base;
91         //facets allowed are: pattern & enumeration
92
if ( facets != null ) {
93             for ( Enumeration JavaDoc e = facets.keys(); e.hasMoreElements(); ) {
94                 String JavaDoc key = (String JavaDoc) e.nextElement();
95                 if ( key.equals(SchemaSymbols.ELT_ENUMERATION) ) {
96                     fFacetsDefined |= DatatypeValidator.FACET_ENUMERATION;
97                     fEnumeration = (Vector JavaDoc)facets.get(key);
98                 }
99                 else if ( key.equals(SchemaSymbols.ELT_PATTERN) ) {
100                     fFacetsDefined |= DatatypeValidator.FACET_PATTERN;
101                     fPattern = (String JavaDoc)facets.get(key);
102                     fRegex = new RegularExpression(fPattern, "X");
103
104
105                 }
106                 else {
107                     throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_UNION_FACET,
108                                                                         DatatypeMessageProvider.MSG_NONE, new Object JavaDoc[] { key }));
109                 }
110             } //end for
111

112             // check 4.3.5.c0 must: enumeration values from the value space of base
113
if ( base != null &&
114                 (fFacetsDefined & DatatypeValidator.FACET_ENUMERATION) != 0 &&
115                 (fEnumeration != null) ) {
116                 int i = 0;
117                 try {
118                     for (; i < fEnumeration.size(); i++) {
119                         base.validate ((String JavaDoc)fEnumeration.elementAt(i), null);
120                     }
121                 } catch ( Exception JavaDoc idve ){
122                     throw new InvalidDatatypeFacetException( "Value of enumeration = '" + fEnumeration.elementAt(i) +
123                                                              "' must be from the value space of base.");
124                 }
125             }
126         }// End of Facets Setting
127

128     }
129
130     public UnionDatatypeValidator ( Vector JavaDoc base) {
131
132         if ( base !=null ) {
133             fValidatorsSize = base.size();
134             fBaseValidators = new Vector JavaDoc(fValidatorsSize);
135             fBaseValidators = base;
136
137         }
138
139     }
140
141
142
143     /**
144      * validate that a string is a W3C string type
145      *
146      * @param content A string containing the content to be validated
147      * @param list
148      * @exception throws InvalidDatatypeException if the content is
149      * not a W3C string type
150      * @exception InvalidDatatypeValueException
151      */

152     public Object JavaDoc validate(String JavaDoc content, Object JavaDoc state) throws InvalidDatatypeValueException
153     {
154         if ( content == null && state != null ) {
155             this.fBaseValidator.validate( content, state );//Passthrough setup information
156
//for state validators
157
}
158         else {
159             checkContentEnum( content, state, false , null );
160         }
161         return(null);
162     }
163
164
165     /**
166      *
167      * @return A Hashtable containing the facets
168      * for this datatype.
169      */

170     public Hashtable JavaDoc getFacets(){
171         return(null);
172     }
173
174     public int compare( String JavaDoc value1, String JavaDoc value2 ){
175         if (fBaseValidator != null) {
176             return this.fBaseValidator.compare(value1, value2);
177         }
178         //union datatype
179
int index=-1;
180         DatatypeValidator currentDV;
181         while ( ++index < fValidatorsSize ) {
182             currentDV = (DatatypeValidator)this.fBaseValidators.elementAt(index);
183             if (currentDV.compare(value1, value2) == 0) {
184                 return 0;
185             }
186         }
187         //REVISIT: what does it mean for UNION1 to be <less than> or <greater than> UNION2 ?
188
return -1;
189     }
190
191     /**
192    * Returns a copy of this object.
193    */

194     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
195         UnionDatatypeValidator newObj = null;
196         try {
197             newObj = new UnionDatatypeValidator();
198             newObj.fLocale = this.fLocale;
199             newObj.fBaseValidator = this.fBaseValidator;
200             newObj.fBaseValidators = (Vector JavaDoc)this.fBaseValidators.clone();
201             newObj.fPattern = this.fPattern;
202             newObj.fEnumeration = this.fEnumeration;
203             newObj.fFacetsDefined = this.fFacetsDefined;
204         }
205         catch ( InvalidDatatypeFacetException ex ) {
206             ex.printStackTrace();
207         }
208         return(newObj);
209
210     }
211
212     // returns the fBaseValidators Vector; added so that
213
// 2.2.4 of SchemaStructures spec section 3.14.6 can be implemented.
214
public Vector JavaDoc getBaseValidators() {
215         return fBaseValidators;
216     }
217
218     /**
219     * check if enum is subset of fEnumeration
220     * enum 1: <enumeration value="1 2"/>
221     * enum 2: <enumeration value="1.0 2"/>
222     *
223     * @param enumeration facet
224     *
225     * @returns true if enumeration is subset of fEnumeration, false otherwise
226     */

227     private boolean verifyEnum (Vector JavaDoc enumer){
228         /* REVISIT: won't work for list datatypes in some cases: */
229         if ((fFacetsDefined & DatatypeValidator.FACET_ENUMERATION ) != 0) {
230             for (Enumeration JavaDoc e = enumer.elements() ; e.hasMoreElements() ;) {
231                 if (fEnumeration.contains(e.nextElement()) == false) {
232                     return false;
233                 }
234             }
235         }
236         return true;
237     }
238
239     /**
240      * validate if the content is valid against base datatype and facets (if any)
241      *
242      * @param content A string containing the content to be validated
243      * @param pattern: true if pattern facet was applied, false otherwise
244      * @param enumeration enumeration facet
245      * @exception throws InvalidDatatypeException if the content is not valid
246      */

247     private void checkContentEnum( String JavaDoc content, Object JavaDoc state, boolean pattern, Vector JavaDoc enumeration ) throws InvalidDatatypeValueException
248     {
249         // for UnionDatatype we have to wait till the union baseValidators are known, thus
250
// if the content is valid "against" ListDatatype, but pattern was applied - report an error. To do so we pass @param pattern;
251
// pass @param enumeration so that when base Datatype is known, we would validate enumeration/content
252
// against value space as well
253
int index = -1; //number of validators
254
boolean valid=false;
255         DatatypeValidator currentDV = null;
256         if (fBaseValidator !=null) { //restriction of union datatype
257
if ( (fFacetsDefined & DatatypeValidator.FACET_PATTERN ) != 0 ) {
258                 if ( fRegex == null || fRegex.matches( content) == false )
259                     throw new InvalidDatatypeValueException("Value '"+content+
260                     "' does not match regular expression facet '" + fPattern + "'." );
261                 pattern = true;
262             }
263
264             if (enumeration!=null) {
265                 if (!verifyEnum(enumeration)) {
266                     throw new InvalidDatatypeValueException("Enumeration '" +enumeration+"' for value '" +content+
267                     "' is based on enumeration '"+fEnumeration+"'");
268                 }
269             }
270             else {
271                 enumeration = (fEnumeration!=null) ? fEnumeration : null;
272             }
273             ((UnionDatatypeValidator)this.fBaseValidator).checkContentEnum( content, state, pattern, enumeration );
274             return;
275         }
276         // native union type
277
while ( ++index < fValidatorsSize) {
278             // check content against each base validator in Union
279
// report an error only in case content is not valid against all base datatypes.
280
currentDV = (DatatypeValidator)this.fBaseValidators.elementAt(index);
281             if ( valid ) break;
282             try {
283                 if ( currentDV instanceof ListDatatypeValidator ) {
284                     if ( pattern ) {
285                         throw new InvalidDatatypeValueException("Facet \"Pattern\" can not be applied to a list datatype" );
286                     }
287                     ((ListDatatypeValidator)currentDV).checkContentEnum( content, state, enumeration );
288                 }
289                 else if ( currentDV instanceof UnionDatatypeValidator ) {
290                     ((UnionDatatypeValidator)currentDV).checkContentEnum( content, state, pattern, enumeration );
291                 }
292                 else {
293                     if (enumeration!=null) {
294                         // check enumeration against value space of double, decimal and float
295
if (currentDV instanceof AbstractNumericValidator) {
296                             ((AbstractNumericValidator)currentDV).checkContentEnum(content, state, enumeration);
297                         }
298                         else {
299                             if (enumeration.contains( content ) == false) {
300                                 throw new InvalidDatatypeValueException("Value '"+content+ "' must be one of "+ enumeration);
301                             }
302                             ((DatatypeValidator)currentDV).validate( content, state );
303                         }
304                     }
305                     else {
306                         ((DatatypeValidator)currentDV).validate( content, state );
307                     }
308                 }
309                 valid=true;
310
311             }
312             catch ( InvalidDatatypeValueException e ) {
313             }
314         }
315         if ( !valid ) {
316             throw new InvalidDatatypeValueException( "Content '"+content+"' does not match any union types" );
317         }
318     }
319
320 }
321
322
323
Popular Tags