KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > Declaration


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema;
24
25 import org.xquark.schema.validation.ValidationContextProvider;
26 import org.xquark.schema.validation.ValidationInfo;
27
28 /**
29  * Abstract class for AttributeDeclaration and ElementDeclaration.
30  *
31  * <p>This is the implementation of the common propertoes of Attribute Declaration
32  * and Element Declaration. The following properties are defined :</p>
33  * <p> 1. type defintion : a simple type or a complex type defintion.
34  * if a declaration is an instance of Attribute Declaration,
35  * the type of this declaration must be a simple type</p>
36  * <p> 2. scope : either null (means gloabal) or a complex type defintion. </p>
37  * <p> 3. value constraint : either a defaultValue or a fixedValue. </p>
38  *
39  * <p>See XML Schema Part 1: Structures 3.2, 3.3
40  * W3C Recommendation 2 May 2001</p>
41  *
42  * @see org.xquark.xml.schema.SchemaComponent
43  * @see org.xquark.xml.schema.AttributeDeclaration
44  * @see org.xquark.xml.schema.ElementDeclaration
45  */

46 public abstract class Declaration extends SchemaComponent
47   implements Cloneable JavaDoc
48 {
49 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
50 private static final String JavaDoc RCSName = "$Name: $";
51   protected SchemaScope scope;
52   private Type type = null;
53   protected String JavaDoc defaultValue = null;
54   protected String JavaDoc fixedValue = null;
55   private boolean qualified = false;
56   
57   /**
58    * Create a new Declaration.
59    *
60    * @param schema The schema which contains this declaration.
61    * @param name The declaration name, a NCName.
62    * @param scope The scope, null if the declaration is in a group
63    * which contains this declaration.
64    */

65   protected Declaration(Schema schema, String JavaDoc name, SchemaScope scope) {
66     super(schema, name);
67     this.scope = scope;
68   }
69
70   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
71     Object JavaDoc result = null;
72     result = super.clone();
73     return result;
74   }
75   
76   public void accept(SchemaVisitor visitor) throws SchemaException {
77     visitor.visit(this);
78   }
79
80   /**
81    * Return the declaration's target namespace.
82    *
83    * @return The target namespace if the declaration is qualified,
84    * or null if the declaration is unqualified.
85    */

86   public String JavaDoc getNamespace() {
87     if (qualified) return super.getNamespace();
88     else return null;
89   }
90
91   /**
92    * Return the declaration's data type.
93    *
94    * @return The data type, if the declaration is an instance of Attribute Declaration,
95    * a simple type must be returned.
96    */

97   public Type getType() {
98     return type;
99   }
100
101   /**
102    * Return the declaration's scope. Can be null is the declaration is part of a group definition
103    *
104    * @return The scope, if the declaration is a top-level declaration, return the schema,
105    * otherwise return a complex type which contains the declaration.
106    */

107   public SchemaScope getScope() {
108     return scope;
109   }
110
111   public String JavaDoc getValueConstraint() {
112     if (defaultValue != null) return defaultValue;
113     else if (fixedValue != null) return fixedValue;
114     else return null;
115   }
116   
117   public void setValueConstraint(String JavaDoc value) {
118     if (defaultValue != null) defaultValue = value;
119     else if (fixedValue != null) fixedValue = value;
120   }
121   
122   /**
123    * Return the declaration's default value, if any.
124    *
125    * @return The default value, or null if there is none.
126    */

127   public String JavaDoc getDefaultValue() {
128     return defaultValue;
129   }
130   
131   public void setDefaultValue(String JavaDoc defaultValue) {
132     this.defaultValue = defaultValue;
133   }
134   
135   /**
136    * Return the declaration's fixed value, if any.
137    *
138    * @return The fixed value, or null if there is none.
139    */

140   public String JavaDoc getFixedValue() {
141     return fixedValue;
142   }
143   
144   public void setFixedValue(String JavaDoc fixedValue) {
145     this.fixedValue = fixedValue;
146   }
147
148   public void setType(Type type) {
149     this.type = type;
150   }
151
152   public void setScope(SchemaScope scope) {
153     this.scope = scope;
154   }
155
156   public void setQualified(boolean qualified) {
157     this.qualified = qualified;
158   }
159
160   /*
161 une nouvelle méthode est ajoutée dans la classe Declaration.
162
163 pourquoi cette méthode est ajoutée dans Declaration, pas dans SimpleType ?
164 La fixed valeur et la default valeur sont definiée dans Declaration.
165
166   public int validate(String value, ValidationContextProvider context) throws SchemaException
167
168 valeurs d'entrée:
169 value : la valeur que l'on veut valider
170 context : conext provider, c'est un fournisseur de prefix et document base.
171           Si le type de cette declaration n'est pas QName or AnyURI,metter contex == null
172
173 valeur de retour:
174 0: value est valid
175 -1: le type de cette Declaration est null ou ComplexType, c'est à dir que cette méthode ne amarche que avec SimpleType
176 SchemaException: value n'est pas valid ou value n'égale pas la fixed valeur de cette Declaration.
177 */

178   public int validate(String JavaDoc value, ValidationContextProvider context) throws SchemaException {
179         if ( type == null ) return -1;
180         SimpleType valueType = type.getValueType();
181         if (valueType == null ) return -1;
182         ValidationInfo info = valueType.validate(value, true, context);
183         SimpleType realType = info.getValidationType();
184         Object JavaDoc actualValue = info.getActualValue();
185         if ( getFixedValue() != null) {
186           if (!getFixedValue().equals(realType.toXMLString(actualValue))) {
187               if ( this instanceof AttributeDeclaration )
188                   throw new SchemaException("cvc-attribute.4", this);
189               else
190                   throw new SchemaException("cvc-elt.5.2.2.2.2", this);
191           }
192         }
193         
194         return 0;
195   }
196       
197 }
198
Popular Tags