~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/schema/schemaui/src/org/netbeans/modules/xml/schema/ui/nodes/categorized/newtype/AdvancedSchemaComponentCreator.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
/*
 
43
 * AdvancedSchemaComponentCreator.java
 
44
 *
 
45
 * Created on April 20, 2006, 5:05 PM
 
46
 *
 
47
 */
 
48
 
 
49
package org.netbeans.modules.xml.schema.ui.nodes.categorized.newtype;
 
50
 
 
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;
 
105
 
 
106
/**
 
107
 * This is advanced creator which creates templates for some components.
 
108
 *
 
109
 * @author Ajit Bhate
 
110
 */
 
111
public class AdvancedSchemaComponentCreator extends SchemaComponentCreator {
 
112
    /** Creates a new instance of AdvancedSchemaComponentCreator */
 
113
    public AdvancedSchemaComponentCreator() {
 
114
    }
 
115
    
 
116
    public void visit(ElementReference le) {
 
117
        super.visit(le);
 
118
        if(getOperation() == Operation.SHOW_CUSTOMIZER) {
 
119
            setCustomizer(new ReferenceCustomizer<ElementReference>(
 
120
                    SchemaComponentReference.create(le),getParent()));
 
121
        }
 
122
    }
 
123
    
 
124
    public void visit(AttributeReference la) {
 
125
        super.visit(la);
 
126
        if(getOperation() == Operation.SHOW_CUSTOMIZER) {
 
127
            setCustomizer(new ReferenceCustomizer<AttributeReference>(
 
128
                    SchemaComponentReference.create(la),getParent()));
 
129
        }
 
130
    }
 
131
    
 
132
    public void visit(GroupReference gr) {
 
133
        super.visit(gr);
 
134
        if(getOperation() == Operation.SHOW_CUSTOMIZER) {
 
135
            setCustomizer(new ReferenceCustomizer<GroupReference>(
 
136
                    SchemaComponentReference.create(gr),getParent()));
 
137
        }
 
138
    }
 
139
    
 
140
    public void visit(AttributeGroupReference agr) {
 
141
        super.visit(agr);
 
142
        if(getOperation() == Operation.SHOW_CUSTOMIZER) {
 
143
            setCustomizer(new ReferenceCustomizer<AttributeGroupReference>(
 
144
                    SchemaComponentReference.create(agr),getParent()));
 
145
        }
 
146
    }
 
147
    
 
148
    public void visit(Import imp) {
 
149
        super.visit(imp);
 
150
        if(getOperation() == Operation.SHOW_CUSTOMIZER) {
 
151
            setCustomizer(new ImportCustomizer(imp));
 
152
        }
 
153
    }
 
154
    
 
155
    public void visit(Include include) {
 
156
        super.visit(include);
 
157
        if(getOperation() == Operation.SHOW_CUSTOMIZER) {
 
158
            setCustomizer(new IncludeCustomizer(include));
 
159
        }
 
160
    }
 
161
    
 
162
    public void visit(Redefine redefine) {
 
163
        super.visit(redefine);
 
164
        if(getOperation() == Operation.SHOW_CUSTOMIZER) {
 
165
            setCustomizer(new RedefineCustomizer(redefine));
 
166
        }
 
167
    }
 
168
    
 
169
    public void visit(LocalSimpleType lst) {
 
170
        super.visit(lst);
 
171
        if(getOperation() == Operation.SHOW_CUSTOMIZER) {
 
172
            SchemaComponent parent = getParent();
 
173
            SimpleTypeRestriction str = getParent().getModel().getFactory().
 
174
                    createSimpleTypeRestriction();
 
175
            lst.setDefinition(str);
 
176
            setParent(lst);
 
177
            visit(str);
 
178
            setCustomizer(new SimpleTypeCustomizer<LocalSimpleType>(
 
179
                    SchemaComponentReference.create(lst)
 
180
                    ,parent, getStringType()));
 
181
        }
 
182
    }
 
183
    
 
184
    public void visit(GlobalSimpleType gst) {
 
185
        super.visit(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);
 
192
            setParent(gst);
 
193
            visit(str);
 
194
            setCustomizer(new SimpleTypeCustomizer<GlobalSimpleType>(
 
195
                    SchemaComponentReference.create(gst),
 
196
                    parent, getStringType()));
 
197
        }
 
198
    }
 
199
    
 
200
    public void visit(SimpleTypeRestriction str) {
 
201
        super.visit(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);
 
208
            }
 
209
        }
 
210
    }
 
211
    
 
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);
 
218
            }
 
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);
 
226
                }
 
227
            }
 
228
            // exclusivity of AnyAttribute
 
229
            if(getChild() instanceof AnyAttribute) {
 
230
                if(lct.getAnyAttribute()!=null)
 
231
                    setAddAllowed(false);
 
232
            }
 
233
            setVisitLevels(4);
 
234
        }
 
235
        super.visit(lct);
 
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()));
 
241
        }
 
242
    }
 
243
    
 
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);
 
250
            }
 
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);
 
258
                }
 
259
            }
 
260
            // anyAttribute can be 0 or 1
 
261
            if(getChild() instanceof AnyAttribute) {
 
262
                if(gct.getAnyAttribute()!=null)
 
263
                    setAddAllowed(false);
 
264
            }
 
265
            // we will look for
 
266
            //1 complextype
 
267
            //2         complexcontent
 
268
            //3                 extension
 
269
            //4                         sequence
 
270
            setVisitLevels(4);
 
271
        }
 
272
        super.visit(gct);
 
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()));
 
279
        }
 
280
    }
 
281
    
 
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);
 
288
            }
 
289
            // anyAttribute can be 0 or 1
 
290
            if(getChild() instanceof AnyAttribute) {
 
291
                if(ccr.getAnyAttribute()!=null)
 
292
                    setAddAllowed(false);
 
293
            }
 
294
        }
 
295
        super.visit(ccr);
 
296
    }
 
297
    
 
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);
 
304
            }
 
305
            // anyAttribute can be 0 or 1
 
306
            if(getChild() instanceof AnyAttribute) {
 
307
                if(ce.getAnyAttribute()!=null)
 
308
                    setAddAllowed(false);
 
309
            }
 
310
        }
 
311
        super.visit(ce);
 
312
    }
 
313
    
 
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);
 
320
            }
 
321
        }
 
322
        super.visit(scr);
 
323
    }
 
324
    
 
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);
 
331
            }
 
332
        }
 
333
        super.visit(se);
 
334
    }
 
335
    
 
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);
 
342
            }
 
343
        }
 
344
        super.visit(gag);
 
345
        if(getOperation() == Operation.ADD) {
 
346
            updateName(gag,"newAttributeGroup"); //TODO FIX hardcoding
 
347
        }
 
348
    }
 
349
    
 
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);
 
356
            }
 
357
            // we will look for
 
358
            //1 element
 
359
            //2         complextype
 
360
            //3                 complexcontent
 
361
            //4                         extension
 
362
            //5                                 sequence
 
363
            setVisitLevels(5);
 
364
        }
 
365
        super.visit(le);
 
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()));
 
374
        }
 
375
    }
 
376
    
 
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);
 
383
            }
 
384
            setVisitLevels(5);
 
385
        }
 
386
        super.visit(ge);
 
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()));
 
395
        }
 
396
    }
 
397
    
 
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);
 
404
            }
 
405
        }
 
406
        super.visit(ga);
 
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);
 
414
            }
 
415
            setCustomizer(new AdvancedGlobalAttributeCustomizer(
 
416
                    SchemaComponentReference.create(ga), getParent(), getStringType()));
 
417
        }
 
418
    }
 
419
    
 
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);
 
426
            }
 
427
        }
 
428
        super.visit(la);
 
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);
 
436
            }
 
437
            setCustomizer(new AdvancedLocalAttributeCustomizer(
 
438
                    SchemaComponentReference.create(la), getParent(), getStringType()));
 
439
        }
 
440
    }
 
441
    
 
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);
 
448
            }
 
449
        }
 
450
        super.visit(gd);
 
451
        if(getOperation() == Operation.ADD) {
 
452
            updateName(gd,"newGroup"); //TODO FIX hardcoding
 
453
            Sequence s = getParent().getModel().getFactory().createSequence();
 
454
            setParent(gd);
 
455
            visit(s);
 
456
        }
 
457
    }
 
458
    
 
459
    public void visit(Unique u) {
 
460
        if(getOperation() == Operation.VERIFY_ADD) {
 
461
            // Selector can be 1
 
462
            if(getChild() instanceof Selector) {
 
463
                if(u.getSelector()!=null)
 
464
                    setAddAllowed(false);
 
465
            }
 
466
        }
 
467
        super.visit(u);
 
468
        if(getOperation() == Operation.ADD) {
 
469
            updateName(u,"newUnique");//TODO FIX hardcoding
 
470
            setParent(u);
 
471
            Selector s = getParent().getModel().getFactory().createSelector();
 
472
            visit(s);
 
473
            Field f = getParent().getModel().getFactory().createField();
 
474
            visit(f);
 
475
        }
 
476
    }
 
477
    
 
478
    public void visit(Key key) {
 
479
        if(getOperation() == Operation.VERIFY_ADD) {
 
480
            // Selector can be 1
 
481
            if(getChild() instanceof Selector) {
 
482
                if(key.getSelector()!=null)
 
483
                    setAddAllowed(false);
 
484
            }
 
485
        }
 
486
        super.visit(key);
 
487
        if(getOperation() == Operation.ADD) {
 
488
            updateName(key,"newKey");//TODO FIX hardcoding
 
489
            setParent(key);
 
490
            Selector s = getParent().getModel().getFactory().createSelector();
 
491
            visit(s);
 
492
            Field f = getParent().getModel().getFactory().createField();
 
493
            visit(f);
 
494
        }
 
495
    }
 
496
    
 
497
    public void visit(KeyRef kr) {
 
498
        if(getOperation() == Operation.VERIFY_ADD) {
 
499
            // Selector can be 1
 
500
            if(getChild() instanceof Selector) {
 
501
                if(kr.getSelector()!=null)
 
502
                    setAddAllowed(false);
 
503
            }
 
504
        }
 
505
        super.visit(kr);
 
506
        if(getOperation() == Operation.ADD) {
 
507
            updateName(kr,"newKeyRef");//TODO FIX hardcoding
 
508
            setParent(kr);
 
509
            Selector s = getParent().getModel().getFactory().createSelector();
 
510
            visit(s);
 
511
            Field f = getParent().getModel().getFactory().createField();
 
512
            visit(f);
 
513
        }
 
514
    }
 
515
    
 
516
    public void visit(Enumeration e) {
 
517
        super.visit(e);
 
518
        if(getOperation() == Operation.ADD) {
 
519
            e.setValue("");
 
520
        }
 
521
    }
 
522
    
 
523
    public void visit(Selector s) {
 
524
        super.visit(s);
 
525
        if(getOperation() == Operation.ADD) {
 
526
            s.setXPath("");
 
527
        }
 
528
    }
 
529
    
 
530
    public void visit(Field f) {
 
531
        super.visit(f);
 
532
        if(getOperation() == Operation.ADD) {
 
533
            f.setXPath("");
 
534
        }
 
535
    }
 
536
    
 
537
    private void updateName(final Nameable<SchemaComponent> component,
 
538
            final String preferedName) {
 
539
        String name = preferedName;
 
540
        HashSet<String> nameSet = new HashSet<String>();
 
541
        String takenName;
 
542
        for(SchemaComponent child :getParent().getChildren(
 
543
                ((SchemaComponent)component).getComponentType())) {
 
544
            takenName = ((Nameable)child).getName();
 
545
            if(takenName!=null) nameSet.add(takenName);
 
546
        }
 
547
        int cnt = 1;
 
548
        if(nameSet.size() <Integer.MAX_VALUE) {
 
549
            while(nameSet.contains(name)) {
 
550
                name = preferedName.concat(""+cnt++);
 
551
            }
 
552
        }
 
553
        component.setName(name);
 
554
    }
 
555
    
 
556
    // creates primitive string global reference
 
557
    private NamedComponentReference<GlobalSimpleType>
 
558
            createStringTypeReference(SchemaComponent component) {
 
559
        GlobalSimpleType stringType = getStringType();
 
560
        if(stringType!=null)
 
561
            return getParent().getModel().getFactory().createGlobalReference
 
562
                    (stringType, GlobalSimpleType.class, component);
 
563
        return null;
 
564
    }
 
565
    
 
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")) {
 
571
                return type;
 
572
            }
 
573
        }
 
574
        return null;
 
575
    }
 
576
}