KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Basic class for Schema package user.
29  *
30  * <p>This is the implementation of The Schema Itself. At abstract level,
31  * the schema itself is just a container for its components.
32  * The following properties are defined</p>
33  * <p> 1. type definitions: a set of named simple and comple type definitions. </p>
34  * <p> 2. attribute declarations: a set of named (top-level) attrubite declarations. </p>
35  * <p> 3. element declarations: a set of named (top-level) element declarations. </p>
36  * <p> 4. attribute group declarations: a set of named attribute group declarations. </p>
37  * <p> 5. model group declarations: a set of named model group declarations. </p>
38  *
39  * <p>See XML Schema Part 1: Structures 3.15.1
40  * W3C Recommendation 2 May 2001</p>
41  *
42  */

43 public class Schema implements SchemaConstants, SchemaVisitable, SchemaScope {
44 private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
45 private static final String JavaDoc RCSName = "$Name: $";
46
47   public static final int DATATYPE = 0;
48   public static final int ELEMENT_DECLARATION = 1;
49   public static final int ATTRIBUTE_DECLARATION = 2;
50   public static final int MODEL_GROUP_DEFINITION = 3;
51   public static final int ATTRIBUTE_GROUP_DEFINITION = 4;
52   public static final int NOTATION_DECLARATION = 5;
53
54   private HashMap dataTypes = new HashMap();
55   private HashMap modelGroupDefinitions = new HashMap();
56   private HashMap attributeGroupDefinitions = new HashMap();
57   private HashMap elementDeclarations = new HashMap();
58   private HashMap attributeDeclarations = new HashMap();
59   private HashMap notationDeclarations = new HashMap();
60
61   private String JavaDoc targetNamespace;
62   private String JavaDoc schemaLocation;
63   private SchemaManager manager;
64   private boolean loaded = false;
65   private HashSet includedLocations = new HashSet();
66   private HashSet importedNamespaces = new HashSet();
67   private LinkedList defaults = new LinkedList();
68   private HashMap prefixMap = null;
69   private String JavaDoc prefix;
70   private InitializationVisitor initializer = null;
71
72   
73   // identity constraints of this schema
74
// this is only used to initialize keyref and to validate keyref
75
private HashMap identityConstraints = null;
76
77   /**
78    * Create a new Schema.
79    *
80    * Schema location example 2: file:/d:/schema/example.xsd
81    *
82    * @param uri The target namesapce.
83    * @param location The schema location.
84    */

85   public Schema(String JavaDoc uri, String JavaDoc location, SchemaManager manager) {
86     targetNamespace = uri;
87     schemaLocation = location;
88     this.manager = manager;
89     pushDefaults();
90     addImportedNamespace(XMLSCHEMA_URI);
91     initializer = new InitializationVisitor(this);
92   }
93
94   // SchemaVisitor is applied to initialize this schema
95
public void accept(SchemaVisitor visitor) throws SchemaException {
96     visitor.visit(this);
97   }
98   
99
100   public void initialize() throws SchemaException {
101
102       java.util.Iterator JavaDoc it = getTypes().iterator();
103       while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer);
104
105       it = getElementDeclarations().iterator();
106       while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer);
107
108       it = getAttributeDeclarations().iterator();
109       while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer);
110       
111       it = getAttributeGroupDefinitions().iterator();
112       while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer);
113   }
114
115   /**
116    * Return the schema's target namespace, if any.
117    *
118    * @return The target namespace, or null if there is none.
119    */

120   public String JavaDoc getNamespace() {
121     return targetNamespace;
122   }
123   
124   /**
125    * Return the schema's target namespace, if any.
126    *
127    * @return The target namespace, or null if there is none.
128    */

129    public String JavaDoc toString() {
130     return targetNamespace;
131   }
132   
133   /**
134    * Return true if schema has the same target namespace.
135    *
136    * @param schema The schema to compare.
137    */

138   public boolean equals(Schema schema) {
139     return ( targetNamespace.equals(schema.getNamespace()) );
140   }
141
142   /**
143    * Return schema location, if any.
144    *
145    * @return The schema location, or null if there is none.
146    */

147   public String JavaDoc getLocation() {
148     return schemaLocation;
149   }
150
151   /**
152    * Return the collection of schema document URIs
153    *
154    * @return the collection of schema document URIs, as strings, or null if there are none
155    */

156   public Collection getSchemaDocuments() {
157     ArrayList result = new ArrayList();
158     if (schemaLocation != null) result.add(schemaLocation);
159     result.addAll(includedLocations);
160     return result;
161   }
162   
163   /**
164    * Return the set of imported schema namespaces.
165    */

166   public Set getImportedNamespaces() {
167     return Collections.unmodifiableSet(importedNamespaces);
168   }
169   
170   /**
171    * Return the schema manager used to load the schema
172    *
173    * @return The schema manager associated to this schema
174    */

175   public SchemaManager getManager() {
176     return manager;
177   }
178
179   /**
180    * Giving a type name, look up the named type (simple type or complex type).
181    *
182    * @param name The type name.
183    *
184    * @return The Type, or null if there is none.
185    */

186   public Type getType(String JavaDoc name) {
187     return (Type)getSchemaComponent(name, dataTypes);
188   }
189
190   /**
191    * Giving an element declaration name, look up the top-level element declaration.
192    *
193    * @param name The element declaration name.
194    *
195    * @return The ElementDeclaration, or null if there is none.
196    */

197   public ElementDeclaration getElementDeclaration(String JavaDoc name) {
198     return (ElementDeclaration)getSchemaComponent(name, elementDeclarations);
199   }
200     
201   /**
202    * Giving an attribute declaration name, look up the top-level attribute declaration.
203    *
204    * @param name The attribute name.
205    *
206    * @return The AttributeDeclaration, or null if there is none.
207    */

208   public AttributeDeclaration getAttributeDeclaration(String JavaDoc name) {
209     return (AttributeDeclaration)getSchemaComponent(name, attributeDeclarations);
210   }
211     
212   /**
213    * Giving a model group name, look up the named model group.
214    *
215    * @param name The model group name.
216    *
217    * @return The ModelGroupDefinition, or null if there is none.
218    */

219   public ModelGroupDefinition getModelGroupDefinition(String JavaDoc name) {
220     return (ModelGroupDefinition)getSchemaComponent(name, modelGroupDefinitions);
221   }
222
223   /**
224    * Giving an attribute group name, look up the named attribute group.
225    *
226    * @param name The attribute group name.
227    *
228    * @return The AttributeGroupDefinition, or null if there is none.
229    */

230   public AttributeGroupDefinition getAttributeGroupDefinition(String JavaDoc name) {
231     return (AttributeGroupDefinition)getSchemaComponent(name, attributeGroupDefinitions);
232   }
233
234   /**
235    * Giving a notation name, look up the named notation.
236    *
237    * @param name The notation name.
238    *
239    * @return The Notation, or null if there is none.
240    */

241   public NotationDeclaration getNotationDeclaration(String JavaDoc name) {
242     return (NotationDeclaration)getSchemaComponent(name, notationDeclarations);
243   }
244
245   public SchemaComponent getSchemaComponent(String JavaDoc name, int type) {
246     switch (type) {
247     case DATATYPE: return getType(name);
248     case ELEMENT_DECLARATION: return getElementDeclaration(name);
249     case ATTRIBUTE_DECLARATION: return getAttributeDeclaration(name);
250     case MODEL_GROUP_DEFINITION: return getModelGroupDefinition(name);
251     case ATTRIBUTE_GROUP_DEFINITION: return getAttributeGroupDefinition(name);
252     case NOTATION_DECLARATION: return getNotationDeclaration(name);
253     default: return null;
254     }
255   }
256   
257   public boolean isGlobalScope() {
258     return true;
259   }
260   
261   public ElementDeclaration getElementDeclaration(String JavaDoc namespace, String JavaDoc name) {
262     if (!targetNamespace.equals(namespace)) return null;
263     return getElementDeclaration(name);
264   }
265
266   public AttributeDeclaration getAttributeDeclaration(String JavaDoc namespace, String JavaDoc name) {
267     if (!targetNamespace.equals(namespace)) return null;
268     return getAttributeDeclaration(name);
269   }
270
271   /**
272    * Look up all named data types(all named simple types and complex types).
273    *
274    * @return A collection view of all named data types.
275    */

276   public Collection getTypes() {
277     return dataTypes.values();
278   }
279   
280   /**
281    * Look up all top-level element declarations.
282    *
283    * @return A collection view of the top-level element declarations
284    */

285   public Collection getElementDeclarations() {
286     return elementDeclarations.values();
287   }
288
289   /**
290    * Look up all top-level attribute declarations.
291    *
292    * @return A collection view of the top-level attribute declarations
293    */

294   public Collection getAttributeDeclarations() {
295     return attributeDeclarations.values();
296   }
297   
298   /**
299    * Look up all named model group definitions.
300    *
301    * @return A collection view of all named model group definitions
302    */

303   public Collection getModelGroupDefinitions() {
304     return modelGroupDefinitions.values();
305   }
306   
307   /**
308    * Look up all named attribute group definitions.
309    *
310    * @return A collection view of all named attribute group definitions
311    */

312   public Collection getAttributeGroupDefinitions() {
313     return attributeGroupDefinitions.values();
314   }
315   /**
316    * Look up all notation declarations.
317    *
318    * @return A collection view of all notation declarations
319    */

320   public Collection getNotationDeclarations() {
321     return notationDeclarations.values();
322   }
323   
324   public HashMap getNotationDeclarationMap() {
325     return notationDeclarations;
326   }
327   
328   private SchemaComponent getSchemaComponent(String JavaDoc name, HashMap map) {
329     SchemaComponent result = (SchemaComponent)map.get(name);
330     return result;
331   }
332
333   boolean isLoaded() {
334     return loaded;
335   }
336   
337   void setLoaded(boolean loaded) {
338     this.loaded = loaded;
339   }
340
341   public boolean isIncluded(String JavaDoc location) {
342     return includedLocations.contains(location);
343   }
344
345   public void addIncludedLocation(String JavaDoc location) {
346     includedLocations.add(location);
347   }
348
349   public boolean isImported(String JavaDoc namespace) {
350     return importedNamespaces.contains(namespace);
351   }
352
353   public void addImportedNamespace(String JavaDoc namespace) {
354     importedNamespaces.add(namespace);
355   }
356
357   public void register(Type entity) throws SchemaException {
358     register(entity, dataTypes);
359   }
360
361   public void register(ElementDeclaration entity) throws SchemaException {
362     register(entity, elementDeclarations);
363   }
364
365   public void register(AttributeDeclaration entity) throws SchemaException {
366     register(entity, attributeDeclarations);
367   }
368   
369   public void register(ModelGroupDefinition entity) throws SchemaException {
370     register(entity, modelGroupDefinitions);
371   }
372
373  public void register(AttributeGroupDefinition entity) throws SchemaException {
374     register(entity, attributeGroupDefinitions);
375   }
376
377   public void register(NotationDeclaration entity) throws SchemaException {
378     register(entity, notationDeclarations);
379   }
380   
381   private void register(SchemaComponent comp, HashMap map) throws SchemaException {
382     String JavaDoc name = comp.getName();
383     SchemaComponent existing = getSchemaComponent(name, map);
384     if (existing != null && !existing.equals(comp))
385       // Redeclaration of schema component
386
throw new SchemaException("sch-props-correct.2", comp);
387     map.put(name, comp);
388   }
389   
390   public void removeRegisteredComponent(Type entity) {
391     dataTypes.remove(entity.getName());
392   }
393
394   public void removeRegisteredComponent(ElementDeclaration entity) {
395     elementDeclarations.remove(entity.getName());
396   }
397
398   public void removeRegisteredComponent(AttributeDeclaration entity) {
399     attributeDeclarations.remove(entity.getName());
400   }
401   
402   public void removeRegisteredComponent(ModelGroupDefinition entity) {
403     modelGroupDefinitions.remove(entity.getName());
404   }
405
406   public void removeRegisteredComponent(AttributeGroupDefinition entity) {
407     attributeGroupDefinitions.remove(entity.getName());
408   }
409     
410   public void removeRegisteredComponent(NotationDeclaration entity) {
411     notationDeclarations.remove(entity.getName());
412   }
413     
414   public void pushDefaults() {
415     defaults.addFirst(new HashMap());
416   }
417
418   public void popDefaults() {
419     defaults.removeFirst();
420   }
421
422   public void setDefault(String JavaDoc key, String JavaDoc val) {
423     ((HashMap)defaults.getFirst()).put(key, val);
424   }
425
426   public String JavaDoc getDefault(String JavaDoc key) {
427     return (String JavaDoc)((HashMap)defaults.getFirst()).get(key);
428   }
429
430   public void setPrefixMap(HashMap map) {
431     prefixMap = map;
432     
433     // get this schema's prefix
434
Iterator it = ((Set)prefixMap.entrySet()). iterator();
435     while (it.hasNext()) {
436       Map.Entry entry = (Map.Entry)it.next();
437       if ( getNamespace() == null ) {
438         prefix = "";
439         break;
440       }
441       if ( getNamespace().equals(entry.getValue()) ) {
442         prefix = (String JavaDoc)entry.getKey();
443         break;
444       }
445     }
446   }
447
448   public HashMap getPrefixMap() {
449     return prefixMap;
450   }
451   
452   public String JavaDoc getPrefix() {
453     return prefix;
454   }
455   
456   // identity-constraint
457
public void register(IdentityConstraint entity) throws SchemaException {
458     if ( identityConstraints == null ) identityConstraints = new HashMap();
459     register(entity, identityConstraints);
460   }
461   
462   public Collection getIdentityConstraints() {
463     if ( identityConstraints == null ) return null;
464     return identityConstraints.values();
465   }
466   
467   // name is IdentityConstraint's name
468
public IdentityConstraint getIdentityConstraint(String JavaDoc name) {
469     if ( identityConstraints == null ) return null;
470     if ( name.indexOf(':') != -1 ) {
471       name = name.substring(name.indexOf(':') + 1);
472     }
473     IdentityConstraint result = (IdentityConstraint)identityConstraints.get(name);
474     return result;
475   }
476   
477 }
478
Popular Tags