KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.xml.XMLConstants JavaDoc;
30 import javax.xml.namespace.QName JavaDoc;
31 import org.netbeans.modules.xml.schema.model.Import;
32 import org.netbeans.modules.xml.schema.model.Include;
33 import org.netbeans.modules.xml.schema.model.Redefine;
34 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
35 import org.netbeans.modules.xml.xam.ModelSource;
36 import org.netbeans.modules.xml.schema.model.Schema;
37 import org.netbeans.modules.xml.schema.model.SchemaComponent;
38 import org.netbeans.modules.xml.schema.model.SchemaComponentFactory;
39 import org.netbeans.modules.xml.schema.model.SchemaModel;
40 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
41 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
42 import org.netbeans.modules.xml.schema.model.impl.xdm.SyncUpdateVisitor;
43 import org.netbeans.modules.xml.schema.model.visitor.FindGlobalReferenceVisitor;
44 import org.netbeans.modules.xml.xam.ComponentUpdater;
45 import org.netbeans.modules.xml.xam.Model;
46 import org.netbeans.modules.xml.xam.Model.State;
47 import org.netbeans.modules.xml.xam.NamedReferenceable;
48 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
49 import org.netbeans.modules.xml.xam.dom.ChangeInfo;
50 import org.netbeans.modules.xml.xam.dom.DocumentModelAccess;
51 import org.netbeans.modules.xml.xam.dom.SyncUnit;
52
53 /**
54  *
55  * @author Vidhya Narayanan
56  */

57 public class SchemaModelImpl extends AbstractDocumentModel<SchemaComponent> implements SchemaModel {
58     
59     private SchemaImpl schema;
60     private SchemaComponentFactory csef;
61     
62     public SchemaModelImpl(ModelSource modelSource) {
63         super(modelSource);
64         csef = new SchemaComponentFactoryImpl(this);
65         //getAccess().setAutoSync(true);
66
}
67     
68     /**
69      *
70      *
71      * @return the schema represented by this model. The returned schema
72      * instance will be valid and well formed, thus attempting to update
73      * from a document which is not well formed will not result in any changes
74      * to the schema model.
75      */

76
77     public SchemaImpl getSchema() {
78         return (SchemaImpl)getRootComponent();
79     }
80     
81     /**
82      *
83      *
84      * @return common schema element factory valid for this instance
85      */

86     public SchemaComponentFactory getFactory() {
87         return csef;
88     }
89     
90     public SchemaComponent createRootComponent(org.w3c.dom.Element JavaDoc root) {
91         SchemaImpl newSchema = (SchemaImpl)csef.create(root, null);
92         if (newSchema != null) {
93             schema = newSchema;
94         } else {
95             return null;
96         }
97         return getSchema();
98     }
99
100
101     public SchemaComponent getRootComponent() {
102         return schema;
103     }
104
105     public <T extends NamedReferenceable>
106             T resolve(String JavaDoc namespace, String JavaDoc localName, Class JavaDoc<T> type)
107     {
108         if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(namespace)){
109             SchemaModel sm = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
110             return sm.findByNameAndType(localName, type);
111         }
112         
113         return resolve(namespace, localName, type, null, new ArrayList JavaDoc<SchemaModel>());
114     }
115     
116     <T extends NamedReferenceable>
117             T resolve(String JavaDoc namespace, String JavaDoc localName, Class JavaDoc<T> type, SchemaModelReference refToMe, Collection JavaDoc<SchemaModel> checked)
118     {
119         if (getState() != State.VALID) {
120             return null;
121         }
122         
123         T found = null;
124         String JavaDoc targetNamespace = getSchema().getTargetNamespace();
125         if (targetNamespace != null && targetNamespace.equals(namespace) ||
126             targetNamespace == null && namespace == null) {
127             found = findByNameAndType(localName, type);
128         }
129         
130         if (found == null && ! (refToMe instanceof Import)) {
131             checked.add(this);
132             
133             Collection JavaDoc<SchemaModelReference> modelRefs = getSchemaModelReferences();
134             for (SchemaModelReference r : modelRefs) {
135                 // import should not have null namespace
136
if (r instanceof Import) {
137                     if (namespace == null || ! namespace.equals(((Import)r).getNamespace())) {
138                         continue;
139                     }
140                 }
141                 
142                 SchemaModelImpl sm = resolve(r);
143                 if (sm != null && ! checked.contains(sm)) {
144                     found = sm.resolve(namespace, localName, type, r, checked);
145                 }
146                 if (found != null) {
147                     break;
148                 }
149             }
150         }
151         
152         return found;
153     }
154     
155     public SchemaModelImpl resolve(SchemaModelReference ref) {
156         try {
157             return (SchemaModelImpl) ref.resolveReferencedModel();
158         } catch (CatalogModelException ex) {
159             return null;
160         }
161     }
162
163     public Collection JavaDoc<SchemaModelReference> getSchemaModelReferences() {
164         Collection JavaDoc<SchemaModelReference> refs = new ArrayList JavaDoc<SchemaModelReference>();
165         refs.addAll(getSchema().getRedefines());
166         refs.addAll(getSchema().getIncludes());
167         refs.addAll(getSchema().getImports());
168         return refs;
169     }
170             
171     public <T extends NamedReferenceable> T findByNameAndType(String JavaDoc localName, Class JavaDoc<T> type) {
172         return new FindGlobalReferenceVisitor<T>().find(type, localName, getSchema());
173     }
174     
175     public Set JavaDoc<Schema> findSchemas(String JavaDoc namespace) {
176         Set JavaDoc<Schema> result = new HashSet JavaDoc<Schema>();
177         
178         // build-in XSD schema is always visible
179
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(namespace)){
180             SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
181             result.add(primitiveModel.getSchema());
182             return result;
183         }
184         
185         return _findSchemas(namespace, result, null);
186     }
187     
188     protected enum ReferenceType { IMPORT, INCLUDE, REDEFINE }
189     
190     Set JavaDoc<Schema> _findSchemas(String JavaDoc namespace, Set JavaDoc<Schema> result, ReferenceType refType) {
191         SchemaImpl schema = getSchema();
192         // schema could be null, if last sync throwed exception
193
if (schema == null) {
194             return result;
195         }
196         
197         String JavaDoc targetNamespace = schema.getTargetNamespace();
198         if (targetNamespace != null && targetNamespace.equals(namespace) ||
199             targetNamespace == null && namespace == null) {
200             result.add(schema);
201         }
202         
203         if (refType != ReferenceType.IMPORT) {
204             checkIncludeSchemas(namespace, result);
205             checkRedefineSchemas(namespace, result);
206             checkImportedSchemas(namespace, result);
207         }
208         
209         return result;
210     }
211     
212     private void checkIncludeSchemas(String JavaDoc namespace, Set JavaDoc<Schema> result) {
213         Collection JavaDoc<Include> includes = getSchema().getIncludes();
214         for (Include include : includes) {
215             try {
216                 SchemaModel model = include.resolveReferencedModel();
217                 if (model.getState() == Model.State.NOT_WELL_FORMED) {
218                     continue;
219                 }
220                 
221                 if (! result.contains(model.getSchema())) {
222                     result.addAll(((SchemaModelImpl)model)._findSchemas(namespace, result, ReferenceType.INCLUDE));
223                 }
224             } catch (CatalogModelException ex) {
225                 // ignore this exception to proceed with search
226
}
227         }
228     }
229     
230     private void checkRedefineSchemas(String JavaDoc namespace, Set JavaDoc<Schema> result) {
231         Collection JavaDoc<Redefine> redefines = getSchema().getRedefines();
232         for (Redefine redefine : redefines) {
233            try {
234            SchemaModel model = redefine.resolveReferencedModel();
235            if (model.getState() == Model.State.NOT_WELL_FORMED)
236                 continue;
237             
238            if (! result.contains(model.getSchema())) {
239                result.addAll(((SchemaModelImpl)model)._findSchemas(namespace, result, ReferenceType.REDEFINE));
240            }
241            } catch (CatalogModelException ex) {
242            // ignore this exception to proceed with search
243
}
244        }
245     }
246     
247     private void checkImportedSchemas(String JavaDoc namespace, Set JavaDoc<Schema> result) {
248         Collection JavaDoc<Import> imports = getSchema().getImports();
249         for (Import imp : imports) {
250         try {
251             SchemaModel model = imp.resolveReferencedModel();
252             if (model.getState() == Model.State.NOT_WELL_FORMED)
253                 continue;
254             
255            if (! result.contains(model.getSchema())) {
256                result.addAll(((SchemaModelImpl)model)._findSchemas(namespace, result, ReferenceType.IMPORT));
257            }
258         } catch (CatalogModelException ex) {
259             // ignore this exception to proceed with search
260
}
261         }
262     }
263     
264     /**
265      * This api returns the effective namespace for a given component.
266      * If given component has a targetNamespace different than the
267      * this schema, that namespace is returned. The special case is that if
268      * the targetNamespace of the component is null, there is no target
269      * namespace defined, then the import statements for this file are
270      * examined to determine if this component is directly or indirectly
271      * imported. If the component is imported, then null if returned
272      * otherwise the component is assumed to be included or redefined and
273      * the namespace of this schema is returned.
274      */

275     public String JavaDoc getEffectiveNamespace(SchemaComponent component) {
276     SchemaModel componentModel = component.getModel();
277     Schema schema = getSchema();
278         Schema componentSchema = componentModel.getSchema();
279         String JavaDoc tns = schema.getTargetNamespace();
280         String JavaDoc componentTNS = componentSchema.getTargetNamespace();
281     if (this == componentModel) {
282         return tns;
283         } else if (componentTNS == null && tns != null) {
284             // only include/redefine model can assum host model targetNamespace
285
// so check if is from imported to just return null
286
Collection JavaDoc<Import> imports = schema.getImports();
287         for (Import imp: imports) {
288         SchemaModel m = null;
289         try {
290             m = imp.resolveReferencedModel();
291         } catch (CatalogModelException ex) {
292             // the import cannot be resolved
293
}
294         if(componentModel.equals(m)) {
295             return null;
296         }
297                 if (m == null || m.getState() == Model.State.NOT_WELL_FORMED) {
298                     continue;
299                 }
300                 String JavaDoc importedTNS = m.getSchema().getTargetNamespace();
301                 if (importedTNS == null) continue;
302                 Set JavaDoc<Schema> visibleSchemas = findSchemas(importedTNS);
303                 for (Schema visible : visibleSchemas) {
304                     if (componentModel.equals(visible.getModel())) {
305                         return null;
306                     }
307                 }
308         }
309             return tns;
310         } else {
311             return componentTNS;
312         }
313     }
314
315     public SchemaComponent createComponent(SchemaComponent parent, org.w3c.dom.Element JavaDoc element) {
316        return csef.create(element, parent);
317     }
318
319     protected ComponentUpdater<SchemaComponent> getComponentUpdater() {
320         return new SyncUpdateVisitor();
321     }
322
323     public Set JavaDoc<QName JavaDoc> getQNames() {
324         return SchemaElements.allQNames();
325     }
326     
327     public SyncUnit prepareSyncUnit(ChangeInfo changes, SyncUnit unit) {
328         unit = super.prepareSyncUnit(changes, unit);
329         if (unit != null) {
330             return new SyncUnitReviewVisitor().review(unit);
331         }
332         return null;
333     }
334     
335     public DocumentModelAccess getAccess() {
336         if (access == null) {
337             super.getAccess().setAutoSync(true); // default autosync true
338
}
339         return super.getAccess();
340     }
341     
342     public Map JavaDoc<QName JavaDoc,List JavaDoc<QName JavaDoc>> getQNameValuedAttributes() {
343         return SchemaAttributes.getQNameValuedAttributes();
344     }
345 }
346
Popular Tags