001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.model.impl;
020:
021: import org.netbeans.modules.xml.xam.AbstractComponent;
022: import org.netbeans.modules.xml.xam.ComponentUpdater;
023: import org.netbeans.modules.xslt.model.ApplyImports;
024: import org.netbeans.modules.xslt.model.ApplyTemplates;
025: import org.netbeans.modules.xslt.model.Attribute;
026: import org.netbeans.modules.xslt.model.AttributeSet;
027: import org.netbeans.modules.xslt.model.CallTemplate;
028: import org.netbeans.modules.xslt.model.Choose;
029: import org.netbeans.modules.xslt.model.Copy;
030: import org.netbeans.modules.xslt.model.CopyOf;
031: import org.netbeans.modules.xslt.model.Document;
032: import org.netbeans.modules.xslt.model.Element;
033: import org.netbeans.modules.xslt.model.ForEach;
034: import org.netbeans.modules.xslt.model.If;
035: import org.netbeans.modules.xslt.model.Import;
036: import org.netbeans.modules.xslt.model.Include;
037: import org.netbeans.modules.xslt.model.Key;
038: import org.netbeans.modules.xslt.model.LiteralResultElement;
039: import org.netbeans.modules.xslt.model.Namespace;
040: import org.netbeans.modules.xslt.model.Number;
041: import org.netbeans.modules.xslt.model.Otherwise;
042: import org.netbeans.modules.xslt.model.Output;
043: import org.netbeans.modules.xslt.model.Param;
044: import org.netbeans.modules.xslt.model.ParamContainer;
045: import org.netbeans.modules.xslt.model.Sequence;
046: import org.netbeans.modules.xslt.model.SequenceConstructor;
047: import org.netbeans.modules.xslt.model.SequenceElement;
048: import org.netbeans.modules.xslt.model.Sort;
049: import org.netbeans.modules.xslt.model.Stylesheet;
050: import org.netbeans.modules.xslt.model.StylesheetChild;
051: import org.netbeans.modules.xslt.model.Template;
052: import org.netbeans.modules.xslt.model.Text;
053: import org.netbeans.modules.xslt.model.ValueOf;
054: import org.netbeans.modules.xslt.model.Variable;
055: import org.netbeans.modules.xslt.model.When;
056: import org.netbeans.modules.xslt.model.XslComponent;
057: import org.netbeans.modules.xslt.model.XslVisitor;
058:
059: /**
060: * @author ads
061: *
062: */
063: class SyncUpdateVisitor implements ComponentUpdater<XslComponent>,
064: XslVisitor {
065:
066: /* (non-Javadoc)
067: * @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)
068: */
069: public void update(XslComponent target, XslComponent child,
070: Operation operation) {
071: update(target, child, -1, operation);
072: }
073:
074: /* (non-Javadoc)
075: * @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)
076: */
077: public void update(XslComponent target, XslComponent child,
078: int index, Operation operation) {
079: assert target != null;
080: assert child != null;
081: assert operation == null || operation == Operation.ADD
082: || operation == Operation.REMOVE;
083:
084: myParent = target;
085: myIndex = index;
086: myOperation = operation;
087: child.accept(this );
088:
089: }
090:
091: /* (non-Javadoc)
092: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Stylesheet)
093: */
094: public void visit(Stylesheet stylesheet) {
095: assert false : "Should never add or remove stylesheet root";
096: }
097:
098: /* (non-Javadoc)
099: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Template)
100: */
101: public void visit(Template template) {
102: updateStylesheetChild(template);
103: }
104:
105: /* (non-Javadoc)
106: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.ApplyTemplates)
107: */
108: public void visit(ApplyTemplates applyTemplates) {
109: updateChildInSequenceConstructor(applyTemplates);
110: }
111:
112: /* (non-Javadoc)
113: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Attribute)
114: */
115: public void visit(Attribute attribute) {
116: if (getParent() instanceof AttributeSet) {
117: AttributeSet set = (AttributeSet) getParent();
118: if (isAdd()) {
119: set.addAttribute(attribute, getIndex());
120: } else if (isRemove())
121: set.removeAttribute(attribute);
122: } else {
123: updateChildInSequenceConstructor(attribute);
124: }
125: }
126:
127: /* (non-Javadoc)
128: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.AttributeSet)
129: */
130: public void visit(AttributeSet attributeSet) {
131: updateStylesheetChild(attributeSet);
132: }
133:
134: /* (non-Javadoc)
135: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.CallTemplate)
136: */
137: public void visit(CallTemplate callTemplate) {
138: updateChildInSequenceConstructor(callTemplate);
139: }
140:
141: /* (non-Javadoc)
142: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Choose)
143: */
144: public void visit(Choose choose) {
145: updateChildInSequenceConstructor(choose);
146: }
147:
148: /* (non-Javadoc)
149: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Copy)
150: */
151: public void visit(Copy copy) {
152: updateChildInSequenceConstructor(copy);
153: }
154:
155: /* (non-Javadoc)
156: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.CopyOf)
157: */
158: public void visit(CopyOf copyOf) {
159: updateChildInSequenceConstructor(copyOf);
160: }
161:
162: /* (non-Javadoc)
163: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Document)
164: */
165: public void visit(Document document) {
166: updateChildInSequenceConstructor(document);
167: }
168:
169: /* (non-Javadoc)
170: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Element)
171: */
172: public void visit(Element element) {
173: updateChildInSequenceConstructor(element);
174: }
175:
176: /* (non-Javadoc)
177: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.ForEach)
178: */
179: public void visit(ForEach forEach) {
180: updateChildInSequenceConstructor(forEach);
181: }
182:
183: /* (non-Javadoc)
184: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.If)
185: */
186: public void visit(If iff) {
187: updateChildInSequenceConstructor(iff);
188: }
189:
190: /* (non-Javadoc)
191: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Import)
192: */
193: public void visit(Import impt) {
194: assert getParent() instanceof Stylesheet;
195: Stylesheet stylesheet = (Stylesheet) getParent();
196: if (isAdd()) {
197: stylesheet.addImport(impt, getIndex());
198: } else if (isRemove()) {
199: stylesheet.removeImport(impt);
200: }
201: }
202:
203: /* (non-Javadoc)
204: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Include)
205: */
206: public void visit(Include include) {
207: updateStylesheetChild(include);
208: }
209:
210: /* (non-Javadoc)
211: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Key)
212: */
213: public void visit(Key key) {
214: updateStylesheetChild(key);
215: }
216:
217: /* (non-Javadoc)
218: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.LiteralResultElement)
219: */
220: public void visit(LiteralResultElement element) {
221: updateChildInSequenceConstructor(element);
222: }
223:
224: /* (non-Javadoc)
225: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Namespace)
226: */
227: public void visit(Namespace namespace) {
228: updateChildInSequenceConstructor(namespace);
229: }
230:
231: /* (non-Javadoc)
232: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Number)
233: */
234: public void visit(Number number) {
235: updateChildInSequenceConstructor(number);
236: }
237:
238: /* (non-Javadoc)
239: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Otherwise)
240: */
241: public void visit(Otherwise otherwise) {
242: assert getParent() instanceof Choose;
243: Choose choose = (Choose) getParent();
244: if (isAdd()) {
245: if (getIndex() > -1) {
246: addChild(Choose.OTHERWISE_PROPERTY, otherwise);
247: } else {
248: choose.setOtherwise(otherwise);
249: }
250: } else if (isRemove()) {
251: removeChild(Choose.OTHERWISE_PROPERTY, otherwise);
252: }
253: }
254:
255: /* (non-Javadoc)
256: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Output)
257: */
258: public void visit(Output output) {
259: updateStylesheetChild(output);
260: }
261:
262: /* (non-Javadoc)
263: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Param)
264: */
265: public void visit(Param param) {
266: if (getParent() instanceof ParamContainer) {
267: ParamContainer container = (ParamContainer) getParent();
268: if (isAdd()) {
269: container.addParam(param, getIndex());
270: } else if (isRemove()) {
271: container.removeParam(param);
272: }
273: } else {
274: updateStylesheetChild(param);
275: }
276: }
277:
278: /* (non-Javadoc)
279: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Sequence)
280: */
281: public void visit(Sequence sequence) {
282: updateChildInSequenceConstructor(sequence);
283: }
284:
285: /* (non-Javadoc)
286: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Sort)
287: */
288: public void visit(Sort sort) {
289: if (getParent() instanceof ApplyTemplates) {
290: ApplyTemplates applyTemplates = (ApplyTemplates) getParent();
291: if (isAdd()) {
292: applyTemplates.addChildElement(sort, getIndex());
293: } else if (isRemove()) {
294: applyTemplates.removeChildElement(sort);
295: }
296: } else {
297: assert getParent() instanceof ForEach;
298: ForEach forEach = (ForEach) getParent();
299: if (isAdd()) {
300: forEach.addSort(sort, getIndex());
301: } else if (isRemove()) {
302: forEach.removeSort(sort);
303: }
304: }
305: }
306:
307: /* (non-Javadoc)
308: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Text)
309: */
310: public void visit(Text text) {
311: updateChildInSequenceConstructor(text);
312: }
313:
314: /* (non-Javadoc)
315: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.ValueOf)
316: */
317: public void visit(ValueOf valueOf) {
318: updateChildInSequenceConstructor(valueOf);
319: }
320:
321: /* (non-Javadoc)
322: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.Variable)
323: */
324: public void visit(Variable variable) {
325: if (getParent() instanceof Stylesheet) {
326: updateStylesheetChild(variable);
327: } else {
328: updateChildInSequenceConstructor(variable);
329: }
330: }
331:
332: /* (non-Javadoc)
333: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.When)
334: */
335: public void visit(When when) {
336: assert getParent() instanceof Choose;
337: Choose choose = (Choose) getParent();
338: if (isAdd()) {
339: choose.addWhen(when, getIndex());
340: } else if (isRemove()) {
341: choose.removeWhen(when);
342: }
343: }
344:
345: /* (non-Javadoc)
346: * @see org.netbeans.modules.xslt.model.XslVisitor#visit(org.netbeans.modules.xslt.model.ApplyImports)
347: */
348: public void visit(ApplyImports impt) {
349: updateChildInSequenceConstructor(impt);
350: }
351:
352: private void updateStylesheetChild(StylesheetChild child) {
353: assert getParent() instanceof Stylesheet;
354: if (isAdd()) {
355: ((Stylesheet) getParent()).addStylesheetChild(child,
356: getIndex());
357: } else if (isRemove()) {
358: ((Stylesheet) getParent()).removeStylesheetChild(child);
359: }
360: }
361:
362: private void updateChildInSequenceConstructor(
363: SequenceElement element) {
364: assert getParent() instanceof SequenceConstructor;
365: SequenceConstructor constructor = (SequenceConstructor) getParent();
366: if (isAdd()) {
367: constructor.addSequenceChild(element, getIndex());
368: } else if (isRemove()) {
369: constructor.removeSequenceChild(element);
370: }
371: }
372:
373: private void removeChild(String propertyName, Otherwise otherwise) {
374: assert getParent() instanceof AbstractComponent;
375: ((AbstractComponent<XslComponent>) getParent()).removeChild(
376: propertyName, otherwise);
377: }
378:
379: private void addChild(String propertyName, XslComponent otherwise) {
380: assert getParent() instanceof AbstractComponent;
381: ((AbstractComponent<XslComponent>) getParent()).insertAtIndex(
382: propertyName, otherwise, getIndex());
383: }
384:
385: private boolean isAdd() {
386: return getOperation() == Operation.ADD;
387: }
388:
389: private boolean isRemove() {
390: return getOperation() == Operation.REMOVE;
391: }
392:
393: private XslComponent getParent() {
394: return myParent;
395: }
396:
397: private int getIndex() {
398: return myIndex;
399: }
400:
401: private Operation getOperation() {
402: return myOperation;
403: }
404:
405: private XslComponent myParent;
406:
407: private int myIndex;
408:
409: private Operation myOperation;
410:
411: }
|