2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
43
* AdvancedSchemaComponentCreator.java
45
* Created on April 20, 2006, 5:05 PM
49
package org.netbeans.modules.xml.schema.ui.nodes.categorized.newtype;
51
import java.util.Collection;
52
import java.util.HashSet;
53
import org.netbeans.modules.xml.schema.model.AnyAttribute;
54
import org.netbeans.modules.xml.schema.model.Attribute;
55
import org.netbeans.modules.xml.schema.model.AttributeGroupReference;
56
import org.netbeans.modules.xml.schema.model.AttributeReference;
57
import org.netbeans.modules.xml.schema.model.ComplexContent;
58
import org.netbeans.modules.xml.schema.model.ComplexContentRestriction;
59
import org.netbeans.modules.xml.schema.model.ComplexExtension;
60
import org.netbeans.modules.xml.schema.model.ComplexExtensionDefinition;
61
import org.netbeans.modules.xml.schema.model.ComplexTypeDefinition;
62
import org.netbeans.modules.xml.schema.model.ElementReference;
63
import org.netbeans.modules.xml.schema.model.Enumeration;
64
import org.netbeans.modules.xml.schema.model.Field;
65
import org.netbeans.modules.xml.schema.model.GlobalAttribute;
66
import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
67
import org.netbeans.modules.xml.schema.model.GlobalComplexType;
68
import org.netbeans.modules.xml.schema.model.GlobalElement;
69
import org.netbeans.modules.xml.schema.model.GlobalGroup;
70
import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
71
import org.netbeans.modules.xml.schema.model.GroupReference;
72
import org.netbeans.modules.xml.schema.model.Import;
73
import org.netbeans.modules.xml.schema.model.Include;
74
import org.netbeans.modules.xml.schema.model.Redefine;
75
import org.netbeans.modules.xml.schema.model.Key;
76
import org.netbeans.modules.xml.schema.model.KeyRef;
77
import org.netbeans.modules.xml.schema.model.LocalAttribute;
78
import org.netbeans.modules.xml.schema.model.LocalComplexType;
79
import org.netbeans.modules.xml.schema.model.LocalElement;
80
import org.netbeans.modules.xml.schema.model.LocalGroupDefinition;
81
import org.netbeans.modules.xml.schema.model.LocalSimpleType;
82
import org.netbeans.modules.xml.schema.model.LocalType;
83
import org.netbeans.modules.xml.schema.model.SchemaComponent;
84
import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
85
import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
86
import org.netbeans.modules.xml.schema.model.Selector;
87
import org.netbeans.modules.xml.schema.model.Sequence;
88
import org.netbeans.modules.xml.schema.model.SimpleContent;
89
import org.netbeans.modules.xml.schema.model.SimpleContentRestriction;
90
import org.netbeans.modules.xml.schema.model.SimpleExtension;
91
import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
92
import org.netbeans.modules.xml.schema.model.Unique;
93
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedGlobalAttributeCustomizer;
94
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedGlobalElementCustomizer;
95
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedLocalAttributeCustomizer;
96
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.AdvancedLocalElementCustomizer;
97
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ComplexTypeCustomizer;
98
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ImportCustomizer;
99
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.IncludeCustomizer;
100
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.RedefineCustomizer;
101
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.SimpleTypeCustomizer;
102
import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ReferenceCustomizer;
103
import org.netbeans.modules.xml.xam.Nameable;
104
import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
107
* This is advanced creator which creates templates for some components.
111
public class AdvancedSchemaComponentCreator extends SchemaComponentCreator {
112
/** Creates a new instance of AdvancedSchemaComponentCreator */
113
public AdvancedSchemaComponentCreator() {
116
public void visit(ElementReference le) {
118
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
119
setCustomizer(new ReferenceCustomizer<ElementReference>(
120
SchemaComponentReference.create(le),getParent()));
124
public void visit(AttributeReference la) {
126
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
127
setCustomizer(new ReferenceCustomizer<AttributeReference>(
128
SchemaComponentReference.create(la),getParent()));
132
public void visit(GroupReference gr) {
134
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
135
setCustomizer(new ReferenceCustomizer<GroupReference>(
136
SchemaComponentReference.create(gr),getParent()));
140
public void visit(AttributeGroupReference agr) {
142
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
143
setCustomizer(new ReferenceCustomizer<AttributeGroupReference>(
144
SchemaComponentReference.create(agr),getParent()));
148
public void visit(Import imp) {
150
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
151
setCustomizer(new ImportCustomizer(imp));
155
public void visit(Include include) {
156
super.visit(include);
157
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
158
setCustomizer(new IncludeCustomizer(include));
162
public void visit(Redefine redefine) {
163
super.visit(redefine);
164
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
165
setCustomizer(new RedefineCustomizer(redefine));
169
public void visit(LocalSimpleType lst) {
171
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
172
SchemaComponent parent = getParent();
173
SimpleTypeRestriction str = getParent().getModel().getFactory().
174
createSimpleTypeRestriction();
175
lst.setDefinition(str);
178
setCustomizer(new SimpleTypeCustomizer<LocalSimpleType>(
179
SchemaComponentReference.create(lst)
180
,parent, getStringType()));
184
public void visit(GlobalSimpleType gst) {
186
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
187
updateName(gst,"newSimpleType"); //TODO FIX hardcoding
188
SchemaComponent parent = getParent();
189
SimpleTypeRestriction str = getParent().getModel().getFactory().
190
createSimpleTypeRestriction();
191
gst.setDefinition(str);
194
setCustomizer(new SimpleTypeCustomizer<GlobalSimpleType>(
195
SchemaComponentReference.create(gst),
196
parent, getStringType()));
200
public void visit(SimpleTypeRestriction str) {
202
if(getOperation() == Operation.ADD || getOperation() == Operation.SHOW_CUSTOMIZER) {
203
// set type to primitive string
204
NamedComponentReference<GlobalSimpleType> stringType =
205
createStringTypeReference(str);
206
if (stringType != null) {
207
str.setBase(stringType);
212
public void visit(LocalComplexType lct) {
213
if(getOperation() == Operation.VERIFY_ADD) {
214
// exclusivity of ComplexTypeDefinition
215
if(getChild() instanceof ComplexTypeDefinition) {
216
if(lct.getDefinition()!=null)
217
setAddAllowed(false);
219
// mutual exclusive attributes and Complex/SimpleContents
220
if(getChild() instanceof AnyAttribute ||
221
getChild() instanceof Attribute ||
222
getChild() instanceof AttributeGroupReference) {
223
if(lct.getDefinition() instanceof ComplexContent ||
224
lct.getDefinition() instanceof SimpleContent) {
225
setAddAllowed(false);
228
// exclusivity of AnyAttribute
229
if(getChild() instanceof AnyAttribute) {
230
if(lct.getAnyAttribute()!=null)
231
setAddAllowed(false);
236
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
237
Sequence sequence = getParent().getModel().getFactory().createSequence();
238
lct.setDefinition(sequence);
239
setCustomizer(new ComplexTypeCustomizer<LocalComplexType>(
240
SchemaComponentReference.create(lct),getParent()));
244
public void visit(GlobalComplexType gct) {
245
if(getOperation() == Operation.VERIFY_ADD) {
246
// exclusivity of ComplexTypeDefinition
247
if(getChild() instanceof ComplexTypeDefinition) {
248
if(gct.getDefinition()!=null)
249
setAddAllowed(false);
251
// mutual exclusive attributes and Complex/SimpleContents
252
if(getChild() instanceof AnyAttribute ||
253
getChild() instanceof Attribute ||
254
getChild() instanceof AttributeGroupReference) {
255
if(gct.getDefinition() instanceof ComplexContent ||
256
gct.getDefinition() instanceof SimpleContent) {
257
setAddAllowed(false);
260
// anyAttribute can be 0 or 1
261
if(getChild() instanceof AnyAttribute) {
262
if(gct.getAnyAttribute()!=null)
263
setAddAllowed(false);
273
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
274
updateName(gct,"newComplexType"); //TODO FIX hardcoding
275
Sequence sequence = getParent().getModel().getFactory().createSequence();
276
gct.setDefinition(sequence);
277
setCustomizer(new ComplexTypeCustomizer<GlobalComplexType>(
278
SchemaComponentReference.create(gct),getParent()));
282
public void visit(ComplexContentRestriction ccr) {
283
if(getOperation() == Operation.VERIFY_ADD) {
284
// exclusivity of ComplexTypeDefinition
285
if(getChild() instanceof ComplexTypeDefinition) {
286
if(ccr.getDefinition()!=null)
287
setAddAllowed(false);
289
// anyAttribute can be 0 or 1
290
if(getChild() instanceof AnyAttribute) {
291
if(ccr.getAnyAttribute()!=null)
292
setAddAllowed(false);
298
public void visit(ComplexExtension ce) {
299
if(getOperation() == Operation.VERIFY_ADD) {
300
// exclusivity of ComplexExtensionDefinition
301
if(getChild() instanceof ComplexExtensionDefinition) {
302
if(ce.getLocalDefinition()!=null)
303
setAddAllowed(false);
305
// anyAttribute can be 0 or 1
306
if(getChild() instanceof AnyAttribute) {
307
if(ce.getAnyAttribute()!=null)
308
setAddAllowed(false);
314
public void visit(SimpleContentRestriction scr) {
315
if(getOperation() == Operation.VERIFY_ADD) {
316
// anyAttribute can be 0 or 1
317
if(getChild() instanceof AnyAttribute) {
318
if(scr.getAnyAttribute()!=null)
319
setAddAllowed(false);
325
public void visit(SimpleExtension se) {
326
if(getOperation() == Operation.VERIFY_ADD) {
327
// anyAttribute can be 0 or 1
328
if(getChild() instanceof AnyAttribute) {
329
if(se.getAnyAttribute()!=null)
330
setAddAllowed(false);
336
public void visit(GlobalAttributeGroup gag) {
337
if(getOperation() == Operation.VERIFY_ADD) {
338
// anyAttribute can be 0 or 1
339
if(getChild() instanceof AnyAttribute) {
340
if(gag.getAnyAttribute()!=null)
341
setAddAllowed(false);
345
if(getOperation() == Operation.ADD) {
346
updateName(gag,"newAttributeGroup"); //TODO FIX hardcoding
350
public void visit(LocalElement le) {
351
if(getOperation() == Operation.VERIFY_ADD) {
352
// exclusivity of type definition
353
if(getChild() instanceof LocalType) {
354
if(le.getType()!=null||le.getInlineType()!=null)
355
setAddAllowed(false);
366
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
367
updateName(le,"newElement"); //TODO FIX hardcoding
368
LocalComplexType lct = getParent().getModel().getFactory().createLocalComplexType();
369
le.setInlineType(lct);
370
Sequence sequence = getParent().getModel().getFactory().createSequence();
371
lct.setDefinition(sequence);
372
setCustomizer(new AdvancedLocalElementCustomizer(
373
SchemaComponentReference.create(le), getParent()));
377
public void visit(GlobalElement ge) {
378
if(getOperation() == Operation.VERIFY_ADD) {
379
// exclusivity of type definition
380
if(getChild() instanceof LocalType) {
381
if(ge.getType()!=null||ge.getInlineType()!=null)
382
setAddAllowed(false);
387
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
388
updateName(ge,"newElement"); //TODO FIX hardcoding
389
LocalComplexType lct = getParent().getModel().getFactory().createLocalComplexType();
390
ge.setInlineType(lct);
391
Sequence sequence = getParent().getModel().getFactory().createSequence();
392
lct.setDefinition(sequence);
393
setCustomizer(new AdvancedGlobalElementCustomizer(
394
SchemaComponentReference.create(ge), getParent()));
398
public void visit(GlobalAttribute ga) {
399
if(getOperation() == Operation.VERIFY_ADD) {
400
// exclusivity of type definition
401
if(getChild() instanceof LocalType) {
402
if(ga.getType()!=null||ga.getInlineType()!=null)
403
setAddAllowed(false);
407
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
408
updateName(ga,"newAttribute"); //TODO FIX hardcoding
409
// set type to primitive string
410
NamedComponentReference<GlobalSimpleType> stringType =
411
createStringTypeReference(ga);
412
if (stringType != null) {
413
ga.setType(stringType);
415
setCustomizer(new AdvancedGlobalAttributeCustomizer(
416
SchemaComponentReference.create(ga), getParent(), getStringType()));
420
public void visit(LocalAttribute la) {
421
if(getOperation() == Operation.VERIFY_ADD) {
422
// exclusivity of type definition
423
if(getChild() instanceof LocalType) {
424
if(la.getType()!=null||la.getInlineType()!=null)
425
setAddAllowed(false);
429
if(getOperation() == Operation.SHOW_CUSTOMIZER) {
430
updateName(la,"newAttribute"); //TODO FIX hardcoding
431
// set type to primitive string
432
NamedComponentReference<GlobalSimpleType> stringType =
433
createStringTypeReference(la);
434
if (stringType != null) {
435
la.setType(stringType);
437
setCustomizer(new AdvancedLocalAttributeCustomizer(
438
SchemaComponentReference.create(la), getParent(), getStringType()));
442
public void visit(GlobalGroup gd) {
443
if(getOperation() == Operation.VERIFY_ADD) {
444
// exclusivity of group definition
445
if(getChild() instanceof LocalGroupDefinition) {
446
if(gd.getDefinition()!=null)
447
setAddAllowed(false);
451
if(getOperation() == Operation.ADD) {
452
updateName(gd,"newGroup"); //TODO FIX hardcoding
453
Sequence s = getParent().getModel().getFactory().createSequence();
459
public void visit(Unique u) {
460
if(getOperation() == Operation.VERIFY_ADD) {
462
if(getChild() instanceof Selector) {
463
if(u.getSelector()!=null)
464
setAddAllowed(false);
468
if(getOperation() == Operation.ADD) {
469
updateName(u,"newUnique");//TODO FIX hardcoding
471
Selector s = getParent().getModel().getFactory().createSelector();
473
Field f = getParent().getModel().getFactory().createField();
478
public void visit(Key key) {
479
if(getOperation() == Operation.VERIFY_ADD) {
481
if(getChild() instanceof Selector) {
482
if(key.getSelector()!=null)
483
setAddAllowed(false);
487
if(getOperation() == Operation.ADD) {
488
updateName(key,"newKey");//TODO FIX hardcoding
490
Selector s = getParent().getModel().getFactory().createSelector();
492
Field f = getParent().getModel().getFactory().createField();
497
public void visit(KeyRef kr) {
498
if(getOperation() == Operation.VERIFY_ADD) {
500
if(getChild() instanceof Selector) {
501
if(kr.getSelector()!=null)
502
setAddAllowed(false);
506
if(getOperation() == Operation.ADD) {
507
updateName(kr,"newKeyRef");//TODO FIX hardcoding
509
Selector s = getParent().getModel().getFactory().createSelector();
511
Field f = getParent().getModel().getFactory().createField();
516
public void visit(Enumeration e) {
518
if(getOperation() == Operation.ADD) {
523
public void visit(Selector s) {
525
if(getOperation() == Operation.ADD) {
530
public void visit(Field f) {
532
if(getOperation() == Operation.ADD) {
537
private void updateName(final Nameable<SchemaComponent> component,
538
final String preferedName) {
539
String name = preferedName;
540
HashSet<String> nameSet = new HashSet<String>();
542
for(SchemaComponent child :getParent().getChildren(
543
((SchemaComponent)component).getComponentType())) {
544
takenName = ((Nameable)child).getName();
545
if(takenName!=null) nameSet.add(takenName);
548
if(nameSet.size() <Integer.MAX_VALUE) {
549
while(nameSet.contains(name)) {
550
name = preferedName.concat(""+cnt++);
553
component.setName(name);
556
// creates primitive string global reference
557
private NamedComponentReference<GlobalSimpleType>
558
createStringTypeReference(SchemaComponent component) {
559
GlobalSimpleType stringType = getStringType();
561
return getParent().getModel().getFactory().createGlobalReference
562
(stringType, GlobalSimpleType.class, component);
566
private GlobalSimpleType getStringType() {
567
Collection<GlobalSimpleType> types = SchemaModelFactory.getDefault().
568
getPrimitiveTypesModel().getSchema().getSimpleTypes();
569
for (GlobalSimpleType type : types) {
570
if (type.getName().equals("string")) {