1 17 18 package org.eclipse.emf.ecore.util; 19 20 21 import java.util.AbstractList ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.ConcurrentModificationException ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.ListIterator ; 29 import java.util.Map ; 30 import java.util.NoSuchElementException ; 31 32 import org.eclipse.emf.common.notify.Notification; 33 import org.eclipse.emf.common.notify.NotificationChain; 34 import org.eclipse.emf.common.util.BasicEList; 35 import org.eclipse.emf.common.util.EList; 36 import org.eclipse.emf.common.util.UniqueEList; 37 import org.eclipse.emf.ecore.EClass; 38 import org.eclipse.emf.ecore.EClassifier; 39 import org.eclipse.emf.ecore.EObject; 40 import org.eclipse.emf.ecore.EStructuralFeature; 41 import org.eclipse.emf.ecore.ETypedElement; 42 import org.eclipse.emf.ecore.InternalEObject; 43 import org.eclipse.emf.ecore.impl.ENotificationImpl; 44 import org.eclipse.emf.ecore.impl.EStructuralFeatureImpl; 45 import org.eclipse.emf.ecore.xml.type.XMLTypePackage; 46 47 48 public final class FeatureMapUtil 49 { 50 protected static final Class VALIDATOR_CLASS = Validator.class; 51 52 private FeatureMapUtil() 53 { 54 } 55 56 public static void addText(FeatureMap featureMap, String text) 57 { 58 featureMap.add(XMLTypeFeatures.TEXT, text); 59 } 60 61 public static void addText(FeatureMap featureMap, int index, String text) 62 { 63 featureMap.add(index, XMLTypeFeatures.TEXT, text); 64 } 65 66 public static boolean isText(FeatureMap.Entry entry) 67 { 68 return entry.getEStructuralFeature() == XMLTypeFeatures.TEXT; 69 } 70 71 public static boolean isText(EStructuralFeature eStructuralFeature) 72 { 73 return eStructuralFeature == XMLTypeFeatures.TEXT; 74 } 75 76 public static void addCDATA(FeatureMap featureMap, String cdata) 77 { 78 featureMap.add(XMLTypeFeatures.CDATA, cdata); 79 } 80 81 public static void addCDATA(FeatureMap featureMap, int index, String cdata) 82 { 83 featureMap.add(index, XMLTypeFeatures.CDATA, cdata); 84 } 85 86 public static boolean isCDATA(FeatureMap.Entry entry) 87 { 88 return entry.getEStructuralFeature() == XMLTypeFeatures.CDATA; 89 } 90 91 public static boolean isCDATA(EStructuralFeature eStructuralFeature) 92 { 93 return eStructuralFeature == XMLTypeFeatures.CDATA; 94 } 95 96 public static void addComment(FeatureMap featureMap, String comment) 97 { 98 featureMap.add(XMLTypeFeatures.COMMENT, comment); 99 } 100 101 public static void addComment(FeatureMap featureMap, int index, String comment) 102 { 103 featureMap.add(index, XMLTypeFeatures.COMMENT, comment); 104 } 105 106 public static boolean isComment(FeatureMap.Entry entry) 107 { 108 return entry.getEStructuralFeature() == XMLTypeFeatures.COMMENT; 109 } 110 111 public static boolean isComment(EStructuralFeature eStructuralFeature) 112 { 113 return eStructuralFeature == XMLTypeFeatures.COMMENT; 114 } 115 116 public static boolean isFeatureMap(EStructuralFeature eStructuralFeature) 117 { 118 return ((EStructuralFeatureImpl)eStructuralFeature).isFeatureMap(); 119 } 120 121 public static boolean isFeatureMapEntry(EClassifier eClassifier) 122 { 123 return eClassifier.getInstanceClassName() == "org.eclipse.emf.ecore.util.FeatureMap$Entry"; 124 } 125 126 public static FeatureMap.Entry createEntry(EStructuralFeature eStructuralFeature, Object value) 127 { 128 return new EntryImpl(eStructuralFeature, value); 129 } 130 131 public static class EntryImpl implements FeatureMap.Entry 132 { 133 protected final EStructuralFeature eStructuralFeature; 134 protected final Object value; 135 136 public EntryImpl(EStructuralFeature eStructuralFeature, Object value) 137 { 138 this.eStructuralFeature = eStructuralFeature; 139 this.value = value; 140 if (value != null && !eStructuralFeature.getEType().isInstance(value)) 141 { 142 throw new ClassCastException (); 143 } 144 } 145 146 public EStructuralFeature getEStructuralFeature() 147 { 148 return eStructuralFeature; 149 } 150 151 public Object getValue() 152 { 153 return value; 154 } 155 156 public boolean equals(Object that) 157 { 158 if (this == that) 159 { 160 return true; 161 } 162 else if (!(that instanceof FeatureMap.Entry)) 163 { 164 return false; 165 } 166 else 167 { 168 FeatureMap.Entry entry = (FeatureMap.Entry)that; 169 return 170 entry.getEStructuralFeature() == eStructuralFeature && 171 (value == null ? entry.getValue() == null : value.equals(entry.getValue())); 172 } 173 } 174 175 public int hashCode() 176 { 177 return eStructuralFeature.hashCode() ^ (value == null ? 0 : value.hashCode()); 178 } 179 180 public String toString() 181 { 182 String prefix = eStructuralFeature.getEContainingClass().getEPackage().getNsPrefix(); 183 eStructuralFeature.getName(); 184 return 185 (prefix != null && prefix.length() != 0 ? 186 prefix + ":" + eStructuralFeature.getName() : 187 eStructuralFeature.getName()) + 188 "=" + value; 189 198 } 199 } 200 201 public static abstract class BasicFeatureEIterator implements ListIterator 202 { 203 protected final EStructuralFeature eStructuralFeature; 204 protected final FeatureMap.Internal featureMap; 205 206 protected int entryCursor; 207 protected int cursor; 208 protected int prepared; 209 protected Object preparedResult; 210 protected int expectedModCount; 211 protected int lastCursor; 212 protected boolean isFeatureMap; 213 protected Validator validator; 214 215 public BasicFeatureEIterator(EStructuralFeature eStructuralFeature, FeatureMap.Internal featureMap) 216 { 217 this.eStructuralFeature = eStructuralFeature; 218 this.featureMap = featureMap; 219 expectedModCount = featureMap.getModCount(); 220 isFeatureMap = isFeatureMap(eStructuralFeature); 221 validator = getValidator(featureMap.getEObject().eClass(), eStructuralFeature); 222 } 223 224 protected boolean resolve() 225 { 226 return false; 227 } 228 229 protected Object extractValue(FeatureMap.Entry entry) 230 { 231 return isFeatureMap ? entry : entry.getValue(); 232 } 233 234 public boolean hasNext() 235 { 236 switch (prepared) 237 { 238 case 2: 239 { 240 return true; 241 } 242 case 1: 243 { 244 return false; 245 } 246 case -1: 247 { 248 ++entryCursor; 249 } 250 default: 251 { 252 return scanNext(); 253 } 254 } 255 } 256 257 protected abstract boolean scanNext(); 258 279 280 public Object next() 281 { 282 if (hasNext()) 283 { 284 checkModCount(); 285 286 if (resolve()) 287 { 288 preparedResult = featureMap.resolveProxy(eStructuralFeature, entryCursor, cursor, preparedResult); 289 } 290 291 lastCursor = cursor; 292 ++cursor; 293 294 ++entryCursor; 295 prepared = 0; 296 return preparedResult; 297 } 298 else 299 { 300 throw new NoSuchElementException (); 301 } 302 } 303 304 public int nextIndex() 305 { 306 return cursor; 307 } 308 309 public boolean hasPrevious() 310 { 311 switch (prepared) 312 { 313 case -2: 314 { 315 return true; 316 } 317 case -1: 318 { 319 return false; 320 } 321 case 1: 322 { 323 --entryCursor; 324 } 325 default: 326 { 327 return scanPrevious(); 328 } 329 } 330 } 331 332 protected abstract boolean scanPrevious(); 333 352 353 public Object previous() 354 { 355 if (hasPrevious()) 356 { 357 checkModCount(); 358 lastCursor = --cursor; 359 if (resolve()) 360 { 361 preparedResult = featureMap.resolveProxy(eStructuralFeature, entryCursor, cursor, preparedResult); 362 } 363 prepared = 0; 365 return preparedResult; 366 } 367 else 368 { 369 throw new NoSuchElementException (); 370 } 371 } 372 373 public int previousIndex() 374 { 375 return cursor - 1; 376 } 377 378 public void add(Object o) 379 { 380 if (lastCursor == -1) 381 { 382 throw new IllegalStateException (); 383 } 384 checkModCount(); 385 386 try 387 { 388 featureMap.add(eStructuralFeature, cursor, o); 389 expectedModCount = featureMap.getModCount(); 390 next(); 391 402 } 403 catch (IndexOutOfBoundsException exception) 404 { 405 throw new ConcurrentModificationException (); 406 } 407 } 408 409 public void remove() 410 { 411 if (lastCursor == -1) 412 { 413 throw new IllegalStateException (); 414 } 415 checkModCount(); 416 417 try 418 { 419 featureMap.remove(eStructuralFeature, lastCursor); 420 expectedModCount = featureMap.getModCount(); 421 if (lastCursor < cursor) 422 { 423 --cursor; 424 --entryCursor; 425 } 426 427 --lastCursor; 428 } 431 catch (IndexOutOfBoundsException exception) 432 { 433 throw new ConcurrentModificationException (); 434 } 435 } 436 437 public void set(Object o) 438 { 439 if (lastCursor == -1) 440 { 441 throw new IllegalStateException (); 442 } 443 checkModCount(); 444 445 try 446 { 447 featureMap.set(eStructuralFeature, lastCursor, o); 448 expectedModCount = featureMap.getModCount(); 449 } 450 catch (IndexOutOfBoundsException exception) 451 { 452 throw new ConcurrentModificationException (); 453 } 454 } 455 456 460 protected void checkModCount() 461 { 462 if (featureMap.getModCount() != expectedModCount) 463 { 464 throw new ConcurrentModificationException (); 465 } 466 } 467 } 468 469 public static class FeatureEList extends AbstractList implements InternalEList.Unsettable, EStructuralFeature.Setting 470 { 471 public static class Basic extends FeatureEList 472 { 473 public Basic(EStructuralFeature feature, FeatureMap.Internal featureMap) 474 { 475 super(feature, featureMap); 476 } 477 478 public Iterator iterator() 479 { 480 return this.basicIterator(); 481 } 482 483 public ListIterator listIterator() 484 { 485 return this.basicListIterator(); 486 } 487 488 public ListIterator listIterator(int index) 489 { 490 return this.basicListIterator(index); 491 } 492 493 public List basicList() 494 { 495 return this; 496 } 497 } 498 499 protected EStructuralFeature feature; 500 protected FeatureMap.Internal featureMap; 501 502 public FeatureEList(EStructuralFeature feature, FeatureMap.Internal featureMap) 503 { 504 this.feature= feature; 505 this.featureMap = featureMap; 506 } 507 508 public int size() 509 { 510 return featureMap.size(getEStructuralFeature()); 511 } 512 513 public boolean isEmpty() 514 { 515 return featureMap.isEmpty(getEStructuralFeature()); 516 } 517 518 public boolean contains(Object object) 519 { 520 return featureMap.contains(getEStructuralFeature(), object); 521 } 522 523 public int indexOf(Object object) 524 { 525 return featureMap.indexOf(getEStructuralFeature(), object); 526 } 527 528 public int lastIndexOf(Object object) 529 { 530 return featureMap.lastIndexOf(getEStructuralFeature(), object); 531 } 532 533 public boolean containsAll(Collection collection) 534 { 535 return featureMap.containsAll(getEStructuralFeature(), collection); 536 } 537 538 public Iterator iterator() 539 { 540 return featureMap.iterator(getEStructuralFeature()); 541 } 542 543 public ListIterator listIterator() 544 { 545 return featureMap.listIterator(getEStructuralFeature()); 546 } 547 548 public ListIterator listIterator(int index) 549 { 550 return featureMap.listIterator(getEStructuralFeature(), index); 551 } 552 553 559 560 public Object basicGet(int index) 561 { 562 return featureMap.get(getEStructuralFeature(), index, false); 563 } 564 565 public List basicList() 566 { 567 return featureMap.basicList(getEStructuralFeature()); 568 } 569 570 public Iterator basicIterator() 571 { 572 return featureMap.basicIterator(getEStructuralFeature()); 573 } 574 575 public ListIterator basicListIterator() 576 { 577 return featureMap.basicListIterator(getEStructuralFeature()); 578 } 579 580 public ListIterator basicListIterator(int index) 581 { 582 return featureMap.basicListIterator(getEStructuralFeature(), index); 583 } 584 585 public Object [] toArray() 586 { 587 return featureMap.toArray(getEStructuralFeature()); 588 } 589 590 public Object [] toArray(Object [] array) 591 { 592 return featureMap.toArray(getEStructuralFeature(), array); 593 } 594 595 public boolean add(Object object) 596 { 597 return featureMap.add(getEStructuralFeature(), object); 598 } 599 600 public void add(int index, Object object) 601 { 602 featureMap.add(getEStructuralFeature(), index, object); 603 } 604 605 public boolean addAll(Collection collection) 606 { 607 return featureMap.addAll(getEStructuralFeature(), collection); 608 } 609 610 public boolean addAll(int index, Collection collection) 611 { 612 return featureMap.addAll(getEStructuralFeature(), index, collection); 613 } 614 615 public void addUnique(Object object) 616 { 617 featureMap.addUnique(getEStructuralFeature(), object); 618 } 619 620 public void addUnique(int index, Object object) 621 { 622 featureMap.addUnique(getEStructuralFeature(), index, object); 623 } 624 625 public NotificationChain basicAdd(Object object, NotificationChain notifications) 626 { 627 return featureMap.basicAdd(getEStructuralFeature(), object, notifications); 628 } 629 630 public boolean remove(Object object) 631 { 632 return featureMap.remove(getEStructuralFeature(), object); 633 } 634 635 public Object remove(int index) 636 { 637 return featureMap.remove(getEStructuralFeature(), index); 638 } 639 640 public NotificationChain basicRemove(Object object, NotificationChain notifications) 641 { 642 return featureMap.basicRemove(getEStructuralFeature(), object, notifications); 643 } 644 645 public boolean removeAll(Collection collection) 646 { 647 return featureMap.removeAll(getEStructuralFeature(), collection); 648 } 649 650 public boolean retainAll(Collection collection) 651 { 652 return featureMap.retainAll(getEStructuralFeature(), collection); 653 } 654 655 public void clear() 656 { 657 featureMap.clear(getEStructuralFeature()); 658 } 659 660 public void move(int index, Object object) 661 { 662 featureMap.move(getEStructuralFeature(), index, object); 663 } 664 665 public Object move(int targetIndex, int sourceIndex) 666 { 667 return featureMap.move(getEStructuralFeature(), targetIndex, sourceIndex); 668 } 669 670 public Object get(int index) 671 { 672 return featureMap.get(getEStructuralFeature(), index, true); 673 } 674 675 public Object set(int index, Object object) 676 { 677 return featureMap.set(getEStructuralFeature(), index, object); 678 } 679 680 public Object setUnique(int index, Object object) 681 { 682 return featureMap.setUnique(getEStructuralFeature(), index, object); 683 } 684 685 public Object get(boolean resolve) 686 { 687 return this; 688 } 689 690 public void set(Object newValue) 691 { 692 clear(); 693 addAll((List )newValue); 694 } 695 696 public boolean isSet() 697 { 698 return !isEmpty(); 699 } 700 701 public void unset() 702 { 703 clear(); 704 } 705 706 public Object getFeature() 707 { 708 return getEStructuralFeature(); 709 } 710 711 public int getFeatureID() 712 { 713 return getEStructuralFeature().getFeatureID(); 714 } 715 716 public EStructuralFeature getEStructuralFeature() 717 { 718 return feature; 720 } 721 722 public EObject getEObject() 723 { 724 return featureMap.getEObject(); 725 } 726 727 public String toString() 728 { 729 StringBuffer stringBuffer = new StringBuffer (); 730 stringBuffer.append("["); 731 for (Iterator i = basicIterator(); i.hasNext(); ) 732 { 733 stringBuffer.append(String.valueOf(i.next())); 734 if (i.hasNext()) 735 { 736 stringBuffer.append(", "); 737 } 738 } 739 stringBuffer.append("]"); 740 return stringBuffer.toString(); 741 } 742 } 743 744 public static class FeatureFeatureMap extends FeatureEList implements FeatureMap.Internal 745 { 746 public FeatureFeatureMap(EStructuralFeature feature, FeatureMap.Internal featureMap) 747 { 748 super(feature, featureMap); 749 } 750 751 public FeatureMap.ValueListIterator valueListIterator() 752 { 753 return featureMap.valueListIterator(); 754 } 755 756 public FeatureMap.ValueListIterator valueListIterator(int index) 757 { 758 return featureMap.valueListIterator(index); 759 } 760 761 public EList list(EStructuralFeature feature) 762 { 763 return featureMap.list(feature); 764 } 765 766 public EStructuralFeature getEStructuralFeature(int index) 767 { 768 return ((Entry)featureMap.get(getEStructuralFeature(), index, false)).getEStructuralFeature(); 769 } 770 771 public Object getValue(int index) 772 { 773 return ((Entry)featureMap.get(getEStructuralFeature(), index, false)).getValue(); 774 } 775 776 public Object setValue(int index, Object value) 777 { 778 Entry entry = (Entry)featureMap.get(getEStructuralFeature(), index, false); 779 set(index, createEntry(entry.getEStructuralFeature(), value)); 780 return entry.getValue(); 781 } 782 783 public boolean add(EStructuralFeature feature, Object value) 784 { 785 return featureMap.add(feature, value); 786 } 787 788 public void add(int index, EStructuralFeature feature, Object value) 789 { 790 add(index, isFeatureMap(feature) ? value : createEntry(feature, value)); 791 } 792 793 public void add(EStructuralFeature feature, int index, Object value) 794 { 795 featureMap.add(feature, index, value); 796 } 797 798 public boolean addAll(EStructuralFeature feature, Collection values) 799 { 800 return featureMap.addAll(feature, values); 801 } 802 803 public boolean addAll(int index, EStructuralFeature feature, Collection values) 804 { 805 if (isFeatureMap(feature)) 806 { 807 return addAll(index, values); 808 } 809 else 810 { 811 Collection entries = new ArrayList (values.size()); 812 for (Iterator i = values.iterator(); i.hasNext(); ) 813 { 814 entries.add(createEntry(feature, i.next())); 815 } 816 return addAll(index, entries); 817 } 818 } 819 820 public boolean addAll(EStructuralFeature feature, int index, Collection values) 821 { 822 return featureMap.addAll(feature, index, values); 823 } 824 825 public int getModCount() 826 { 827 return featureMap.getModCount(); 828 } 829 830 public EObject getEObject() 831 { 832 return featureMap.getEObject(); 833 } 834 835 public Object resolveProxy(EStructuralFeature feature, int entryIndex, int index, Object object) 836 { 837 return featureMap.resolveProxy(feature, entryIndex, index, object); 838 } 839 840 public int size(EStructuralFeature feature) 841 { 842 return featureMap.size(feature); 843 } 844 845 public boolean isEmpty(EStructuralFeature feature) 846 { 847 return featureMap.isEmpty(feature); 848 } 849 850 public boolean contains(EStructuralFeature feature, Object object) 851 { 852 return featureMap.contains(feature, object); 853 } 854 855 public boolean containsAll(EStructuralFeature feature, Collection collection) 856 { 857 return featureMap.containsAll(feature, collection); 858 } 859 860 public int indexOf(EStructuralFeature feature, Object object) 861 { 862 return featureMap.indexOf(feature, object); 863 } 864 865 public int lastIndexOf(EStructuralFeature feature, Object object) 866 { 867 return featureMap.lastIndexOf(feature, object); 868 } 869 870 public Iterator iterator(EStructuralFeature feature) 871 { 872 return featureMap.iterator(feature); 873 } 874 875 public ListIterator listIterator(EStructuralFeature feature) 876 { 877 return featureMap.listIterator(feature); 878 } 879 880 public ListIterator listIterator(EStructuralFeature feature, int index) 881 { 882 return featureMap.listIterator(feature, index); 883 } 884 885 public EStructuralFeature.Setting setting(EStructuralFeature feature) 888 { 889 return featureMap.setting(feature); 890 } 891 892 public List basicList(EStructuralFeature feature) 893 { 894 return featureMap.basicList(feature); 895 } 896 897 public Iterator basicIterator(EStructuralFeature feature) 898 { 899 return featureMap.basicIterator(feature); 900 } 901 902 public ListIterator basicListIterator(EStructuralFeature feature) 903 { 904 return featureMap.basicListIterator(feature); 905 } 906 907 public ListIterator basicListIterator(EStructuralFeature feature, int index) 908 { 909 return featureMap.basicListIterator(feature, index); 910 } 911 912 public Object [] toArray(EStructuralFeature feature) 913 { 914 return featureMap.toArray(feature); 915 } 916 917 public Object [] toArray(EStructuralFeature feature, Object [] array) 918 { 919 return featureMap.toArray(feature, array); 920 } 921 922 943 944 public void addUnique(EStructuralFeature feature, Object object) 945 { 946 featureMap.addUnique(feature, object); 947 } 948 949 public void addUnique(EStructuralFeature feature, int index, Object object) 950 { 951 featureMap.addUnique(feature, index, object); 952 } 953 954 public NotificationChain basicAdd(EStructuralFeature feature, Object object, NotificationChain notifications) 955 { 956 return featureMap.basicAdd(feature, object, notifications); 957 } 958 959 public boolean remove(EStructuralFeature feature, Object object) 960 { 961 return featureMap.remove(feature, object); 962 } 963 964 public Object remove(EStructuralFeature feature, int index) 965 { 966 return featureMap.remove(feature, index); 967 } 968 969 public boolean removeAll(EStructuralFeature feature, Collection collection) 970 { 971 return featureMap.removeAll(feature, collection); 972 } 973 974 public NotificationChain basicRemove(EStructuralFeature feature, Object object, NotificationChain notifications) 975 { 976 return featureMap.basicRemove(feature, object, notifications); 977 } 978 979 public boolean retainAll(EStructuralFeature feature, Collection collection) 980 { 981 return featureMap.retainAll(feature, collection); 982 } 983 984 public void clear(EStructuralFeature feature) 985 { 986 featureMap.clear(feature); 987 } 988 989 public void move(EStructuralFeature feature, int index, Object object) 990 { 991 featureMap.move(feature, index, object); 992 } 993 994 public Object move(EStructuralFeature feature, int targetIndex, int sourceIndex) 995 { 996 return featureMap.move(feature, targetIndex, sourceIndex); 997 } 998 999 public Object get(EStructuralFeature feature, boolean resolve) 1000 { 1001 return featureMap.get(feature, resolve); 1002 } 1003 1004 public Object get(EStructuralFeature feature, int index, boolean resolve) 1005 { 1006 return featureMap.get(feature, index, resolve); 1007 } 1008 1009 public void set(EStructuralFeature feature, Object object) 1010 { 1011 featureMap.set(feature, object); 1012 } 1013 1014 public Object set(EStructuralFeature feature, int index, Object object) 1015 { 1016 return featureMap.set(feature, index, object); 1017 } 1018 1019 public Object setUnique(EStructuralFeature feature, int index, Object object) 1020 { 1021 return featureMap.setUnique(feature, index, object); 1022 } 1023 1024 public boolean isSet(EStructuralFeature feature) 1025 { 1026 return featureMap.isSet(feature); 1027 } 1028 1029 public void unset(EStructuralFeature feature) 1030 { 1031 featureMap.unset(feature); 1032 } 1033 } 1034 1035 public static class FeatureValue implements EStructuralFeature.Setting 1036 { 1037 protected EStructuralFeature feature; 1038 protected FeatureMap.Internal featureMap; 1039 1040 public FeatureValue(EStructuralFeature feature, FeatureMap.Internal featureMap) 1041 { 1042 this.feature = feature; 1043 this.featureMap = featureMap; 1044 } 1045 1046 public Object get(boolean resolve) 1047 { 1048 return featureMap.get(getEStructuralFeature(), -1, resolve); 1049 } 1050 1051 public void set(Object newValue) 1052 { 1053 featureMap.set(getEStructuralFeature(), newValue); 1054 } 1055 1056 public boolean isSet() 1057 { 1058 return !featureMap.isEmpty(getEStructuralFeature()); 1059 } 1060 1061 public void unset() 1062 { 1063 featureMap.clear(getEStructuralFeature()); 1064 } 1065 1066 public Object getFeature() 1067 { 1068 return getEStructuralFeature(); 1069 } 1070 1071 public int getFeatureID() 1072 { 1073 return getEStructuralFeature().getFeatureID(); 1074 } 1075 1076 public EStructuralFeature getEStructuralFeature() 1077 { 1078 return feature; 1079 } 1080 1081 public EObject getEObject() 1082 { 1083 return featureMap.getEObject(); 1084 } 1085 } 1086 1087 public static class FeatureENotificationImpl extends ENotificationImpl 1088 { 1089 public FeatureENotificationImpl 1090 (InternalEObject owner, int eventType, EStructuralFeature feature, Object oldObject, Object newObject, int index) 1091 { 1092 super(owner, eventType, feature, oldObject, newObject, index); 1093 } 1094 1095 public FeatureENotificationImpl 1096 (InternalEObject owner, int eventType, EStructuralFeature feature, Object oldObject, Object newObject, int index, boolean wasSet) 1097 { 1098 super(owner, eventType, feature, oldObject, newObject, index, wasSet); 1099 } 1100 1101 public boolean merge(Notification notification) 1102 { 1103 switch (eventType) 1104 { 1105 case Notification.SET: 1106 case Notification.UNSET: 1107 { 1108 Object notificationNotifier = notification.getNotifier(); 1109 if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null)) 1110 { 1111 newValue = notification.getNewValue(); 1112 if (notification.getEventType() == Notification.SET) 1113 { 1114 eventType = Notification.SET; 1115 } 1116 return true; 1117 } 1118 break; 1119 } 1120 case Notification.ADD: 1121 { 1122 int notificationEventType = notification.getEventType(); 1123 switch (notificationEventType) 1124 { 1125 case Notification.ADD: 1126 { 1127 Object notificationNotifier = notification.getNotifier(); 1128 if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null)) 1129 { 1130 eventType = Notification.ADD_MANY; 1131 BasicEList addedValues = new BasicEList(2); 1132 addedValues.add(newValue); 1133 addedValues.add(notification.getNewValue()); 1134 newValue = addedValues; 1135 return true; 1136 } 1137 break; 1138 } 1139 } 1140 break; 1141 } 1142 case Notification.ADD_MANY: 1143 { 1144 int notificationEventType = notification.getEventType(); 1145 switch (notificationEventType) 1146 { 1147 case Notification.ADD: 1148 { 1149 Object notificationNotifier = notification.getNotifier(); 1150 if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null)) 1151 { 1152 ((Collection )newValue).add(notification.getNewValue()); 1153 return true; 1154 } 1155 break; 1156 } 1157 } 1158 break; 1159 } 1160 case Notification.REMOVE: 1161 { 1162 int notificationEventType = notification.getEventType(); 1163 switch (notificationEventType) 1164 { 1165 case Notification.ADD: 1166 { 1167 Object notificationNotifier = notification.getNotifier(); 1168 if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null)) 1169 { 1170 eventType = Notification.SET; 1171 newValue = notification.getNewValue(); 1172 return true; 1173 } 1174 break; 1175 } 1176 case Notification.REMOVE: 1177 { 1178 Object notificationNotifier = notification.getNotifier(); 1179 if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null)) 1180 { 1181 eventType = Notification.REMOVE_MANY; 1182 BasicEList removedValues = new BasicEList(2); 1183 removedValues.add(oldValue); 1184 removedValues.add(notification.getOldValue()); 1185 oldValue = removedValues; 1186 1187 int [] positions = new int [] { position, notification.getPosition() }; 1188 newValue = positions; 1189 return true; 1190 } 1191 break; 1192 } 1193 } 1194 break; 1195 } 1196 case Notification.REMOVE_MANY: 1197 { 1198 int notificationEventType = notification.getEventType(); 1199 switch (notificationEventType) 1200 { 1201 case Notification.REMOVE: 1202 { 1203 Object notificationNotifier = notification.getNotifier(); 1204 if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null)) 1205 { 1206 ((Collection )oldValue).add(notification.getOldValue()); 1207 1208 int [] positions = (int [])newValue; 1209 int [] newPositions = new int [positions.length + 1]; 1210 1211 System.arraycopy(positions, 0, newPositions, 0, positions.length); 1212 newPositions[positions.length] = notification.getPosition(); 1213 newValue = newPositions; 1214 1215 return true; 1216 } 1217 break; 1218 } 1219 } 1220 break; 1221 } 1222 } 1223 1224 return false; 1225 } 1226 } 1227 1228 public interface Validator 1229 { 1230 boolean isValid(EStructuralFeature feature); 1231 } 1232 1233 public static class BasicValidator implements Validator 1234 { 1235 protected EClass containingClass; 1236 protected EStructuralFeature eStructuralFeature; 1237 protected List groupMembers; 1238 protected List wildcards; 1239 protected String name; 1240 protected boolean isElement; 1241 1242 public BasicValidator(EClass containingClass, EStructuralFeature eStructuralFeature) 1243 { 1244 this.containingClass = containingClass; 1245 this.eStructuralFeature = eStructuralFeature; 1246 1247 wildcards = ExtendedMetaData.INSTANCE.getWildcards(eStructuralFeature); 1248 if (!wildcards.isEmpty()) 1249 { 1250 isElement = ExtendedMetaData.INSTANCE.getFeatureKind(eStructuralFeature) == ExtendedMetaData.ELEMENT_WILDCARD_FEATURE; 1251 } 1252 else if (ExtendedMetaData.INSTANCE.getMixedFeature(containingClass) == eStructuralFeature) 1253 { 1254 isElement = true; 1255 groupMembers = new ArrayList (); 1256 wildcards = new UniqueEList(); 1257 wildcards.add(XMLTypePackage.eNS_URI); 1258 for (Iterator i = ExtendedMetaData.INSTANCE.getAllElements(containingClass).iterator(); i.hasNext(); ) 1259 { 1260 EStructuralFeature feature = (EStructuralFeature)i.next(); 1261 switch (ExtendedMetaData.INSTANCE.getFeatureKind(feature)) 1262 { 1263 case ExtendedMetaData.ELEMENT_FEATURE: 1264 { 1265 groupMembers.add(feature); 1266 break; 1267 } 1268 case ExtendedMetaData.ELEMENT_WILDCARD_FEATURE: 1269 { 1270 wildcards.addAll(ExtendedMetaData.INSTANCE.getWildcards(feature)); 1271 break; 1272 } 1273 } 1274 } 1275 } 1276 else if (isFeatureMap(eStructuralFeature)) 1277 { 1278 isElement = true; 1279 wildcards = null; 1280 groupMembers = new ArrayList (); 1281 for (int i = 0, size = containingClass.getFeatureCount(); i < size; ++i) 1282 { 1283 EStructuralFeature feature = containingClass.getEStructuralFeature(i); 1284 for (EStructuralFeature group = ExtendedMetaData.INSTANCE.getGroup(feature); 1285 group != null; 1286 group = ExtendedMetaData.INSTANCE.getGroup(group)) 1287 { 1288 if (group == eStructuralFeature) 1289 { 1290 groupMembers.add(feature); 1291 } 1292 } 1293 } 1294 } 1295 else 1296 { 1297 wildcards = null; 1298 isElement = true; 1299 groupMembers = Collections.singletonList(eStructuralFeature); 1300 } 1301 } 1302 1303 public boolean isValid(EStructuralFeature feature) 1304 { 1305 if (groupMembers != null && 1306 (groupMembers.contains(feature) || 1307 groupMembers.contains(ExtendedMetaData.INSTANCE.getGroup(feature)) || 1308 groupMembers.contains(ExtendedMetaData.INSTANCE.getAffiliation(containingClass, feature)))) 1309 { 1310 return true; 1311 } 1312 1313 if (wildcards != null) 1314 { 1315 if (ExtendedMetaData.INSTANCE.matches(wildcards, ExtendedMetaData.INSTANCE.getNamespace(feature))) 1316 { 1317 return isElement == (ExtendedMetaData.INSTANCE.getFeatureKind(feature) == ExtendedMetaData.ELEMENT_FEATURE); 1318 } 1319 } 1320 1321 return false; 1322 } 1323 } 1324 1325 protected static Validator NULL_VALIDATOR = 1326 new Validator() 1327 { 1328 public boolean isValid(EStructuralFeature eStructuralFeature) 1329 { 1330 return true; 1331 } 1332 }; 1333 1334 public static Validator getValidator(EClass containingClass, EStructuralFeature eStructuralFeature) 1335 { 1336 if (eStructuralFeature == null) 1337 { 1338 return NULL_VALIDATOR; 1339 } 1340 else 1341 { 1342 BasicExtendedMetaData.EStructuralFeatureExtendedMetaData.Holder holder = 1343 (BasicExtendedMetaData.EStructuralFeatureExtendedMetaData.Holder)eStructuralFeature; 1344 BasicExtendedMetaData.EStructuralFeatureExtendedMetaData extendedMetaData = holder.getExtendedMetaData(); 1345 if (extendedMetaData == null) 1346 { 1347 ExtendedMetaData.INSTANCE.getName(eStructuralFeature); 1350 extendedMetaData = holder.getExtendedMetaData(); 1351 } 1352 Map validatorMap = extendedMetaData.getValidatorMap(); 1353 Validator result = (Validator)validatorMap.get(containingClass); 1354 if (result == null) 1355 { 1356 validatorMap.put(containingClass, result = new BasicValidator(containingClass, eStructuralFeature)); 1357 } 1358 return result; 1359 } 1360 } 1361 1362 public static boolean isMany(EObject owner, EStructuralFeature feature) 1363 { 1364 int upperBound = feature.getUpperBound(); 1365 if (upperBound == ETypedElement.UNSPECIFIED_MULTIPLICITY) 1366 { 1367 EClass eclass = owner.eClass(); 1368 if (eclass.getFeatureID(feature) >= 0) 1369 { 1370 return false; 1371 } 1372 else if (feature.getEContainingClass().getEPackage() == XMLTypePackage.eINSTANCE) 1373 { 1374 return true; 1375 } 1376 else 1377 { 1378 EStructuralFeature affiliation = ExtendedMetaData.INSTANCE.getAffiliation(eclass, feature); 1379 return 1380 affiliation == null || 1381 affiliation.isMany() && ExtendedMetaData.INSTANCE.getFeatureKind(affiliation) != ExtendedMetaData.ATTRIBUTE_WILDCARD_FEATURE; 1382 } 1383 } 1384 else 1385 { 1386 return upperBound == ETypedElement.UNBOUNDED_MULTIPLICITY || upperBound > 1 || FeatureMapUtil.isFeatureMap(feature); 1387 } 1388 } 1389} 1390 1391final class XMLTypeFeatures 1392{ 1393 public static final EStructuralFeature TEXT = XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Text(); 1394 public static final EStructuralFeature CDATA = XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_CDATA(); 1395 public static final EStructuralFeature COMMENT = XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Comment(); 1396} 1397 1398 | Popular Tags |