KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > impl > SchemaImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.model.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.modules.xml.schema.model.Annotation;
28 import org.netbeans.modules.xml.xam.Component;
29 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
30 import org.netbeans.modules.xml.schema.model.GlobalType;
31 import org.netbeans.modules.xml.schema.model.Notation;
32 import org.netbeans.modules.xml.schema.model.SchemaComponent;
33 import org.netbeans.modules.xml.schema.model.Form;
34 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
35 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
36 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
37 import org.netbeans.modules.xml.schema.model.GlobalElement;
38 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
39 import org.netbeans.modules.xml.schema.model.GlobalGroup;
40 import org.netbeans.modules.xml.schema.model.Import;
41 import org.netbeans.modules.xml.schema.model.Include;
42 import org.netbeans.modules.xml.schema.model.Redefine;
43 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
44 import org.netbeans.modules.xml.schema.model.Schema;
45 import org.netbeans.modules.xml.schema.model.Schema.Block;
46 import org.netbeans.modules.xml.schema.model.Schema.Final;
47 import org.netbeans.modules.xml.schema.model.SchemaModel;
48 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
49 import org.netbeans.modules.xml.schema.model.visitor.SchemaVisitor;
50 import org.w3c.dom.Element JavaDoc;
51
52 /**
53  * @author Vidhya Narayanan
54  * @author Nam Nguyen
55  */

56
57 public class SchemaImpl extends SchemaComponentImpl implements Schema {
58     
59     public static final String JavaDoc TNS = "tns"; //NOI18N
60

61     private Component foreignParent;
62     
63     /** Creates a new instance of SchemaImpl */
64     public SchemaImpl(SchemaModelImpl model) {
65         this(model, createNewComponent(SchemaElements.SCHEMA, model));
66     }
67     
68     public SchemaImpl(SchemaModelImpl model, Element e){
69         super(model,e);
70     }
71     
72     /**
73      *
74      *
75      */

76     public Class JavaDoc<? extends SchemaComponent> getComponentType() {
77         return Schema.class;
78     }
79     
80     public Collection JavaDoc<SchemaModelReference> getSchemaReferences() {
81         return getChildren(SchemaModelReference.class);
82     }
83     
84     /**
85      * Visitor providing
86      */

87     public void accept(SchemaVisitor visitor) {
88         visitor.visit(this);
89     }
90     
91     public Collection JavaDoc<GlobalElement> getElements() {
92         return getChildren(GlobalElement.class);
93     }
94     
95     public void removeElement(GlobalElement element) {
96         removeChild(ELEMENTS_PROPERTY, element);
97     }
98     
99     public void addElement(GlobalElement element) {
100         appendChild(ELEMENTS_PROPERTY, element);
101     }
102     
103     public Collection JavaDoc<GlobalAttributeGroup> getAttributeGroups() {
104         return getChildren(GlobalAttributeGroup.class);
105     }
106     
107     public void removeAttributeGroup(GlobalAttributeGroup group) {
108         removeChild(ATTRIBUTE_GROUPS_PROPERTY, group);
109     }
110     
111     public void addAttributeGroup(GlobalAttributeGroup group) {
112         appendChild(ATTRIBUTE_GROUPS_PROPERTY, group);
113     }
114     
115     public void removeExternalReference(SchemaModelReference ref) {
116         removeChild(SCHEMA_REFERENCES_PROPERTY, ref);
117     }
118     
119     public void addExternalReference(SchemaModelReference ref) {
120         List JavaDoc<Class JavaDoc<? extends SchemaComponent>> afterList = new ArrayList JavaDoc<Class JavaDoc<? extends SchemaComponent>>();
121         afterList.add(Annotation.class);
122         afterList.add(SchemaModelReference.class);
123         addAfter(SCHEMA_REFERENCES_PROPERTY, ref, afterList);
124     }
125     
126     public Collection JavaDoc<GlobalComplexType> getComplexTypes() {
127         return getChildren(GlobalComplexType.class);
128     }
129     
130     public void removeComplexType(GlobalComplexType type) {
131         removeChild(COMPLEX_TYPES_PROPERTY, type);
132     }
133     
134     public void addComplexType(GlobalComplexType type) {
135         appendChild(COMPLEX_TYPES_PROPERTY, type);
136     }
137     
138     public Collection JavaDoc<GlobalAttribute> getAttributes() {
139         return getChildren(GlobalAttribute.class);
140     }
141     
142     public void addAttribute(GlobalAttribute attr) {
143         appendChild(ATTRIBUTES_PROPERTY, attr);
144     }
145     
146     public void removeAttribute(GlobalAttribute attr) {
147         removeChild(ATTRIBUTES_PROPERTY, attr);
148     }
149     
150     public void setVersion(String JavaDoc ver) {
151         setAttribute(VERSION_PROPERTY, SchemaAttributes.VERSION, ver);
152     }
153     
154     public String JavaDoc getVersion() {
155         return getAttribute(SchemaAttributes.VERSION);
156     }
157     
158     public void setLanguage(String JavaDoc language) {
159         setAttribute(LANGUAGE_PROPERTY, SchemaAttributes.LANGUAGE, language);
160     }
161     
162     public String JavaDoc getLanguage() {
163         return getAttribute(SchemaAttributes.LANGUAGE);
164     }
165     
166     public void setFinalDefault(Set JavaDoc<Final> finalDefault) {
167         setAttribute(FINAL_DEFAULT_PROPERTY, SchemaAttributes.FINAL_DEFAULT,
168                 finalDefault == null ? null :
169                     Util.convertEnumSet(Final.class, finalDefault));
170     }
171     
172     public Set JavaDoc<Final> getFinalDefault() {
173         String JavaDoc s = getAttribute(SchemaAttributes.FINAL_DEFAULT);
174         return s == null ? null : Util.valuesOf(Final.class, s);
175     }
176     
177     public Set JavaDoc<Final> getFinalDefaultEffective() {
178         Set JavaDoc<Final> v = getFinalDefault();
179         return v == null ? getFinalDefaultDefault() : v;
180     }
181     
182     public Set JavaDoc<Final> getFinalDefaultDefault() {
183         return new DerivationsImpl.DerivationSet<Final>();
184     }
185     
186     public void setTargetNamespace(String JavaDoc uri) {
187         String JavaDoc currentTargetNamespace = getTargetNamespace();
188         setAttribute(TARGET_NAMESPACE_PROPERTY, SchemaAttributes.TARGET_NS, uri);
189         ensureValueNamespaceDeclared(uri, currentTargetNamespace, TNS);
190     }
191     
192     public String JavaDoc getTargetNamespace() {
193         return getAttribute(SchemaAttributes.TARGET_NS);
194     }
195     
196     public void setElementFormDefault(Form form) {
197         setAttribute(ELEMENT_FORM_DEFAULT_PROPERTY, SchemaAttributes.ELEM_FORM_DEFAULT, form);
198     }
199     
200     public Form getElementFormDefault() {
201         String JavaDoc s = getAttribute(SchemaAttributes.ELEM_FORM_DEFAULT);
202         return s == null ? null : Util.parse(Form.class, s);
203     }
204     
205     public void setAttributeFormDefault(Form form) {
206         setAttribute(ATTRIBUTE_FORM_DEFAULT_PROPERTY, SchemaAttributes.ATTR_FORM_DEFAULT, form);
207     }
208     
209     public Form getAttributeFormDefault() {
210         String JavaDoc s = getAttribute(SchemaAttributes.ATTR_FORM_DEFAULT);
211         return s == null ? null : Util.parse(Form.class, s);
212     }
213     
214     public Collection JavaDoc<GlobalSimpleType> getSimpleTypes() {
215         return getChildren(GlobalSimpleType.class);
216     }
217     
218     public void removeSimpleType(GlobalSimpleType type) {
219         removeChild(SIMPLE_TYPES_PROPERTY, type);
220     }
221     
222     public void addSimpleType(GlobalSimpleType type) {
223         appendChild(SIMPLE_TYPES_PROPERTY, type);
224     }
225     
226     public Collection JavaDoc<GlobalGroup> getGroups() {
227         return getChildren(GlobalGroup.class);
228     }
229     
230     public void removeGroup(GlobalGroup group) {
231         removeChild(GROUPS_PROPERTY, group);
232     }
233     
234     public void addGroup(GlobalGroup group) {
235         appendChild(GROUPS_PROPERTY, group);
236     }
237     
238     public Collection JavaDoc<Notation> getNotations() {
239         return getChildren(Notation.class);
240     }
241     
242     public void removeNotation(Notation notation) {
243         removeChild(NOTATIONS_PROPERTY, notation);
244     }
245     
246     public void addNotation(Notation notation) {
247         appendChild(NOTATIONS_PROPERTY, notation);
248     }
249     
250     public void setBlockDefault(Set JavaDoc<Block> blockDefault) {
251         setAttribute(BLOCK_DEFAULT_PROPERTY, SchemaAttributes.BLOCK_DEFAULT,
252                 blockDefault == null ? null :
253                     Util.convertEnumSet(Block.class, blockDefault));
254     }
255     
256     public Set JavaDoc<Block> getBlockDefault() {
257         String JavaDoc s = getAttribute(SchemaAttributes.BLOCK_DEFAULT);
258         return s == null ? null : Util.valuesOf(Block.class, s);
259     }
260     
261     public Set JavaDoc<Block> getBlockDefaultEffective() {
262         Set JavaDoc<Block> v = getBlockDefault();
263         return v == null ? getBlockDefaultDefault() : v;
264     }
265     
266     public Set JavaDoc<Block> getBlockDefaultDefault() {
267         return new DerivationsImpl.DerivationSet<Block>();
268     }
269     
270     public Form getElementFormDefaultEffective() {
271         Form v = getElementFormDefault();
272         return v == null ? getElementFormDefaultDefault() : v;
273     }
274     
275     public Form getElementFormDefaultDefault() {
276         return Form.UNQUALIFIED;
277     }
278     
279     public Form getAttributeFormDefaultEffective() {
280         Form v = getAttributeFormDefault();
281         return v == null ? getAttributeFormDefaultDefault() : v;
282     }
283     
284     public Form getAttributeFormDefaultDefault() {
285         return Form.UNQUALIFIED;
286     }
287     
288     public Collection JavaDoc<Redefine> getRedefines() {
289         return getChildren(Redefine.class);
290     }
291     
292     public Collection JavaDoc<Include> getIncludes() {
293         return getChildren(Include.class);
294     }
295     
296     public Collection JavaDoc<Import> getImports() {
297         return getChildren(Import.class);
298     }
299     
300     public Collection JavaDoc<GlobalElement> findAllGlobalElements() {
301         Collection JavaDoc<GlobalElement> result = new ArrayList JavaDoc<GlobalElement>();
302         Collection JavaDoc<GlobalElement> tempCollection = this.getElements();
303         if(tempCollection != null) {
304             result.addAll(tempCollection);
305         }
306     // TODO need to add redefined elements to search
307
result.addAll(getExternalGlobalElements(getImports()));
308         result.addAll(getExternalGlobalElements(getIncludes()));
309         result.addAll(getExternalGlobalElements(getRedefines()));
310         return result;
311     }
312     
313     private Collection JavaDoc<GlobalElement> getExternalGlobalElements(
314     Collection JavaDoc<? extends SchemaModelReference> externalRefs){
315     
316     Collection JavaDoc<GlobalElement> result = new ArrayList JavaDoc<GlobalElement>();
317     for(SchemaModelReference smr : externalRefs) {
318         try {
319         SchemaModel sm = smr.resolveReferencedModel();
320         if (sm.getState().equals(SchemaModel.State.VALID)) {
321             result.addAll(sm.getSchema().findAllGlobalElements());
322         }
323         } catch (CatalogModelException ex) {
324         // we are swalling this exception as the model cannot be found
325
// we still want to continue to try and find the reference though
326
}
327     }
328     return result;
329     }
330     
331     public Collection JavaDoc<GlobalType> findAllGlobalTypes() {
332         Collection JavaDoc<GlobalType> result = new ArrayList JavaDoc<GlobalType>();
333         //add all SimpleTypes
334
Collection JavaDoc<? extends GlobalType> tempCollection = this.getSimpleTypes();
335         if(tempCollection != null)
336             result.addAll(tempCollection);
337         //add all complex types
338
tempCollection = this.getComplexTypes();
339         if(tempCollection != null)
340             result.addAll(tempCollection);
341         //add from all the referenced docs
342
result.addAll(getExternalGlobalTypes(getImports()));
343         result.addAll(getExternalGlobalTypes(getIncludes()));
344         result.addAll(getExternalGlobalTypes(getRedefines()));
345         return result;
346     }
347     
348     private Collection JavaDoc<GlobalType> getExternalGlobalTypes(
349     Collection JavaDoc<? extends SchemaModelReference> externalRefs){
350     
351         Collection JavaDoc<GlobalType> result = new ArrayList JavaDoc<GlobalType>();
352         for(SchemaModelReference smr : externalRefs){
353         try {
354         SchemaModel sm = smr.resolveReferencedModel();
355         if (sm.getState().equals(SchemaModel.State.VALID)) {
356             result.addAll(sm.getSchema().findAllGlobalTypes());
357         }
358         } catch (CatalogModelException ex) {
359         // swallow this exception to allow some resolution to occur
360
}
361     }
362         return result;
363     }
364
365     public Component getForeignParent() {
366         return foreignParent;
367     }
368
369     public void setForeignParent(Component component) {
370         foreignParent = component;
371     }
372 }
373
Popular Tags