1 17 package org.eclipse.emf.ecore.impl; 18 19 20 import java.math.BigDecimal ; 21 import java.math.BigInteger ; 22 import java.text.ParseException ; 23 import java.util.Date ; 24 import java.util.Map ; 25 26 import org.eclipse.emf.common.util.WrappedException; 27 import org.eclipse.emf.ecore.*; 28 import org.eclipse.emf.ecore.EClass; 29 import org.eclipse.emf.ecore.EObject; 30 import org.eclipse.emf.ecore.util.FeatureMap; 31 32 38 public class EcoreFactoryImpl extends EFactoryImpl implements EcoreFactory 39 { 40 46 public EcoreFactoryImpl() 47 { 48 super(); 49 } 50 51 56 public EObject create(EClass eClass) 57 { 58 switch (eClass.getClassifierID()) 59 { 60 case EcorePackage.EATTRIBUTE: return createEAttribute(); 61 case EcorePackage.EANNOTATION: return createEAnnotation(); 62 case EcorePackage.ECLASS: return createEClass(); 63 case EcorePackage.EDATA_TYPE: return createEDataType(); 64 case EcorePackage.EENUM: return createEEnum(); 65 case EcorePackage.EENUM_LITERAL: return createEEnumLiteral(); 66 case EcorePackage.EFACTORY: return createEFactory(); 67 case EcorePackage.EOBJECT: return createEObject(); 68 case EcorePackage.EOPERATION: return createEOperation(); 69 case EcorePackage.EPACKAGE: return createEPackage(); 70 case EcorePackage.EPARAMETER: return createEParameter(); 71 case EcorePackage.EREFERENCE: return createEReference(); 72 case EcorePackage.ESTRING_TO_STRING_MAP_ENTRY: return (EObject)createEStringToStringMapEntry(); 73 default: 74 throw new IllegalArgumentException ("The class '" + eClass.getName() + "' is not a valid classifier"); 75 } 76 } 77 78 83 public Object createFromString(EDataType eDataType, String initialValue) 84 { 85 switch (eDataType.getClassifierID()) 86 { 87 case EcorePackage.EBIG_DECIMAL: 88 return createEBigDecimalFromString(eDataType, initialValue); 89 case EcorePackage.EBIG_INTEGER: 90 return createEBigIntegerFromString(eDataType, initialValue); 91 case EcorePackage.EBOOLEAN: 92 return createEBooleanFromString(eDataType, initialValue); 93 case EcorePackage.EBOOLEAN_OBJECT: 94 return createEBooleanObjectFromString(eDataType, initialValue); 95 case EcorePackage.EBYTE: 96 return createEByteFromString(eDataType, initialValue); 97 case EcorePackage.EBYTE_ARRAY: 98 return createEByteArrayFromString(eDataType, initialValue); 99 case EcorePackage.EBYTE_OBJECT: 100 return createEByteObjectFromString(eDataType, initialValue); 101 case EcorePackage.ECHAR: 102 return createECharFromString(eDataType, initialValue); 103 case EcorePackage.ECHARACTER_OBJECT: 104 return createECharacterObjectFromString(eDataType, initialValue); 105 case EcorePackage.EDATE: 106 return createEDateFromString(eDataType, initialValue); 107 case EcorePackage.EDOUBLE: 108 return createEDoubleFromString(eDataType, initialValue); 109 case EcorePackage.EDOUBLE_OBJECT: 110 return createEDoubleObjectFromString(eDataType, initialValue); 111 case EcorePackage.EFEATURE_MAP_ENTRY: 112 return createEFeatureMapEntryFromString(eDataType, initialValue); 113 case EcorePackage.EFLOAT: 114 return createEFloatFromString(eDataType, initialValue); 115 case EcorePackage.EFLOAT_OBJECT: 116 return createEFloatObjectFromString(eDataType, initialValue); 117 case EcorePackage.EINT: 118 return createEIntFromString(eDataType, initialValue); 119 case EcorePackage.EINTEGER_OBJECT: 120 return createEIntegerObjectFromString(eDataType, initialValue); 121 case EcorePackage.EJAVA_CLASS: 122 return createEJavaClassFromString(eDataType, initialValue); 123 case EcorePackage.ELONG: 124 return createELongFromString(eDataType, initialValue); 125 case EcorePackage.ELONG_OBJECT: 126 return createELongObjectFromString(eDataType, initialValue); 127 case EcorePackage.ESHORT: 128 return createEShortFromString(eDataType, initialValue); 129 case EcorePackage.ESHORT_OBJECT: 130 return createEShortObjectFromString(eDataType, initialValue); 131 case EcorePackage.ESTRING: 132 return createEStringFromString(eDataType, initialValue); 133 default: 134 throw new IllegalArgumentException ("The datatype '" + eDataType.getName() + "' is not a valid classifier"); 135 } 136 } 137 138 143 public String convertToString(EDataType eDataType, Object instanceValue) 144 { 145 switch (eDataType.getClassifierID()) 146 { 147 case EcorePackage.EBIG_DECIMAL: 148 return convertEBigDecimalToString(eDataType, instanceValue); 149 case EcorePackage.EBIG_INTEGER: 150 return convertEBigIntegerToString(eDataType, instanceValue); 151 case EcorePackage.EBOOLEAN: 152 return convertEBooleanToString(eDataType, instanceValue); 153 case EcorePackage.EBOOLEAN_OBJECT: 154 return convertEBooleanObjectToString(eDataType, instanceValue); 155 case EcorePackage.EBYTE: 156 return convertEByteToString(eDataType, instanceValue); 157 case EcorePackage.EBYTE_ARRAY: 158 return convertEByteArrayToString(eDataType, instanceValue); 159 case EcorePackage.EBYTE_OBJECT: 160 return convertEByteObjectToString(eDataType, instanceValue); 161 case EcorePackage.ECHAR: 162 return convertECharToString(eDataType, instanceValue); 163 case EcorePackage.ECHARACTER_OBJECT: 164 return convertECharacterObjectToString(eDataType, instanceValue); 165 case EcorePackage.EDATE: 166 return convertEDateToString(eDataType, instanceValue); 167 case EcorePackage.EDOUBLE: 168 return convertEDoubleToString(eDataType, instanceValue); 169 case EcorePackage.EDOUBLE_OBJECT: 170 return convertEDoubleObjectToString(eDataType, instanceValue); 171 case EcorePackage.EFEATURE_MAP_ENTRY: 172 return convertEFeatureMapEntryToString(eDataType, instanceValue); 173 case EcorePackage.EFLOAT: 174 return convertEFloatToString(eDataType, instanceValue); 175 case EcorePackage.EFLOAT_OBJECT: 176 return convertEFloatObjectToString(eDataType, instanceValue); 177 case EcorePackage.EINT: 178 return convertEIntToString(eDataType, instanceValue); 179 case EcorePackage.EINTEGER_OBJECT: 180 return convertEIntegerObjectToString(eDataType, instanceValue); 181 case EcorePackage.EJAVA_CLASS: 182 return convertEJavaClassToString(eDataType, instanceValue); 183 case EcorePackage.ELONG: 184 return convertELongToString(eDataType, instanceValue); 185 case EcorePackage.ELONG_OBJECT: 186 return convertELongObjectToString(eDataType, instanceValue); 187 case EcorePackage.ESHORT: 188 return convertEShortToString(eDataType, instanceValue); 189 case EcorePackage.ESHORT_OBJECT: 190 return convertEShortObjectToString(eDataType, instanceValue); 191 case EcorePackage.ESTRING: 192 return convertEStringToString(eDataType, instanceValue); 193 default: 194 throw new IllegalArgumentException ("The datatype '" + eDataType.getName() + "' is not a valid classifier"); 195 } 196 } 197 198 203 public EObject createEObject() 204 { 205 EObjectImpl eObject = new EObjectImpl(); 206 return eObject; 207 } 208 209 214 public EAttribute createEAttribute() 215 { 216 EAttributeImpl eAttribute = new EAttributeImpl(); 217 return eAttribute; 218 } 219 220 225 public EAnnotation createEAnnotation() 226 { 227 EAnnotationImpl eAnnotation = new EAnnotationImpl(); 228 return eAnnotation; 229 } 230 231 236 public EClass createEClass() 237 { 238 EClassImpl eClass = new EClassImpl(); 239 return eClass; 240 } 241 242 247 public EDataType createEDataType() 248 { 249 EDataTypeImpl eDataType = new EDataTypeImpl(); 250 return eDataType; 251 } 252 253 258 public EParameter createEParameter() 259 { 260 EParameterImpl eParameter = new EParameterImpl(); 261 return eParameter; 262 } 263 264 269 public EOperation createEOperation() 270 { 271 EOperationImpl eOperation = new EOperationImpl(); 272 return eOperation; 273 } 274 275 280 public EPackage createEPackage() 281 { 282 EPackageImpl ePackage = new EPackageImpl(); 283 return ePackage; 284 } 285 286 291 public EFactory createEFactory() 292 { 293 EFactoryImpl eFactory = new EFactoryImpl(); 294 return eFactory; 295 } 296 297 302 public EEnumLiteral createEEnumLiteral() 303 { 304 EEnumLiteralImpl eEnumLiteral = new EEnumLiteralImpl(); 305 return eEnumLiteral; 306 } 307 308 313 public EEnum createEEnum() 314 { 315 EEnumImpl eEnum = new EEnumImpl(); 316 return eEnum; 317 } 318 319 protected Boolean booleanValueOf(String initialValue) 320 { 321 if ("true".equalsIgnoreCase(initialValue)) 322 { 323 return Boolean.TRUE; 324 } 325 else if ("false".equalsIgnoreCase(initialValue)) 326 { 327 return Boolean.FALSE; 328 } 329 else 330 { 331 throw new IllegalArgumentException ("Expecting true or false"); 332 } 333 } 334 335 340 public Boolean createEBooleanObjectFromString(EDataType metaObject, String initialValue) 341 { 342 return initialValue == null ? null : booleanValueOf(initialValue); 343 } 344 345 350 public String convertEBooleanObjectToString(EDataType metaObject, Object instanceValue) 351 { 352 return instanceValue == null ? null : instanceValue.toString(); 353 } 354 355 360 public Character createECharacterObjectFromString(EDataType metaObject, String initialValue) 361 { 362 if (initialValue == null) 363 { 364 return null; 365 } 366 367 char charValue = 0; 368 try 369 { 370 Integer value = new Integer (initialValue); 371 charValue = (char) value.intValue(); 372 } 373 catch (NumberFormatException e) 374 { 375 char[] carray = initialValue.toCharArray(); 376 charValue = carray[0]; 377 } 378 return new Character (charValue); 379 } 380 381 386 public String convertECharacterObjectToString(EDataType metaObject, Object instanceValue) 387 { 388 if (instanceValue instanceof Character ) 389 { 390 int charInt = ((Character ) instanceValue).charValue(); 391 Integer value = new Integer (charInt); 392 return value.toString(); 393 } 394 395 return instanceValue == null ? null : instanceValue.toString(); 396 } 397 398 403 public Date createEDateFromString(EDataType eDataType, String initialValue) 404 { 405 if (initialValue == null) 406 { 407 return null; 408 } 409 410 Exception exception = null; 411 for (int i = 0; i < EDATE_FORMATS.length; ++i) 412 { 413 try 414 { 415 return EDATE_FORMATS[i].parse(initialValue); 416 } 417 catch (ParseException parseException) 418 { 419 exception = parseException; 420 } 421 } 422 throw new WrappedException(exception); 423 } 424 425 430 public String convertEDateToString(EDataType eDataType, Object instanceValue) 431 { 432 if (instanceValue == null) 433 { 434 return null; 435 } 436 else 437 { 438 return EDATE_FORMATS[0].format((Date )instanceValue); 439 } 440 } 441 442 447 public Double createEDoubleObjectFromString(EDataType metaObject, String initialValue) 448 { 449 return initialValue == null ? null : Double.valueOf(initialValue); 450 } 451 452 457 public String convertEDoubleObjectToString(EDataType metaObject, Object instanceValue) 458 { 459 return instanceValue == null ? null : instanceValue.toString(); 460 } 461 462 467 public Float createEFloatObjectFromString(EDataType metaObject, String initialValue) 468 { 469 return initialValue == null ? null : Float.valueOf(initialValue); 470 } 471 472 477 public String convertEFloatObjectToString(EDataType metaObject, Object instanceValue) 478 { 479 return instanceValue == null ? null : instanceValue.toString(); 480 } 481 482 487 public Integer createEIntegerObjectFromString(EDataType metaObject, String initialValue) 488 { 489 return initialValue == null ? null : Integer.valueOf(initialValue); 490 } 491 492 497 public String convertEIntegerObjectToString(EDataType metaObject, Object instanceValue) 498 { 499 return instanceValue == null ? null : instanceValue.toString(); 500 } 501 502 507 public EReference createEReference() 508 { 509 EReferenceImpl eReference = new EReferenceImpl(); 510 return eReference; 511 } 512 513 518 public Map.Entry createEStringToStringMapEntry() 519 { 520 EStringToStringMapEntryImpl eStringToStringMapEntry = new EStringToStringMapEntryImpl(); 521 return eStringToStringMapEntry; 522 } 523 524 529 public BigDecimal createEBigDecimalFromString(EDataType eDataType, String initialValue) 530 { 531 return initialValue == null ? null : new BigDecimal (initialValue); 532 } 533 534 539 public String convertEBigDecimalToString(EDataType eDataType, Object instanceValue) 540 { 541 return instanceValue == null ? null : instanceValue.toString(); 542 } 543 544 549 public BigInteger createEBigIntegerFromString(EDataType eDataType, String initialValue) 550 { 551 return initialValue == null ? null : new BigInteger (initialValue); 552 } 553 554 559 public String convertEBigIntegerToString(EDataType eDataType, Object instanceValue) 560 { 561 return instanceValue == null ? null : instanceValue.toString(); 562 } 563 564 569 public EcorePackage getEcorePackage() 570 { 571 return (EcorePackage)getEPackage(); 572 } 573 574 577 public static EcorePackage getPackage() 578 { 579 return EcorePackage.eINSTANCE; 580 } 581 582 587 public String createEStringFromString(EDataType metaObject, String initialValue) 588 { 589 return initialValue; 590 } 591 592 597 public String convertEStringToString(EDataType metaObject, Object instanceValue) 598 { 599 return (String )instanceValue; 600 } 601 602 607 public FeatureMap.Entry createEFeatureMapEntryFromString(EDataType eDataType, String initialValue) 608 { 609 return (FeatureMap.Entry)super.createFromString(eDataType, initialValue); 610 } 611 612 617 public String convertEFeatureMapEntryToString(EDataType eDataType, Object instanceValue) 618 { 619 return super.convertToString(eDataType, instanceValue); 620 } 621 622 627 public Integer createEIntFromString(EDataType metaObject, String initialValue) 628 { 629 return initialValue == null ? null : Integer.valueOf(initialValue); 630 } 631 632 637 public String convertEIntToString(EDataType metaObject, Object instanceValue) 638 { 639 return instanceValue == null ? null : instanceValue.toString(); 640 } 641 642 647 public Boolean createEBooleanFromString(EDataType metaObject, String initialValue) 648 { 649 return initialValue == null ? null : booleanValueOf(initialValue); 650 } 651 652 657 public String convertEBooleanToString(EDataType metaObject, Object instanceValue) 658 { 659 return instanceValue == null ? null : instanceValue.toString(); 660 } 661 662 667 public Byte createEByteObjectFromString(EDataType metaObject, String initialValue) 668 { 669 return initialValue == null ? null : Byte.valueOf(initialValue); 670 } 671 672 677 public String convertEByteObjectToString(EDataType metaObject, Object instanceValue) 678 { 679 return instanceValue == null ? null : instanceValue.toString(); 680 } 681 682 687 public Float createEFloatFromString(EDataType metaObject, String initialValue) 688 { 689 return initialValue == null ? null : Float.valueOf(initialValue); 690 } 691 692 697 public String convertEFloatToString(EDataType metaObject, Object instanceValue) 698 { 699 return instanceValue == null ? null : instanceValue.toString(); 700 } 701 702 707 public Character createECharFromString(EDataType metaObject, String initialValue) 708 { 709 if (initialValue == null) 710 { 711 return null; 712 } 713 char charValue = 0; 714 try 715 { 716 Integer value = new Integer (initialValue); 717 charValue = (char) value.intValue(); 718 } 719 catch (NumberFormatException e) 720 { 721 char[] carray = initialValue.toCharArray(); 722 charValue = carray[0]; 723 } 724 return new Character (charValue); 725 } 726 727 732 public String convertECharToString(EDataType metaObject, Object instanceValue) 733 { 734 if (instanceValue instanceof Character ) 735 { 736 int charInt = ((Character ) instanceValue).charValue(); 737 Integer value = new Integer (charInt); 738 return value.toString(); 739 } 740 741 return instanceValue == null ? null : instanceValue.toString(); 742 } 743 744 749 public Long createELongFromString(EDataType metaObject, String initialValue) 750 { 751 return initialValue == null ? null : Long.valueOf(initialValue); 752 } 753 754 759 public String convertELongToString(EDataType metaObject, Object instanceValue) 760 { 761 return instanceValue == null ? null : instanceValue.toString(); 762 } 763 764 769 public Double createEDoubleFromString(EDataType metaObject, String initialValue) 770 { 771 return initialValue == null ? null : Double.valueOf(initialValue); 772 } 773 774 779 public String convertEDoubleToString(EDataType metaObject, Object instanceValue) 780 { 781 return instanceValue == null ? null : instanceValue.toString(); 782 } 783 784 789 public Byte createEByteFromString(EDataType metaObject, String initialValue) 790 { 791 return initialValue == null ? null : Byte.valueOf(initialValue); 792 } 793 794 799 public String convertEByteToString(EDataType metaObject, Object instanceValue) 800 { 801 return instanceValue == null ? null : instanceValue.toString(); 802 } 803 804 805 810 public byte[] createEByteArrayFromString(EDataType eDataType, String initialValue) 811 { 812 if (initialValue == null) 813 { 814 return null; 815 } 816 817 int size = initialValue.length(); 818 int limit = (size + 1) / 2; 819 byte [] result = new byte[limit]; 820 if (size % 2 != 0) 821 { 822 result[--limit] = hexCharToByte(initialValue.charAt(size - 1)); 823 } 824 825 for (int i = 0, j = 0; i < limit; ++i) 826 { 827 byte high = hexCharToByte(initialValue.charAt(j++)); 828 byte low = hexCharToByte(initialValue.charAt(j++)); 829 result[i] = (byte)(high << 4 | low); 830 } 831 return result; 832 } 833 834 protected static byte hexCharToByte(char character) 835 { 836 switch (character) 837 { 838 case '0': 839 case '1': 840 case '2': 841 case '3': 842 case '4': 843 case '5': 844 case '6': 845 case '7': 846 case '8': 847 case '9': 848 { 849 return (byte)(character - '0'); 850 } 851 case 'a': 852 case 'b': 853 case 'c': 854 case 'd': 855 case 'e': 856 case 'f': 857 { 858 return (byte)(character - 'a' + 10); 859 } 860 case 'A': 861 case 'B': 862 case 'C': 863 case 'D': 864 case 'E': 865 case 'F': 866 { 867 return (byte)(character - 'A' + 10); 868 } 869 default: 870 { 871 throw new NumberFormatException ("Invalid hexadecimal"); 872 } 873 } 874 } 875 876 protected static final char [] HEX_DIGITS = 877 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 878 879 884 public String convertEByteArrayToString(EDataType eDataType, Object instanceValue) 885 { 886 if (instanceValue == null) 887 { 888 return null; 889 } 890 else 891 { 892 byte [] bytes = (byte[])instanceValue; 893 char [] result = new char[2 * bytes.length]; 894 for (int i = 0, j = 0; i < bytes.length; ++i) 895 { 896 int high = (bytes[i] >> 4) & 0xF; 897 int low = bytes[i] & 0xF; 898 result[j++] = HEX_DIGITS[high]; 899 result[j++] = HEX_DIGITS[low]; 900 } 901 return new String (result); 902 } 903 } 904 905 910 public Short createEShortFromString(EDataType metaObject, String initialValue) 911 { 912 return initialValue == null ? null : Short.valueOf(initialValue); 913 } 914 915 920 public String convertEShortToString(EDataType metaObject, Object instanceValue) 921 { 922 return instanceValue == null ? null : instanceValue.toString(); 923 } 924 925 930 public Class createEJavaClassFromString(EDataType metaObject, String initialValue) 931 { 932 try 933 { 934 if (initialValue == null) return null; 935 else if ("boolean".equals(initialValue)) return boolean.class; 936 else if ("byte".equals(initialValue)) return byte.class; 937 else if ("char".equals(initialValue)) return char.class; 938 else if ("double".equals(initialValue)) return double.class; 939 else if ("float".equals(initialValue)) return float.class; 940 else if ("int".equals(initialValue)) return int.class; 941 else if ("long".equals(initialValue)) return long.class; 942 else if ("short".equals(initialValue)) return short.class; 943 else return Class.forName(initialValue); 944 } 945 catch (ClassNotFoundException e) 946 { 947 throw new WrappedException(e); 948 } 949 } 950 951 956 public String convertEJavaClassToString(EDataType metaObject, Object instanceValue) 957 { 958 return instanceValue == null ? "" : ((Class )instanceValue).getName(); 959 } 960 961 966 public Long createELongObjectFromString(EDataType metaObject, String initialValue) 967 { 968 return initialValue == null ? null : Long.valueOf(initialValue); 969 } 970 971 976 public String convertELongObjectToString(EDataType metaObject, Object instanceValue) 977 { 978 return instanceValue == null ? null : instanceValue.toString(); 979 } 980 981 986 public Short createEShortObjectFromString(EDataType metaObject, String initialValue) 987 { 988 return initialValue == null ? null : Short.valueOf(initialValue); 989 } 990 991 996 public String convertEShortObjectToString(EDataType metaObject, Object instanceValue) 997 { 998 return instanceValue == null ? null : instanceValue.toString(); 999 } 1000} | Popular Tags |