KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xslt > model > impl > 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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xslt.model.impl;
20
21 import org.netbeans.modules.xml.xam.AbstractComponent;
22 import org.netbeans.modules.xml.xam.ComponentUpdater;
23 import org.netbeans.modules.xslt.model.ApplyImports;
24 import org.netbeans.modules.xslt.model.ApplyTemplates;
25 import org.netbeans.modules.xslt.model.Attribute;
26 import org.netbeans.modules.xslt.model.AttributeSet;
27 import org.netbeans.modules.xslt.model.CallTemplate;
28 import org.netbeans.modules.xslt.model.Choose;
29 import org.netbeans.modules.xslt.model.Copy;
30 import org.netbeans.modules.xslt.model.CopyOf;
31 import org.netbeans.modules.xslt.model.Document;
32 import org.netbeans.modules.xslt.model.Element;
33 import org.netbeans.modules.xslt.model.ForEach;
34 import org.netbeans.modules.xslt.model.If;
35 import org.netbeans.modules.xslt.model.Import;
36 import org.netbeans.modules.xslt.model.Include;
37 import org.netbeans.modules.xslt.model.Key;
38 import org.netbeans.modules.xslt.model.LiteralResultElement;
39 import org.netbeans.modules.xslt.model.Namespace;
40 import org.netbeans.modules.xslt.model.Number;
41 import org.netbeans.modules.xslt.model.Otherwise;
42 import org.netbeans.modules.xslt.model.Output;
43 import org.netbeans.modules.xslt.model.Param;
44 import org.netbeans.modules.xslt.model.ParamContainer;
45 import org.netbeans.modules.xslt.model.Sequence;
46 import org.netbeans.modules.xslt.model.SequenceConstructor;
47 import org.netbeans.modules.xslt.model.SequenceElement;
48 import org.netbeans.modules.xslt.model.Sort;
49 import org.netbeans.modules.xslt.model.Stylesheet;
50 import org.netbeans.modules.xslt.model.StylesheetChild;
51 import org.netbeans.modules.xslt.model.Template;
52 import org.netbeans.modules.xslt.model.Text;
53 import org.netbeans.modules.xslt.model.ValueOf;
54 import org.netbeans.modules.xslt.model.Variable;
55 import org.netbeans.modules.xslt.model.When;
56 import org.netbeans.modules.xslt.model.XslComponent;
57 import org.netbeans.modules.xslt.model.XslVisitor;
58
59
60 /**
61  * @author ads
62  *
63  */

64 class SyncUpdateVisitor implements ComponentUpdater<XslComponent>, XslVisitor
65 {
66
67     /* (non-Javadoc)
68      * @see org.netbeans.modules.xml.xam.ComponentUpdater#update(org.netbeans.modules.xml.xam.Component, org.netbeans.modules.xml.xam.Component, org.netbeans.modules.xml.xam.ComponentUpdater.Operation)
69      */

70     public void update( XslComponent target, XslComponent child,
71             Operation operation )
72     {
73         update(target, child, -1, operation);
74     }
75
76     /* (non-Javadoc)
77      * @see org.netbeans.modules.xml.xam.ComponentUpdater#update(org.netbeans.modules.xml.xam.Component, org.netbeans.modules.xml.xam.Component, int, org.netbeans.modules.xml.xam.ComponentUpdater.Operation)
78      */

79     public void update( XslComponent target, XslComponent child, int index,
80             Operation operation )
81     {
82         assert target != null;
83         assert child != null;
84         assert operation == null || operation == Operation.ADD ||
85             operation == Operation.REMOVE;
86
87         myParent = target;
88         myIndex = index;
89         myOperation = operation;
90         child.accept(this);
91         
92     }
93     
94     /* (non-Javadoc)
95      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Stylesheet)
96      */

97     public void visit( Stylesheet stylesheet )
98     {
99         assert false : "Should never add or remove stylesheet root";
100     }
101
102     /* (non-Javadoc)
103      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Template)
104      */

105     public void visit( Template template )
106     {
107         updateStylesheetChild(template);
108     }
109
110     /* (non-Javadoc)
111      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.ApplyTemplates)
112      */

113     public void visit( ApplyTemplates applyTemplates ) {
114         updateChildInSequenceConstructor( applyTemplates );
115     }
116
117     /* (non-Javadoc)
118      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Attribute)
119      */

120     public void visit( Attribute attribute ) {
121         if ( getParent() instanceof AttributeSet ) {
122             AttributeSet set = (AttributeSet) getParent();
123             if ( isAdd() ) {
124                 set.addAttribute(attribute, getIndex() );
125             }
126             else if( isRemove() )
127                 set.removeAttribute( attribute );
128         }
129         else {
130             updateChildInSequenceConstructor( attribute );
131         }
132     }
133
134     /* (non-Javadoc)
135      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.AttributeSet)
136      */

137     public void visit( AttributeSet attributeSet ) {
138         updateStylesheetChild( attributeSet );
139     }
140
141     /* (non-Javadoc)
142      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.CallTemplate)
143      */

144     public void visit( CallTemplate callTemplate ) {
145         updateChildInSequenceConstructor( callTemplate );
146     }
147
148     /* (non-Javadoc)
149      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Choose)
150      */

151     public void visit( Choose choose ) {
152         updateChildInSequenceConstructor( choose );
153     }
154
155     /* (non-Javadoc)
156      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Copy)
157      */

158     public void visit( Copy copy ) {
159         updateChildInSequenceConstructor( copy );
160     }
161
162     /* (non-Javadoc)
163      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.CopyOf)
164      */

165     public void visit( CopyOf copyOf ) {
166         updateChildInSequenceConstructor( copyOf );
167     }
168
169     /* (non-Javadoc)
170      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Document)
171      */

172     public void visit( Document document ) {
173         updateChildInSequenceConstructor( document );
174     }
175
176     /* (non-Javadoc)
177      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Element)
178      */

179     public void visit( Element element ) {
180         updateChildInSequenceConstructor( element );
181     }
182
183     /* (non-Javadoc)
184      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.ForEach)
185      */

186     public void visit( ForEach forEach ) {
187         updateChildInSequenceConstructor( forEach );
188     }
189
190     /* (non-Javadoc)
191      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.If)
192      */

193     public void visit( If iff ) {
194         updateChildInSequenceConstructor( iff );
195     }
196
197     /* (non-Javadoc)
198      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Import)
199      */

200     public void visit( Import impt ) {
201         assert getParent() instanceof Stylesheet;
202         Stylesheet stylesheet = (Stylesheet)getParent();
203         if ( isAdd() ) {
204             stylesheet.addImport(impt, getIndex() );
205         }
206         else if ( isRemove() ) {
207             stylesheet.removeImport(impt);
208         }
209     }
210
211     /* (non-Javadoc)
212      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Include)
213      */

214     public void visit( Include include ) {
215         updateStylesheetChild( include );
216     }
217
218     /* (non-Javadoc)
219      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Key)
220      */

221     public void visit( Key key ) {
222         updateStylesheetChild( key );
223     }
224
225     /* (non-Javadoc)
226      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.LiteralResultElement)
227      */

228     public void visit( LiteralResultElement element ) {
229         updateChildInSequenceConstructor( element );
230     }
231
232     /* (non-Javadoc)
233      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Namespace)
234      */

235     public void visit( Namespace namespace ) {
236         updateChildInSequenceConstructor( namespace );
237     }
238
239     /* (non-Javadoc)
240      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Number)
241      */

242     public void visit( Number JavaDoc number ) {
243         updateChildInSequenceConstructor( number );
244     }
245
246     /* (non-Javadoc)
247      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Otherwise)
248      */

249     public void visit( Otherwise otherwise ) {
250         assert getParent() instanceof Choose;
251         Choose choose = (Choose) getParent();
252         if ( isAdd() ) {
253             if ( getIndex() > -1 ) {
254                 addChild( Choose.OTHERWISE_PROPERTY , otherwise );
255             }
256             else {
257                 choose.setOtherwise(otherwise);
258             }
259         }
260         else if ( isRemove() ) {
261             removeChild( Choose.OTHERWISE_PROPERTY , otherwise );
262         }
263     }
264
265     /* (non-Javadoc)
266      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Output)
267      */

268     public void visit( Output output ) {
269         updateStylesheetChild( output );
270     }
271
272     /* (non-Javadoc)
273      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Param)
274      */

275     public void visit( Param param ) {
276         if ( getParent() instanceof ParamContainer ) {
277             ParamContainer container = (ParamContainer) getParent();
278             if ( isAdd() ) {
279                 container.addParam(param, getIndex() );
280             }
281             else if ( isRemove() ) {
282                 container.removeParam(param);
283             }
284         }
285         else {
286             updateStylesheetChild( param );
287         }
288     }
289
290     /* (non-Javadoc)
291      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Sequence)
292      */

293     public void visit( Sequence sequence ) {
294         updateChildInSequenceConstructor( sequence );
295     }
296
297     /* (non-Javadoc)
298      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Sort)
299      */

300     public void visit( Sort sort ) {
301         if ( getParent() instanceof ApplyTemplates ) {
302             ApplyTemplates applyTemplates = (ApplyTemplates) getParent();
303             if ( isAdd() ) {
304                 applyTemplates.addChildElement(sort, getIndex());
305             }
306             else if ( isRemove() ) {
307                 applyTemplates.removeChildElement(sort);
308             }
309         }
310         else {
311             assert getParent() instanceof ForEach;
312             ForEach forEach = (ForEach) getParent();
313             if (isAdd()) {
314                 forEach.addSort(sort, getIndex());
315             }
316             else if (isRemove()) {
317                 forEach.removeSort(sort);
318             }
319         }
320     }
321
322     /* (non-Javadoc)
323      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Text)
324      */

325     public void visit( Text text ) {
326         updateChildInSequenceConstructor( text );
327     }
328
329     /* (non-Javadoc)
330      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.ValueOf)
331      */

332     public void visit( ValueOf valueOf ) {
333         updateChildInSequenceConstructor( valueOf );
334     }
335
336     /* (non-Javadoc)
337      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Variable)
338      */

339     public void visit( Variable variable ) {
340         if ( getParent() instanceof Stylesheet ) {
341             updateStylesheetChild( variable );
342         }
343         else {
344             updateChildInSequenceConstructor( variable );
345         }
346     }
347
348     /* (non-Javadoc)
349      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.When)
350      */

351     public void visit( When when ) {
352         assert getParent() instanceof Choose;
353         Choose choose = (Choose) getParent();
354         if ( isAdd() ) {
355             choose.addWhen(when, getIndex() );
356         }
357         else if ( isRemove() ) {
358             choose.removeWhen(when);
359         }
360     }
361     
362     /* (non-Javadoc)
363      * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.ApplyImports)
364      */

365     public void visit( ApplyImports impt ) {
366         updateChildInSequenceConstructor(impt);
367     }
368     
369     private void updateStylesheetChild( StylesheetChild child ) {
370         assert getParent() instanceof Stylesheet;
371         if ( isAdd() ) {
372             ((Stylesheet)getParent()).addStylesheetChild( child, getIndex() );
373         }
374         else if ( isRemove() ) {
375             ((Stylesheet)getParent()).removeStylesheetChild( child );
376         }
377     }
378     
379     private void updateChildInSequenceConstructor( SequenceElement element ) {
380         assert getParent() instanceof SequenceConstructor;
381         SequenceConstructor constructor = (SequenceConstructor) getParent();
382         if ( isAdd() ) {
383             constructor.addSequenceChild(element, getIndex());
384         }
385         else if ( isRemove() ){
386             constructor.removeSequenceChild(element);
387         }
388     }
389     
390     private void removeChild( String JavaDoc propertyName, Otherwise otherwise ) {
391         assert getParent() instanceof AbstractComponent;
392         ((AbstractComponent<XslComponent>) getParent()).removeChild(
393                 propertyName, otherwise );
394     }
395
396     private void addChild( String JavaDoc propertyName, XslComponent otherwise ) {
397         assert getParent() instanceof AbstractComponent;
398         ((AbstractComponent<XslComponent>) getParent()).insertAtIndex(
399                 propertyName, otherwise, getIndex());
400     }
401     
402     private boolean isAdd() {
403         return getOperation() == Operation.ADD;
404     }
405     
406     private boolean isRemove() {
407         return getOperation() == Operation.REMOVE;
408     }
409
410     private XslComponent getParent() {
411         return myParent;
412     }
413     
414     private int getIndex() {
415         return myIndex;
416     }
417     
418     private Operation getOperation() {
419         return myOperation;
420     }
421     
422     private XslComponent myParent;
423     
424     private int myIndex;
425     
426     private Operation myOperation;
427
428 }
429
Popular Tags