1 17 package org.eclipse.emf.ecore.util; 18 19 20 import java.io.PrintStream ; 21 import java.security.SecureRandom ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.GregorianCalendar ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.StringTokenizer ; 31 32 import org.eclipse.emf.common.notify.Adapter; 33 import org.eclipse.emf.common.notify.AdapterFactory; 34 import org.eclipse.emf.common.notify.Notifier; 35 import org.eclipse.emf.common.util.AbstractTreeIterator; 36 import org.eclipse.emf.common.util.ECollections; 37 import org.eclipse.emf.common.util.EList; 38 import org.eclipse.emf.common.util.TreeIterator; 39 import org.eclipse.emf.common.util.URI; 40 import org.eclipse.emf.ecore.EAnnotation; 41 import org.eclipse.emf.ecore.EAttribute; 42 import org.eclipse.emf.ecore.EClass; 43 import org.eclipse.emf.ecore.EClassifier; 44 import org.eclipse.emf.ecore.EDataType; 45 import org.eclipse.emf.ecore.EModelElement; 46 import org.eclipse.emf.ecore.EObject; 47 import org.eclipse.emf.ecore.EPackage; 48 import org.eclipse.emf.ecore.EReference; 49 import org.eclipse.emf.ecore.EStructuralFeature; 50 import org.eclipse.emf.ecore.EcoreFactory; 51 import org.eclipse.emf.ecore.EcorePackage; 52 import org.eclipse.emf.ecore.InternalEObject; 53 import org.eclipse.emf.ecore.impl.EPackageImpl; 54 import org.eclipse.emf.ecore.resource.Resource; 55 import org.eclipse.emf.ecore.resource.ResourceSet; 56 57 58 61 public class EcoreUtil 62 { 63 68 74 public static Adapter getExistingAdapter(Notifier notifier, Object type) 75 { 76 return getAdapter(notifier.eAdapters(), type); 77 } 78 79 86 public static Adapter getRegisteredAdapter(EObject eObject, Object type) 87 { 88 Adapter result = getExistingAdapter(eObject, type); 89 if (result == null) 90 { 91 Resource resource = eObject.eResource(); 92 if (resource != null) 93 { 94 ResourceSet resourceSet = resource.getResourceSet(); 95 if (resourceSet != null) 96 { 97 AdapterFactory factory = getAdapterFactory(resourceSet.getAdapterFactories(), type); 98 if (factory != null) 99 { 100 result = factory.adaptNew(eObject, type); 101 } 102 } 103 } 104 } 105 return result; 106 } 107 108 115 public static Adapter getRegisteredAdapter(Resource resource, Object type) 116 { 117 Adapter result = getExistingAdapter(resource, type); 118 if (result == null) 119 { 120 ResourceSet resourceSet = resource.getResourceSet(); 121 if (resourceSet != null) 122 { 123 AdapterFactory factory = getAdapterFactory(resourceSet.getAdapterFactories(), type); 124 if (factory != null) 125 { 126 result = factory.adaptNew(resource, type); 127 } 128 } 129 } 130 return result; 131 } 132 133 139 public static Adapter getAdapter(List adapters, Object type) 140 { 141 for (int i = 0, size = adapters.size(); i < size; ++i) 142 { 143 Adapter adapter = (Adapter)adapters.get(i); 144 if (adapter.isAdapterForType(type)) 145 { 146 return adapter; 147 } 148 } 149 return null; 150 } 151 152 158 public static AdapterFactory getAdapterFactory(List adapterFactories, Object type) 159 { 160 for (int i = 0, size = adapterFactories.size(); i < size; ++i) 161 { 162 AdapterFactory factory = (AdapterFactory)adapterFactories.get(i); 163 if (factory.isFactoryForType(type)) 164 { 165 return factory; 166 } 167 } 168 return null; 169 } 170 171 180 public static EObject resolve(EObject proxy, ResourceSet resourceSet) 181 { 182 URI proxyURI = ((InternalEObject)proxy).eProxyURI(); 183 if (proxyURI != null) 184 { 185 try 186 { 187 EObject resolvedObject = null; 188 189 if (resourceSet != null) 190 { 191 resolvedObject = resourceSet.getEObject(proxyURI, true); 192 } 193 else 194 { 195 EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(proxyURI.trimFragment().toString()); 196 if (ePackage != null) 197 { 198 Resource resource = ePackage.eResource(); 199 if (resource != null) 200 { 201 resolvedObject = resource.getEObject(proxyURI.fragment().toString()); 202 } 203 } 204 } 205 206 if (resolvedObject != null && resolvedObject != proxy) 207 { 208 return resolve(resolvedObject, resourceSet); 209 } 210 } 211 catch (RuntimeException exception) 212 { 213 } 215 } 216 return proxy; 217 } 218 219 228 public static EObject resolve(EObject proxy, Resource resourceContext) 229 { 230 return resolve(proxy, resourceContext != null ? resourceContext.getResourceSet() : null); 231 } 232 233 243 public static EObject resolve(EObject proxy, EObject objectContext) 244 { 245 Resource resourceContext = objectContext != null ? objectContext.eResource() : null; 246 return resolve(proxy, resourceContext != null ? resourceContext.getResourceSet() : null); 247 } 248 249 253 public static void resolveAll(ResourceSet resourceSet) 254 { 255 List resources = resourceSet.getResources(); 256 for (int i = 0; i < resources.size(); ++i) 257 { 258 resolveAll((Resource)resources.get(i)); 259 } 260 } 261 262 266 public static void resolveAll(Resource resource) 267 { 268 for (Iterator i = resource.getAllContents(); i.hasNext(); ) 269 { 270 EObject eObject = (EObject)i.next(); 271 for (Iterator j = eObject.eCrossReferences().iterator(); j.hasNext(); j.next()) 272 { 273 } 274 } 275 } 276 277 281 public static void resolveAll(EObject eObject) 282 { 283 for (Iterator i = eObject.eAllContents(); i.hasNext(); ) 284 { 285 EObject childEObject = (EObject)i.next(); 286 for (Iterator j = childEObject.eCrossReferences().iterator(); j.hasNext(); j.next()) 287 { 288 } 289 } 290 } 291 292 298 public static Object getObjectByType(Collection objects, EClassifier type) 299 { 300 for (Iterator i = objects.iterator(); i.hasNext();) 301 { 302 Object object = i.next(); 303 if (type.isInstance(object)) 304 { 305 return object; 306 } 307 } 308 return null; 309 } 310 311 317 public static Collection getObjectsByType(Collection objects, EClassifier type) 318 { 319 Collection result = new ArrayList (); 320 for (Iterator i = objects.iterator(); i.hasNext();) 321 { 322 Object object = i.next(); 323 if (type.isInstance(object)) 324 { 325 result.add(object); 326 } 327 } 328 return result; 329 } 330 331 337 public static EObject copy(EObject eObject) 338 { 339 Copier copier = new Copier(); 340 EObject result = copier.copy(eObject); 341 copier.copyReferences(); 342 return result; 343 } 344 345 351 public static Collection copyAll(Collection eObjects) 352 { 353 Copier copier = new Copier(); 354 Collection result = copier.copyAll(eObjects); 355 copier.copyReferences(); 356 return result; 357 } 358 359 378 public static class Copier extends HashMap 379 { 380 385 public Collection copyAll(Collection eObjects) 386 { 387 Collection result = new ArrayList (eObjects.size()); 388 for (Iterator i = eObjects.iterator(); i.hasNext();) 389 { 390 result.add(copy((EObject)i.next())); 391 } 392 return result; 393 } 394 395 400 public EObject copy(EObject eObject) 401 { 402 EObject copyEObject = createCopy(eObject); 403 put(eObject, copyEObject); 404 EClass eClass = eObject.eClass(); 405 for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i) 406 { 407 EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(i); 408 if (eStructuralFeature.isChangeable() && !eStructuralFeature.isDerived()) 409 { 410 if (eStructuralFeature instanceof EAttribute) 411 { 412 copyAttribute((EAttribute)eStructuralFeature, eObject, copyEObject); 413 } 414 else 415 { 416 EReference eReference = (EReference)eStructuralFeature; 417 if (eReference.isContainment()) 418 { 419 copyContainment(eReference, eObject, copyEObject); 420 } 421 } 422 } 423 } 424 425 return copyEObject; 426 } 427 428 435 protected EObject createCopy(EObject eObject) 436 { 437 return create(getTarget(eObject.eClass())); 438 } 439 440 446 protected EClass getTarget(EClass eClass) 447 { 448 return eClass; 449 } 450 451 457 protected EStructuralFeature getTarget(EStructuralFeature eStructuralFeature) 458 { 459 return eStructuralFeature; 460 } 461 462 469 protected void copyContainment(EReference eReference, EObject eObject, EObject copyEObject) 470 { 471 if (eObject.eIsSet(eReference)) 472 { 473 if (eReference.isMany()) 474 { 475 List source = (List )eObject.eGet(eReference); 476 List target = (List )copyEObject.eGet(getTarget(eReference)); 477 if (source.isEmpty()) 478 { 479 target.clear(); 480 } 481 else 482 { 483 target.addAll(copyAll(source)); 484 } 485 } 486 else 487 { 488 EObject childEObject = (EObject)eObject.eGet(eReference); 489 copyEObject.eSet(getTarget(eReference), childEObject == null ? null : copy(childEObject)); 490 } 491 } 492 } 493 494 501 protected void copyAttribute(EAttribute eAttribute, EObject eObject, EObject copyEObject) 502 { 503 if (eObject.eIsSet(eAttribute)) 504 { 505 if (FeatureMapUtil.isFeatureMap(eAttribute)) 506 { 507 FeatureMap featureMap = (FeatureMap)eObject.eGet(eAttribute); 508 for (int i = 0, size = featureMap.size(); i < size; ++i) 509 { 510 EStructuralFeature feature = featureMap.getEStructuralFeature(i); 511 if (feature instanceof EReference && ((EReference)feature).isContainment()) 512 { 513 Object value = featureMap.getValue(i); 514 if (value != null) 515 { 516 copy((EObject)value); 517 } 518 } 519 } 520 } 521 else if (eAttribute.isMany()) 522 { 523 List source = (List )eObject.eGet(eAttribute); 524 List target = (List )copyEObject.eGet(getTarget(eAttribute)); 525 if (source.isEmpty()) 526 { 527 target.clear(); 528 } 529 else 530 { 531 target.addAll(source); 532 } 533 } 534 else 535 { 536 copyEObject.eSet(getTarget(eAttribute), eObject.eGet(eAttribute)); 537 } 538 } 539 } 540 541 544 public void copyReferences() 545 { 546 for (Iterator i = entrySet().iterator(); i.hasNext();) 547 { 548 Map.Entry entry = (Map.Entry )i.next(); 549 EObject eObject = (EObject)entry.getKey(); 550 EObject copyEObject = (EObject)entry.getValue(); 551 EClass eClass = eObject.eClass(); 552 for (int j = 0, size = eClass.getFeatureCount(); j < size; ++j) 553 { 554 EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(j); 555 if (eStructuralFeature.isChangeable() && !eStructuralFeature.isDerived()) 556 { 557 if (eStructuralFeature instanceof EReference) 558 { 559 EReference eReference = (EReference)eStructuralFeature; 560 if (!eReference.isContainment()) 561 { 562 copyReference(eReference, eObject, copyEObject); 563 } 564 } 565 else if (FeatureMapUtil.isFeatureMap(eStructuralFeature)) 566 { 567 FeatureMap featureMap = (FeatureMap)eObject.eGet(eStructuralFeature); 568 FeatureMap copyFeatureMap = (FeatureMap)copyEObject.eGet(getTarget(eStructuralFeature)); 569 for (int k = 0, featureMapSize = featureMap.size(); k < featureMapSize; ++k) 570 { 571 EStructuralFeature feature = featureMap.getEStructuralFeature(k); 572 if (feature instanceof EReference) 573 { 574 Object referencedEObject = featureMap.getValue(k); 575 Object copyReferencedEObject = get(referencedEObject); 576 copyFeatureMap.add(feature, copyReferencedEObject == null ? referencedEObject : copyReferencedEObject); 577 } 578 else 579 { 580 copyFeatureMap.add(featureMap.get(k)); 581 } 582 } 583 } 584 } 585 } 586 } 587 } 588 589 597 protected void copyReference(EReference eReference, EObject eObject, EObject copyEObject) 598 { 599 if (eObject.eIsSet(eReference)) 600 { 601 if (eReference.isMany()) 602 { 603 List source = (List )eObject.eGet(eReference); 604 InternalEList target = (InternalEList)copyEObject.eGet(getTarget(eReference)); 605 if (source.isEmpty()) 606 { 607 target.clear(); 608 } 609 else 610 { 611 boolean isBidirectional = eReference.getEOpposite() != null; 612 int index = 0; 613 for (Iterator k = source.iterator(); k.hasNext();) 614 { 615 Object referencedEObject = k.next(); 616 Object copyReferencedEObject = get(referencedEObject); 617 if (copyReferencedEObject == null) 618 { 619 if (!isBidirectional) 620 { 621 target.addUnique(index, referencedEObject); 622 ++index; 623 } 624 } 625 else 626 { 627 if (isBidirectional) 628 { 629 int position = target.indexOf(copyReferencedEObject); 630 if (position == -1) 631 { 632 target.addUnique(index, copyReferencedEObject); 633 } 634 else if (index != position) 635 { 636 target.move(index, copyReferencedEObject); 637 } 638 } 639 else 640 { 641 target.addUnique(index, copyReferencedEObject); 642 } 643 ++index; 644 } 645 } 646 } 647 } 648 else 649 { 650 Object referencedEObject = eObject.eGet(eReference); 651 if (referencedEObject == null) 652 { 653 copyEObject.eSet(getTarget(eReference), null); 654 } 655 else 656 { 657 Object copyReferencedEObject = get(referencedEObject); 658 if (copyReferencedEObject == null) 659 { 660 if (eReference.getEOpposite() == null) 661 { 662 copyEObject.eSet(getTarget(eReference), referencedEObject); 663 } 664 } 665 else 666 { 667 copyEObject.eSet(getTarget(eReference), copyReferencedEObject); 668 } 669 } 670 } 671 } 672 } 673 } 674 675 688 public static EObject getRootContainer(EObject eObject) 689 { 690 EObject result = eObject; 691 for (EObject parent = eObject; parent != null; parent = parent.eContainer()) 692 { 693 result = parent; 694 } 695 return result; 696 } 697 698 706 public static boolean isAncestor(EObject ancestorEObject, EObject eObject) 707 { 708 while (eObject != null) 709 { 710 if (eObject == ancestorEObject) 711 { 712 return true; 713 } 714 eObject = eObject.eContainer(); 715 } 716 717 return false; 718 } 719 720 729 public static boolean isAncestor(Resource ancestorResource, EObject eObject) 730 { 731 return eObject.eResource() == ancestorResource; 732 } 733 734 744 public static boolean isAncestor(ResourceSet ancestorResourceSet, EObject eObject) 745 { 746 Resource resource = eObject.eResource(); 747 return resource != null && resource.getResourceSet() == ancestorResourceSet; 748 } 749 750 756 public static boolean isAncestor(Collection ancestorEMFObjects, EObject eObject) 757 { 758 if (ancestorEMFObjects.contains(eObject)) 761 { 762 return true; 763 } 764 765 for (EObject container = eObject.eContainer(); container != null; container = container.eContainer()) 768 { 769 if (ancestorEMFObjects.contains(container)) 770 { 771 return true; 772 } 773 eObject = container; 774 } 775 776 Resource resource = eObject.eResource(); 779 if (resource != null) 780 { 781 if (ancestorEMFObjects.contains(resource)) 782 { 783 return true; 784 } 785 786 ResourceSet resourceSet = resource.getResourceSet(); 789 if (resourceSet != null) 790 { 791 if (ancestorEMFObjects.contains(resourceSet)) 792 { 793 return true; 794 } 795 } 796 } 797 798 return false; 801 } 802 803 817 public static TreeIterator getAllContents(Collection emfObjects) 818 { 819 return new ContentTreeIterator(emfObjects); 820 } 821 822 830 public static class ContentTreeIterator extends AbstractTreeIterator 831 { 832 835 protected Collection emfObjects; 836 837 843 protected ResourcesIterator resourceSetIterator; 844 845 849 protected ContentTreeIterator(Collection emfObjects) 850 { 851 super(emfObjects, false); 852 this.emfObjects = emfObjects; 853 } 854 855 860 public Iterator getChildren(Object object) 861 { 862 if (object instanceof EObject) 863 { 864 return getEObjectChildren((EObject)object); 865 } 866 else if (object instanceof Resource) 867 { 868 return getResourceChildren((Resource)object); 869 } 870 else if (object instanceof ResourceSet) 871 { 872 return getResourceSetChildren((ResourceSet)object); 873 } 874 else if (object == emfObjects) 875 { 876 return emfObjects.iterator(); 877 } 878 else 879 { 880 return getObjectChildren(object); 881 } 882 } 883 884 889 protected Iterator getEObjectChildren(EObject eObject) 890 { 891 return eObject.eContents().iterator(); 892 } 893 894 899 protected Iterator getResourceChildren(Resource resource) 900 { 901 return resource.getContents().iterator(); 902 } 903 904 910 public boolean hasNext() 911 { 912 Iterator iterator; 913 if (!includeRoot && data == null) 914 { 915 nextPruneIterator = getChildren(object); 916 add(nextPruneIterator); 917 iterator = nextPruneIterator; 918 } 919 else 920 { 921 if (data == null) 925 { 926 return true; 927 } 928 else if (isEmpty()) 929 { 930 return false; 931 } 932 else 933 { 934 iterator = (Iterator )data[size - 1]; 935 } 936 } 937 938 if (iterator == resourceSetIterator && !resourceSetIterator.reallyHasNext()) 941 { 942 next(); 945 return hasNext(); 946 } 947 else 948 { 949 return iterator.hasNext(); 950 } 951 } 952 953 957 protected static class ResourcesIterator implements Iterator 958 { 959 962 protected List resources; 963 964 967 protected int index = 0; 968 969 973 public ResourcesIterator(List resources) 974 { 975 this.resources = resources; 976 } 977 978 982 public boolean reallyHasNext() 983 { 984 return index < resources.size(); 985 } 986 987 994 public boolean hasNext() 995 { 996 return index <= resources.size(); 997 } 998 999 1003 public Object next() 1004 { 1005 if (index >= resources.size()) 1006 { 1007 ++index; 1008 return null; 1009 } 1010 else 1011 { 1012 return resources.get(index++); 1013 } 1014 } 1015 1016 1019 public void remove() 1020 { 1021 throw new UnsupportedOperationException (); 1022 } 1023 } 1024 1025 1031 protected Iterator getResourceSetChildren(ResourceSet resourceSet) 1032 { 1033 return resourceSetIterator = new ResourcesIterator(resourceSet.getResources()); 1034 } 1035 1036 1041 protected Iterator getObjectChildren(Object object) 1042 { 1043 return ECollections.EMPTY_ELIST.iterator(); 1044 } 1045 } 1046 1047 1055 public static class CrossReferencer extends HashMap 1056 { 1057 1060 protected Collection emfObjects; 1061 1062 1066 protected CrossReferencer(EObject eObject) 1067 { 1068 this.emfObjects = Collections.singleton(eObject); 1069 } 1070 1071 1075 protected CrossReferencer(Resource resource) 1076 { 1077 this.emfObjects = Collections.singleton(resource); 1078 } 1079 1080 1084 protected CrossReferencer(ResourceSet resourceSet) 1085 { 1086 this.emfObjects = Collections.singleton(resourceSet); 1087 } 1088 1089 1093 protected CrossReferencer(Collection emfObjects) 1094 { 1095 this.emfObjects = emfObjects; 1096 } 1097 1098 1103 protected boolean containment(EObject eObject) 1104 { 1105 return true; 1106 } 1107 1108 1116 protected boolean crossReference(EObject eObject, EReference eReference, EObject crossReferencedEObject) 1117 { 1118 return true; 1119 } 1120 1121 1125 protected boolean resolve() 1126 { 1127 return true; 1128 } 1129 1130 1134 protected Collection newCollection() 1135 { 1136 return new ArrayList (); 1137 } 1138 1139 1145 protected Collection getCollection(Object key) 1146 { 1147 Collection result = (Collection )get(key); 1148 if (result == null) 1149 { 1150 put(key, result = newCollection()); 1151 } 1152 return result; 1153 } 1154 1155 1159 protected TreeIterator newContentsIterator() 1160 { 1161 return new ContentTreeIterator(emfObjects); 1162 } 1163 1164 1167 protected void crossReference() 1168 { 1169 for (TreeIterator contents = newContentsIterator(); contents.hasNext();) 1170 { 1171 Object content = contents.next(); 1172 if (content instanceof EObject) 1173 { 1174 EObject eObject = (EObject)content; 1175 if (containment(eObject)) 1176 { 1177 handleCrossReference(eObject); 1178 } 1179 else 1180 { 1181 contents.prune(); 1182 } 1183 } 1184 } 1185 } 1186 1187 protected EContentsEList.FeatureIterator getCrossReferences(EObject eObject) 1188 { 1189 return 1190 (EContentsEList.FeatureIterator) 1191 (resolve() ? 1192 eObject.eCrossReferences().iterator() : 1193 ((InternalEList)eObject.eCrossReferences()).basicIterator()); 1194 } 1195 1196 protected void handleCrossReference(EObject eObject) 1197 { 1198 InternalEObject internalEObject = (InternalEObject)eObject; 1199 for (EContentsEList.FeatureIterator crossReferences = getCrossReferences(internalEObject); crossReferences.hasNext();) 1200 { 1201 EObject crossReferencedEObject = (EObject)crossReferences.next(); 1202 if (crossReferencedEObject != null) 1203 { 1204 EReference eReference = (EReference)crossReferences.feature(); 1205 if (crossReference(internalEObject, eReference, crossReferencedEObject)) 1206 { 1207 getCollection(crossReferencedEObject).add(internalEObject.eSetting(eReference)); 1208 } 1209 } 1210 } 1211 } 1212 1213 1216 protected void done() 1217 { 1218 emfObjects = null; 1219 } 1220 1221 1226 public static Map find(Collection emfObjects) 1227 { 1228 CrossReferencer result = new CrossReferencer(emfObjects); 1229 result.crossReference(); 1230 result.done(); 1231 return result; 1232 } 1233 1234 1238 public String toString() 1239 { 1240 StringBuffer result = new StringBuffer ("{"); 1242 for (Iterator i = entrySet().iterator(); i.hasNext();) 1243 { 1244 Map.Entry entry = (Map.Entry )i.next(); 1245 EObject eObject = (EObject)entry.getKey(); 1246 result.append(getIdentification(eObject)); 1247 result.append("=["); 1248 Collection collection = (Collection )entry.getValue(); 1249 for (Iterator j = collection.iterator(); j.hasNext();) 1250 { 1251 EStructuralFeature.Setting setting = (EStructuralFeature.Setting)j.next(); 1252 EStructuralFeature eStructuralFeature = setting.getEStructuralFeature(); 1253 result.append(eStructuralFeature.getName()); 1254 result.append("<-"); 1255 result.append(getIdentification(setting.getEObject())); 1256 if (j.hasNext()) 1257 { 1258 result.append(", "); 1259 } 1260 } 1261 result.append(']'); 1262 } 1263 1264 result.append('}'); 1266 return result.toString(); 1267 } 1268 1269 1274 public static void print(PrintStream out, Map crossReferenceMap) 1275 { 1276 out.println('{'); 1278 for (Iterator i = crossReferenceMap.entrySet().iterator(); i.hasNext();) 1279 { 1280 Map.Entry entry = (Map.Entry )i.next(); 1281 EObject eObject = (EObject)entry.getKey(); 1282 out.print(" "); 1283 out.print(getIdentification(eObject)); 1284 Collection collection = (Collection )entry.getValue(); 1285 if (collection.isEmpty()) 1286 { 1287 out.println(" =[]"); 1288 } 1289 else 1290 { 1291 out.println(" =["); 1292 for (Iterator j = collection.iterator(); j.hasNext();) 1293 { 1294 EStructuralFeature.Setting setting = (EStructuralFeature.Setting)j.next(); 1295 EStructuralFeature eStructuralFeature = setting.getEStructuralFeature(); 1296 out.print(" "); 1297 out.print(eStructuralFeature.getName()); 1298 out.print("<-"); 1299 out.print(getIdentification(setting.getEObject())); 1300 if (j.hasNext()) 1301 { 1302 out.println(","); 1303 } 1304 } 1305 out.println(']'); 1306 } 1307 } 1308 1309 out.println('}'); 1311 } 1312 1313 1319 public static void print(PrintStream out, Collection settings) 1320 { 1321 if (settings.isEmpty()) 1322 { 1323 out.println("[]"); 1324 } 1325 else 1326 { 1327 out.println("["); 1328 for (Iterator j = settings.iterator(); j.hasNext();) 1329 { 1330 EStructuralFeature.Setting setting = (EStructuralFeature.Setting)j.next(); 1331 EStructuralFeature eStructuralFeature = setting.getEStructuralFeature(); 1332 out.print(" "); 1333 out.print(eStructuralFeature.getName()); 1334 out.print("<-"); 1335 out.print(getIdentification(setting.getEObject())); 1336 if (j.hasNext()) 1337 { 1338 out.println(","); 1339 } 1340 } 1341 out.println(']'); 1342 } 1343 } 1344 } 1345 1346 1349 public static class ExternalCrossReferencer extends CrossReferencer 1350 { 1351 1355 protected ExternalCrossReferencer(Collection emfObjects) 1356 { 1357 super(emfObjects); 1358 } 1359 1360 1364 protected ExternalCrossReferencer(EObject eObject) 1365 { 1366 super(eObject); 1367 } 1368 1369 1373 protected ExternalCrossReferencer(Resource resource) 1374 { 1375 super(Collections.singleton(resource)); 1376 } 1377 1378 1382 protected ExternalCrossReferencer(ResourceSet resourceSet) 1383 { 1384 super(Collections.singleton(resourceSet)); 1385 } 1386 1387 1395 protected boolean crossReference(EObject eObject, EReference eReference, EObject crossReferencedEObject) 1396 { 1397 return !isAncestor(emfObjects, crossReferencedEObject); 1398 } 1399 1400 1404 protected Map findExternalCrossReferences() 1405 { 1406 crossReference(); 1407 done(); 1408 return this; 1409 } 1410 1411 1416 public static Map find(EObject eObject) 1417 { 1418 return new ExternalCrossReferencer(eObject).findExternalCrossReferences(); 1419 } 1420 1421 1426 public static Map find(Resource resource) 1427 { 1428 return new ExternalCrossReferencer(resource).findExternalCrossReferences(); 1429 } 1430 1431 1436 public static Map find(ResourceSet resourceSet) 1437 { 1438 return new ExternalCrossReferencer(resourceSet).findExternalCrossReferences(); 1439 } 1440 1441 1446 public static Map find(Collection emfObjectsToSearch) 1447 { 1448 return new ExternalCrossReferencer(emfObjectsToSearch).findExternalCrossReferences(); 1449 } 1450 } 1451 1452 1459 public static boolean equals(EObject eObject1, EObject eObject2) 1460 { 1461 EqualityHelper equalityHelper = new EqualityHelper(); 1462 return equalityHelper.equals(eObject1, eObject2); 1463 } 1464 1465 1503 public static class EqualityHelper extends HashMap 1504 { 1505 1511 public boolean equals(EObject eObject1, EObject eObject2) 1512 { 1513 if (eObject1 == null) 1516 { 1517 return eObject2 == null; 1518 } 1519 1520 if (eObject2 == null) 1523 { 1524 return false; 1525 } 1526 1527 Object eObject1MappedValue = get(eObject1); 1531 if (eObject1MappedValue != null) 1532 { 1533 return eObject1MappedValue == eObject2; 1536 } 1537 1538 Object eObject2MappedValue = get(eObject2); 1541 if (eObject2MappedValue != null) 1542 { 1543 return eObject2MappedValue == eObject1; 1546 } 1547 1548 1550 if (eObject1 == eObject2) 1553 { 1554 put(eObject1, eObject2); 1557 put(eObject2, eObject1); 1558 return true; 1559 } 1560 1561 EClass eClass = eObject1.eClass(); 1564 if (eClass != eObject2.eClass()) 1565 { 1566 return false; 1567 } 1568 1569 put(eObject1, eObject2); 1572 put(eObject2, eObject1); 1573 1574 1575 for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i) 1578 { 1579 EStructuralFeature feature = eClass.getEStructuralFeature(i); 1582 if (!feature.isDerived()) 1583 { 1584 boolean eIsSet = eObject1.eIsSet(feature); 1587 if (eIsSet != eObject2.eIsSet(feature) || 1588 (feature instanceof EReference ? 1589 !haveEqualReference(eObject1, eObject2, (EReference)feature) : 1590 !haveEqualAttribute(eObject1, eObject2, (EAttribute)feature))) 1591 { 1592 return false; 1593 } 1594 } 1595 } 1596 1597 return true; 1600 } 1601 1602 1609 public boolean equals(List list1, List list2) 1610 { 1611 int size = list1.size(); 1612 if (size != list2.size()) 1613 { 1614 return false; 1615 } 1616 1617 for (int i = 0; i < size; i++) 1618 { 1619 EObject eObject1 = (EObject)list1.get(i); 1620 EObject eObject2 = (EObject)list2.get(i); 1621 if (!equals(eObject1, eObject2)) 1622 { 1623 return false; 1624 } 1625 } 1626 1627 return true; 1628 } 1629 1630 1637 protected boolean haveEqualReference(EObject eObject1, EObject eObject2, EReference reference) 1638 { 1639 Object value1 = eObject1.eGet(reference); 1640 Object value2 = eObject2.eGet(reference); 1641 1642 return 1643 reference.isMany() ? 1644 equals((List )value1, (List )value2) : 1645 equals((EObject)value1, (EObject)value2); 1646 } 1647 1648 1649 1655 protected boolean haveEqualAttribute(EObject eObject1, EObject eObject2, EAttribute attribute) 1656 { 1657 Object value1 = eObject1.eGet(attribute); 1658 Object value2 = eObject2.eGet(attribute); 1659 1660 if (value1 == null) 1663 { 1664 return value2 == null; 1665 } 1666 1667 if (value2 == null) 1670 { 1671 return false; 1672 } 1673 1674 if (FeatureMapUtil.isFeatureMap(attribute)) 1677 { 1678 FeatureMap featureMap1 = (FeatureMap)value1; 1681 FeatureMap featureMap2 = (FeatureMap)value2; 1682 return equalFeatureMaps(featureMap1, featureMap2); 1683 } 1684 else 1685 { 1686 return value1.equals(value2); 1689 } 1690 } 1691 1692 1697 protected boolean equalFeatureMaps(FeatureMap featureMap1, FeatureMap featureMap2) 1698 { 1699 int size = featureMap1.size(); 1702 if (size != featureMap2.size()) 1703 { 1704 return false; 1705 } 1706 1707 for (int i = 0; i < size; i++) 1710 { 1711 EStructuralFeature feature = featureMap1.getEStructuralFeature(i); 1714 if (feature != featureMap2.getEStructuralFeature(i)) 1715 { 1716 return false; 1717 } 1718 1719 Object value1 = featureMap1.getValue(i); 1720 Object value2 = featureMap2.getValue(i); 1721 1722 if (feature instanceof EReference) 1723 { 1724 if (!equals((EObject)value1, (EObject)value2)) 1727 { 1728 return false; 1729 } 1730 } 1731 else if (value1 == null ? value2 != null : !value1.equals(value2)) 1734 { 1735 return false; 1736 } 1737 } 1738 1739 return true; 1742 } 1743 } 1745 1748 public static class UsageCrossReferencer extends CrossReferencer 1749 { 1750 1753 protected Collection eObjectsOfInterest; 1754 1755 1759 protected UsageCrossReferencer(EObject eObject) 1760 { 1761 super(eObject); 1762 } 1763 1764 1768 protected UsageCrossReferencer(Resource resource) 1769 { 1770 super(resource); 1771 } 1772 1773 1777 protected UsageCrossReferencer(ResourceSet resourceSet) 1778 { 1779 super(resourceSet); 1780 } 1781 1782 1786 protected UsageCrossReferencer(Collection emfObjects) 1787 { 1788 super(emfObjects); 1789 } 1790 1791 1799 protected boolean crossReference(EObject eObject, EReference eReference, EObject crossReferencedEObject) 1800 { 1801 return eObjectsOfInterest.contains(crossReferencedEObject); 1802 } 1803 1804 1809 protected Collection findUsage(EObject eObject) 1810 { 1811 eObjectsOfInterest = Collections.singleton(eObject); 1812 crossReference(); 1813 this.eObjectsOfInterest = null; 1814 done(); 1815 return getCollection(eObject); 1816 } 1817 1818 1823 protected Map findAllUsage(Collection eObjectsOfInterest) 1824 { 1825 this.eObjectsOfInterest = eObjectsOfInterest; 1826 crossReference(); 1827 this.eObjectsOfInterest = null; 1828 done(); 1829 return this; 1830 } 1831 1832 1838 public static Collection find(EObject eObjectOfInterest, EObject eObject) 1839 { 1840 return new UsageCrossReferencer(eObject).findUsage(eObjectOfInterest); 1841 } 1842 1843 1849 public static Collection find(EObject eObjectOfInterest, Resource resource) 1850 { 1851 return new UsageCrossReferencer(resource).findUsage(eObjectOfInterest); 1852 } 1853 1854 1860 public static Collection find(EObject eObjectOfInterest, ResourceSet resourceSet) 1861 { 1862 return new UsageCrossReferencer(resourceSet).findUsage(eObjectOfInterest); 1863 } 1864 1865 1871 public static Collection find(EObject eObjectOfInterest, Collection emfObjectsToSearch) 1872 { 1873 return new UsageCrossReferencer(emfObjectsToSearch).findUsage(eObjectOfInterest); 1874 } 1875 1876 1882 public static Map findAll(Collection eObjectsOfInterest, EObject eObject) 1883 { 1884 return new UsageCrossReferencer(eObject).findAllUsage(eObjectsOfInterest); 1885 } 1886 1887 1893 public static Map findAll(Collection eObjectsOfInterest, Resource resource) 1894 { 1895 return new UsageCrossReferencer(resource).findAllUsage(eObjectsOfInterest); 1896 } 1897 1898 1904 public static Map findAll(Collection eObjectsOfInterest, ResourceSet resourceSet) 1905 { 1906 return new UsageCrossReferencer(resourceSet).findAllUsage(eObjectsOfInterest); 1907 } 1908 1909 1915 public static Map findAll(Collection eObjectsOfInterest, Collection emfObjectsToSearch) 1916 { 1917 return new UsageCrossReferencer(emfObjectsToSearch).findAllUsage(eObjectsOfInterest); 1918 } 1919 } 1920 1921 1924 public static class ProxyCrossReferencer extends CrossReferencer 1925 { 1926 1930 protected ProxyCrossReferencer(EObject eObject) 1931 { 1932 super(eObject); 1933 } 1934 1935 1939 protected ProxyCrossReferencer(Resource resource) 1940 { 1941 super(Collections.singleton(resource)); 1942 } 1943 1944 1948 protected ProxyCrossReferencer(ResourceSet resourceSet) 1949 { 1950 super(Collections.singleton(resourceSet)); 1951 } 1952 1953 1957 protected ProxyCrossReferencer(Collection emfObjects) 1958 { 1959 super(emfObjects); 1960 } 1961 1962 1966 protected boolean resolve() 1967 { 1968 return false; 1969 } 1970 1971 1979 protected boolean crossReference(EObject eObject, EReference eReference, EObject crossReferencedEObject) 1980 { 1981 return crossReferencedEObject.eIsProxy(); 1982 } 1983 1984 1988 protected Map findProxyCrossReferences() 1989 { 1990 crossReference(); 1991 done(); 1992 return this; 1993 } 1994 1995 2000 public static Map find(EObject eObject) 2001 { 2002 return new ProxyCrossReferencer(eObject).findProxyCrossReferences(); 2003 } 2004 2005 2010 public static Map find(Resource resource) 2011 { 2012 return new ProxyCrossReferencer(resource).findProxyCrossReferences(); 2013 } 2014 2015 2020 public static Map find(ResourceSet resourceSet) 2021 { 2022 return new ProxyCrossReferencer(resourceSet).findProxyCrossReferences(); 2023 } 2024 2025 2030 public static Map find(Collection emfObjects) 2031 { 2032 return new ProxyCrossReferencer(emfObjects).findProxyCrossReferences(); 2033 } 2034 } 2035 2036 2039 public static class UnresolvedProxyCrossReferencer extends CrossReferencer 2040 { 2041 2045 protected UnresolvedProxyCrossReferencer(EObject eObject) 2046 { 2047 super(eObject); 2048 } 2049 2050 2054 protected UnresolvedProxyCrossReferencer(Resource resource) 2055 { 2056 super(Collections.singleton(resource)); 2057 } 2058 2059 2063 protected UnresolvedProxyCrossReferencer(ResourceSet resourceSet) 2064 { 2065 super(Collections.singleton(resourceSet)); 2066 } 2067 2068 2072 protected UnresolvedProxyCrossReferencer(Collection emfObjects) 2073 { 2074 super(emfObjects); 2075 } 2076 2077 2085 protected boolean crossReference(EObject eObject, EReference eReference, EObject crossReferencedEObject) 2086 { 2087 return crossReferencedEObject.eIsProxy(); 2088 } 2089 2090 2094 protected Map findUnresolvedProxyCrossReferences() 2095 { 2096 crossReference(); 2097 done(); 2098 return this; 2099 } 2100 2101 2106 public static Map find(EObject eObject) 2107 { 2108 return new UnresolvedProxyCrossReferencer(eObject).findUnresolvedProxyCrossReferences(); 2109 } 2110 2111 2116 public static Map find(Resource resource) 2117 { 2118 return new UnresolvedProxyCrossReferencer(resource).findUnresolvedProxyCrossReferences(); 2119 } 2120 2121 2126 public static Map find(ResourceSet resourceSet) 2127 { 2128 return new UnresolvedProxyCrossReferencer(resourceSet).findUnresolvedProxyCrossReferences(); 2129 } 2130 2131 2136 public static Map find(Collection emfObjects) 2137 { 2138 return new UnresolvedProxyCrossReferencer(emfObjects).findUnresolvedProxyCrossReferences(); 2139 } 2140 } 2141 2142 2151 public static String getIdentification(EObject eObject) 2152 { 2153 StringBuffer result = new StringBuffer (eObject.getClass().getName()); 2154 EClass eClass = eObject.eClass(); 2155 if (eClass.getInstanceClassName() == null) 2156 { 2157 result.append('/'); 2158 result.append(eClass.getEPackage().getNsURI()); 2159 result.append('#'); 2160 result.append(eClass.getName()); 2161 } 2162 result.append('@'); 2163 result.append(Integer.toHexString(eObject.hashCode())); 2164 2165 result.append('{'); 2166 result.append(getURI(eObject)); 2167 result.append('}'); 2168 2169 return result.toString(); 2170 } 2171 2172 2182 public static URI getURI(EObject eObject) 2183 { 2184 URI proxyURI = ((InternalEObject)eObject).eProxyURI(); 2187 if (proxyURI != null) 2188 { 2189 return proxyURI; 2190 } 2191 else 2192 { 2193 EObject eRootContainer = EcoreUtil.getRootContainer(eObject); 2196 Resource resource = eRootContainer.eResource(); 2197 if (resource != null) 2198 { 2199 return resource.getURI().appendFragment(resource.getURIFragment(eObject)); 2200 } 2201 else 2202 { 2203 StringBuffer result = new StringBuffer ("#//"); 2206 List uriFragmentPath = new ArrayList (); 2207 for (EObject container = eObject.eContainer(); container != null; container = eObject.eContainer()) 2208 { 2209 uriFragmentPath.add(((InternalEObject)container).eURIFragmentSegment(eObject.eContainmentFeature(), eObject)); 2210 eObject = container; 2211 } 2212 2213 int size = uriFragmentPath.size(); 2214 if (size > 0) 2215 { 2216 for (int i = size - 1;; --i) 2217 { 2218 result.append((String )uriFragmentPath.get(i)); 2219 if (i == 0) 2220 { 2221 break; 2222 } 2223 else 2224 { 2225 result.append('/'); 2226 } 2227 } 2228 } 2229 2230 return URI.createURI(result.toString()); 2231 } 2232 } 2233 } 2234 2235 2247 public static int indexOf(List list, Object o, int fromIndex) 2248 { 2249 return ECollections.indexOf(list, o, fromIndex); 2250 } 2251 2252 2262 public static void setEList(EList eList, Collection prototypeCollection) 2263 { 2264 ECollections.setEList(eList, new ArrayList (prototypeCollection)); 2265 } 2266 2267 2277 public static void setEList(EList eList, List prototypeList) 2278 { 2279 ECollections.setEList(eList, prototypeList); 2280 } 2281 2282 2287 public static void remove(EStructuralFeature.Setting setting, Object value) 2288 { 2289 if (setting.getEStructuralFeature().isMany()) 2290 { 2291 ((List )setting.get(false)).remove(value); 2292 } 2293 else 2294 { 2295 setting.unset(); 2296 } 2297 } 2298 2299 2305 public static void replace(EStructuralFeature.Setting setting, Object oldValue, Object newValue) 2306 { 2307 if (setting.getEStructuralFeature().isMany()) 2308 { 2309 List list = (List )setting.get(false); 2310 list.set(list.indexOf(oldValue), newValue); 2311 } 2312 else 2313 { 2314 setting.set(newValue); 2315 } 2316 } 2317 2318 2324 public static void remove(EObject eObject, EStructuralFeature eStructuralFeature, Object value) 2325 { 2326 if (eStructuralFeature.isMany()) 2327 { 2328 ((List )eObject.eGet(eStructuralFeature)).remove(value); 2329 } 2330 else 2331 { 2332 eObject.eUnset(eStructuralFeature); 2333 } 2334 } 2335 2336 2343 public static void replace(EObject eObject, EStructuralFeature eStructuralFeature, Object oldValue, Object newValue) 2344 { 2345 if (eStructuralFeature.isMany()) 2346 { 2347 List list = (List )eObject.eGet(eStructuralFeature); 2348 list.set(list.indexOf(oldValue), newValue); 2349 } 2350 else 2351 { 2352 eObject.eSet(eStructuralFeature, newValue); 2353 } 2354 } 2355 2356 2361 public static void remove(EObject eObject) 2362 { 2363 EObject container = eObject.eContainer(); 2364 if (container != null) 2365 { 2366 EReference feature = eObject.eContainmentFeature(); 2367 if (feature.isMany()) 2368 { 2369 ((EList)container.eGet(feature)).remove(eObject); 2370 } 2371 else 2372 { 2373 container.eUnset(feature); 2374 } 2375 } 2376 else 2377 { 2378 Resource resource = eObject.eResource(); 2379 if (resource != null) 2380 { 2381 resource.getContents().remove(eObject); 2382 } 2383 } 2384 } 2385 2386 2393 public static void replace(EObject eObject, EObject replacementEObject) 2394 { 2395 EObject container = eObject.eContainer(); 2396 if (container != null) 2397 { 2398 EReference feature = eObject.eContainmentFeature(); 2399 if (feature.isMany()) 2400 { 2401 List list = (List )container.eGet(feature); 2402 list.set(list.indexOf(eObject), replacementEObject); 2403 } 2404 else 2405 { 2406 container.eSet(feature, replacementEObject); 2407 } 2408 } 2409 else 2410 { 2411 Resource resource = eObject.eResource(); 2412 if (resource != null) 2413 { 2414 List list = resource.getContents(); 2415 list.set(list.indexOf(eObject), replacementEObject); 2416 } 2417 } 2418 } 2419 2420 2425 public static EObject create(EClass eClass) 2426 { 2427 return eClass.getEPackage().getEFactoryInstance().create(eClass); 2428 } 2429 2430 2437 public static Object createFromString(EDataType eDataType, String literal) 2438 { 2439 return eDataType.getEPackage().getEFactoryInstance().createFromString(eDataType, literal); 2440 } 2441 2442 2449 public static String convertToString(EDataType eDataType, Object value) 2450 { 2451 return eDataType.getEPackage().getEFactoryInstance().convertToString(eDataType, value); 2452 } 2453 2454 2463 public static String getID(EObject eObject) 2464 { 2465 EClass eClass = eObject.eClass(); 2466 EAttribute eIDAttribute = eClass.getEIDAttribute(); 2467 return eIDAttribute == null || !eObject.eIsSet(eIDAttribute) ? null : convertToString( 2468 eIDAttribute.getEAttributeType(), 2469 eObject.eGet(eIDAttribute)); 2470 } 2471 2472 2483 public static void setID(EObject eObject, String id) 2484 { 2485 EClass eClass = eObject.eClass(); 2486 EAttribute eIDAttribute = eClass.getEIDAttribute(); 2487 if (eIDAttribute == null) 2488 { 2489 throw new IllegalArgumentException ("The object doesn't have an ID feature."); 2490 } 2491 else if (id == null) 2492 { 2493 eObject.eUnset(eIDAttribute); 2494 } 2495 else 2496 { 2497 eObject.eSet(eIDAttribute, createFromString(eIDAttribute.getEAttributeType(), id)); 2498 } 2499 } 2500 2501 2505 public static Class wrapperClassFor(Class javaClass) 2506 { 2507 if (javaClass == null) 2508 { 2509 return null; 2510 } 2511 else if (javaClass.isPrimitive()) 2512 { 2513 if (javaClass == Boolean.TYPE) 2514 { 2515 return Boolean .class; 2516 } 2517 else if (javaClass == Integer.TYPE) 2518 { 2519 return Integer .class; 2520 } 2521 else if (javaClass == Float.TYPE) 2522 { 2523 return Float .class; 2524 } 2525 else if (javaClass == Double.TYPE) 2526 { 2527 return Double .class; 2528 } 2529 else if (javaClass == Long.TYPE) 2530 { 2531 return Long .class; 2532 } 2533 else if (javaClass == Short.TYPE) 2534 { 2535 return Short .class; 2536 } 2537 else if (javaClass == Byte.TYPE) 2538 { 2539 return Byte .class; 2540 } 2541 else 2542 { 2544 return Character .class; 2545 } 2546 } 2547 else 2548 { 2549 return javaClass; 2550 } 2551 } 2552 2553 protected static final String GEN_MODEL_PACKAGE_NS_URI = "http://www.eclipse.org/emf/2002/GenModel"; 2554 2555 public static String getDocumentation(EModelElement eModelElement) 2556 { 2557 EAnnotation eAnnotation = eModelElement.getEAnnotation(GEN_MODEL_PACKAGE_NS_URI); 2558 return eAnnotation == null ? null : (String )eAnnotation.getDetails().get("documentation"); 2559 } 2560 2561 public static void setDocumentation(EModelElement eModelElement, String documentation) 2562 { 2563 EAnnotation eAnnotation = eModelElement.getEAnnotation(GEN_MODEL_PACKAGE_NS_URI); 2564 if (documentation == null) 2565 { 2566 if (eAnnotation != null) 2567 { 2568 eAnnotation.getDetails().remove("documentation"); 2569 } 2570 } 2571 else 2572 { 2573 if (eAnnotation == null) 2574 { 2575 eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); 2576 eAnnotation.setSource(GEN_MODEL_PACKAGE_NS_URI); 2577 eModelElement.getEAnnotations().add(eAnnotation); 2578 } 2579 eAnnotation.getDetails().put("documentation", documentation); 2580 } 2581 } 2582 2583 public static List getConstraints(EModelElement eModelElement) 2584 { 2585 EAnnotation eAnnotation = eModelElement.getEAnnotation(EcorePackage.eNS_URI); 2586 if (eAnnotation != null) 2587 { 2588 String constraints = (String )eAnnotation.getDetails().get("constraints"); 2589 if (constraints != null) 2590 { 2591 List result = new ArrayList (); 2592 for (StringTokenizer stringTokenizer = new StringTokenizer (constraints); stringTokenizer.hasMoreTokens();) 2593 { 2594 String constraint = stringTokenizer.nextToken(); 2595 result.add(constraint); 2596 } 2597 return result; 2598 } 2599 } 2600 return Collections.EMPTY_LIST; 2601 } 2602 2603 public static void setConstraints(EModelElement eModelElement, List constraints) 2604 { 2605 EAnnotation eAnnotation = eModelElement.getEAnnotation(EcorePackage.eNS_URI); 2606 if (constraints == null || constraints.isEmpty()) 2607 { 2608 if (eAnnotation != null) 2609 { 2610 eAnnotation.getDetails().remove("constraints"); 2611 } 2612 } 2613 else 2614 { 2615 if (eAnnotation == null) 2616 { 2617 eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); 2618 eAnnotation.setSource(EcorePackage.eNS_URI); 2619 eModelElement.getEAnnotations().add(eAnnotation); 2620 } 2621 StringBuffer value = new StringBuffer (); 2622 for (Iterator i = constraints.iterator(); i.hasNext();) 2623 { 2624 value.append(i.next()); 2625 if (i.hasNext()) 2626 { 2627 value.append(' '); 2628 } 2629 } 2630 eAnnotation.getDetails().put("constraints", value.toString()); 2631 } 2632 } 2633 2634 public static String getAnnotation(EModelElement eModelElement, String sourceURI, String key) 2635 { 2636 EAnnotation eAnnotation = eModelElement.getEAnnotation(sourceURI); 2637 return eAnnotation == null ? null : (String )eAnnotation.getDetails().get(key); 2638 } 2639 2640 public static void setAnnotation(EModelElement eModelElement, String sourceURI, String key, String value) 2641 { 2642 EAnnotation eAnnotation = eModelElement.getEAnnotation(sourceURI); 2643 if (eAnnotation == null) 2644 { 2645 eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); 2646 eAnnotation.setSource(sourceURI); 2647 eModelElement.getEAnnotations().add(eAnnotation); 2648 } 2649 eAnnotation.getDetails().put(key, value); 2650 } 2651 2652 2658 public static final int GET = 0; 2659 2660 2666 public static final int SET = 1; 2667 2668 2674 public static final int IS_SET = 2; 2675 2676 2682 public static final int UNSET = 3; 2683 2684 static final String [] ACCESSOR_KEYS = 2687 { 2688 "suppressedGetVisibility", 2689 "suppressedSetVisibility", 2690 "suppressedIsSetVisibility", 2691 "suppressedUnsetVisibility" 2692 }; 2693 2694 static final String TRUE = "true"; 2697 2698 2706 public static boolean isSuppressedVisibility(EStructuralFeature eStructuralFeature, int accessor) 2707 { 2708 if (accessor < GET || accessor > UNSET) throw new IllegalArgumentException ("Invalid accessor identifier: " + accessor); 2709 2710 EAnnotation eAnnotation = eStructuralFeature.getEAnnotation(GEN_MODEL_PACKAGE_NS_URI); 2711 return eAnnotation == null ? false : TRUE.equalsIgnoreCase((String )eAnnotation.getDetails().get(ACCESSOR_KEYS[accessor])); 2712 } 2713 2714 2721 public static void setSuppressedVisibility(EStructuralFeature eStructuralFeature, int accessor, boolean suppress) 2722 { 2723 if (accessor < GET || accessor > UNSET) throw new IllegalArgumentException ("Invalid accessor identifier: " + accessor); 2724 2725 EAnnotation eAnnotation = eStructuralFeature.getEAnnotation(GEN_MODEL_PACKAGE_NS_URI); 2726 if (!suppress) 2727 { 2728 if (eAnnotation != null) 2729 { 2730 eAnnotation.getDetails().removeKey(ACCESSOR_KEYS[accessor]); 2731 } 2732 } 2733 else 2734 { 2735 if (eAnnotation == null) 2736 { 2737 eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); 2738 eAnnotation.setSource(GEN_MODEL_PACKAGE_NS_URI); 2739 eStructuralFeature.getEAnnotations().add(eAnnotation); 2740 } 2741 eAnnotation.getDetails().put(ACCESSOR_KEYS[accessor], TRUE); 2742 } 2743 } 2744 2745 2755 public static String generateUUID() 2756 { 2757 return UUID.generate(); 2758 } 2759 2760 private static final class UUID 2761 { 2762 public synchronized static String generate() 2763 { 2764 updateCurrentTime(); 2765 2766 for (int i = 0; i < 5; ++i) 2769 { 2770 buffer[4 * i + 1] = BASE64_DIGITS[(uuid[i * 3] >> 2) & 0x3F]; 2771 buffer[4 * i + 2] = BASE64_DIGITS[((uuid[i * 3] << 4) & 0x30) | ((uuid[i * 3 + 1] >> 4) & 0xF)]; 2772 buffer[4 * i + 3] = BASE64_DIGITS[((uuid[i * 3 + 1] << 2) & 0x3C) | ((uuid[i * 3 + 2] >> 6) & 0x3)]; 2773 buffer[4 * i + 4] = BASE64_DIGITS[uuid[i * 3 + 2] & 0x3F]; 2774 } 2775 2776 buffer[21] = BASE64_DIGITS[(uuid[15] >> 2) & 0x3F]; 2779 buffer[22] = BASE64_DIGITS[(uuid[15] << 4) & 0x30]; 2780 2781 return new String (buffer); 2782 } 2783 2784 private UUID() 2785 { 2786 } 2787 2788 private static final char[] BASE64_DIGITS = { 2789 'A', 2790 'B', 2791 'C', 2792 'D', 2793 'E', 2794 'F', 2795 'G', 2796 'H', 2797 'I', 2798 'J', 2799 'K', 2800 'L', 2801 'M', 2802 'N', 2803 'O', 2804 'P', 2805 'Q', 2806 'R', 2807 'S', 2808 'T', 2809 'U', 2810 'V', 2811 'W', 2812 'X', 2813 'Y', 2814 'Z', 2815 'a', 2816 'b', 2817 'c', 2818 'd', 2819 'e', 2820 'f', 2821 'g', 2822 'h', 2823 'i', 2824 'j', 2825 'k', 2826 'l', 2827 'm', 2828 'n', 2829 'o', 2830 'p', 2831 'q', 2832 'r', 2833 's', 2834 't', 2835 'u', 2836 'v', 2837 'w', 2838 'x', 2839 'y', 2840 'z', 2841 '0', 2842 '1', 2843 '2', 2844 '3', 2845 '4', 2846 '5', 2847 '6', 2848 '7', 2849 '8', 2850 '9', 2851 '-', 2852 '_' }; 2853 2854 2858 private static final long EPOCH_ADJUSTMENT = new GregorianCalendar (1970, 0, 1, 0, 0, 0).getTime().getTime() 2859 - new GregorianCalendar (1582, 9, 15, 0, 0, 0).getTime().getTime(); 2860 2861 private static long lastTime = System.currentTimeMillis() + EPOCH_ADJUSTMENT; 2862 2863 private static short clockSequence; 2864 2865 private static short timeAdjustment; 2866 2867 private static int sleepTime = 1; 2868 2869 2873 private static final byte[] uuid = new byte [16]; 2874 2875 private static final char[] buffer = new char [23]; 2876 2877 static 2878 { 2879 SecureRandom random = new SecureRandom (); 2880 2881 clockSequence = (short)random.nextInt(16384); 2882 updateClockSequence(); 2883 2884 byte[] nodeAddress = new byte [6]; 2888 2889 random.nextBytes(nodeAddress); 2890 2891 nodeAddress[0] |= (byte)0x80; 2894 2895 for (int i = 0; i < 6; ++i) 2899 { 2900 uuid[i + 10] = nodeAddress[i]; 2901 } 2902 2903 buffer[0] = '_'; 2904 } 2905 2906 2912 private static void updateClockSequence() 2913 { 2914 uuid[8] = (byte)(((clockSequence >> 8) & 0x3F) | 0x80); 2916 uuid[9] = (byte)(clockSequence & 0xFF); 2918 } 2919 2920 2926 private static void updateCurrentTime() 2927 { 2928 long currentTime = System.currentTimeMillis() + EPOCH_ADJUSTMENT; 2932 2933 if (lastTime > currentTime) 2934 { 2935 ++clockSequence; 2939 2940 if (16384 == clockSequence) 2941 { 2942 clockSequence = 0; 2943 } 2944 2945 updateClockSequence(); 2946 } 2947 else if (lastTime == currentTime) 2948 { 2949 ++timeAdjustment; 2952 2953 if (timeAdjustment > 9999) 2954 { 2955 try 2957 { 2958 Thread.sleep(sleepTime); 2959 } 2960 catch (InterruptedException exception) 2961 { 2962 } 2963 2964 timeAdjustment = 0; 2965 currentTime = System.currentTimeMillis() + EPOCH_ADJUSTMENT; 2966 2967 while (lastTime == currentTime) 2968 { 2969 try 2970 { 2971 ++sleepTime; 2972 Thread.sleep(1); 2973 } 2974 catch (InterruptedException exception) 2975 { 2976 } 2977 currentTime = System.currentTimeMillis() + EPOCH_ADJUSTMENT; 2978 } 2979 } 2980 } 2981 else 2982 { 2983 timeAdjustment = 0; 2984 } 2985 2986 lastTime = currentTime; 2987 2988 currentTime *= 10000; 2993 currentTime += timeAdjustment; 2994 currentTime |= 0x1000000000000000L; 2995 2996 for (int i = 0; i < 4; ++i) 2999 { 3000 uuid[i] = (byte)((currentTime >> 8 * (3 - i)) & 0xFFL); 3003 } 3004 3005 for (int i = 0; i < 2; ++i) 3006 { 3007 uuid[i + 4] = (byte)((currentTime >> 8 * (1 - i) + 32) & 0xFFL); 3010 } 3011 3012 for (int i = 0; i < 2; ++i) 3013 { 3014 uuid[i + 6] = (byte)((currentTime >> 8 * (1 - i) + 48) & 0xFFL); 3017 } 3018 } 3019 } 3020 3021 3025 public static void freeze(EPackage ePackage) 3026 { 3027 try 3028 { 3029 ((EPackageImpl)ePackage).freeze(); 3030 } 3031 catch (ClassCastException exception) 3032 { 3033 } 3034 } 3035 3036 3155} 3156 3157 | Popular Tags |