KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.Collator JavaDoc;
61 import java.util.Hashtable JavaDoc;
62 import java.util.Locale JavaDoc;
63
64 import org.enhydra.apache.xerces.validators.schema.SchemaSymbols;
65
66 /**
67  * StringValidator validates that XML content is a W3C string type.
68  * @author Ted Leung
69  * @author Kito D. Mann, Virtua Communications Corp.
70  * @author Jeffrey Rodriguez
71  * @author Mark Swinkles - List Validation refactoring
72  * @version $Id: StringDatatypeValidator.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
73  */

74 public class StringDatatypeValidator extends AbstractStringValidator {
75
76     private short fWhiteSpace;
77
78     public StringDatatypeValidator () throws InvalidDatatypeFacetException{
79         this ( null, null, false ); // Native, No Facets defined, Restriction
80

81     }
82     public StringDatatypeValidator ( DatatypeValidator base, Hashtable JavaDoc facets,
83                                      boolean derivedByList ) throws InvalidDatatypeFacetException {
84
85         super (base, facets, derivedByList);
86     }
87
88
89     protected void assignAdditionalFacets(String JavaDoc key, Hashtable JavaDoc facets) throws InvalidDatatypeFacetException{
90         fWhiteSpace = DatatypeValidator.PRESERVE;
91         if ( key.equals(SchemaSymbols.ELT_WHITESPACE) ) {
92             fFacetsDefined |= DatatypeValidator.FACET_WHITESPACE;
93             String JavaDoc ws = (String JavaDoc)facets.get(key);
94             // check 4.3.6.c0 must:
95
// whiteSpace = preserve || whiteSpace = replace || whiteSpace = collapse
96
if ( ws.equals(SchemaSymbols.ATT_PRESERVE) ) {
97                 fWhiteSpace = DatatypeValidator.PRESERVE;
98             }
99             else if ( ws.equals(SchemaSymbols.ATT_REPLACE) ) {
100                 fWhiteSpace = DatatypeValidator.REPLACE;
101             }
102             else if ( ws.equals(SchemaSymbols.ATT_COLLAPSE) ) {
103                 fWhiteSpace = DatatypeValidator.COLLAPSE;
104             }
105             else {
106                 throw new InvalidDatatypeFacetException("whiteSpace value '" + ws +
107                                                         "' must be one of 'preserve', 'replace', 'collapse'.");
108             }
109         }
110         else {
111             throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_STRING_FACET,
112                                                                     DatatypeMessageProvider.MSG_NONE, new Object JavaDoc[] { key}));
113         }
114     }
115
116
117     protected void inheritAdditionalFacets() {
118         // inherit whiteSpace
119
if ( (fFacetsDefined & DatatypeValidator.FACET_WHITESPACE) == 0 &&
120              (((StringDatatypeValidator)fBaseValidator).fFacetsDefined & DatatypeValidator.FACET_WHITESPACE) != 0 ) {
121             fFacetsDefined |= DatatypeValidator.FACET_WHITESPACE;
122             fWhiteSpace = ((StringDatatypeValidator)fBaseValidator).fWhiteSpace;
123         }
124     }
125
126     //
127
// string needs to check constraints on whiteSpace
128
// check is done against fBaseValidator
129
//
130
protected void checkBaseFacetConstraints() throws InvalidDatatypeFacetException {
131         // check 4.3.6.c1 error:
132
// (whiteSpace = preserve || whiteSpace = replace) && base.whiteSpace = collapese or
133
// whiteSpace = preserve && base.whiteSpace = replace
134

135         if ( (fFacetsDefined & DatatypeValidator.FACET_WHITESPACE) != 0 &&
136              (((StringDatatypeValidator)fBaseValidator).fFacetsDefined & DatatypeValidator.FACET_WHITESPACE) != 0 ) {
137                         if ( (((StringDatatypeValidator)fBaseValidator).fFlags & DatatypeValidator.FACET_WHITESPACE) != 0 &&
138                          fWhiteSpace != ((StringDatatypeValidator)fBaseValidator).fWhiteSpace ) {
139                         throw new InvalidDatatypeFacetException( "whiteSpace value = '" + whiteSpaceValue(fWhiteSpace) +
140                                                                  "' must be equal to base.whiteSpace value = '" +
141                                                                  whiteSpaceValue(((StringDatatypeValidator)fBaseValidator).fWhiteSpace) + "' with attribute {fixed} = true" );
142             }
143             if ( (fWhiteSpace == DatatypeValidator.PRESERVE ||
144                   fWhiteSpace == DatatypeValidator.REPLACE) &&
145                  ((StringDatatypeValidator)fBaseValidator).fWhiteSpace == DatatypeValidator.COLLAPSE ){
146                 throw new InvalidDatatypeFacetException( "It is an error if whiteSpace = 'preserve' or 'replace' and base.whiteSpace = 'collapse'.");
147             }
148             if ( fWhiteSpace == DatatypeValidator.PRESERVE &&
149                  ((StringDatatypeValidator)fBaseValidator).fWhiteSpace == DatatypeValidator.REPLACE )
150                 throw new InvalidDatatypeFacetException( "It is an error if whiteSpace = 'preserve' and base.whiteSpace = 'replace'.");
151         }
152     }
153
154
155     /**
156      * return value of whiteSpace facet
157      */

158     public short getWSFacet() {
159         return fWhiteSpace;
160     }
161
162
163     public int compare( String JavaDoc value1, String JavaDoc value2 ) {
164         Locale JavaDoc loc = Locale.getDefault();
165         Collator JavaDoc collator = Collator.getInstance( loc );
166         return collator.compare( value1, value2 );
167     }
168
169     private String JavaDoc whiteSpaceValue (short ws){
170        return (ws != DatatypeValidator.PRESERVE)?
171               (ws == DatatypeValidator.REPLACE)?"replace":"collapse":"preserve";
172     }
173     /**
174    * Returns a copy of this object.
175    */

176     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
177         StringDatatypeValidator newObj = null;
178         try {
179             newObj = new StringDatatypeValidator();
180
181             newObj.fLocale = this.fLocale;
182             newObj.fBaseValidator = this.fBaseValidator;
183             newObj.fLength = this.fLength;
184             newObj.fMaxLength = this.fMaxLength;
185             newObj.fMinLength = this.fMinLength;
186             newObj.fPattern = this.fPattern;
187             newObj.fWhiteSpace = this.fWhiteSpace;
188             newObj.fEnumeration = this.fEnumeration;
189             newObj.fFacetsDefined = this.fFacetsDefined;
190         }
191         catch ( InvalidDatatypeFacetException ex ) {
192             ex.printStackTrace();
193         }
194         return newObj;
195     }
196
197 }
198
199
Popular Tags