KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
61
62 import org.enhydra.apache.xerces.readers.DefaultEntityHandler;
63 import org.enhydra.apache.xerces.utils.StringPool;
64 import org.enhydra.apache.xerces.utils.XMLMessages;
65
66
67 /**
68  * ENTITYDatatypeValidator implements the
69  * DatattypeValidator interface.
70  * This validator embodies the ENTITY attribute type
71  * from XML1.0 recommendation.
72  * The Value space of ENTITY is the set of all strings
73  * that match the NCName production and have been
74  * declared as an unparsed entity in a document
75  * type definition.
76  * The Lexical space of Entity is the set of all
77  * strings that match the NCName production.
78  * The value space of ENTITY is scoped to a specific
79  * instance document.
80  *
81  * Some caveats:
82  *
83  * Because of the Xerces Architecture, where all
84  * symbols are stored in a StringPool and Strings
85  * are referenced by int then this datatype needs
86  * to know about StringPool.
87  * The first time that this datatype is invoked
88  * we pass a message containing 2 references needed
89  * by this validator:
90  * - a reference to the DefaultEntityHandler used
91  * by the XMLValidator.
92  * - a reference to the StringPool.
93  *
94  *
95  * This validator extends also the XML1.0 validation
96  * provided in DTD by providing "only on Schemas"
97  * facet validation.
98  * This validator also embodies the Derived datatype
99  * ENTITIES which is an ENTITY derived by list.
100  *
101  * These validators can be supplied by the application writer and may be useful as
102  * standalone code as well as plugins to the validator architecture.
103  *
104  * @author Jeffrey Rodriguez-
105  * @author Mark Swinkles - List Validation refactoring
106  * @version $Id: ENTITYDatatypeValidator.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
107  * @see org.enhydra.apache.xerces.validators.datatype.DatatypeValidator
108  * @see org.enhydra.apache.xerces.validators.datatype.DatatypeValidatorFactoryImpl
109  * @see org.enhydra.apache.xerces.validators.datatype.DatatypeValidatorFactory
110  * @see org.enhydra.apache.xerces.validators.common.XMLValidator
111  */

112 public class ENTITYDatatypeValidator extends StringDatatypeValidator {
113
114     public static final int ENTITY_VALIDATE = 0;
115
116     public ENTITYDatatypeValidator () throws InvalidDatatypeFacetException {
117         this( null, null, false ); // Native, No Facets defined, Restriction
118
}
119
120     public ENTITYDatatypeValidator ( DatatypeValidator base, Hashtable JavaDoc facets,
121                                      boolean derivedByList ) throws InvalidDatatypeFacetException {
122
123         // all facets are handled in StringDatatypeValidator
124
super (base, facets, derivedByList);
125
126         // list types are handled by ListDatatypeValidator, we do nothing here.
127
if ( derivedByList )
128             return;
129
130         setTokenType(SPECIAL_TOKEN_ENTITY);
131     }
132
133     /**
134      * return value of whiteSpace facet
135      */

136     public short getWSFacet(){
137         return COLLAPSE;
138     }
139
140     /**
141      * Checks that "content" string is valid
142      * datatype.
143      * If invalid a Datatype validation exception is thrown.
144      *
145      * @param content A string containing the content to be validated
146      * @param derivedBylist
147      * Flag which is true when type
148      * is derived by list otherwise it
149      * it is derived by extension.
150      *
151      * @exception throws InvalidDatatypeException if the content is
152      * invalid according to the rules for the validators
153      * @exception InvalidDatatypeValueException
154      * @see org.enhydra.apache.xerces.validators.datatype.InvalidDatatypeValueException
155      */

156     public Object JavaDoc validate(String JavaDoc content, Object JavaDoc state ) throws InvalidDatatypeValueException{
157         // use StringDatatypeValidator to validate content against facets
158
super.validate(content, state);
159
160         StateMessageDatatype message = (StateMessageDatatype) state;
161         if (message != null && message.getDatatypeState() == ENTITY_VALIDATE) {
162             Object JavaDoc[] params = (Object JavaDoc[])message.getDatatypeObject();
163             DefaultEntityHandler entityHandler = (DefaultEntityHandler)params[0];
164             StringPool stringPool = (StringPool)params[1];
165
166             int attValueHandle = stringPool.addSymbol( content );
167             if (!entityHandler.isUnparsedEntity( attValueHandle ) ) {
168                 InvalidDatatypeValueException error =
169                 new InvalidDatatypeValueException( "ENTITY '"+ content +"' is not valid" );
170                 error.setMinorCode(XMLMessages.MSG_ENTITY_INVALID);
171                 error.setMajorCode(XMLMessages.VC_ENTITY_DECLARED);
172                 throw error;
173             }
174         }
175
176         return null;
177     }
178
179     /**
180      * REVISIT
181      * Compares two Datatype for order
182      *
183      * @return
184      */

185     public int compare( String JavaDoc content1, String JavaDoc content2){
186         // TO BE DONE!!!
187
return content1.equals(content2)?0:-1;
188     }
189
190     // Private methods start here
191

192     /**
193        * Returns a copy of this object.
194        */

195     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
196         throw new CloneNotSupportedException JavaDoc("clone() is not supported in "+this.getClass().getName());
197     }
198 }
199
Popular Tags