KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > categorized > newtype > AdvancedSchemaComponentCreator


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
20 /*
21  * AdvancedSchemaComponentCreator.java
22  *
23  * Created on April 20, 2006, 5:05 PM
24  *
25  */

26
27 package org.netbeans.modules.xml.schema.ui.nodes.categorized.newtype;
28
29 import java.util.Collection JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import org.netbeans.modules.xml.schema.model.AnyAttribute;
32 import org.netbeans.modules.xml.schema.model.Attribute;
33 import org.netbeans.modules.xml.schema.model.AttributeGroupReference;
34 import org.netbeans.modules.xml.schema.model.AttributeReference;
35 import org.netbeans.modules.xml.schema.model.ComplexContent;
36 import org.netbeans.modules.xml.schema.model.ComplexContentRestriction;
37 import org.netbeans.modules.xml.schema.model.ComplexExtension;
38 import org.netbeans.modules.xml.schema.model.ComplexExtensionDefinition;
39 import org.netbeans.modules.xml.schema.model.ComplexTypeDefinition;
40 import org.netbeans.modules.xml.schema.model.ElementReference;
41 import org.netbeans.modules.xml.schema.model.Enumeration;
42 import org.netbeans.modules.xml.schema.model.Field;
43 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
44 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
45 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
46 import org.netbeans.modules.xml.schema.model.GlobalElement;
47 import org.netbeans.modules.xml.schema.model.GlobalGroup;
48 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
49 import org.netbeans.modules.xml.schema.model.GroupReference;
50 import org.netbeans.modules.xml.schema.model.Import;
51 import org.netbeans.modules.xml.schema.model.Include;
52 import org.netbeans.modules.xml.schema.model.Redefine;
53 import org.netbeans.modules.xml.schema.model.Key;
54 import org.netbeans.modules.xml.schema.model.KeyRef;
55 import org.netbeans.modules.xml.schema.model.LocalAttribute;
56 import org.netbeans.modules.xml.schema.model.LocalComplexType;
57 import org.netbeans.modules.xml.schema.model.LocalElement;
58 import org.netbeans.modules.xml.schema.model.LocalGroupDefinition;
59 import org.netbeans.modules.xml.schema.model.LocalSimpleType;
60 import org.netbeans.modules.xml.schema.model.LocalType;
61 import org.netbeans.modules.xml.schema.model.SchemaComponent;
62 import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
63 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
64 import org.netbeans.modules.xml.schema.model.Selector;
65 import org.netbeans.modules.xml.schema.model.Sequence;
66 import org.netbeans.modules.xml.schema.model.SimpleContent;
67 import org.netbeans.modules.xml.schema.model.SimpleContentRestriction;
68 import org.netbeans.modules.xml.schema.model.SimpleExtension;
69 import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
70 import org.netbeans.modules.xml.schema.model.Unique;
71 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedGlobalAttributeCustomizer;
72 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedGlobalElementCustomizer;
73 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedLocalAttributeCustomizer;
74 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedLocalElementCustomizer;
75 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ComplexTypeCustomizer;
76 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ImportCustomizer;
77 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.IncludeCustomizer;
78 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.RedefineCustomizer;
79 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.SimpleTypeCustomizer;
80 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ReferenceCustomizer;
81 import org.netbeans.modules.xml.xam.Nameable;
82 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
83
84 /**
85  * This is advanced creator which creates templates for some components.
86  *
87  * @author Ajit Bhate
88  */

89 public class AdvancedSchemaComponentCreator extends SchemaComponentCreator {
90     /** Creates a new instance of AdvancedSchemaComponentCreator */
91     public AdvancedSchemaComponentCreator() {
92     }
93     
94     public void visit(ElementReference le) {
95         super.visit(le);
96         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
97             setCustomizer(new ReferenceCustomizer<ElementReference>(
98                     SchemaComponentReference.create(le),getParent()));
99         }
100     }
101     
102     public void visit(AttributeReference la) {
103         super.visit(la);
104         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
105             setCustomizer(new ReferenceCustomizer<AttributeReference>(
106                     SchemaComponentReference.create(la),getParent()));
107         }
108     }
109     
110     public void visit(GroupReference gr) {
111         super.visit(gr);
112         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
113             setCustomizer(new ReferenceCustomizer<GroupReference>(
114                     SchemaComponentReference.create(gr),getParent()));
115         }
116     }
117     
118     public void visit(AttributeGroupReference agr) {
119         super.visit(agr);
120         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
121             setCustomizer(new ReferenceCustomizer<AttributeGroupReference>(
122                     SchemaComponentReference.create(agr),getParent()));
123         }
124     }
125     
126     public void visit(Import imp) {
127         super.visit(imp);
128         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
129             setCustomizer(new ImportCustomizer(imp));
130         }
131     }
132     
133     public void visit(Include include) {
134         super.visit(include);
135         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
136             setCustomizer(new IncludeCustomizer(include));
137         }
138     }
139     
140     public void visit(Redefine redefine) {
141         super.visit(redefine);
142         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
143             setCustomizer(new RedefineCustomizer(redefine));
144         }
145     }
146     
147     public void visit(LocalSimpleType lst) {
148         super.visit(lst);
149         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
150             SchemaComponent parent = getParent();
151             SimpleTypeRestriction str = getParent().getModel().getFactory().
152                     createSimpleTypeRestriction();
153             lst.setDefinition(str);
154             setParent(lst);
155             visit(str);
156             setCustomizer(new SimpleTypeCustomizer<LocalSimpleType>(
157                     SchemaComponentReference.create(lst)
158                     ,parent, getStringType()));
159         }
160     }
161     
162     public void visit(GlobalSimpleType gst) {
163         super.visit(gst);
164         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
165             updateName(gst,"newSimpleType"); //TODO FIX hardcoding
166
SchemaComponent parent = getParent();
167             SimpleTypeRestriction str = getParent().getModel().getFactory().
168                     createSimpleTypeRestriction();
169             gst.setDefinition(str);
170             setParent(gst);
171             visit(str);
172             setCustomizer(new SimpleTypeCustomizer<GlobalSimpleType>(
173                     SchemaComponentReference.create(gst),
174                     parent, getStringType()));
175         }
176     }
177     
178     public void visit(SimpleTypeRestriction str) {
179         super.visit(str);
180         if(getOperation() == Operation.ADD || getOperation() == Operation.SHOW_CUSTOMIZER) {
181             // set type to primitive string
182
NamedComponentReference<GlobalSimpleType> stringType =
183                     createStringTypeReference(str);
184             if (stringType != null) {
185                 str.setBase(stringType);
186             }
187         }
188     }
189     
190     public void visit(LocalComplexType lct) {
191         if(getOperation() == Operation.VERIFY_ADD) {
192             // exclusivity of ComplexTypeDefinition
193
if(getChild() instanceof ComplexTypeDefinition) {
194                 if(lct.getDefinition()!=null)
195                     setAddAllowed(false);
196             }
197             // mutual exclusive attributes and Complex/SimpleContents
198
if(getChild() instanceof AnyAttribute ||
199                     getChild() instanceof Attribute ||
200                     getChild() instanceof AttributeGroupReference) {
201                 if(lct.getDefinition() instanceof ComplexContent ||
202                         lct.getDefinition() instanceof SimpleContent) {
203                     setAddAllowed(false);
204                 }
205             }
206             // exclusivity of AnyAttribute
207
if(getChild() instanceof AnyAttribute) {
208                 if(lct.getAnyAttribute()!=null)
209                     setAddAllowed(false);
210             }
211             setVisitLevels(4);
212         }
213         super.visit(lct);
214         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
215             Sequence sequence = getParent().getModel().getFactory().createSequence();
216             lct.setDefinition(sequence);
217             setCustomizer(new ComplexTypeCustomizer<LocalComplexType>(
218                     SchemaComponentReference.create(lct),getParent()));
219         }
220     }
221     
222     public void visit(GlobalComplexType gct) {
223         if(getOperation() == Operation.VERIFY_ADD) {
224             // exclusivity of ComplexTypeDefinition
225
if(getChild() instanceof ComplexTypeDefinition) {
226                 if(gct.getDefinition()!=null)
227                     setAddAllowed(false);
228             }
229             // mutual exclusive attributes and Complex/SimpleContents
230
if(getChild() instanceof AnyAttribute ||
231                     getChild() instanceof Attribute ||
232                     getChild() instanceof AttributeGroupReference) {
233                 if(gct.getDefinition() instanceof ComplexContent ||
234                         gct.getDefinition() instanceof SimpleContent) {
235                     setAddAllowed(false);
236                 }
237             }
238             // anyAttribute can be 0 or 1
239
if(getChild() instanceof AnyAttribute) {
240                 if(gct.getAnyAttribute()!=null)
241                     setAddAllowed(false);
242             }
243             // we will look for
244
//1 complextype
245
//2 complexcontent
246
//3 extension
247
//4 sequence
248
setVisitLevels(4);
249         }
250         super.visit(gct);
251         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
252             updateName(gct,"newComplexType"); //TODO FIX hardcoding
253
Sequence sequence = getParent().getModel().getFactory().createSequence();
254             gct.setDefinition(sequence);
255             setCustomizer(new ComplexTypeCustomizer<GlobalComplexType>(
256                     SchemaComponentReference.create(gct),getParent()));
257         }
258     }
259     
260     public void visit(ComplexContentRestriction ccr) {
261         if(getOperation() == Operation.VERIFY_ADD) {
262             // exclusivity of ComplexTypeDefinition
263
if(getChild() instanceof ComplexTypeDefinition) {
264                 if(ccr.getDefinition()!=null)
265                     setAddAllowed(false);
266             }
267             // anyAttribute can be 0 or 1
268
if(getChild() instanceof AnyAttribute) {
269                 if(ccr.getAnyAttribute()!=null)
270                     setAddAllowed(false);
271             }
272         }
273         super.visit(ccr);
274     }
275     
276     public void visit(ComplexExtension ce) {
277         if(getOperation() == Operation.VERIFY_ADD) {
278             // exclusivity of ComplexExtensionDefinition
279
if(getChild() instanceof ComplexExtensionDefinition) {
280                 if(ce.getLocalDefinition()!=null)
281                     setAddAllowed(false);
282             }
283             // anyAttribute can be 0 or 1
284
if(getChild() instanceof AnyAttribute) {
285                 if(ce.getAnyAttribute()!=null)
286                     setAddAllowed(false);
287             }
288         }
289         super.visit(ce);
290     }
291     
292     public void visit(SimpleContentRestriction scr) {
293         if(getOperation() == Operation.VERIFY_ADD) {
294             // anyAttribute can be 0 or 1
295
if(getChild() instanceof AnyAttribute) {
296                 if(scr.getAnyAttribute()!=null)
297                     setAddAllowed(false);
298             }
299         }
300         super.visit(scr);
301     }
302     
303     public void visit(SimpleExtension se) {
304         if(getOperation() == Operation.VERIFY_ADD) {
305             // anyAttribute can be 0 or 1
306
if(getChild() instanceof AnyAttribute) {
307                 if(se.getAnyAttribute()!=null)
308                     setAddAllowed(false);
309             }
310         }
311         super.visit(se);
312     }
313     
314     public void visit(GlobalAttributeGroup gag) {
315         if(getOperation() == Operation.VERIFY_ADD) {
316             // anyAttribute can be 0 or 1
317
if(getChild() instanceof AnyAttribute) {
318                 if(gag.getAnyAttribute()!=null)
319                     setAddAllowed(false);
320             }
321         }
322         super.visit(gag);
323         if(getOperation() == Operation.ADD) {
324             updateName(gag,"newAttributeGroup"); //TODO FIX hardcoding
325
}
326     }
327     
328     public void visit(LocalElement le) {
329         if(getOperation() == Operation.VERIFY_ADD) {
330             // exclusivity of type definition
331
if(getChild() instanceof LocalType) {
332                 if(le.getType()!=null||le.getInlineType()!=null)
333                     setAddAllowed(false);
334             }
335             // we will look for
336
//1 element
337
//2 complextype
338
//3 complexcontent
339
//4 extension
340
//5 sequence
341
setVisitLevels(5);
342         }
343         super.visit(le);
344         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
345             updateName(le,"newElement"); //TODO FIX hardcoding
346
LocalComplexType lct = getParent().getModel().getFactory().createLocalComplexType();
347             le.setInlineType(lct);
348             Sequence sequence = getParent().getModel().getFactory().createSequence();
349             lct.setDefinition(sequence);
350             setCustomizer(new AdvancedLocalElementCustomizer(
351                     SchemaComponentReference.create(le), getParent()));
352         }
353     }
354     
355     public void visit(GlobalElement ge) {
356         if(getOperation() == Operation.VERIFY_ADD) {
357             // exclusivity of type definition
358
if(getChild() instanceof LocalType) {
359                 if(ge.getType()!=null||ge.getInlineType()!=null)
360                     setAddAllowed(false);
361             }
362             setVisitLevels(5);
363         }
364         super.visit(ge);
365         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
366             updateName(ge,"newElement"); //TODO FIX hardcoding
367
LocalComplexType lct = getParent().getModel().getFactory().createLocalComplexType();
368             ge.setInlineType(lct);
369             Sequence sequence = getParent().getModel().getFactory().createSequence();
370             lct.setDefinition(sequence);
371             setCustomizer(new AdvancedGlobalElementCustomizer(
372                     SchemaComponentReference.create(ge), getParent()));
373         }
374     }
375     
376     public void visit(GlobalAttribute ga) {
377         if(getOperation() == Operation.VERIFY_ADD) {
378             // exclusivity of type definition
379
if(getChild() instanceof LocalType) {
380                 if(ga.getType()!=null||ga.getInlineType()!=null)
381                     setAddAllowed(false);
382             }
383         }
384         super.visit(ga);
385         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
386             updateName(ga,"newAttribute"); //TODO FIX hardcoding
387
// set type to primitive string
388
NamedComponentReference<GlobalSimpleType> stringType =
389                     createStringTypeReference(ga);
390             if (stringType != null) {
391                 ga.setType(stringType);
392             }
393             setCustomizer(new AdvancedGlobalAttributeCustomizer(
394                     SchemaComponentReference.create(ga), getParent(), getStringType()));
395         }
396     }
397     
398     public void visit(LocalAttribute la) {
399         if(getOperation() == Operation.VERIFY_ADD) {
400             // exclusivity of type definition
401
if(getChild() instanceof LocalType) {
402                 if(la.getType()!=null||la.getInlineType()!=null)
403                     setAddAllowed(false);
404             }
405         }
406         super.visit(la);
407         if(getOperation() == Operation.SHOW_CUSTOMIZER) {
408             updateName(la,"newAttribute"); //TODO FIX hardcoding
409
// set type to primitive string
410
NamedComponentReference<GlobalSimpleType> stringType =
411                     createStringTypeReference(la);
412             if (stringType != null) {
413                 la.setType(stringType);
414             }
415             setCustomizer(new AdvancedLocalAttributeCustomizer(
416                     SchemaComponentReference.create(la), getParent(), getStringType()));
417         }
418     }
419     
420     public void visit(GlobalGroup gd) {
421         if(getOperation() == Operation.VERIFY_ADD) {
422             // exclusivity of group definition
423
if(getChild() instanceof LocalGroupDefinition) {
424                 if(gd.getDefinition()!=null)
425                     setAddAllowed(false);
426             }
427         }
428         super.visit(gd);
429         if(getOperation() == Operation.ADD) {
430             updateName(gd,"newGroup"); //TODO FIX hardcoding
431
Sequence s = getParent().getModel().getFactory().createSequence();
432             setParent(gd);
433             visit(s);
434         }
435     }
436     
437     public void visit(Unique u) {
438         if(getOperation() == Operation.VERIFY_ADD) {
439             // Selector can be 1
440
if(getChild() instanceof Selector) {
441                 if(u.getSelector()!=null)
442                     setAddAllowed(false);
443             }
444         }
445         super.visit(u);
446         if(getOperation() == Operation.ADD) {
447             updateName(u,"newUnique");//TODO FIX hardcoding
448
setParent(u);
449             Selector s = getParent().getModel().getFactory().createSelector();
450             visit(s);
451             Field f = getParent().getModel().getFactory().createField();
452             visit(f);
453         }
454     }
455     
456     public void visit(Key key) {
457         if(getOperation() == Operation.VERIFY_ADD) {
458             // Selector can be 1
459
if(getChild() instanceof Selector) {
460                 if(key.getSelector()!=null)
461                     setAddAllowed(false);
462             }
463         }
464         super.visit(key);
465         if(getOperation() == Operation.ADD) {
466             updateName(key,"newKey");//TODO FIX hardcoding
467
setParent(key);
468             Selector s = getParent().getModel().getFactory().createSelector();
469             visit(s);
470             Field f = getParent().getModel().getFactory().createField();
471             visit(f);
472         }
473     }
474     
475     public void visit(KeyRef kr) {
476         if(getOperation() == Operation.VERIFY_ADD) {
477             // Selector can be 1
478
if(getChild() instanceof Selector) {
479                 if(kr.getSelector()!=null)
480                     setAddAllowed(false);
481             }
482         }
483         super.visit(kr);
484         if(getOperation() == Operation.ADD) {
485             updateName(kr,"newKeyRef");//TODO FIX hardcoding
486
setParent(kr);
487             Selector s = getParent().getModel().getFactory().createSelector();
488             visit(s);
489             Field f = getParent().getModel().getFactory().createField();
490             visit(f);
491         }
492     }
493     
494     public void visit(Enumeration e) {
495         super.visit(e);
496         if(getOperation() == Operation.ADD) {
497             e.setValue("");
498         }
499     }
500     
501     public void visit(Selector s) {
502         super.visit(s);
503         if(getOperation() == Operation.ADD) {
504             s.setXPath("");
505         }
506     }
507     
508     public void visit(Field f) {
509         super.visit(f);
510         if(getOperation() == Operation.ADD) {
511             f.setXPath("");
512         }
513     }
514     
515     private void updateName(final Nameable<SchemaComponent> component,
516             final String JavaDoc preferedName) {
517         String JavaDoc name = preferedName;
518         HashSet JavaDoc<String JavaDoc> nameSet = new HashSet JavaDoc<String JavaDoc>();
519         String JavaDoc takenName;
520         for(SchemaComponent child :getParent().getChildren(
521                 ((SchemaComponent)component).getComponentType())) {
522             takenName = ((Nameable)child).getName();
523             if(takenName!=null) nameSet.add(takenName);
524         }
525         int cnt = 1;
526         if(nameSet.size() <Integer.MAX_VALUE) {
527             while(nameSet.contains(name)) {
528                 name = preferedName.concat(""+cnt++);
529             }
530         }
531         component.setName(name);
532     }
533     
534     // creates primitive string global reference
535
private NamedComponentReference<GlobalSimpleType>
536             createStringTypeReference(SchemaComponent component) {
537         GlobalSimpleType stringType = getStringType();
538         if(stringType!=null)
539             return getParent().getModel().getFactory().createGlobalReference
540                     (stringType, GlobalSimpleType.class, component);
541         return null;
542     }
543     
544     private GlobalSimpleType getStringType() {
545         Collection JavaDoc<GlobalSimpleType> types = SchemaModelFactory.getDefault().
546                 getPrimitiveTypesModel().getSchema().getSimpleTypes();
547         for (GlobalSimpleType type : types) {
548             if (type.getName().equals("string")) {
549                 return type;
550             }
551         }
552         return null;
553     }
554 }
555
Popular Tags