1 17 package org.eclipse.emf.ecore.sdo.impl; 18 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.Externalizable ; 23 import java.io.IOException ; 24 import java.io.ObjectInput ; 25 import java.io.ObjectOutput ; 26 import java.io.Serializable ; 27 import java.util.Iterator ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.eclipse.emf.common.notify.Adapter; 33 import org.eclipse.emf.common.notify.Notification; 34 import org.eclipse.emf.common.notify.NotificationChain; 35 import org.eclipse.emf.common.notify.Notifier; 36 import org.eclipse.emf.common.notify.impl.AdapterImpl; 37 import org.eclipse.emf.common.util.URI; 38 import org.eclipse.emf.ecore.EClass; 39 import org.eclipse.emf.ecore.EClassifier; 40 import org.eclipse.emf.ecore.EObject; 41 import org.eclipse.emf.ecore.EPackage; 42 import org.eclipse.emf.ecore.EStructuralFeature; 43 import org.eclipse.emf.ecore.InternalEObject; 44 import org.eclipse.emf.ecore.impl.ENotificationImpl; 45 import org.eclipse.emf.ecore.impl.EObjectImpl; 46 import org.eclipse.emf.ecore.resource.Resource; 47 import org.eclipse.emf.ecore.resource.ResourceSet; 48 import org.eclipse.emf.ecore.sdo.EChangeSummary; 49 import org.eclipse.emf.ecore.sdo.EDataGraph; 50 import org.eclipse.emf.ecore.sdo.EDataObject; 51 import org.eclipse.emf.ecore.sdo.EType; 52 import org.eclipse.emf.ecore.sdo.SDOFactory; 53 import org.eclipse.emf.ecore.sdo.SDOPackage; 54 import org.eclipse.emf.ecore.sdo.util.SDOUtil; 55 import org.eclipse.emf.ecore.util.BasicExtendedMetaData; 56 import org.eclipse.emf.ecore.util.EcoreUtil; 57 import org.eclipse.emf.ecore.util.ExtendedMetaData; 58 59 import commonj.sdo.ChangeSummary; 60 import commonj.sdo.DataGraph; 61 import commonj.sdo.DataObject; 62 import commonj.sdo.Type; 63 64 65 84 public class EDataGraphImpl extends EObjectImpl implements EDataGraph, DataGraph, Adapter, EDataGraph.Internal, Serializable 85 { 86 94 protected static final ResourceSet RESOURCE_SET_EDEFAULT = null; 95 96 104 protected ResourceSet resourceSet = RESOURCE_SET_EDEFAULT; 105 106 114 protected static final Resource ROOT_RESOURCE_EDEFAULT = null; 115 116 124 protected EChangeSummary eChangeSummary = null; 125 126 134 protected EObject eRootObject = null; 135 136 139 protected ExtendedMetaData extendedMetaData; 140 141 146 protected EDataGraphImpl() 147 { 148 super(); 149 } 150 151 156 protected EClass eStaticClass() 157 { 158 return SDOPackage.eINSTANCE.getEDataGraph(); 159 } 160 161 public boolean isAdapterForType(Object type) 162 { 163 return type == EDataGraph.class; 164 } 165 166 public void notifyChanged(Notification msg) 167 { 168 } 169 170 public Notifier getTarget() 171 { 172 return getResourceSet(); 173 } 174 175 public void setTarget(Notifier newTarget) 176 { 177 } 178 179 protected ExtendedMetaData getExtendedMetaData() 180 { 181 if (extendedMetaData == null) 182 { 183 getResourceSet(); 184 } 185 return extendedMetaData; 186 } 187 188 193 public ResourceSet getResourceSet() 194 { 195 if (resourceSet == null) 196 { 197 if (eResource() != null) 198 { 199 resourceSet = eResource().getResourceSet(); 200 } 201 if (resourceSet == null) 202 { 203 resourceSet = createResourceSet(); 204 } 205 initializeResourceSet(resourceSet); 206 } 207 return resourceSet; 208 } 209 210 protected ResourceSet createResourceSet() 211 { 212 return SDOUtil.createResourceSet(); 213 } 214 215 protected Resource createRootResource() 216 { 217 Resource result = resourceSet.createResource(URI.createURI("root.xml")); 218 result.getContents().clear(); 219 return result; 220 } 221 222 protected Resource createChangeSummaryResource() 223 { 224 Resource result = resourceSet.createResource(URI.createURI("change-summary.xml")); 225 result.getContents().clear(); 226 return result; 227 } 228 229 protected void initializeResourceSet(ResourceSet resourceSet) 230 { 231 if (resourceSet == null) 232 { 233 extendedMetaData = null; 234 } 235 else 236 { 237 extendedMetaData = new BasicExtendedMetaData(resourceSet.getPackageRegistry()); 238 resourceSet.eAdapters().add(this); 239 } 240 } 241 242 247 public void setResourceSetGen(ResourceSet newResourceSet) 248 { 249 ResourceSet oldResourceSet = resourceSet; 250 resourceSet = newResourceSet; 251 if (eNotificationRequired()) 252 eNotify(new ENotificationImpl(this, Notification.SET, SDOPackage.EDATA_GRAPH__RESOURCE_SET, oldResourceSet, resourceSet)); 253 } 254 255 public void setResourceSet(ResourceSet newResourceSet) 256 { 257 if (resourceSet != null) 258 { 259 resourceSet.eAdapters().remove(this); 260 } 261 setResourceSetGen(newResourceSet); 262 initializeResourceSet(newResourceSet); 263 } 264 265 270 public Resource getRootResource() 271 { 272 if (eRootObject == null) 273 { 274 Resource result = getResourceSet().getResource(URI.createURI("root.xml"), false); 275 if (result == null) 276 { 277 result = createRootResource(); 278 } 279 280 return result; 281 } 282 else 283 { 284 return eRootObject.eResource(); 285 } 286 } 287 288 public Resource getDataGraphResource() 289 { 290 Resource result = eResource(); 291 if (result == null) 292 { 293 result = getResourceSet().createResource(URI.createURI("all.datagraph")); 294 result.getContents().add(this); 295 } 296 return result; 297 } 298 299 304 public EChangeSummary getEChangeSummary() 305 { 306 return eChangeSummary; 307 } 308 309 314 public NotificationChain basicSetEChangeSummary(EChangeSummary newEChangeSummary, NotificationChain msgs) 315 { 316 EChangeSummary oldEChangeSummary = eChangeSummary; 317 eChangeSummary = newEChangeSummary; 318 if (eNotificationRequired()) 319 { 320 ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, SDOPackage.EDATA_GRAPH__ECHANGE_SUMMARY, oldEChangeSummary, newEChangeSummary); 321 if (msgs == null) msgs = notification; else msgs.add(notification); 322 } 323 return msgs; 324 } 325 326 331 public void setEChangeSummary(EChangeSummary newEChangeSummary) 332 { 333 if (newEChangeSummary != eChangeSummary) 334 { 335 NotificationChain msgs = null; 336 if (eChangeSummary != null) 337 msgs = ((InternalEObject)eChangeSummary).eInverseRemove(this, SDOPackage.ECHANGE_SUMMARY__EDATA_GRAPH, EChangeSummary.class, msgs); 338 if (newEChangeSummary != null) 339 msgs = ((InternalEObject)newEChangeSummary).eInverseAdd(this, SDOPackage.ECHANGE_SUMMARY__EDATA_GRAPH, EChangeSummary.class, msgs); 340 msgs = basicSetEChangeSummary(newEChangeSummary, msgs); 341 if (msgs != null) msgs.dispatch(); 342 } 343 else if (eNotificationRequired()) 344 eNotify(new ENotificationImpl(this, Notification.SET, SDOPackage.EDATA_GRAPH__ECHANGE_SUMMARY, newEChangeSummary, newEChangeSummary)); 345 } 346 347 352 public EObject getERootObject() 353 { 354 return eRootObject; 355 } 356 357 362 public void setERootObjectGen(EObject newERootObject) 363 { 364 EObject oldERootObject = eRootObject; 365 eRootObject = newERootObject; 366 if (eNotificationRequired()) 367 eNotify(new ENotificationImpl(this, Notification.SET, SDOPackage.EDATA_GRAPH__EROOT_OBJECT, oldERootObject, eRootObject)); 368 } 369 370 public void setERootObject(EObject newERootObject) 371 { 372 if (resourceSet != null) 375 { 376 List contents = getRootResource().getContents(); 380 if (eRootObject != null) 381 { 382 contents.clear(); 383 } 384 setERootObjectGen(newERootObject); 385 if (newERootObject != null) 386 { 387 contents.add(newERootObject); 388 } 389 } 390 else 391 { 392 setERootObjectGen(newERootObject); 395 Resource rootResource = newERootObject.eResource(); 396 if (rootResource != null) 397 { 398 if (rootResource.getResourceSet() == null) 401 { 402 getResourceSet().getResources().add(rootResource); 405 } 406 else 407 { 408 setResourceSet(rootResource.getResourceSet()); 411 } 412 } 413 else 414 { 415 getResourceSet(); 418 createRootResource().getContents().add(newERootObject); 419 } 420 } 421 } 422 423 428 public EClassifier getEClassifier(String namespaceURI, String typeName) 429 { 430 ExtendedMetaData extendedMetaData = getExtendedMetaData(); 431 EPackage ePackage = extendedMetaData.getPackage(namespaceURI); 432 if (ePackage != null) 433 { 434 EClassifier eClassifier = ePackage.getEClassifier(typeName); 435 if (eClassifier == null) 436 { 437 eClassifier = extendedMetaData.getType(ePackage, typeName); 438 } 439 return eClassifier; 440 } 441 else 442 { 443 return null; 444 } 445 } 446 447 452 public Type getType(String namespaceURI, String typeName) 453 { 454 return getEType(namespaceURI, typeName); 455 } 456 457 public EType getEType(String namespaceURI, String typeName) 458 { 459 EClassifier eClassifier = getEClassifier(namespaceURI, typeName); 460 return eClassifier == null ? null : SDOUtil.adaptType(eClassifier); 461 } 462 463 468 public EObject createEObject(EClass type) 469 { 470 return EcoreUtil.create(type); 471 } 472 473 public EDataObject createEDataObject(EType type) 474 { 475 return SDOUtil.create(type); 476 } 477 478 public DataObject getRootObject() 479 { 480 return (EDataObject)getERootObject(); 481 } 482 483 public DataObject createRootObject(String namespaceURI, String typeName) 484 { 485 EDataObject eDataObject = SDOUtil.create(getEType(namespaceURI, typeName)); 486 setERootObject(eDataObject); 487 return eDataObject; 488 } 489 490 public DataObject createRootObject(Type type) 491 { 492 EDataObject eDataObject = SDOUtil.create((EType)type); 493 setERootObject(eDataObject); 494 return eDataObject; 495 } 496 497 public ChangeSummary getChangeSummary() 498 { 499 EChangeSummary changeSummary = getEChangeSummary(); 501 if (changeSummary == null) 502 { 503 getRootResource(); 504 changeSummary = createEChangeSummary(); 505 setEChangeSummary(changeSummary); 506 } 507 508 if (changeSummary.eResource() == null) 509 { 510 Resource changeSummaryResource = createChangeSummaryResource(); 511 changeSummaryResource.getContents().add(changeSummary); 512 } 513 514 return changeSummary; 515 } 516 517 protected EChangeSummary createEChangeSummary() 518 { 519 return SDOFactory.eINSTANCE.createEChangeSummary(); 520 } 521 522 public static class EDataGraphExternalizable implements Externalizable 523 { 524 protected EDataGraph eDataGraph; 525 protected Map writeReplacements = new HashMap (); 526 527 public EDataGraphExternalizable() 528 { 529 } 530 531 public EDataGraphExternalizable(EDataGraph eDataGraph) 532 { 533 this.eDataGraph = eDataGraph; 534 } 535 536 public void writeExternal(ObjectOutput objectOutput) throws IOException 537 { 538 ByteArrayOutputStream byteArrayOutputStream = 539 new ByteArrayOutputStream () 540 { 541 public byte [] toByteArray() 542 { 543 return super.toByteArray(); 544 } 545 }; 546 547 eDataGraph.getDataGraphResource().save(byteArrayOutputStream, null); 548 objectOutput.writeInt(byteArrayOutputStream.toByteArray().length); 549 objectOutput.write(byteArrayOutputStream.toByteArray()); 550 } 551 552 public void readExternal(ObjectInput objectInput) throws IOException 553 { 554 int length = objectInput.readInt(); 555 byte [] bytes = new byte [length]; 556 objectInput.readFully(bytes); 557 558 ResourceSet resourceSet = createResourceSet(); 559 Resource resource = resourceSet.createResource(URI.createURI("all.datagraph")); 560 resource.load(new ByteArrayInputStream (bytes), null); 561 eDataGraph = (EDataGraph)resource.getContents().get(0); 562 } 563 564 protected ResourceSet createResourceSet() 565 { 566 return SDOUtil.createResourceSet(); 567 } 568 569 public Object getWriteReplacement(EObject eObject) 570 { 571 Object result = writeReplacements.get(eObject); 572 if (result == null) 573 { 574 result = new EDataObjectExternalizable(eDataGraph, eObject); 575 writeReplacements.put(eObject, result); 576 } 577 return result; 578 } 579 580 protected Object readResolve() 581 { 582 return eDataGraph; 583 } 584 } 585 586 public static class EDataObjectExternalizable implements Externalizable 587 { 588 protected EObject eObject; 589 protected EDataGraph eDataGraph; 590 591 public EDataObjectExternalizable() 592 { 593 } 594 595 public EDataObjectExternalizable(EDataGraph eDataGraph, EObject eObject) 596 { 597 this.eDataGraph = eDataGraph; 598 this.eObject = eObject; 599 } 600 601 public void writeExternal(ObjectOutput objectOutput) throws IOException 602 { 603 String uri = eObject.eResource().getURIFragment(eObject); 605 objectOutput.writeUTF(uri); 606 objectOutput.writeObject(((EDataGraph.Internal)eDataGraph).getWriteReplacement()); 607 } 608 609 public void readExternal(ObjectInput objectInput) throws IOException , ClassNotFoundException 610 { 611 String uri = objectInput.readUTF(); 612 eDataGraph = (EDataGraph)objectInput.readObject(); 613 eObject = eDataGraph.getRootResource().getEObject(uri); 614 } 615 616 protected Object readResolve() 617 { 618 return eObject; 619 } 620 } 621 622 623 protected EDataGraphExternalizable eDataGraphExternalizable; 624 protected Adapter modificationTracker; 625 626 public Object getWriteReplacement() 627 { 628 if (eDataGraphExternalizable == null) 629 { 630 eDataGraphExternalizable = createEDataGraphExternalizable(); 631 getRootResource().setTrackingModification(true); 632 modificationTracker = 633 new AdapterImpl() 634 { 635 public void notifyChanged(Notification notification) 636 { 637 if (notification.getFeatureID(Resource.class) == Resource.RESOURCE__IS_MODIFIED && notification.getNewBooleanValue()) 638 { 639 eDataGraphExternalizable = null; 640 modificationTracker = null; 641 ((Resource)target).eAdapters().remove(this); 642 } 643 } 644 }; 645 getRootResource().eAdapters().add(modificationTracker); 646 } 647 return eDataGraphExternalizable; 648 } 649 650 protected EDataGraphExternalizable createEDataGraphExternalizable() 651 { 652 return new EDataGraphExternalizable(this); 653 } 654 655 public Object getWriteReplacement(EObject eObject) 656 { 657 return ((EDataGraphExternalizable)getWriteReplacement()).getWriteReplacement(eObject); 658 } 659 660 public Object writeReplace() 661 { 662 return getWriteReplacement(); 663 } 664 665 public EObject eObjectForURIFragmentSegment(String uriFragmentSegment) 666 { 667 if (uriFragmentSegment.startsWith("@models.")) 668 { 669 if (resourceSet != null) 670 { 671 int count = Integer.parseInt(uriFragmentSegment.substring(8)); 672 for (Iterator i = resourceSet.getResources().iterator(); i.hasNext(); ) 673 { 674 Resource ePackageResource = (Resource)i.next(); 675 List resourceContents = ePackageResource.getContents(); 676 if (resourceContents.size() == 1 && resourceContents.get(0) instanceof EPackage && count-- == 0) 677 { 678 return (EObject)resourceContents.get(0); 679 } 680 } 681 } 682 683 return null; 684 } 685 else 686 { 687 return super.eObjectForURIFragmentSegment(uriFragmentSegment); 688 } 689 } 690 691 696 public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 697 { 698 if (featureID >= 0) 699 { 700 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 701 { 702 case SDOPackage.EDATA_GRAPH__ECHANGE_SUMMARY: 703 if (eChangeSummary != null) 704 msgs = ((InternalEObject)eChangeSummary).eInverseRemove(this, SDOPackage.ECHANGE_SUMMARY__EDATA_GRAPH, EChangeSummary.class, msgs); 705 return basicSetEChangeSummary((EChangeSummary)otherEnd, msgs); 706 default: 707 return eDynamicInverseAdd(otherEnd, featureID, baseClass, msgs); 708 } 709 } 710 if (eContainer != null) 711 msgs = eBasicRemoveFromContainer(msgs); 712 return eBasicSetContainer(otherEnd, featureID, msgs); 713 } 714 715 720 public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 721 { 722 if (featureID >= 0) 723 { 724 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 725 { 726 case SDOPackage.EDATA_GRAPH__ECHANGE_SUMMARY: 727 return basicSetEChangeSummary(null, msgs); 728 default: 729 return eDynamicInverseRemove(otherEnd, featureID, baseClass, msgs); 730 } 731 } 732 return eBasicSetContainer(null, featureID, msgs); 733 } 734 735 740 public Object eGet(EStructuralFeature eFeature, boolean resolve) 741 { 742 switch (eDerivedStructuralFeatureID(eFeature)) 743 { 744 case SDOPackage.EDATA_GRAPH__ROOT_OBJECT: 745 return getRootObject(); 746 case SDOPackage.EDATA_GRAPH__CHANGE_SUMMARY: 747 return getChangeSummary(); 748 case SDOPackage.EDATA_GRAPH__RESOURCE_SET: 749 return getResourceSet(); 750 case SDOPackage.EDATA_GRAPH__ROOT_RESOURCE: 751 return getRootResource(); 752 case SDOPackage.EDATA_GRAPH__ECHANGE_SUMMARY: 753 return getEChangeSummary(); 754 case SDOPackage.EDATA_GRAPH__EROOT_OBJECT: 755 return getERootObject(); 756 } 757 return eDynamicGet(eFeature, resolve); 758 } 759 760 765 public void eSet(EStructuralFeature eFeature, Object newValue) 766 { 767 switch (eDerivedStructuralFeatureID(eFeature)) 768 { 769 case SDOPackage.EDATA_GRAPH__RESOURCE_SET: 770 setResourceSet((ResourceSet)newValue); 771 return; 772 case SDOPackage.EDATA_GRAPH__ECHANGE_SUMMARY: 773 setEChangeSummary((EChangeSummary)newValue); 774 return; 775 case SDOPackage.EDATA_GRAPH__EROOT_OBJECT: 776 setERootObject((EObject)newValue); 777 return; 778 } 779 eDynamicSet(eFeature, newValue); 780 } 781 782 787 public void eUnset(EStructuralFeature eFeature) 788 { 789 switch (eDerivedStructuralFeatureID(eFeature)) 790 { 791 case SDOPackage.EDATA_GRAPH__RESOURCE_SET: 792 setResourceSet(RESOURCE_SET_EDEFAULT); 793 return; 794 case SDOPackage.EDATA_GRAPH__ECHANGE_SUMMARY: 795 setEChangeSummary((EChangeSummary)null); 796 return; 797 case SDOPackage.EDATA_GRAPH__EROOT_OBJECT: 798 setERootObject((EObject)null); 799 return; 800 } 801 eDynamicUnset(eFeature); 802 } 803 804 809 public boolean eIsSet(EStructuralFeature eFeature) 810 { 811 switch (eDerivedStructuralFeatureID(eFeature)) 812 { 813 case SDOPackage.EDATA_GRAPH__ROOT_OBJECT: 814 return getRootObject() != null; 815 case SDOPackage.EDATA_GRAPH__CHANGE_SUMMARY: 816 return getChangeSummary() != null; 817 case SDOPackage.EDATA_GRAPH__RESOURCE_SET: 818 return RESOURCE_SET_EDEFAULT == null ? resourceSet != null : !RESOURCE_SET_EDEFAULT.equals(resourceSet); 819 case SDOPackage.EDATA_GRAPH__ROOT_RESOURCE: 820 return ROOT_RESOURCE_EDEFAULT == null ? getRootResource() != null : !ROOT_RESOURCE_EDEFAULT.equals(getRootResource()); 821 case SDOPackage.EDATA_GRAPH__ECHANGE_SUMMARY: 822 return eChangeSummary != null; 823 case SDOPackage.EDATA_GRAPH__EROOT_OBJECT: 824 return eRootObject != null; 825 } 826 return eDynamicIsSet(eFeature); 827 } 828 829 834 public String toString() 835 { 836 if (eIsProxy()) return super.toString(); 837 838 StringBuffer result = new StringBuffer (super.toString()); 839 result.append(" (resourceSet: "); 840 result.append(resourceSet); 841 result.append(')'); 842 return result.toString(); 843 } 844 845 } | Popular Tags |