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-2006 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.
42
package org.netbeans.modules.xml.schema.model.impl;
43
import java.util.Collection;
44
import org.netbeans.modules.xml.schema.model.*;
45
import org.netbeans.modules.xml.schema.model.Attribute.Use;
46
import org.netbeans.modules.xml.schema.model.visitor.SchemaVisitor;
47
import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
48
import org.w3c.dom.Node;
51
* This class provides the algorithm to determine what classes to create
52
* based on the current node context.
53
* @author Chris Webster
55
class SchemaElementNodeVisitor implements SchemaVisitor {
58
* This method determines the incoming component and creates the appropriate
61
* @return SchemaComponent for the specified element or null if the
62
* element is not recognized as a type specified in schema.
64
public SchemaComponent createSubComponent(
65
SchemaComponent parent,
66
org.w3c.dom.Element e,
67
SchemaModelImpl model) {
70
setNewComponent(null);
73
if (SchemaElements.SCHEMA.getName().equals(e.getLocalName())) {
74
setNewComponent(new SchemaImpl(model,e));
77
if (SchemaElements.ANNOTATION.getName().equals(e.getLocalName())) {
78
setNewComponent(new AnnotationImpl(model,e));
83
return newCommonSchemaComponent;
86
public void visit(SimpleTypeRestriction str) {
87
allSimpleRestriction();
90
public void visit(GlobalAttributeGroup gag) {
91
recognizeAttributes();
94
public void visit(MinLength ml) {
98
public void visit(AnyAttribute anyAttr) {
99
// only child is annotation
102
public void visit(AttributeGroupReference agr) {
103
// only child is annotation
106
public void visit(SimpleContentRestriction scr) {
107
allSimpleRestriction();
108
recognizeAttributes();
111
public void visit(LocalSimpleType type) {
115
public void visit(MaxInclusive mi) {
119
public void visit(Documentation d) {
123
public void visit(AppInfo d) {
127
public void visit(Notation d) {
131
public void visit(ComplexExtension ce) {
132
recognizeGroupAllChoiceSequence();
133
recognizeAttributes();
136
public void visit(Field f) {
140
public void visit(Schema s) {
141
if (SchemaElements.INCLUDE.getName().equals(e.getLocalName())) {
142
setNewComponent(new IncludeImpl(model,e));
144
} else if (SchemaElements.IMPORT.getName().equals(e.getLocalName())) {
145
setNewComponent(new ImportImpl(model,e));
147
} else if (SchemaElements.REDEFINE.getName().equals(e.getLocalName())) {
148
setNewComponent(new RedefineImpl(model,e));
150
} else if (SchemaElements.SIMPLE_TYPE.getName().equals(
152
setNewComponent(new GlobalSimpleTypeImpl(model,e));
154
} else if (SchemaElements.COMPLEX_TYPE.getName().equals(
156
setNewComponent(new GlobalComplexTypeImpl(model,e));
158
} else if (SchemaElements.GROUP.getName().equals(e.getLocalName())) {
159
setNewComponent(new GlobalGroupImpl(model,e));
161
} else if (SchemaElements.ATTRIBUTE_GROUP.getName().equals(
163
setNewComponent(new GlobalAttributeGroupImpl(model,e));
165
} else if (SchemaElements.ELEMENT.getName().equals(e.getLocalName())) {
166
setNewComponent(new GlobalElementImpl(model,e));
168
} else if (SchemaElements.ATTRIBUTE.getName().equals(e.getLocalName())) {
169
setNewComponent(new GlobalAttributeImpl(model,e));
171
} else if (SchemaElements.NOTATION.getName().equals(e.getLocalName())) {
172
setNewComponent(new NotationImpl(model,e));
177
public void visit(Union u) {
178
if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
179
setNewComponent(new LocalSimpleTypeImpl(model,e));
183
public void visit(MinExclusive me) {
187
public void visit(Whitespace ws) {
191
public void visit(MinInclusive mi) {
195
public void visit(Redefine rd) {
196
if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
197
setNewComponent(new GlobalSimpleTypeImpl(model,e));
199
} else if (SchemaElements.COMPLEX_TYPE.getName().equals(e.getLocalName())) {
200
setNewComponent(new GlobalComplexTypeImpl(model,e));
202
} else if (SchemaElements.GROUP.getName().equals(e.getLocalName())) {
203
setNewComponent(new GlobalGroupImpl(model,e));
205
} else if (SchemaElements.ATTRIBUTE_GROUP.getName().equals(e.getLocalName())) {
206
setNewComponent(new GlobalAttributeGroupImpl(model,e));
210
public void visit(Choice choice) {
214
public void visit(GroupReference gr) {
218
public void visit(FractionDigits fd) {
222
public void visit(GlobalGroup gd) {
223
if (SchemaElements.ALL.getName().equals(e.getLocalName())) {
224
setNewComponent(new AllImpl(model,e));
226
} else if (SchemaElements.CHOICE.getName().equals(e.getLocalName())) {
227
setNewComponent(new ChoiceImpl(model,e));
229
} else if (SchemaElements.SEQUENCE.getName().equals(e.getLocalName())) {
230
setNewComponent(new SequenceImpl(model,e));
234
public void visit(GlobalElement ge) {
238
public void visit(All all) {
242
public void visit(ComplexContent cc) {
243
if (SchemaElements.RESTRICTION.getName().equals(e.getLocalName())) {
244
setNewComponent(new ComplexContentRestrictionImpl(model,e));
246
} else if (SchemaElements.EXTENSION.getName().equals(e.getLocalName())) {
247
setNewComponent(new ComplexExtensionImpl(model,e));
252
public void visit(LocalComplexType type) {
256
public void visit(MaxExclusive me) {
260
public void visit(Include include) {
264
public void visit(ComplexContentRestriction ccr) {
265
recognizeGroupAllChoiceSequence();
266
recognizeAttributes();
269
public void visit(GlobalComplexType gct) {
273
public void visit(GlobalAttribute ga) {
274
recognizeInlineSimpleType();
277
public void visit(Annotation ann) {
279
if (SchemaElements.DOCUMENTATION.getName().equals(e.getLocalName())) {
280
setNewComponent(new DocumentationImpl(model,e));
281
} else if (SchemaElements.APPINFO.getName().equals(e.getLocalName())) {
282
setNewComponent(new AppInfoImpl(model, e));
286
public void visit(SimpleContent sc) {
287
if (SchemaElements.RESTRICTION.getName().equals(e.getLocalName())) {
288
setNewComponent(new SimpleContentRestrictionImpl(model,e));
290
} else if (SchemaElements.EXTENSION.getName().equals(e.getLocalName())) {
291
setNewComponent(new SimpleExtensionImpl(model,e));
295
public void visit(Key key) {
296
recognizeSelectorAndField();
299
public void visit(KeyRef kr) {
300
recognizeSelectorAndField();
303
public void visit(LocalElement le) {
307
public void visit(MaxLength ml) {
311
public void visit(Sequence s) {
312
recognizeGroupAnyChoiceSequenceElement();
315
public void visit(Pattern p) {
319
public void visit(List l) {
320
if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
321
setNewComponent(new LocalSimpleTypeImpl(model,e));
325
public void visit(Import im) {
329
public void visit(Enumeration e) {
330
// only child is annotation
333
public void visit(AnyElement any) {
334
// only child is annotation
337
public void visit(LocalAttribute la) {
338
recognizeInlineSimpleType();
341
public void visit(GlobalSimpleType gst) {
345
public void visit(TotalDigits td) {
349
public void visit(Unique u) {
350
recognizeSelectorAndField();
353
public void visit(Length length) {
357
public void visit(Selector selector) {
361
public void visit(SimpleExtension se) {
362
recognizeAttributes();
365
public void visit(ElementReference er) {
369
public void visit(AttributeReference reference) {
373
private void allSimpleRestriction() {
374
if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
375
setNewComponent(new LocalSimpleTypeImpl(model,e));
377
} else if (SchemaElements.MIN_EXCLUSIVE.getName().equals(
379
setNewComponent(new MinExclusiveImpl(model,e));
381
} else if (SchemaElements.MIN_INCLUSIVE.getName().equals(
383
setNewComponent(new MinInclusiveImpl(model,e));
385
} else if (SchemaElements.MAX_EXCLUSIVE.getName().equals(
387
setNewComponent(new MaxExclusiveImpl(model,e));
389
} else if (SchemaElements.MAX_INCLUSIVE.getName().equals(
391
setNewComponent(new MaxInclusiveImpl(model,e));
393
} else if (SchemaElements.TOTAL_DIGITS.getName().equals(
395
setNewComponent(new TotalDigitsImpl(model,e));
397
} else if (SchemaElements.FRACTION_DIGITS.getName().equals(
399
setNewComponent(new FractionDigitsImpl(model,e));
401
} else if (SchemaElements.LENGTH.getName().equals(
403
setNewComponent(new LengthImpl(model,e));
405
} else if (SchemaElements.MIN_LENGTH.getName().equals(
407
setNewComponent(new MinLengthImpl(model,e));
409
} else if (SchemaElements.MAX_LENGTH.getName().equals(
411
setNewComponent(new MaxLengthImpl(model,e));
413
} else if (SchemaElements.ENUMERATION.getName().equals(
415
setNewComponent(new EnumerationImpl(model,e));
417
} else if (SchemaElements.WHITESPACE.getName().equals(
419
setNewComponent(new WhitespaceImpl(model,e));
421
} else if (SchemaElements.PATTERN.getName().equals(e.getLocalName())) {
422
setNewComponent(new PatternImpl(model,e));
427
private void recognizeSelectorAndField() {
428
if (SchemaElements.SELECTOR.getName().equals(e.getLocalName())) {
429
setNewComponent(new SelectorImpl(model,e));
431
} else if (SchemaElements.FIELD.getName().equals(e.getLocalName())) {
432
setNewComponent(new FieldImpl(model,e));
436
private void recognizeAttributes() {
437
if (SchemaElements.ATTRIBUTE.getName().equals(e.getLocalName())) {
438
if (isAttributeDefined(SchemaAttributes.REF)) {
439
setNewComponent(new AttributeReferenceImpl(model,e));
441
setNewComponent(new LocalAttributeImpl(model,e));
443
} else if (SchemaElements.ATTRIBUTE_GROUP.getName().equals(
445
setNewComponent(new AttributeGroupReferenceImpl(model,e));
446
} else if (SchemaElements.ANY_ATTRIBUTE.getName().equals(
448
setNewComponent(new AnyAttributeImpl(model,e));
452
private void allComplexType() {
453
recognizeAttributes();
454
recognizeGroupAllChoiceSequence();
455
if (SchemaElements.SIMPLE_CONTENT.getName().equals(e.getLocalName())) {
456
setNewComponent(new SimpleContentImpl(model,e));
457
} else if (SchemaElements.COMPLEX_CONTENT.getName().equals(e.getLocalName())) {
458
setNewComponent(new ComplexContentImpl(model,e));
462
private boolean isAttributeDefined(SchemaAttributes attribute) {
463
return e.getAttribute(attribute.getName()) != null;
466
private void createLocalElement() {
467
if (isAttributeDefined(SchemaAttributes.REF)) {
468
setNewComponent(new ElementReferenceImpl(model,e));
470
setNewComponent(new LocalElementImpl(model,e));
474
private void recognizeGroupAnyChoiceSequenceElement() {
475
if (SchemaElements.ELEMENT.getName().equals(e.getLocalName())) {
476
createLocalElement();
478
recognizeGroupChoiceSequenceAny();
482
private void recognizeGroupAllChoiceSequence() {
483
if (SchemaElements.ALL.getName().equals(e.getLocalName())) {
484
setNewComponent(new AllImpl(model,e));
486
recognizeGroupChoiceSequence();
490
private void allElement() {
491
if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
492
setNewComponent(new LocalSimpleTypeImpl(model,e));
494
} else if (SchemaElements.COMPLEX_TYPE.getName().equals(e.getLocalName())) {
495
setNewComponent(new LocalComplexTypeImpl(model,e));
497
} else if (SchemaElements.UNIQUE.getName().equals(e.getLocalName())) {
498
setNewComponent(new UniqueImpl(model,e));
500
} else if (SchemaElements.KEY.getName().equals(e.getLocalName())) {
501
setNewComponent(new KeyImpl(model,e));
503
} else if (SchemaElements.KEYREF.getName().equals(e.getLocalName())) {
504
setNewComponent(new KeyRefImpl(model,e));
508
private void allChoice() {
509
if (SchemaElements.ELEMENT.getName().equals(e.getLocalName())) {
510
createLocalElement();
512
recognizeGroupChoiceSequenceAny();
518
private void recognizeGroupChoiceSequenceAny() {
519
if (SchemaElements.ANY.getName().equals(e.getLocalName())) {
520
setNewComponent(new AnyImpl(model,e));
522
recognizeGroupChoiceSequence();
526
private void recognizeGroupChoiceSequence() {
527
if (SchemaElements.GROUP.getName().equals(e.getLocalName())) {
528
setNewComponent(new GroupReferenceImpl(model,e));
530
} else if (SchemaElements.CHOICE.getName().equals(e.getLocalName())) {
531
setNewComponent(new ChoiceImpl(model,e));
533
} else if (SchemaElements.SEQUENCE.getName().equals(e.getLocalName())) {
534
setNewComponent(new SequenceImpl(model,e));
539
private void recognizeInlineSimpleType() {
540
if (SchemaElements.SIMPLE_TYPE.getName().equals(e.getLocalName())) {
541
setNewComponent(new LocalSimpleTypeImpl(model,e));
545
private void allSimpleType() {
546
if (SchemaElements.RESTRICTION.getName().equals(e.getLocalName())) {
547
setNewComponent(new SimpleTypeRestrictionImpl(model,e));
549
} else if (SchemaElements.LIST.getName().equals(e.getLocalName())) {
550
setNewComponent(new ListImpl(model,e));
552
} else if (SchemaElements.UNION.getName().equals(e.getLocalName())) {
553
setNewComponent(new UnionImpl(model,e));
558
private void allCreation() {
559
if (SchemaElements.ELEMENT.getName().equals(e.getLocalName())) {
560
if (isAttributeDefined(SchemaAttributes.REF)) {
561
setNewComponent(new ElementReferenceImpl(model,e));
563
setNewComponent(new LocalElementImpl(model,e));
568
private void setNewComponent(SchemaComponent c) {
569
newCommonSchemaComponent = c;
572
private SchemaComponent newCommonSchemaComponent;
573
private org.w3c.dom.Element e;
574
private SchemaModelImpl model;