KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > impl > AXIModelBuilder


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.axi.impl;
20
21 import java.util.List JavaDoc;
22 import org.netbeans.modules.xml.axi.AXIComponent;
23 import org.netbeans.modules.xml.axi.AXIDocument;
24 import org.netbeans.modules.xml.axi.ContentModel;
25 import org.netbeans.modules.xml.schema.model.*;
26 import org.netbeans.modules.xml.schema.model.visitor.DeepSchemaVisitor;
27 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
28
29 /**
30  * Creates and populates children for a parent AXIComponent.
31  * Parent AXIComponent must first construct a AXIModelBuilder and then
32  * call its populateChildren method to populate the list of children.
33  *
34  * This is a visitor, which visits each node in the schema model tree
35  * to construct the AXI model.
36  *
37  * @author Samaresh (Samaresh.Panda@Sun.Com)
38  */

39 public class AXIModelBuilder extends AbstractModelBuilder {
40
41     /**
42      * Creates a new instance of AXIModelBuilder
43      */

44     public AXIModelBuilder(AXIComponent parent) {
45         super((AXIModelImpl)parent.getModel());
46         this.parent = parent;
47     }
48             
49     /**
50      * Populates the children list for the specified schema component.
51      * @param schemaComponent the component, for which, children and attribute
52      * lists are to be populated.
53      * @param visitChildren in general, this should be true, except when this is
54      * being called for an Element's type. Purely to keep the type tree in the sharedPool.
55      * @children children to be populated.
56      * @attributes attributes to be populated.
57      */

58     public void populateChildren(SchemaComponent schemaComponent,
59             boolean visitChildren, List JavaDoc<AXIComponent> children) {
60         this.children = children;
61                 
62         //For an Element's type (if global), we want to visit the
63
//type so that it gets stored in the shared pool.
64
if(!visitChildren) {
65             schemaComponent.accept(this);
66             return;
67         }
68         
69         //visit all children
70
for(SchemaComponent child : schemaComponent.getChildren()) {
71             child.accept(this);
72         }
73     }
74
75     /**
76      * Visit Schema.
77      */

78     public void visit(Schema schema) {
79     }
80     
81     /**
82      * Visit AnyElement.
83      */

84     public void visit(org.netbeans.modules.xml.schema.model.AnyElement schemaComponent) {
85         AXIComponent child = getAXIComponent(schemaComponent, false);
86         addChild(child);
87     }
88     
89     /**
90      * Visit AnyAttribute.
91      */

92     public void visit(AnyAttribute schemaComponent) {
93         AXIComponent child = getAXIComponent(schemaComponent, false);
94         addChild(child);
95     }
96     
97     /**
98      * Visit GlobalElement.
99      */

100     public void visit(GlobalElement component) {
101         AXIComponent child = getAXIComponent(component, true);
102         addChild(child);
103     }
104     
105     /**
106      * Visit LocalElement.
107      */

108     public void visit(LocalElement component) {
109         AXIComponent child = getAXIComponent(component, false);
110         addChild(child);
111     }
112     
113     /**
114      * Visit ElementReference.
115      */

116     public void visit(ElementReference component) {
117         AXIComponent child = getAXIComponent(component, false);
118         addChild(child);
119     }
120     
121     /**
122      * Visit GlobalAttribute.
123      */

124     public void visit(GlobalAttribute component) {
125         AXIComponent child = getAXIComponent(component, true);
126         addChild(child);
127     }
128     
129     /**
130      * Visit LocalAttribute.
131      */

132     public void visit(LocalAttribute component) {
133         AXIComponent child = getAXIComponent(component, false);
134         addChild(child);
135     }
136     
137     /**
138      * Visit AttributeReference.
139      */

140     public void visit(AttributeReference component) {
141         AXIComponent child = getAXIComponent(component, false);
142         addChild(child);
143     }
144     
145     /**
146      * Visit Sequence.
147      */

148     public void visit(Sequence component) {
149         AXIComponent child = getAXIComponent(component, false);
150         addChild(child);
151     }
152     
153     /**
154      * Visit Choice.
155      */

156     public void visit(Choice component) {
157         AXIComponent child = getAXIComponent(component, false);
158         addChild(child);
159     }
160     
161     /**
162      * Visit All.
163      */

164     public void visit(All component) {
165         AXIComponent child = getAXIComponent(component, false);
166         addChild(child);
167     }
168     
169     /**
170      * Visit GlobalGroup.
171      */

172     public void visit(GlobalGroup component) {
173         AXIComponent child = getAXIComponent(component, true);
174         addChild(child);
175     }
176     
177     /**
178      * Visit GroupReference.
179      */

180     public void visit(GroupReference component) {
181         NamedComponentReference ref = component.getRef();
182         if(ref == null)
183             return;
184         SchemaComponent sc = model.getReferenceableSchemaComponent(ref);
185         if(sc == null)
186             return;
187         AXIComponent child = getAXIComponent(sc, true);
188         addChild(child);
189     }
190     
191     /**
192      * Visit GlobalAttributeGroup.
193      */

194     public void visit(GlobalAttributeGroup component) {
195         AXIComponent child = getAXIComponent(component, true);
196         addChild(child);
197     }
198     
199     /**
200      * Visit AttributeGroupReference.
201      */

202     public void visit(AttributeGroupReference component) {
203         NamedComponentReference ref = component.getGroup();
204         if(ref == null)
205             return;
206         SchemaComponent sc = model.getReferenceableSchemaComponent(ref);
207         if(sc == null)
208             return;
209         AXIComponent child = getAXIComponent(sc, true);
210         addChild(child);
211     }
212         
213     /**
214      * Visit GlobalComplexType.
215      */

216     public void visit(GlobalComplexType component) {
217         AXIComponent child = getAXIComponent(component, true);
218         addChild(child);
219     }
220         
221     /**
222      * Visit LocalComplexType.
223      */

224     public void visit(LocalComplexType component) {
225         visitChildren(component);
226     }
227     
228     /**
229      * Visit ComplexContent.
230      */

231     public void visit(ComplexContent component) {
232         visitChildren(component);
233     }
234     
235     /**
236      * Visit SimpleContent.
237      */

238     public void visit(SimpleContent component) {
239         visitChildren(component);
240     }
241     
242     /**
243      * Visit ComplexExtension.
244      */

245     public void visit(SimpleExtension component) {
246         NamedComponentReference ref = component.getBase();
247         if(ref != null) {
248             SchemaComponent type = model.getReferenceableSchemaComponent(ref);
249             if(type != null) {
250                 AXIComponent child = getAXIComponent(type, true);
251                 addChild(child);
252             }
253         }
254         visitChildren(component);
255     }
256     
257     /**
258      * Visit ComplexExtension.
259      */

260     public void visit(ComplexExtension component) {
261         NamedComponentReference ref = component.getBase();
262         if(ref != null) {
263             SchemaComponent type = model.getReferenceableSchemaComponent(ref);
264             if(type != null) {
265                 AXIComponent child = getAXIComponent(type, true);
266                 addChild(child);
267             }
268         }
269         visitChildren(component);
270     }
271
272     /**
273      * Returns an AXIComponent if one exists or creates one.
274      *
275      * When this method gets called with a global component, it looks up the model
276      * and returns the global AXI component, unless the parent is AXIDocument.
277      *
278      * For local components, it always creates one.
279      */

280     private AXIComponent getAXIComponent(final SchemaComponent schemaComponent, boolean isGlobal) {
281         //when parent is schema or for non-global components,
282
//always create one.
283
if(parent instanceof AXIDocument || !isGlobal) {
284             return new AXIComponentCreator(getModel()).
285                     createNew(schemaComponent);
286         }
287
288         if(!getModel().fromSameSchemaModel(schemaComponent)) {
289             return getModel().lookupFromOtherModel(schemaComponent);
290         }
291         
292         return getModel().lookup(schemaComponent);
293     }
294         
295     /**
296      * Adds the new component to the children list.
297      */

298     private void addChild(AXIComponent child) {
299         if(child == null)
300             return;
301         
302         if(parent instanceof AXIDocument) {
303             children.add(child);
304             ((AXIDocumentImpl)parent).addToCache(child);
305             return;
306         }
307                 
308         if(child instanceof ContentModel) {
309             Util.addProxyChildren(parent, child, children);
310             return;
311         }
312         children.add(child);
313     }
314         
315     ////////////////////////////////////////////////////////////////////
316
////////////////////////// member variables ////////////////////////
317
////////////////////////////////////////////////////////////////////
318
/**
319      * Parent AXIComponent for whom, children are being populated.
320      */

321     private AXIComponent parent;
322     
323     /**
324      * Ref to the original children list being passed by the caller.
325      */

326     private List JavaDoc<AXIComponent> children;
327 }
328
329
330
Popular Tags