KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collection JavaDoc;
22 import org.netbeans.modules.xml.schema.model.*;
23 import org.netbeans.modules.xml.schema.model.Attribute.Use;
24 import org.netbeans.modules.xml.schema.model.visitor.SchemaVisitor;
25 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
26 import org.w3c.dom.Node JavaDoc;
27
28 /**
29  * This class provides the algorithm to determine what classes to create
30  * based on the current node context.
31  * @author Chris Webster
32  */

33 class SchemaElementNodeVisitor implements SchemaVisitor {
34     
35     /**
36      * This method determines the incoming component and creates the appropriate
37      * subcomponent.
38      *
39      * @return SchemaComponent for the specified element or null if the
40      * element is not recognized as a type specified in schema.
41      */

42     public SchemaComponent createSubComponent(
43         SchemaComponent parent,
44         org.w3c.dom.Element JavaDoc e,
45         SchemaModelImpl model) {
46         assert model != null;
47         this.e = e;
48         setNewComponent(null);
49         this.model = model;
50         if (parent == null) {
51             if (SchemaElements.SCHEMA.getName().equals(e.getLocalName())) {
52                 setNewComponent(new SchemaImpl(model,e));
53             }
54         } else {
55             if (SchemaElements.ANNOTATION.getName().equals(e.getLocalName())) {
56                 setNewComponent(new AnnotationImpl(model,e));
57             } else {
58                 parent.accept(this);
59             }
60         }
61         return newCommonSchemaComponent;
62     }
63     
64     public void visit(SimpleTypeRestriction str) {
65         allSimpleRestriction();
66     }
67     
68     public void visit(GlobalAttributeGroup gag) {
69         recognizeAttributes();
70     }
71     
72     public void visit(MinLength ml) {
73         // only annotation
74
}
75     
76     public void visit(AnyAttribute anyAttr) {
77         // only child is annotation
78
}
79     
80     public void visit(AttributeGroupReference agr) {
81         // only child is annotation
82
}
83     
84     public void visit(SimpleContentRestriction scr) {
85         allSimpleRestriction();
86         recognizeAttributes();
87     }
88     
89     public void visit(LocalSimpleType type) {
90         allSimpleType();
91     }
92     
93     public void visit(MaxInclusive mi) {
94         // only annotation
95
}
96     
97     public void visit(Documentation d) {
98         // any content
99
}
100
101     public void visit(AppInfo d) {
102         // any content
103
}
104     
105     public void visit(Notation d) {
106         // only annotation
107
}
108     
109     public void visit(ComplexExtension ce) {
110         recognizeGroupAllChoiceSequence();
111         recognizeAttributes();
112     }
113     
114     public void visit(Field f) {
115         // only annotation
116
}
117     
118     public void visit(Schema s) {
119         if (SchemaElements.INCLUDE.getName().equals(e.getLocalName())) {
120             setNewComponent(new IncludeImpl(model,e));
121             
122         } else if (SchemaElements.IMPORT.getName().equals(e.getLocalName())) {
123             setNewComponent(new ImportImpl(model,e));
124             
125         } else if (SchemaElements.REDEFINE.getName().equals(e.getLocalName())) {
126             setNewComponent(new RedefineImpl(model,e));
127             
128         } else if (SchemaElements.SIMPLE_TYPE.getName().equals(
129             e.getLocalName())) {
130             setNewComponent(new GlobalSimpleTypeImpl(model,e));
131             
132         } else if (SchemaElements.COMPLEX_TYPE.getName().equals(
133             e.getLocalName())) {
134             setNewComponent(new GlobalComplexTypeImpl(model,e));
135             
136         } else if (SchemaElements.GROUP.getName().equals(e.getLocalName())) {
137             setNewComponent(new GlobalGroupImpl(model,e));
138             
139         } else if (SchemaElements.ATTRIBUTE_GROUP.getName().equals(
140             e.getLocalName())) {
141             setNewComponent(new GlobalAttributeGroupImpl(model,e));
142             
143         } else if (SchemaElements.ELEMENT.getName().equals(e.getLocalName())) {
144             setNewComponent(new GlobalElementImpl(model,e));
145             
146         } else if (SchemaElements.ATTRIBUTE.getName().equals(e.getLocalName())) {
147             setNewComponent(new GlobalAttributeImpl(model,e));
148             
149         } else if (SchemaElements.NOTATION.getName().equals(e.getLocalName())) {
150             setNewComponent(new NotationImpl(model,e));
151             
152         }
153     }
154     
155     public void visit(Union u) {
156         if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
157             setNewComponent(new LocalSimpleTypeImpl(model,e));
158         }
159     }
160     
161     public void visit(MinExclusive me) {
162         // only annotation
163
}
164     
165     public void visit(Whitespace ws) {
166         // only annotation
167
}
168     
169     public void visit(MinInclusive mi) {
170         // only annotation
171
}
172     
173     public void visit(Redefine rd) {
174         if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
175             setNewComponent(new GlobalSimpleTypeImpl(model,e));
176             
177         } else if (SchemaElements.COMPLEX_TYPE.getName().equals(e.getLocalName())) {
178             setNewComponent(new GlobalComplexTypeImpl(model,e));
179             
180         } else if (SchemaElements.GROUP.getName().equals(e.getLocalName())) {
181             setNewComponent(new GlobalGroupImpl(model,e));
182             
183         } else if (SchemaElements.ATTRIBUTE_GROUP.getName().equals(e.getLocalName())) {
184             setNewComponent(new GlobalAttributeGroupImpl(model,e));
185         }
186     }
187     
188     public void visit(Choice choice) {
189         allChoice();
190     }
191     
192     public void visit(GroupReference gr) {
193         // only annotation
194
}
195     
196     public void visit(FractionDigits fd) {
197         // only annotation
198
}
199     
200     public void visit(GlobalGroup gd) {
201         if (SchemaElements.ALL.getName().equals(e.getLocalName())) {
202             setNewComponent(new AllImpl(model,e));
203             
204         } else if (SchemaElements.CHOICE.getName().equals(e.getLocalName())) {
205             setNewComponent(new ChoiceImpl(model,e));
206             
207         } else if (SchemaElements.SEQUENCE.getName().equals(e.getLocalName())) {
208             setNewComponent(new SequenceImpl(model,e));
209         }
210     }
211     
212     public void visit(GlobalElement ge) {
213         allElement();
214     }
215     
216     public void visit(All all) {
217         allCreation();
218     }
219     
220     public void visit(ComplexContent cc) {
221         if (SchemaElements.RESTRICTION.getName().equals(e.getLocalName())) {
222             setNewComponent(new ComplexContentRestrictionImpl(model,e));
223             
224         } else if (SchemaElements.EXTENSION.getName().equals(e.getLocalName())) {
225             setNewComponent(new ComplexExtensionImpl(model,e));
226         }
227         
228     }
229     
230     public void visit(LocalComplexType type) {
231         allComplexType();
232     }
233     
234     public void visit(MaxExclusive me) {
235         // annotation only
236
}
237     
238     public void visit(Include include) {
239         // only annotation
240
}
241     
242     public void visit(ComplexContentRestriction ccr) {
243         recognizeGroupAllChoiceSequence();
244         recognizeAttributes();
245     }
246     
247     public void visit(GlobalComplexType gct) {
248         allComplexType();
249     }
250     
251     public void visit(GlobalAttribute ga) {
252         recognizeInlineSimpleType();
253     }
254     
255     public void visit(Annotation ann) {
256         // TODO add appinfo
257
if (SchemaElements.DOCUMENTATION.getName().equals(e.getLocalName())) {
258             setNewComponent(new DocumentationImpl(model,e));
259         } else if (SchemaElements.APPINFO.getName().equals(e.getLocalName())) {
260             setNewComponent(new AppInfoImpl(model, e));
261         }
262     }
263     
264     public void visit(SimpleContent sc) {
265         if (SchemaElements.RESTRICTION.getName().equals(e.getLocalName())) {
266             setNewComponent(new SimpleContentRestrictionImpl(model,e));
267             
268         } else if (SchemaElements.EXTENSION.getName().equals(e.getLocalName())) {
269             setNewComponent(new SimpleExtensionImpl(model,e));
270         }
271     }
272     
273     public void visit(Key key) {
274         recognizeSelectorAndField();
275     }
276     
277     public void visit(KeyRef kr) {
278         recognizeSelectorAndField();
279     }
280     
281     public void visit(LocalElement le) {
282         allElement();
283     }
284     
285     public void visit(MaxLength ml) {
286         // annotation only
287
}
288     
289     public void visit(Sequence s) {
290         recognizeGroupAnyChoiceSequenceElement();
291     }
292     
293     public void visit(Pattern p) {
294         // only annotation
295
}
296     
297     public void visit(List l) {
298         if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
299             setNewComponent(new LocalSimpleTypeImpl(model,e));
300         }
301     }
302     
303     public void visit(Import im) {
304         // only annotation
305
}
306     
307     public void visit(Enumeration e) {
308         // only child is annotation
309
}
310     
311     public void visit(AnyElement any) {
312         // only child is annotation
313
}
314     
315     public void visit(LocalAttribute la) {
316         recognizeInlineSimpleType();
317     }
318     
319     public void visit(GlobalSimpleType gst) {
320         allSimpleType();
321     }
322     
323     public void visit(TotalDigits td) {
324         // annotation only
325
}
326     
327     public void visit(Unique u) {
328         recognizeSelectorAndField();
329     }
330     
331     public void visit(Length length) {
332         // annotation only
333
}
334     
335     public void visit(Selector selector) {
336         // annotation only
337
}
338     
339     public void visit(SimpleExtension se) {
340         recognizeAttributes();
341     }
342     
343     public void visit(ElementReference er) {
344     // annotation only
345
}
346     
347     public void visit(AttributeReference reference) {
348     // annotation only
349
}
350     
351     private void allSimpleRestriction() {
352         if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
353             setNewComponent(new LocalSimpleTypeImpl(model,e));
354             
355         } else if (SchemaElements.MIN_EXCLUSIVE.getName().equals(
356             e.getLocalName())) {
357             setNewComponent(new MinExclusiveImpl(model,e));
358             
359         } else if (SchemaElements.MIN_INCLUSIVE.getName().equals(
360             e.getLocalName())) {
361             setNewComponent(new MinInclusiveImpl(model,e));
362             
363         } else if (SchemaElements.MAX_EXCLUSIVE.getName().equals(
364             e.getLocalName())) {
365             setNewComponent(new MaxExclusiveImpl(model,e));
366             
367         } else if (SchemaElements.MAX_INCLUSIVE.getName().equals(
368             e.getLocalName())) {
369             setNewComponent(new MaxInclusiveImpl(model,e));
370             
371         } else if (SchemaElements.TOTAL_DIGITS.getName().equals(
372             e.getLocalName())) {
373             setNewComponent(new TotalDigitsImpl(model,e));
374             
375         } else if (SchemaElements.FRACTION_DIGITS.getName().equals(
376             e.getLocalName())) {
377             setNewComponent(new FractionDigitsImpl(model,e));
378             
379         } else if (SchemaElements.LENGTH.getName().equals(
380             e.getLocalName())) {
381             setNewComponent(new LengthImpl(model,e));
382             
383         } else if (SchemaElements.MIN_LENGTH.getName().equals(
384             e.getLocalName())) {
385             setNewComponent(new MinLengthImpl(model,e));
386             
387         } else if (SchemaElements.MAX_LENGTH.getName().equals(
388             e.getLocalName())) {
389             setNewComponent(new MaxLengthImpl(model,e));
390             
391         } else if (SchemaElements.ENUMERATION.getName().equals(
392             e.getLocalName())) {
393             setNewComponent(new EnumerationImpl(model,e));
394             
395         } else if (SchemaElements.WHITESPACE.getName().equals(
396             e.getLocalName())) {
397             setNewComponent(new WhitespaceImpl(model,e));
398             
399         } else if (SchemaElements.PATTERN.getName().equals(e.getLocalName())) {
400             setNewComponent(new PatternImpl(model,e));
401         }
402         
403     }
404     
405     private void recognizeSelectorAndField() {
406         if (SchemaElements.SELECTOR.getName().equals(e.getLocalName())) {
407             setNewComponent(new SelectorImpl(model,e));
408             
409         } else if (SchemaElements.FIELD.getName().equals(e.getLocalName())) {
410             setNewComponent(new FieldImpl(model,e));
411         }
412     }
413     
414     private void recognizeAttributes() {
415         if (SchemaElements.ATTRIBUTE.getName().equals(e.getLocalName())) {
416         if (isAttributeDefined(SchemaAttributes.REF)) {
417         setNewComponent(new AttributeReferenceImpl(model,e));
418         } else {
419         setNewComponent(new LocalAttributeImpl(model,e));
420         }
421         } else if (SchemaElements.ATTRIBUTE_GROUP.getName().equals(
422             e.getLocalName())) {
423             setNewComponent(new AttributeGroupReferenceImpl(model,e));
424         } else if (SchemaElements.ANY_ATTRIBUTE.getName().equals(
425             e.getLocalName())) {
426             setNewComponent(new AnyAttributeImpl(model,e));
427         }
428     }
429     
430     private void allComplexType() {
431         recognizeAttributes();
432         recognizeGroupAllChoiceSequence();
433         if (SchemaElements.SIMPLE_CONTENT.getName().equals(e.getLocalName())) {
434             setNewComponent(new SimpleContentImpl(model,e));
435         } else if (SchemaElements.COMPLEX_CONTENT.getName().equals(e.getLocalName())) {
436             setNewComponent(new ComplexContentImpl(model,e));
437         }
438     }
439     
440     private boolean isAttributeDefined(SchemaAttributes attribute) {
441     return e.getAttribute(attribute.getName()) != null;
442     }
443     
444     private void createLocalElement() {
445     if (isAttributeDefined(SchemaAttributes.REF)) {
446         setNewComponent(new ElementReferenceImpl(model,e));
447     } else {
448         setNewComponent(new LocalElementImpl(model,e));
449     }
450     }
451     
452     private void recognizeGroupAnyChoiceSequenceElement() {
453         if (SchemaElements.ELEMENT.getName().equals(e.getLocalName())) {
454             createLocalElement();
455         } else {
456             recognizeGroupChoiceSequenceAny();
457         }
458     }
459     
460     private void recognizeGroupAllChoiceSequence() {
461         if (SchemaElements.ALL.getName().equals(e.getLocalName())) {
462             setNewComponent(new AllImpl(model,e));
463         } else {
464             recognizeGroupChoiceSequence();
465         }
466     }
467     
468     private void allElement() {
469         if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
470             setNewComponent(new LocalSimpleTypeImpl(model,e));
471             
472         } else if (SchemaElements.COMPLEX_TYPE.getName().equals(e.getLocalName())) {
473             setNewComponent(new LocalComplexTypeImpl(model,e));
474             
475         } else if (SchemaElements.UNIQUE.getName().equals(e.getLocalName())) {
476             setNewComponent(new UniqueImpl(model,e));
477             
478         } else if (SchemaElements.KEY.getName().equals(e.getLocalName())) {
479             setNewComponent(new KeyImpl(model,e));
480             
481         } else if (SchemaElements.KEYREF.getName().equals(e.getLocalName())) {
482             setNewComponent(new KeyRefImpl(model,e));
483         }
484     }
485     
486     private void allChoice() {
487         if (SchemaElements.ELEMENT.getName().equals(e.getLocalName())) {
488             createLocalElement();
489         } else {
490             recognizeGroupChoiceSequenceAny();
491         }
492     }
493     
494     
495     
496     private void recognizeGroupChoiceSequenceAny() {
497         if (SchemaElements.ANY.getName().equals(e.getLocalName())) {
498             setNewComponent(new AnyImpl(model,e));
499         } else {
500             recognizeGroupChoiceSequence();
501         }
502     }
503     
504     private void recognizeGroupChoiceSequence() {
505         if (SchemaElements.GROUP.getName().equals(e.getLocalName())) {
506             setNewComponent(new GroupReferenceImpl(model,e));
507             
508         } else if (SchemaElements.CHOICE.getName().equals(e.getLocalName())) {
509             setNewComponent(new ChoiceImpl(model,e));
510             
511         } else if (SchemaElements.SEQUENCE.getName().equals(e.getLocalName())) {
512             setNewComponent(new SequenceImpl(model,e));
513             
514         }
515     }
516     
517     private void recognizeInlineSimpleType() {
518         if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
519             setNewComponent(new LocalSimpleTypeImpl(model,e));
520         }
521     }
522     
523     private void allSimpleType() {
524         if (SchemaElements.RESTRICTION.getName().equals(e.getLocalName())) {
525             setNewComponent(new SimpleTypeRestrictionImpl(model,e));
526             
527         } else if (SchemaElements.LIST.getName().equals(e.getLocalName())) {
528             setNewComponent(new ListImpl(model,e));
529             
530         } else if (SchemaElements.UNION.getName().equals(e.getLocalName())) {
531             setNewComponent(new UnionImpl(model,e));
532             
533         }
534     }
535     
536     private void allCreation() {
537         if (SchemaElements.ELEMENT.getName().equals(e.getLocalName())) {
538             if (isAttributeDefined(SchemaAttributes.REF)) {
539         setNewComponent(new ElementReferenceImpl(model,e));
540         } else {
541         setNewComponent(new LocalElementImpl(model,e));
542         }
543         }
544     }
545     
546     private void setNewComponent(SchemaComponent c) {
547         newCommonSchemaComponent = c;
548     }
549     
550     private SchemaComponent newCommonSchemaComponent;
551     private org.w3c.dom.Element JavaDoc e;
552     private SchemaModelImpl model;
553 }
554
Popular Tags