KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
26
27 /**
28  * class for Element Declaration.
29  *
30  * <p>This is the implementation of the Element Declaration.
31  * The following properties are defined:</p>
32  * <p> 1. name : a NCName, defined in SchemaComponent.</p>
33  * <p> 2. target namespace : null or a namespace name, defined in SchemaComponent.</p>
34  * <p> 3. type defintion : a simple or complex type defintion, defined in Declaration.</p>
35  * <p> 4. scope : either a Schema (means gloabal) or a complex type definition, defined in Declaration. </p>
36  * <p> 5. value constraint : optional. Either a defaultValue or a fixedValue, defined in Declaration. </p>
37  * <p> 6. nillable : a boolean, true if the element declaration is nillable. </p>
38  * <p> 7. identity-constraint definitions : optional. A set of constraint definitions. </p>
39  * <p> 8. substitution group affiliation : optional. A top level element declaration. </p>
40  *
41  * <p>See XML Schema Part 1: Structures 3.3
42  * W3C Recommendation 2 May 2001</p>
43  *
44  * @see org.xquark.xml.schema.SchemaComponent
45  * @see org.xquark.xml.schema.Declaration
46  * @see org.xquark.xml.schema.IdentityConstraint
47  */

48 public class ElementDeclaration extends Declaration {
49 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
50 private static final String JavaDoc RCSName = "$Name: $";
51   
52   private boolean nillable = false;
53   private boolean abs = false;
54   private int block = 0;
55   private int fin = 0;
56   private SchemaComponent substitutionGroup = null;
57   private ArrayList substitutionElementList = null;
58   private HashMap identityConstraints = null;
59   
60   /**
61    * Create a new ElementDeclaration.
62    *
63    * @param schema The schema which contains this element declaration.
64    * @param name The element declaration name, a NCName.
65    * @param scope The scope, null if the element declaration belongs to a group
66    * which contains this element declaration.
67    */

68   public ElementDeclaration(Schema schema, String JavaDoc name, SchemaScope scope) {
69     super(schema, name, scope);
70   }
71   
72   public void accept(SchemaVisitor visitor) throws SchemaException {
73     visitor.visit(this);
74   }
75
76   public boolean isAbstract() {
77     return abs;
78   }
79   
80   /**
81    * Return true if the element declaration is nillable, false, if not
82    *
83    * @return The nillable property
84    */

85   public boolean isNillable() {
86     return nillable;
87   }
88
89   public int getBlock() {
90     return block;
91   }
92
93   public int getFinal() {
94     return fin;
95   }
96
97   /**
98    * Return substitution group affiliation, if any
99    *
100    * @return The substitution group affiliation property - a top level element
101    * declaration, or null if there is none
102    */

103   public SchemaComponent getSubstitutionGroup() {
104     return substitutionGroup;
105   }
106     
107   public List getSubstitutionElementList() {
108     //@@Scope
109
// if (scope.isGlobalScope()) return substitutionElementList;
110
// else {
111
// ElementDeclaration anElement = getSchema().getElementDeclaration(getName());
112
// if (anElement == null) return null;
113
// return anElement.getSubstitutionElementList();
114
// }
115
if (scope.isGlobalScope()) return substitutionElementList;
116     else return null;
117   }
118     
119   public void setAbstract(boolean abs) {
120     this.abs = abs;
121   }
122
123   public void setNillable(boolean nillable) {
124     this.nillable = nillable;
125   }
126
127   public void setBlock(int block) {
128     this.block = block;
129   }
130
131   public void setFinal(int fin) {
132     this.fin = fin;
133   }
134
135   public void setSubstitutionGroup(SchemaComponent substitutionGroup) {
136     this.substitutionGroup = substitutionGroup;
137   }
138
139   void addSubstitutionMember(ElementDeclaration anElement) {
140     if (substitutionElementList == null)
141       substitutionElementList = new ArrayList();
142     substitutionElementList.add(anElement);
143   }
144
145   public ElementDeclaration getSubstitutionElement(String JavaDoc namespace, String JavaDoc localName)
146   {
147     List list = getSubstitutionElementList();
148     if (list == null) return null;
149     Iterator it = list.iterator();
150     while (it.hasNext()) {
151       ElementDeclaration anElement = (ElementDeclaration)it.next();
152       // find localName in this element
153
if (anElement.hasName(namespace, localName)) return anElement;
154     }
155         
156     return null;
157   }
158   
159   public void fillAllConcreteSubstitutionElements(List result) {
160     List list = getSubstitutionElementList();
161     if ( list == null ) return;
162     Iterator it = list.iterator();
163     while (it.hasNext()) {
164       ElementDeclaration anElement = (ElementDeclaration)it.next();
165       if (!anElement.isAbstract()) result.add(anElement);
166     }
167   }
168   
169   // identity-constraint
170
public void register(IdentityConstraint identityConstraint) {
171     if ( identityConstraints == null ) identityConstraints = new HashMap();
172     identityConstraints.put(identityConstraint.getName(), identityConstraint);
173   }
174   
175   /**
176    * Look up all identity-constraint definitions.
177    *
178    * @return A collection view of all identity-constraint definitions
179    */

180   public Collection getIdentityConstraints() {
181     if ( identityConstraints == null ) return null;
182     return identityConstraints.values();
183   }
184   
185   public IdentityConstraint getIdentityConstraint(String JavaDoc name) {
186     if ( identityConstraints == null ) return null;
187     if ( name.indexOf(':') != -1 ) {
188       name = name.substring(name.indexOf(':') + 1);
189     }
190     IdentityConstraint result = (IdentityConstraint)identityConstraints.get(name);
191     return result;
192   }
193   
194   boolean equals(ElementDeclaration decl) {
195     if ( this == decl ) return true;
196     if ( decl == null ) return false;
197     if ( this.getName().equals(decl.getName()) == false ) return false;
198     if ( this.getNamespace() == null && decl.getNamespace() != null ) return false;
199     if ( this.getNamespace() != null && !(this.getNamespace().equals(decl.getNamespace())) )
200       return false;
201     if ( this.abs != decl.abs ) return false;
202     if ( this.nillable != decl.nillable ) return false;
203     if ( this.fin != decl.fin ) return false;
204     if ( this.block != decl.block ) return false;
205     if ( this.defaultValue == null && decl.defaultValue != null ) return false;
206     if ( this.defaultValue != null && !(this.defaultValue.equals(decl.defaultValue)) ) return false;
207     if ( this.fixedValue == null && decl.fixedValue != null ) return false;
208     if ( this.fixedValue != null && !(this.fixedValue.equals(decl.fixedValue)) ) return false;
209     if ( this.getType() == decl.getType() ) return true;
210     else return false;
211   }
212   
213   boolean overlapUPAC(Object JavaDoc obj) {
214     if ( obj == null ) return false;
215     if ( obj instanceof ElementDeclaration )
216       return overlapUPAC((ElementDeclaration)obj);
217     else if ( obj instanceof Wildcard )
218        return overlapUPAC((Wildcard)obj);
219     else
220       return false;
221  }
222   
223   boolean overlapUPAC(ElementDeclaration decl) {
224     if ( this == decl ) return true;
225     if ( decl == null ) return false;
226     
227     if ( this.getName().equals(decl.getName()) &&
228          ( ( this.getNamespace() == null && decl.getNamespace() == null ) ||
229            ( this.getNamespace() != null && this.getNamespace().equals(decl.getNamespace())) ) )
230       return true;
231     
232     // substitution group
233
if ( this.substitutionGroup == decl || this == decl.substitutionGroup ) return true;
234     
235     return false;
236   }
237   
238   boolean overlapUPAC(Wildcard wild) {
239     if ( wild == null ) return false;
240
241     if ( wild.isAllowed(this.getNamespace()) ) return true;
242     
243     // perhaps, there is a problem, here
244
// overlapUPAC is called when ComplexType is initialized,
245
// then ElementDeclaration isn't already initialized,
246
// substitution group isn't initialized
247
// Solution : 3rd pass of initialization is necessary ?
248
// noted Jan. 10, 2001
249
if ( this.getSubstitutionElementList() != null ) {
250       java.util.Iterator JavaDoc it = this.getSubstitutionElementList().iterator();
251       while ( it.hasNext() ) {
252         ElementDeclaration subElement = (ElementDeclaration)it.next();
253         if ( wild.isAllowed(subElement.getNamespace()) ) return true;
254       }
255     }
256      
257     return false;
258   }
259   
260 }
261
262
Popular Tags