1 19 package org.netbeans.modules.xml.axi.impl; 20 21 import java.util.ArrayList ; 22 import java.util.List ; 23 import org.netbeans.modules.xml.axi.AXIComponent; 24 import org.netbeans.modules.xml.axi.AXIComponent.ComponentType; 25 import org.netbeans.modules.xml.axi.AXIDocument; 26 import org.netbeans.modules.xml.axi.AXIType; 27 import org.netbeans.modules.xml.axi.AnyAttribute; 28 import org.netbeans.modules.xml.axi.AnyElement; 29 import org.netbeans.modules.xml.axi.Attribute; 30 import org.netbeans.modules.xml.axi.Compositor; 31 import org.netbeans.modules.xml.axi.Element; 32 import org.netbeans.modules.xml.axi.visitor.DeepAXITreeVisitor; 33 import org.netbeans.modules.xml.schema.model.AttributeReference; 34 import org.netbeans.modules.xml.schema.model.ElementReference; 35 import org.netbeans.modules.xml.schema.model.GlobalAttribute; 36 import org.netbeans.modules.xml.schema.model.GlobalElement; 37 import org.netbeans.modules.xml.schema.model.LocalAttribute; 38 import org.netbeans.modules.xml.schema.model.LocalElement; 39 import org.netbeans.modules.xml.schema.model.SchemaComponent; 40 import org.netbeans.modules.xml.axi.ContentModel; 41 42 56 public class AXIModelUpdater extends DeepAXITreeVisitor { 57 58 61 public AXIModelUpdater(AXIModelImpl model) { 62 this.model = model; 63 } 64 65 69 public boolean doSync() { 70 try { 71 syncCompleted = false; 72 model.getRoot().accept(this); 74 75 for(AXIComponent child : model.getRoot().getChildren()) { 77 if(child instanceof ContentModel) 78 continue; 79 80 child.accept(this); 81 } 82 83 for(ContentModel contentModel : model.getRoot().getContentModels()) { 85 contentModel.accept(this); 86 } 87 syncCompleted = true; 88 } catch(Exception ex) { 89 } 91 return syncCompleted; 92 } 93 94 95 98 public void syncOne(AXIComponent component) { 99 component.accept(this); 100 } 101 102 108 protected void visitChildren(AXIComponent original) { 109 if(!original.canVisitChildren()) 110 return; 111 112 if(original.getComponentType() == ComponentType.PROXY && 114 original.getModel() == original.getOriginal().getModel()) { 115 return; 116 } 117 118 AXIComponentCreator creator = new AXIComponentCreator(model); 119 AXIComponent altered = getAltered(original); 120 assert(altered != null); 121 122 List <AXIComponent> modifiedChildren = synchronize(original, altered); 123 if( (modifiedChildren == null) || 124 (original instanceof AXIDocument) ) { 125 return; 126 } 127 128 for(AXIComponent child : modifiedChildren) { 130 child.accept(this); 131 } 132 } 133 134 138 private AXIComponent getAltered(AXIComponent original) { 139 if(original.getComponentType() == ComponentType.PROXY) { 140 return original.getOriginal(); 141 } 142 143 AXIComponentCreator creator = new AXIComponentCreator(model); 145 return creator.createNew(original.getPeer()); 146 } 147 148 154 private List <AXIComponent> synchronize(AXIComponent original, AXIComponent altered) { 155 List <AXIComponent> modifiedChildren = removeRemovedChildren(original, altered); 157 158 addNewChildren(original, altered); 160 161 return modifiedChildren; 162 } 163 164 167 private List <AXIComponent> removeRemovedChildren(AXIComponent original, AXIComponent altered) { 168 List <AXIComponent> removedChildren = new ArrayList <AXIComponent>(); 169 List <AXIComponent> dirtyChildren = new ArrayList <AXIComponent>(); 170 for(AXIComponent oChild : original.getChildren()) { 171 int index = childExists(oChild, altered, true); 172 if( (index == -1) || 173 oChild.getPeer().getParent() == null || 174 oChild.getPeer().getModel() == null) { 175 removedChildren.add(oChild); 176 continue; 177 } 178 dirtyChildren.add(oChild); 179 } 180 for(AXIComponent child : removedChildren) { 181 original.removeChild(child); 182 } 183 184 return dirtyChildren; 185 } 186 187 191 private void addNewChildren(AXIComponent original, AXIComponent altered) { 192 int size = altered.getChildren().size(); 193 for(int index=0; index<size; index++) { 194 AXIComponent aChild = altered.getChildren().get(index); 195 int indexInOriginalTree = childExists(aChild, original, false); 196 if(indexInOriginalTree == -1) { 198 if(original.getComponentType() == ComponentType.PROXY) { 199 AXIComponent proxy = model.getComponentFactory().createProxy(aChild); 200 original.addChildAtIndex(proxy, index); } else { 202 original.addChildAtIndex(aChild, index); } 204 continue; 205 } 206 if(aChild.getComponentType() == ComponentType.PROXY) { 208 aChild.getSharedComponent().removeListener(aChild); 209 } 210 } 211 } 212 213 217 private int childExists(AXIComponent child, AXIComponent parent, boolean checkOriginal) { 218 int size = parent.getChildren().size(); 219 for(int index=0; index<size; index++) { 220 AXIComponent c = parent.getChildren().get(index); 221 if(c.getPeer() == child.getPeer()) { 222 if(checkOriginal) { 223 if(!validateOriginal(child, c)) 224 return -1; 225 } 226 return index; 227 } 228 } 229 230 return -1; 231 } 232 233 237 private boolean validateOriginal(AXIComponent original, AXIComponent altered) { 238 PeerValidator validator = new PeerValidator(); 240 if(!validator.validate(original)) 241 return false; 242 243 if(altered.getComponentType() == ComponentType.PROXY && 248 original.getComponentType() != ComponentType.PROXY) { 249 return false; 250 } 251 252 return true; 253 } 254 255 258 public void visit(AXIDocument document) { 259 Util.updateAXIDocument(document); 260 visitChildren(document); 261 } 262 263 266 public void visit(Element element) { 267 if(element instanceof ElementImpl) 268 visit((ElementImpl)element); 269 if(element instanceof ElementRef) 270 visit((ElementRef)element); 271 if(element instanceof ElementProxy) 272 ((ElementProxy)element).forceFireEvent(); 273 } 274 275 280 public void visit(ElementImpl element) { 281 SchemaComponent schemaComponent = element.getPeer(); 282 if(schemaComponent instanceof LocalElement) 283 Util.updateLocalElement(element); 284 if(schemaComponent instanceof GlobalElement) 285 Util.updateGlobalElement(element); 286 287 SchemaComponent newType = Util.getSchemaType(model, element.getPeer()); 289 AXIType axiType = Util.getAXIType(element, newType); 290 if(element.getType() != axiType) 291 element.setType(axiType); 292 293 visitChildren(element); 295 } 296 297 public void visit(ElementRef elementRef) { 298 ElementReference ref = (ElementReference)elementRef.getPeer(); 299 GlobalElement newGE = ref.getRef().get(); 300 SchemaComponent originalGE = elementRef.getReferent().getPeer(); 301 if(originalGE == newGE) { 302 Util.updateElementReference(elementRef); 303 elementRef.forceFireEvent(); 304 visitChildren(elementRef); 305 return; 306 } 307 AXIComponent newElement = Util.lookup(elementRef.getModel(), newGE); 309 if(newElement != null && newElement instanceof Element) { 310 elementRef.setRef((Element)newElement); 311 elementRef.forceFireEvent(); 312 } 313 } 314 315 public void visit(Attribute attribute) { 316 if(attribute instanceof AttributeImpl) 317 visit((AttributeImpl)attribute); 318 if(attribute instanceof AttributeRef) 319 visit((AttributeRef)attribute); 320 if(attribute instanceof AttributeProxy) 321 ((AttributeProxy)attribute).forceFireEvent(); 322 } 323 324 public void visit(AttributeImpl attribute) { 325 SchemaComponent schemaComponent = attribute.getPeer(); 326 if(schemaComponent instanceof LocalAttribute) { 327 Util.updateLocalAttribute(attribute); 328 AXIType type = Util.getDatatype(attribute.getModel(), (LocalAttribute)schemaComponent); 329 if(type != null) 330 attribute.setType(type); 331 } 332 if(schemaComponent instanceof GlobalAttribute) { 333 Util.updateGlobalAttribute(attribute); 334 AXIType type = Util.getDatatype(attribute.getModel(), (GlobalAttribute)schemaComponent); 335 if(type != null) 336 attribute.setType(type); 337 } 338 } 339 340 public void visit(AttributeRef attributeRef) { 341 AttributeReference ref = (AttributeReference)attributeRef.getPeer(); 342 SchemaComponent originalGA = attributeRef.getReferent().getPeer(); 343 GlobalAttribute newGA = ref.getRef().get(); 344 if(originalGA == newGA) { 345 Util.updateAttributeReference(attributeRef); 346 attributeRef.forceFireEvent(); 347 return; 348 } 349 350 AXIComponent newAttr = Util.lookup(attributeRef.getModel(), newGA); 352 if(newAttr != null && newAttr instanceof Attribute) { 353 attributeRef.setRef((Attribute)newAttr); 354 attributeRef.forceFireEvent(); 355 } 356 } 357 358 public void visit(Compositor compositor) { 359 Util.updateCompositor(compositor); 360 visitChildren(compositor); 361 } 362 363 public void visit(ContentModel contentModel) { 364 Util.updateContentModel(contentModel); 365 visitChildren(contentModel); 366 } 367 368 public void visit(AnyAttribute attribute) { 369 Util.updateAnyAttribute(attribute); 370 } 371 372 public void visit(AnyElement element) { 373 Util.updateAnyElement(element); 374 } 375 376 private AXIModelImpl model; 380 private boolean syncCompleted; 381 } 382 | Popular Tags |