1 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 64 class SyncUpdateVisitor implements ComponentUpdater<XslComponent>, XslVisitor 65 { 66 67 70 public void update( XslComponent target, XslComponent child, 71 Operation operation ) 72 { 73 update(target, child, -1, operation); 74 } 75 76 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 97 public void visit( Stylesheet stylesheet ) 98 { 99 assert false : "Should never add or remove stylesheet root"; 100 } 101 102 105 public void visit( Template template ) 106 { 107 updateStylesheetChild(template); 108 } 109 110 113 public void visit( ApplyTemplates applyTemplates ) { 114 updateChildInSequenceConstructor( applyTemplates ); 115 } 116 117 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 137 public void visit( AttributeSet attributeSet ) { 138 updateStylesheetChild( attributeSet ); 139 } 140 141 144 public void visit( CallTemplate callTemplate ) { 145 updateChildInSequenceConstructor( callTemplate ); 146 } 147 148 151 public void visit( Choose choose ) { 152 updateChildInSequenceConstructor( choose ); 153 } 154 155 158 public void visit( Copy copy ) { 159 updateChildInSequenceConstructor( copy ); 160 } 161 162 165 public void visit( CopyOf copyOf ) { 166 updateChildInSequenceConstructor( copyOf ); 167 } 168 169 172 public void visit( Document document ) { 173 updateChildInSequenceConstructor( document ); 174 } 175 176 179 public void visit( Element element ) { 180 updateChildInSequenceConstructor( element ); 181 } 182 183 186 public void visit( ForEach forEach ) { 187 updateChildInSequenceConstructor( forEach ); 188 } 189 190 193 public void visit( If iff ) { 194 updateChildInSequenceConstructor( iff ); 195 } 196 197 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 214 public void visit( Include include ) { 215 updateStylesheetChild( include ); 216 } 217 218 221 public void visit( Key key ) { 222 updateStylesheetChild( key ); 223 } 224 225 228 public void visit( LiteralResultElement element ) { 229 updateChildInSequenceConstructor( element ); 230 } 231 232 235 public void visit( Namespace namespace ) { 236 updateChildInSequenceConstructor( namespace ); 237 } 238 239 242 public void visit( Number number ) { 243 updateChildInSequenceConstructor( number ); 244 } 245 246 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 268 public void visit( Output output ) { 269 updateStylesheetChild( output ); 270 } 271 272 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 293 public void visit( Sequence sequence ) { 294 updateChildInSequenceConstructor( sequence ); 295 } 296 297 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 325 public void visit( Text text ) { 326 updateChildInSequenceConstructor( text ); 327 } 328 329 332 public void visit( ValueOf valueOf ) { 333 updateChildInSequenceConstructor( valueOf ); 334 } 335 336 339 public void visit( Variable variable ) { 340 if ( getParent() instanceof Stylesheet ) { 341 updateStylesheetChild( variable ); 342 } 343 else { 344 updateChildInSequenceConstructor( variable ); 345 } 346 } 347 348 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 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 propertyName, Otherwise otherwise ) { 391 assert getParent() instanceof AbstractComponent; 392 ((AbstractComponent<XslComponent>) getParent()).removeChild( 393 propertyName, otherwise ); 394 } 395 396 private void addChild( String 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 |