KickJava   Java API By Example, From Geeks To Geeks.

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


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.xdm;
21
22 import org.netbeans.modules.xml.schema.model.*;
23 import org.netbeans.modules.xml.schema.model.impl.SchemaImpl;
24 import org.netbeans.modules.xml.schema.model.visitor.SchemaVisitor;
25 import org.netbeans.modules.xml.xam.AbstractComponent;
26 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
27 import org.netbeans.modules.xml.xam.ComponentUpdater;
28 import org.netbeans.modules.xml.xam.ComponentUpdater.Operation;
29
30 /**
31  *
32  * @author Nam Nguyen
33  */

34 public class SyncUpdateVisitor<T extends SchemaComponent> implements SchemaVisitor, ComponentUpdater<T> {
35
36     private Operation operation;
37     private SchemaComponent parent;
38     private int index;
39     private boolean canAdd;
40     
41     public SyncUpdateVisitor(){}
42     
43     public boolean canAdd(SchemaComponent target, DocumentComponent child) {
44         if (!(child instanceof SchemaComponent)) return false;
45         update(target, (SchemaComponent) child, null);
46         return canAdd;
47     }
48     
49     public void update(SchemaComponent target, SchemaComponent child,
50             Operation operation) {
51         update(target, child, -1, operation);
52     }
53     
54     public void update(SchemaComponent target, SchemaComponent child, int index,
55             Operation operation) {
56         assert target != null;
57         assert child != null;
58         assert operation == null || operation == Operation.ADD || operation == Operation.REMOVE;
59
60         this.parent = target;
61         this.index = index;
62         this.operation = operation;
63         child.accept(this);
64     }
65
66     private SchemaImpl getSchema() {
67         assert (parent instanceof Schema) : "Expect parent component is 'schema'"; //NOI18N
68
return (SchemaImpl) parent;
69     }
70     
71     public void visit(Schema schema) {
72         if (operation == null) {
73             canAdd = false;
74         } else {
75             assert false : "Should never add or remove schema root";
76         }
77     }
78     
79     private boolean isParentSchemaRoot() {
80         return parent.getModel().getRootComponent() == parent;
81     }
82     
83     private boolean isParentRedefine() {
84         return parent instanceof Redefine;
85     }
86
87     private void addChild(String JavaDoc eventName, DocumentComponent child) {
88         ((AbstractComponent) parent).insertAtIndex(eventName, child, index);
89     }
90     
91     private void removeChild(String JavaDoc eventName, DocumentComponent child) {
92         ((AbstractComponent) parent).removeChild(eventName, child);
93     }
94     
95     public void visit(GlobalAttribute child) {
96         if(operation == Operation.ADD) {
97             getSchema().insertAtIndex(Schema.ATTRIBUTES_PROPERTY, child, index);
98         } else if (operation == Operation.REMOVE) {
99             getSchema().removeAttribute(child);
100         } else {
101             canAdd = isParentSchemaRoot();
102         }
103     }
104     
105     public void visit(GlobalAttributeGroup child) {
106         canAdd = isParentSchemaRoot() || isParentRedefine();
107         if (operation == null || !canAdd) return;
108         if(operation == Operation.ADD) {
109             if (isParentRedefine()) {
110                 addChild(Redefine.ATTRIBUTE_GROUP_PROPERTY, child);
111             } else {
112                 addChild(Schema.ATTRIBUTE_GROUPS_PROPERTY, child);
113             }
114         } else if (operation == Operation.REMOVE) {
115             if (isParentRedefine()) {
116                 ((Redefine)parent).removeAttributeGroup(child);
117             } else {
118                 getSchema().removeAttributeGroup(child);
119             }
120         } else {
121             canAdd = isParentSchemaRoot() || isParentRedefine();
122         }
123     }
124     
125     public void visit(GlobalElement child) {
126         if(operation == Operation.ADD) {
127             getSchema().insertAtIndex(Schema.ELEMENTS_PROPERTY, child, index);
128         } else if (operation == Operation.REMOVE) {
129             getSchema().removeElement(child);
130         } else {
131             canAdd = isParentSchemaRoot();
132         }
133     }
134     
135     public void visit(GlobalGroup child) {
136         canAdd = isParentSchemaRoot() || isParentRedefine();
137         if (operation == null || !canAdd) return;
138         if(operation == Operation.ADD) {
139             if (isParentRedefine()) {
140                 addChild(Redefine.GROUP_DEFINITION_PROPERTY, child);
141             } else {
142                 addChild(Schema.GROUPS_PROPERTY, child);
143             }
144         } else if (operation == Operation.REMOVE) {
145             if (isParentRedefine()) {
146                 ((Redefine)parent).removeGroupDefinition(child);
147             } else {
148                 getSchema().removeGroup(child);
149             }
150         } else {
151             canAdd = isParentSchemaRoot() || isParentRedefine();
152         }
153     }
154     
155     public void visit(GlobalSimpleType child) {
156         canAdd = isParentSchemaRoot() || isParentRedefine();
157         if (operation == null || !canAdd) return;
158         if(operation == Operation.ADD) {
159             if (isParentRedefine()) {
160                 addChild(Redefine.SIMPLE_TYPE_PROPERTY, child);
161             } else {
162                 addChild(Schema.SIMPLE_TYPES_PROPERTY, child);
163             }
164         } else if (operation == Operation.REMOVE) {
165             if (isParentRedefine()) {
166                 ((Redefine)parent).removeSimpleType(child);
167             } else {
168                 getSchema().removeSimpleType(child);
169             }
170         } else {
171             canAdd = isParentSchemaRoot() || isParentRedefine();
172         }
173     }
174     
175     public void visit(GlobalComplexType child) {
176         canAdd = isParentSchemaRoot() || isParentRedefine();
177         if (operation == null || !canAdd) return;
178         if(operation == Operation.ADD) {
179             if (isParentRedefine()) {
180                 addChild(Redefine.COMPLEX_TYPE_PROPERTY, child);
181             } else {
182                 addChild(Schema.COMPLEX_TYPES_PROPERTY, child);
183             }
184         } else if (operation == Operation.REMOVE) {
185             if (isParentRedefine()) {
186                 ((Redefine)parent).removeComplexType(child);
187             } else {
188                 getSchema().removeComplexType(child);
189             }
190         }
191     }
192     
193     public void visit(Notation child) {
194         if(operation == Operation.ADD) {
195             addChild(Schema.NOTATIONS_PROPERTY, child);
196         } else if (operation == Operation.REMOVE) {
197             getSchema().removeNotation(child);
198         } else {
199             canAdd = isParentSchemaRoot();
200         }
201     }
202     
203     public void visit(Import child) {
204         if(operation == Operation.ADD) {
205             getSchema().addExternalReference(child);
206         } else if (operation == Operation.REMOVE) {
207             getSchema().removeExternalReference(child);
208         } else {
209             canAdd = isParentSchemaRoot();
210         }
211     }
212     
213     public void visit(Include child) {
214         if(operation == Operation.ADD) {
215             getSchema().addExternalReference(child);
216         } else if (operation == Operation.REMOVE) {
217             getSchema().removeExternalReference(child);
218         } else {
219             canAdd = isParentSchemaRoot();
220         }
221     }
222     
223     public void visit(Redefine child) {
224         if(operation == Operation.ADD) {
225             getSchema().addExternalReference(child);
226         } else if (operation == Operation.REMOVE) {
227             getSchema().removeExternalReference(child);
228         } else {
229             canAdd = isParentSchemaRoot();
230         }
231     }
232
233     public void visit(LocalSimpleType child) {
234         if (parent instanceof List) {
235             List list = (List) parent;
236             if (operation == Operation.ADD) {
237                 if (index > -1) { // from sync or paste
238
addChild(List.INLINE_TYPE_PROPERTY, child);
239                 } else { // from new component
240
list.setInlineType(child);
241                 }
242             } else if (operation == Operation.REMOVE) {
243                 removeChild(List.INLINE_TYPE_PROPERTY, child);
244             } else {
245                 canAdd = (list.getInlineType() == null);
246             }
247         } else if (parent instanceof Union) {
248             Union union = (Union) parent;
249             if (operation == Operation.ADD) {
250                 addChild(Union.INLINE_TYPE_PROPERTY, child);
251             } else if (operation == Operation.REMOVE) {
252                 union.removeInlineType(child);
253             } else {
254                 canAdd = true;
255             }
256         } else if (parent instanceof LocalAttribute ||
257            parent instanceof GlobalAttribute) {
258         if (parent instanceof LocalAttribute) {
259         LocalAttribute a = (LocalAttribute) parent;
260         if (operation == Operation.ADD) {
261             if (index > -1) {
262                 addChild(LocalAttribute.INLINE_TYPE_PROPERTY, child);
263             } else {
264                 a.setInlineType(child);
265             }
266         } else if (operation == Operation.REMOVE) {
267             removeChild(LocalAttribute.INLINE_TYPE_PROPERTY, child);
268         } else {
269                     canAdd = (a.getInlineType() == null);
270                 }
271         } else {
272             GlobalAttribute a = (GlobalAttribute) parent;
273             if (operation == Operation.ADD) {
274                 if (index > -1) {
275                     addChild(GlobalAttribute.INLINE_TYPE_PROPERTY, child);
276                 } else {
277                     a.setInlineType(child);
278                 }
279             } else if (operation == Operation.REMOVE) {
280                 removeChild(GlobalAttribute.INLINE_TYPE_PROPERTY, child);
281             } else {
282                 canAdd = (a.getInlineType() == null);
283                 }
284         }
285         } else if (parent instanceof SimpleRestriction) {
286             SimpleRestriction sr = (SimpleRestriction) parent;
287             if (operation == Operation.ADD) {
288                 if (index > -1) {
289                     addChild(SimpleRestriction.INLINETYPE_PROPERTY, child);
290                 } else {
291                     sr.setInlineType(child);
292                 }
293             } else if (operation == Operation.REMOVE) {
294                 removeChild(SimpleRestriction.INLINETYPE_PROPERTY, child);
295             } else {
296                 canAdd = (sr.getInlineType() == null);
297             }
298         } else if (parent instanceof TypeContainer) {
299             TypeContainer target = (TypeContainer) parent;
300             if (operation == Operation.ADD) {
301                 if (index > -1) {
302                     addChild(TypeContainer.INLINE_TYPE_PROPERTY, child);
303                 } else {
304                     target.setInlineType(child);
305                 }
306             } else if (operation == Operation.REMOVE) {
307                 removeChild(TypeContainer.INLINE_TYPE_PROPERTY, child);
308             } else {
309                 canAdd = (target.getInlineType() == null);
310             }
311         } else if (operation == null) {
312             canAdd = false;
313         } else {
314             assert false: "Wrong parent "+parent.getClass().getName();
315         }
316     }
317
318     public void visit(All child) {
319         if (parent instanceof ComplexContentRestriction ||
320                 parent instanceof ComplexType ||
321                 parent instanceof GlobalGroup) {
322             // OK
323
} else if (operation == null) {
324             canAdd = false;
325             return;
326         } else {
327             assert false: "Wrong parent "+parent.getClass().getName();
328         }
329         if (parent instanceof ComplexContentRestriction) {
330             ComplexContentRestriction target = (ComplexContentRestriction) parent;
331             if (operation == Operation.ADD) {
332                 if (index > -1) {
333                     addChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
334                 } else {
335                     target.setDefinition(child);
336                 }
337             } else if (operation == Operation.REMOVE) {
338                 removeChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
339             } else {
340                 canAdd = (target.getDefinition() == null);
341             }
342         } else if (parent instanceof ComplexType) {
343             ComplexType target = (ComplexType) parent;
344             if (operation == Operation.ADD) {
345                 if (index > -1) {
346                     addChild(ComplexType.DEFINITION_PROPERTY, child);
347                 } else {
348                     target.setDefinition(child);
349                 }
350             } else if (operation == Operation.REMOVE) {
351                 removeChild(ComplexType.DEFINITION_PROPERTY, child);
352             } else {
353                 canAdd = (target.getDefinition() == null);
354             }
355         } else if (parent instanceof GlobalGroup) {
356             updateGlobalGroup(child);
357         } else if (operation == null) {
358             canAdd = false;
359         } else {
360             assert false: "Wrong parent "+parent.getClass().getName();
361     }
362     }
363
364     public void visit(ComplexContentRestriction child) {
365         if (parent instanceof ComplexContent) {
366             //OK
367
} else if (operation == null) {
368             canAdd = false;
369             return;
370         } else {
371             assert false: "Wrong parent "+parent.getClass().getName();
372         }
373         
374         ComplexContent target = (ComplexContent) parent;
375         if (operation == Operation.ADD) {
376             if (index > -1) {
377                 addChild(ComplexContent.LOCAL_DEFINITION_PROPERTY, child);
378             } else {
379                 target.setLocalDefinition(child);
380             }
381         } else if (operation == Operation.REMOVE) {
382             removeChild(ComplexContent.LOCAL_DEFINITION_PROPERTY, child);
383         } else {
384             canAdd = (target.getLocalDefinition() == null);
385         }
386     }
387
388     public void visit(AnyElement child) {
389         if (parent instanceof Choice) {
390             Choice target = (Choice) parent;
391             if (operation == Operation.ADD)
392                 addChild(Choice.ANY_PROPERTY, child);
393             else if (operation == Operation.REMOVE) {
394                 target.removeAny(child);
395             } else {
396                 canAdd = true;
397             }
398         } else if (parent instanceof Sequence) {
399             Sequence target = (Sequence) parent;
400             if (operation == Operation.ADD)
401                 target.addContent(child, index);
402             else if (operation == Operation.REMOVE) {
403                 target.removeContent(child);
404             } else {
405                 canAdd = true;
406             }
407         }
408     }
409
410     public void visit(GroupReference child) {
411     if (parent instanceof ComplexType) {
412         ComplexType target = (ComplexType) parent;
413         if (operation == Operation.ADD) {
414             if (index > -1) {
415                 addChild(ComplexType.DEFINITION_PROPERTY, child);
416             } else {
417                 target.setDefinition(child);
418             }
419         } else if (operation == Operation.REMOVE) {
420             removeChild(ComplexType.DEFINITION_PROPERTY, child);
421         } else if (operation == null) {
422             canAdd = (target.getDefinition() == null);
423         }
424     } else if (parent instanceof Sequence) {
425         Sequence target = (Sequence) parent;
426         if (operation == Operation.ADD)
427         target.addContent(child, index);
428         else if (operation == Operation.REMOVE) {
429         target.removeContent(child);
430         } else if (operation == null) {
431         canAdd = true;
432         }
433     } else if (parent instanceof ComplexContentRestriction) {
434         ComplexContentRestriction target = (ComplexContentRestriction) parent;
435         if (operation == Operation.ADD) {
436             if (index > -1) {
437                 addChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
438             } else {
439                 target.setDefinition(child);
440             }
441         } else if (operation == Operation.REMOVE) {
442             removeChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
443         } else if (operation == null) {
444             canAdd = (target.getDefinition() == null);
445         }
446     } else if (parent instanceof ComplexExtension) {
447         ComplexExtension target = (ComplexExtension) parent;
448         if (operation == Operation.ADD)
449             if (index > -1) { // from sync
450
addChild(ComplexExtension.LOCAL_DEFINITION_PROPERTY, child);
451             } else { // from ui
452
target.setLocalDefinition(child);
453             }
454         else if (operation == Operation.REMOVE) {
455             removeChild(ComplexExtension.LOCAL_DEFINITION_PROPERTY, child);
456         } else if (operation == null) {
457             canAdd = (target.getLocalDefinition() == null);
458         }
459         
460     } else if (parent instanceof Choice) {
461         Choice target = (Choice) parent;
462         if (operation == Operation.ADD) {
463             addChild(Choice.GROUP_REF_PROPERTY, child);
464         } else if (operation == Operation.REMOVE) {
465             target.removeGroupReference(child);
466         } else if (operation == null) {
467             canAdd = true;
468         }
469     } else if (operation == null) {
470         canAdd = false;
471         return;
472     } else {
473         assert false: "Wrong parent "+parent.getClass().getName();
474     }
475     }
476
477     public void visit(Enumeration child) {
478         if (parent instanceof SimpleRestriction) {
479             //OK
480
} else if (operation == null) {
481             canAdd = false;
482             return;
483         } else {
484             assert false: "Wrong parent "+parent.getClass().getName();
485         }
486         SimpleRestriction target = (SimpleRestriction) parent;
487         if (operation == Operation.ADD)
488             addChild(SimpleRestriction.ENUMERATION_PROPERTY, child);
489         else if (operation == Operation.REMOVE) {
490             target.removeEnumeration(child);
491         } else {
492             canAdd = true;
493         }
494     }
495
496     private void updateConstraintOnCommonElement(Constraint child) {
497         if (parent instanceof Element) {
498             //OK
499
} else if (operation == null) {
500             canAdd = false;
501             return;
502         } else {
503             assert false: "Wrong parent "+parent.getClass().getName();
504         }
505         Element target = (Element) parent;
506         if (operation == Operation.ADD)
507             addChild(Element.CONSTRAINT_PROPERTY, child);
508         else if (operation == Operation.REMOVE) {
509             target.removeConstraint(child);
510         } else {
511             canAdd = true;
512         }
513     }
514     
515     public void visit(KeyRef child) {
516         updateConstraintOnCommonElement(child);
517     }
518
519     public void visit(Key child) {
520         updateConstraintOnCommonElement(child);
521     }
522
523     public void visit(Unique child) {
524         updateConstraintOnCommonElement(child);
525     }
526
527     public void visit(AttributeGroupReference child) {
528         if (parent instanceof LocalAttributeContainer) {
529             //OK
530
} else if (operation == null) {
531             canAdd = false;
532             return;
533         } else {
534             assert false: "Wrong parent "+parent.getClass().getName();
535         }
536         LocalAttributeContainer target = (LocalAttributeContainer) parent;
537         if (operation == Operation.ADD)
538             addChild(LocalAttributeContainer.ATTRIBUTE_GROUP_REFERENCE_PROPERTY, child);
539         else if (operation == Operation.REMOVE) {
540             target.removeAttributeGroupReference(child);
541         } else {
542             canAdd = true;
543         }
544     }
545
546     private void updateGlobalGroup(LocalGroupDefinition child) {
547         if (parent instanceof GlobalGroup) {
548             //OK
549
} else if (operation == null) {
550             canAdd = false;
551             return;
552         } else {
553             assert false: "Wrong parent "+parent.getClass().getName();
554         }
555         GlobalGroup target = (GlobalGroup) parent;
556         if (operation == Operation.ADD) {
557             if (index > -1) {
558                 addChild(GlobalGroup.DEFINITION_PROPERTY, child);
559             } else {
560                 target.setDefinition(child);
561             }
562         } else if (operation == Operation.REMOVE) {
563             removeChild(GlobalGroup.DEFINITION_PROPERTY, child);
564         } else {
565             canAdd = (target.getDefinition() == null);
566         }
567     }
568
569     public void visit(Documentation child) {
570         if (parent instanceof Annotation) {
571             //OK
572
} else if (operation == null) {
573             canAdd = false;
574             return;
575         } else {
576             assert false: "Wrong parent "+parent.getClass().getName();
577         }
578         Annotation target = (Annotation) parent;
579         if (operation == Operation.ADD)
580             addChild(Annotation.DOCUMENTATION_PROPERTY, child);
581         else if (operation == Operation.REMOVE) {
582             target.removeDocumentation(child);
583         } else {
584             canAdd = true;
585         }
586     }
587
588     public void visit(AppInfo child) {
589         if (parent instanceof Annotation) {
590             //OK
591
} else if (operation == null) {
592             canAdd = false;
593             return;
594         } else {
595             assert false: "Wrong parent "+parent.getClass().getName();
596         }
597         Annotation target = (Annotation) parent;
598         if (operation == Operation.ADD)
599             addChild(Annotation.APPINFO_PROPERTY, child);
600         else if (operation == Operation.REMOVE) {
601             target.removeAppInfo(child);
602         } else {
603             canAdd = true;
604         }
605     }
606     
607     public void visit(Choice child) {
608         if (parent instanceof Choice) {
609             Choice target = (Choice) parent;
610             if (operation == Operation.ADD)
611                 addChild(Choice.CHOICE_PROPERTY, child);
612             else if (operation == Operation.REMOVE) {
613                 target.removeChoice(child);
614             } else {
615                 canAdd = true;
616             }
617         } else if (parent instanceof ComplexExtension) {
618             ComplexExtension target = (ComplexExtension) parent;
619             if (operation == Operation.ADD) {
620                 if (index > -1) {
621                     addChild(ComplexExtension.LOCAL_DEFINITION_PROPERTY, child);
622                 } else {
623                     target.setLocalDefinition(child);
624                 }
625             } else if (operation == Operation.REMOVE) {
626                 removeChild(ComplexExtension.LOCAL_DEFINITION_PROPERTY, child);
627             } else {
628                 canAdd = (target.getLocalAttributes() == null);
629             }
630         } else if (parent instanceof ComplexContentRestriction) {
631             ComplexContentRestriction target = (ComplexContentRestriction) parent;
632             if (operation == Operation.ADD) {
633                 if (index > -1) {
634                     addChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
635                 } else {
636                     target.setDefinition(child);
637                 }
638             } else if (operation == Operation.REMOVE) {
639                 removeChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
640             } else {
641                 canAdd = (target.getDefinition() == null);
642             }
643         } else if (parent instanceof Sequence) {
644             Sequence target = (Sequence) parent;
645             if (operation == Operation.ADD)
646                 target.addContent(child, index);
647             else if (operation == Operation.REMOVE) {
648                 target.removeContent(child);
649             } else {
650                 canAdd = true;
651             }
652         } else if (parent instanceof ComplexType) {
653             ComplexType target = (ComplexType) parent;
654             if (operation == Operation.ADD) {
655                 if (index > -1) { // from sync
656
addChild(ComplexType.DEFINITION_PROPERTY, child);
657                 } else { // from UI
658
target.setDefinition(child);
659                 }
660             } else if (operation == Operation.REMOVE) {
661                 removeChild(ComplexType.DEFINITION_PROPERTY, child);
662             } else {
663                 canAdd = (target.getDefinition() == null);
664             }
665         } else if (parent instanceof GlobalGroup) {
666         updateGlobalGroup(child);
667         } else if (operation == null) {
668             canAdd = false;
669         } else {
670             assert false: "Wrong parent "+parent.getClass().getName();
671         }
672     }
673
674     public void visit(SimpleContentRestriction child) {
675         if (parent instanceof SimpleContent) {
676             //OK
677
} else if (operation == null) {
678             canAdd = false;
679             return;
680         } else {
681             assert false: "Wrong parent "+parent.getClass().getName();
682         }
683         SimpleContent target = (SimpleContent) parent;
684         if (operation == Operation.ADD) {
685             if (index > -1) {
686                 addChild(SimpleContent.LOCAL_DEFINITION_PROPERTY, child);
687             } else {
688                 target.setLocalDefinition(child);
689             }
690         } else if (operation == Operation.REMOVE) {
691             removeChild(SimpleContent.LOCAL_DEFINITION_PROPERTY, child);
692         } else {
693             canAdd = (target.getLocalDefinition() == null);
694         }
695     }
696
697     public void visit(Selector child) {
698         if (parent instanceof Constraint) {
699             //OK
700
} else if (operation == null) {
701             canAdd = false;
702             return;
703         } else {
704             assert false: "Wrong parent "+parent.getClass().getName();
705         }
706         Constraint target = (Constraint) parent;
707         if (operation == Operation.ADD) {
708             if (index > -1) {
709                 addChild(Constraint.SELECTOR_PROPERTY, child);
710             } else {
711                 target.setSelector(child);
712             }
713         } else if (operation == Operation.REMOVE) {
714             removeChild(Constraint.SELECTOR_PROPERTY, child);
715         } else {
716             canAdd = (target.getSelector() == null);
717         }
718     }
719
720     public void visit(LocalElement child) {
721     if (parent instanceof Choice) {
722         Choice target = (Choice) parent;
723         if (operation == Operation.ADD) {
724             addChild(Choice.LOCAL_ELEMENT_PROPERTY, child);
725         } else if (operation == Operation.REMOVE) {
726             target.removeLocalElement(child);
727         } else {
728             canAdd = true;
729         }
730     } else if (parent instanceof Sequence) {
731         Sequence target = (Sequence) parent;
732         if (operation == Operation.ADD) {
733             target.addContent(child, index);
734         } else if (operation == Operation.REMOVE) {
735             target.removeContent(child);
736         } else {
737             canAdd = true;
738         }
739     } else if (parent instanceof All) {
740         All target = (All) parent;
741             if (operation == Operation.ADD) {
742                 addChild(All.ELEMENT_PROPERTY, child);
743             } else if (operation == Operation.REMOVE) {
744                 target.removeElement(child);
745             } else {
746                 canAdd = true;
747             }
748         } else if (operation == null) {
749             canAdd = false;
750         } else {
751             assert false: "Wrong parent "+parent.getClass().getName();
752         }
753     }
754     
755     public void visit(ElementReference child) {
756     if (parent instanceof Choice ||
757            parent instanceof All ||
758            parent instanceof Sequence) {
759             //OK
760
} else if (operation == null) {
761             canAdd = false;
762             return;
763         } else {
764             assert false: "Wrong parent "+parent.getClass().getName();
765         }
766     
767     if (parent instanceof Choice) {
768             Choice target = (Choice) parent;
769             if (operation == Operation.ADD)
770                 addChild(Choice.ELEMENT_REFERENCE_PROPERTY, child);
771             else if (operation == Operation.REMOVE) {
772                 target.removeElementReference(child);
773             } else {
774                 canAdd = true;
775             }
776         } else if (parent instanceof Sequence) {
777             Sequence target = (Sequence) parent;
778             if (operation == Operation.ADD)
779                 target.addContent(child, index);
780             else if (operation == Operation.REMOVE) {
781                 target.removeContent(child);
782             } else {
783                 canAdd = true;
784             }
785         } else if (parent instanceof All) {
786             All target = (All) parent;
787             if (operation == Operation.ADD)
788                 addChild(All.ELEMENT_REFERENCE_PROPERTY, child);
789             else if (operation == Operation.REMOVE) {
790                 target.removeElementReference(child);
791             } else {
792                 canAdd = true;
793             }
794         }
795     
796     }
797
798     public void visit(Annotation child) {
799         if (parent instanceof Annotation ||
800             parent instanceof Documentation ||
801             parent instanceof AppInfo) {
802             if (operation == null) {
803                 canAdd = false;
804                 return;
805             } else {
806                 assert false: "Wrong parent "+parent.getClass().getName();
807                 return;
808             }
809         }
810         if (operation == Operation.ADD) {
811             if (index > -1) {
812                 addChild(SchemaComponent.ANNOTATION_PROPERTY, child);
813             } else {
814                 parent.setAnnotation(child);
815             }
816         } else if (operation == Operation.REMOVE) {
817             removeChild(SchemaComponent.ANNOTATION_PROPERTY, child);
818         } else {
819             canAdd = (parent.getAnnotation() == null);
820         }
821     }
822
823     public void visit(ComplexExtension child) {
824         if (parent instanceof ComplexContent) {
825             //OK
826
} else if (operation == null) {
827             canAdd = false;
828             return;
829         } else {
830             assert false: "Wrong parent "+parent.getClass().getName();
831         }
832         ComplexContent target = (ComplexContent) parent;
833         if (operation == Operation.ADD) {
834             if (index > -1) {
835                 addChild(ComplexContent.LOCAL_DEFINITION_PROPERTY, child);
836             } else {
837                 target.setLocalDefinition(child);
838             }
839         } else if (operation == Operation.REMOVE) {
840             removeChild(ComplexContent.LOCAL_DEFINITION_PROPERTY, child);
841         } else {
842             canAdd = (target.getLocalDefinition() == null);
843         }
844     }
845
846     public void visit(SimpleExtension child) {
847         if (parent instanceof SimpleContent) {
848             //OK
849
} else if (operation == null) {
850             canAdd = false;
851             return;
852         } else {
853             assert false: "Wrong parent "+parent.getClass().getName();
854         }
855         SimpleContent target = (SimpleContent) parent;
856         if (operation == Operation.ADD) {
857             if (index > -1) {
858                 addChild(SimpleContent.LOCAL_DEFINITION_PROPERTY, child);
859             } else {
860                 target.setLocalDefinition(child);
861             }
862         } else if (operation == Operation.REMOVE) {
863             removeChild(SimpleContent.LOCAL_DEFINITION_PROPERTY, child);
864         } else {
865             canAdd = (target.getLocalDefinition() == null);
866         }
867     }
868
869     public void visit(Sequence child) {
870         if (parent instanceof Choice) {
871             Choice target = (Choice) parent;
872             if (operation == Operation.ADD) {
873                 addChild(Choice.SEQUENCE_PROPERTY, child);
874             } else if (operation == Operation.REMOVE) {
875                 removeChild(Choice.SEQUENCE_PROPERTY, child);
876             } else {
877                 canAdd = true;
878             }
879         } else if (parent instanceof Sequence) {
880             Sequence target = (Sequence) parent;
881             if (operation == Operation.ADD)
882                 target.addContent(child, index);
883             else if (operation == Operation.REMOVE) {
884                 target.removeContent(child);
885             } else {
886                 canAdd = true;
887             }
888         } else if (parent instanceof ComplexExtension) {
889             ComplexExtension target = (ComplexExtension) parent;
890             if (operation == Operation.ADD) {
891                 if (index > -1) {
892                     addChild(ComplexExtension.LOCAL_DEFINITION_PROPERTY, child);
893                 } else {
894                     target.setLocalDefinition(child);
895                 }
896             } else if (operation == Operation.REMOVE) {
897                 removeChild(ComplexExtension.LOCAL_DEFINITION_PROPERTY, child);
898             } else {
899                 canAdd = (target.getLocalDefinition() == null);
900             }
901         } else if (parent instanceof ComplexContentRestriction) {
902             ComplexContentRestriction target = (ComplexContentRestriction) parent;
903             if (operation == Operation.ADD) {
904                 if (index > -1) {
905                     addChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
906                 } else {
907                     target.setDefinition(child);
908                 }
909             } else if (operation == Operation.REMOVE) {
910                 removeChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
911             } else {
912                 canAdd = (target.getDefinition() == null);
913             }
914         } else if (parent instanceof ComplexType) {
915             ComplexType target = (ComplexType) parent;
916             if (operation == Operation.ADD) {
917                 if (index > -1) {
918                     addChild(ComplexType.DEFINITION_PROPERTY, child);
919                 } else {
920                     target.setDefinition(child);
921                 }
922             } else if (operation == Operation.REMOVE) {
923                 removeChild(ComplexType.DEFINITION_PROPERTY, child);
924             } else {
925                 canAdd = (target.getDefinition() == null);
926             }
927         } else if (parent instanceof GlobalGroup) {
928             updateGlobalGroup(child);
929         } else if (operation == null) {
930             canAdd = false;
931         } else {
932             assert false: "Wrong parent "+parent.getClass().getName();
933         }
934     }
935
936     public void visit(MinExclusive child) {
937         if (parent instanceof SimpleRestriction) {
938             //OK
939
} else if (operation == null) {
940             canAdd = false;
941             return;
942         } else {
943             assert false: "Wrong parent "+parent.getClass().getName();
944         }
945         SimpleRestriction target = (SimpleRestriction) parent;
946         if (operation == Operation.ADD)
947             addChild(SimpleRestriction.MIN_EXCLUSIVE_PROPERTY, child);
948         else if (operation == Operation.REMOVE) {
949             target.removeMinExclusive(child);
950         } else {
951             canAdd = true;
952         }
953     }
954
955     public void visit(MinInclusive child) {
956         if (parent instanceof SimpleRestriction) {
957             //OK
958
} else if (operation == null) {
959             canAdd = false;
960             return;
961         } else {
962             assert false: "Wrong parent "+parent.getClass().getName();
963         }
964         SimpleRestriction target = (SimpleRestriction) parent;
965         if (operation == Operation.ADD) {
966             addChild(SimpleRestriction.MIN_INCLUSIVE_PROPERTY, child);
967         } else if (operation == Operation.REMOVE) {
968             target.removeMinInclusive(child);
969         } else {
970             canAdd = true;
971         }
972     }
973
974     public void visit(Pattern child) {
975         if (parent instanceof SimpleRestriction) {
976             //OK
977
} else if (operation == null) {
978             canAdd = false;
979             return;
980         } else {
981             assert false: "Wrong parent "+parent.getClass().getName();
982         }
983             
984         SimpleRestriction target = (SimpleRestriction) parent;
985         
986         if (operation == Operation.ADD) {
987             addChild(SimpleRestriction.PATTERN_PROPERTY, child);
988         } else if (operation == Operation.REMOVE) {
989             target.removePattern(child);
990         } else {
991             canAdd = true;
992         }
993     }
994
995     public void visit(MinLength child) {
996         if (parent instanceof SimpleRestriction) {
997             //OK
998
} else if (operation == null) {
999             canAdd = false;
1000            return;
1001        } else {
1002            assert false: "Wrong parent "+parent.getClass().getName();
1003        }
1004        SimpleRestriction target = (SimpleRestriction) parent;
1005        if (operation == Operation.ADD) {
1006            addChild(SimpleRestriction.MIN_LENGTH_PROPERTY, child);
1007        } else if (operation == Operation.REMOVE) {
1008            target.removeMinLength(child);
1009        } else {
1010            canAdd = true;
1011        }
1012    }
1013
1014    public void visit(MaxLength child) {
1015        if (parent instanceof SimpleRestriction) {
1016            //OK
1017
} else if (operation == null) {
1018            canAdd = false;
1019            return;
1020        } else {
1021            assert false: "Wrong parent "+parent.getClass().getName();
1022        }
1023        SimpleRestriction target = (SimpleRestriction) parent;
1024        if (operation == Operation.ADD) {
1025            addChild(SimpleRestriction.MAX_LENGTH_PROPERTY, child);
1026        } else if (operation == Operation.REMOVE) {
1027            target.removeMaxLength(child);
1028        } else {
1029            canAdd = true;
1030        }
1031    }
1032
1033    public void visit(Whitespace child) {
1034        if (parent instanceof SimpleRestriction) {
1035            //OK
1036
} else if (operation == null) {
1037            canAdd = false;
1038            return;
1039        } else {
1040            assert false: "Wrong parent "+parent.getClass().getName();
1041        }
1042        SimpleRestriction target = (SimpleRestriction) parent;
1043        if (operation == Operation.ADD)
1044            addChild(SimpleRestriction.WHITESPACE_PROPERTY, child);
1045        else if (operation == Operation.REMOVE) {
1046            target.removeWhitespace(child);
1047        } else {
1048            canAdd = true;
1049        }
1050    }
1051
1052    public void visit(MaxInclusive child) {
1053        if (parent instanceof SimpleRestriction) {
1054            //OK
1055
} else if (operation == null) {
1056            canAdd = false;
1057            return;
1058        } else {
1059            assert false: "Wrong parent "+parent.getClass().getName();
1060        }
1061        SimpleRestriction target = (SimpleRestriction) parent;
1062        if (operation == Operation.ADD) {
1063            addChild(SimpleRestriction.MAX_INCLUSIVE_PROPERTY, child);
1064        } else if (operation == Operation.REMOVE) {
1065            target.removeMaxInclusive(child);
1066        } else {
1067            canAdd = true;
1068        }
1069    }
1070
1071    public void visit(LocalComplexType child) {
1072    if (parent instanceof TypeContainer) {
1073            //OK
1074
} else if (operation == null) {
1075            canAdd = false;
1076            return;
1077        } else {
1078            assert false: "Wrong parent "+parent.getClass().getName();
1079        }
1080        
1081        TypeContainer target = (TypeContainer) parent;
1082        if (operation == Operation.ADD) {
1083            if (index > -1) {
1084                addChild(TypeContainer.INLINE_TYPE_PROPERTY, child);
1085            } else {
1086                target.setInlineType(child);
1087            }
1088        } else if (operation == Operation.REMOVE) {
1089            removeChild(TypeContainer.INLINE_TYPE_PROPERTY, child);
1090        } else {
1091            canAdd = (target.getInlineType() == null);
1092        }
1093    }
1094
1095    public void visit(FractionDigits child) {
1096        if (parent instanceof SimpleRestriction) {
1097            //OK
1098
} else if (operation == null) {
1099            canAdd = false;
1100            return;
1101        } else {
1102            assert false: "Wrong parent "+parent.getClass().getName();
1103        }
1104        SimpleRestriction target = (SimpleRestriction) parent;
1105        if (operation == Operation.ADD)
1106            addChild(SimpleRestriction.FRACTION_DIGITS_PROPERTY, child);
1107        else if (operation == Operation.REMOVE) {
1108            target.removeFractionDigits(child);
1109        } else {
1110            canAdd = true;
1111        }
1112    }
1113
1114    public void visit(TotalDigits child) {
1115        if (parent instanceof SimpleRestriction) {
1116            //OK
1117
} else if (operation == null) {
1118            canAdd = false;
1119            return;
1120        } else {
1121            assert false: "Wrong parent "+parent.getClass().getName();
1122        }
1123        SimpleRestriction target = (SimpleRestriction) parent;
1124        if (operation == Operation.ADD)
1125            addChild(SimpleRestriction.TOTAL_DIGITS_PROPERTY, child);
1126        else if (operation == Operation.REMOVE) {
1127            target.removeTotalDigit(child);
1128        } else {
1129            canAdd = true;
1130        }
1131    }
1132
1133    private void updateSimpleType(SimpleTypeDefinition child) {
1134        if (parent instanceof SimpleType) {
1135            //OK
1136
} else if (operation == null) {
1137            canAdd = false;
1138            return;
1139        } else {
1140            assert false: "Wrong parent "+parent.getClass().getName();
1141        }
1142        SimpleType target = (SimpleType) parent;
1143        if (operation == Operation.ADD) {
1144            if (index > -1) {
1145                addChild(SimpleType.DEFINITION_PROPERTY, child);
1146            } else {
1147                target.setDefinition(child);
1148            }
1149        } else if (operation == Operation.REMOVE) {
1150            removeChild(SimpleType.DEFINITION_PROPERTY, child);
1151        } else {
1152            canAdd = (target.getDefinition() == null);
1153        }
1154    }
1155    
1156    public void visit(List child) {
1157        updateSimpleType(child);
1158    }
1159
1160    public void visit(SimpleTypeRestriction child) {
1161        updateSimpleType(child);
1162    }
1163
1164    public void visit(Union child) {
1165        updateSimpleType(child);
1166    }
1167
1168    public void visit(MaxExclusive child) {
1169        if (parent instanceof SimpleRestriction) {
1170            //OK
1171
} else if (operation == null) {
1172            canAdd = false;
1173            return;
1174        } else {
1175            assert false: "Wrong parent "+parent.getClass().getName();
1176        }
1177        SimpleRestriction target = (SimpleRestriction) parent;
1178        if (operation == Operation.ADD)
1179            addChild(SimpleRestriction.MAX_EXCLUSIVE_PROPERTY, child);
1180        else if (operation == Operation.REMOVE) {
1181            target.removeMaxExclusive(child);
1182        } else {
1183            canAdd = true;
1184        }
1185    }
1186
1187    public void visit(AttributeReference child) {
1188        if (parent instanceof LocalAttributeContainer) {
1189            //OK
1190
} else if (operation == null) {
1191            canAdd = false;
1192            return;
1193        } else {
1194            assert false: "Wrong parent "+parent.getClass().getName();
1195        }
1196        LocalAttributeContainer target = (LocalAttributeContainer) parent;
1197        if (operation == Operation.ADD)
1198            addChild(LocalAttributeContainer.LOCAL_ATTRIBUTE_PROPERTY, child);
1199        else if (operation == Operation.REMOVE) {
1200            target.removeAttributeReference(child);
1201        } else {
1202            canAdd = true;
1203        }
1204    }
1205    
1206    public void visit(LocalAttribute child) {
1207        if (parent instanceof LocalAttributeContainer) {
1208            //OK
1209
} else if (operation == null) {
1210            canAdd = false;
1211            return;
1212        } else {
1213            assert false: "Wrong parent "+parent.getClass().getName();
1214        }
1215        LocalAttributeContainer target = (LocalAttributeContainer) parent;
1216        if (operation == Operation.ADD)
1217            addChild(LocalAttributeContainer.LOCAL_ATTRIBUTE_PROPERTY, child);
1218        else if (operation == Operation.REMOVE) {
1219            target.removeLocalAttribute(child);
1220        } else {
1221            canAdd = true;
1222        }
1223    }
1224
1225    public void visit(SimpleContent child) {
1226        if (parent instanceof ComplexContentRestriction) {
1227            ComplexContentRestriction target = (ComplexContentRestriction) parent;
1228            if (operation == Operation.ADD) {
1229                if (index > -1) {
1230                    addChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
1231                } else {
1232                    target.setDefinition(child);
1233                }
1234            } else if (operation == Operation.REMOVE) {
1235                removeChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
1236            } else {
1237                canAdd = (target.getDefinition() == null);
1238            }
1239        } else if (parent instanceof ComplexType) {
1240            ComplexType target = (ComplexType) parent;
1241            if (operation == Operation.ADD) {
1242                if (index > -1) {
1243                    addChild(ComplexType.DEFINITION_PROPERTY, child);
1244                } else {
1245                    target.setDefinition(child);
1246                }
1247            } else if (operation == Operation.REMOVE) {
1248                removeChild(ComplexType.DEFINITION_PROPERTY, child);
1249            } else {
1250                canAdd = (target.getDefinition() == null);
1251            }
1252        } else if (operation == null) {
1253            canAdd = false;
1254            return;
1255        } else {
1256            assert false: "Wrong parent "+parent.getClass().getName();
1257        }
1258    }
1259
1260    public void visit(ComplexContent child) {
1261        if (parent instanceof ComplexContentRestriction) {
1262            ComplexContentRestriction target = (ComplexContentRestriction) parent;
1263            if (operation == Operation.ADD) {
1264                if (index > -1) {
1265                    addChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
1266                } else {
1267                    target.setDefinition(child);
1268                }
1269            } else if (operation == Operation.REMOVE) {
1270                removeChild(ComplexContentRestriction.DEFINITION_CHANGED_PROPERTY, child);
1271            } else {
1272                canAdd = (target.getDefinition() == null);
1273            }
1274        } else if (parent instanceof ComplexType) {
1275            ComplexType target = (ComplexType) parent;
1276            if (operation == Operation.ADD) {
1277                if (index > -1) {
1278                    addChild(ComplexType.DEFINITION_PROPERTY, child);
1279                } else {
1280                    target.setDefinition(child);
1281                }
1282            } else if (operation == Operation.REMOVE) {
1283                removeChild(ComplexType.DEFINITION_PROPERTY, child);
1284            } else {
1285                canAdd = (target.getDefinition() == null);
1286            }
1287        } else if (operation == null) {
1288            canAdd = false;
1289            return;
1290        } else {
1291            assert false: "Wrong parent "+parent.getClass().getName();
1292        }
1293        
1294    }
1295
1296    public void visit(AnyAttribute child) {
1297        if (parent instanceof LocalAttributeContainer) {
1298            //OK
1299
} else if (operation == null) {
1300            canAdd = false;
1301            return;
1302        } else {
1303            assert false: "Wrong parent "+parent.getClass().getName();
1304        }
1305        LocalAttributeContainer target = (LocalAttributeContainer) parent;
1306        if (operation == Operation.ADD) {
1307            if (index > -1) {
1308                addChild(LocalAttributeContainer.ANY_ATTRIBUTE_PROPERTY, child);
1309            } else {
1310                target.setAnyAttribute(child);
1311            }
1312        } else if (operation == Operation.REMOVE) {
1313            removeChild(LocalAttributeContainer.ANY_ATTRIBUTE_PROPERTY, child);
1314        } else {
1315            canAdd = (target.getAnyAttribute() == null);
1316        }
1317    }
1318
1319    public void visit(Length child) {
1320        if (parent instanceof SimpleRestriction) {
1321            //OK
1322
} else if (operation == null) {
1323            canAdd = false;
1324            return;
1325        } else {
1326            assert false: "Wrong parent "+parent.getClass().getName();
1327        }
1328        SimpleRestriction target = (SimpleRestriction) parent;
1329        if (operation == Operation.ADD)
1330            addChild(SimpleRestriction.LENGTH_PROPERTY, child);
1331        else if (operation == Operation.REMOVE) {
1332            target.removeLength(child);
1333        } else {
1334            canAdd = true;
1335        }
1336    }
1337
1338    public void visit(Field child) {
1339        if (parent instanceof Constraint) {
1340            //OK
1341
} else if (operation == null) {
1342            canAdd = false;
1343            return;
1344        } else {
1345            assert false: "Wrong parent "+parent.getClass().getName();
1346        }
1347        Constraint target = (Constraint) parent;
1348        if (operation == Operation.ADD)
1349            addChild(Constraint.FIELD_PROPERTY, child);
1350        else if (operation == Operation.REMOVE) {
1351            target.deleteField(child);
1352        } else {
1353            canAdd = true;
1354        }
1355    }
1356}
1357
Popular Tags