1 17 package org.eclipse.emf.ecore.impl; 18 19 20 import java.lang.reflect.Constructor ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.text.DateFormat ; 24 import java.text.FieldPosition ; 25 import java.text.ParseException ; 26 import java.text.SimpleDateFormat ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Date ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.StringTokenizer ; 33 34 import org.eclipse.emf.common.notify.Notification; 35 import org.eclipse.emf.common.notify.NotificationChain; 36 import org.eclipse.emf.ecore.EClass; 37 import org.eclipse.emf.ecore.EDataType; 38 import org.eclipse.emf.ecore.EEnum; 39 import org.eclipse.emf.ecore.EFactory; 40 import org.eclipse.emf.ecore.EObject; 41 import org.eclipse.emf.ecore.EPackage; 42 import org.eclipse.emf.ecore.EStructuralFeature; 43 import org.eclipse.emf.ecore.EcorePackage; 44 import org.eclipse.emf.ecore.InternalEObject; 45 import org.eclipse.emf.ecore.util.EcoreUtil; 46 import org.eclipse.emf.ecore.util.ExtendedMetaData; 47 import org.eclipse.emf.ecore.util.InternalEList; 48 import org.eclipse.emf.ecore.xml.type.util.XMLTypeUtil; 49 50 51 64 public class EFactoryImpl extends EModelElementImpl implements EFactory 65 { 66 74 protected EPackage ePackage = null; 75 76 81 protected EFactoryImpl() 82 { 83 super(); 84 } 85 86 91 protected EClass eStaticClass() 92 { 93 return EcorePackage.eINSTANCE.getEFactory(); 94 } 95 96 101 public EPackage getEPackage() 102 { 103 return ePackage; 104 } 105 106 111 public void setEPackage(EPackage newEPackage) 112 { 113 if (newEPackage != ePackage) 114 { 115 NotificationChain msgs = null; 116 if (ePackage != null) 117 msgs = ((InternalEObject)ePackage).eInverseRemove(this, EcorePackage.EPACKAGE__EFACTORY_INSTANCE, EPackage.class, msgs); 118 if (newEPackage != null) 119 msgs = ((InternalEObject)newEPackage).eInverseAdd(this, EcorePackage.EPACKAGE__EFACTORY_INSTANCE, EPackage.class, msgs); 120 msgs = basicSetEPackage(newEPackage, msgs); 121 if (msgs != null) msgs.dispatch(); 122 } 123 else if (eNotificationRequired()) 124 eNotify(new ENotificationImpl(this, Notification.SET, EcorePackage.EFACTORY__EPACKAGE, newEPackage, newEPackage)); 125 } 126 127 132 public NotificationChain basicSetEPackage(EPackage newEPackage, NotificationChain msgs) 133 { 134 EPackage oldEPackage = ePackage; 135 ePackage = newEPackage; 136 if (eNotificationRequired()) 137 { 138 ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, EcorePackage.EFACTORY__EPACKAGE, oldEPackage, newEPackage); 139 if (msgs == null) msgs = notification; else msgs.add(notification); 140 } 141 return msgs; 142 } 143 144 149 public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 150 { 151 if (featureID >= 0) 152 { 153 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 154 { 155 case EcorePackage.EFACTORY__EANNOTATIONS: 156 return ((InternalEList)getEAnnotations()).basicAdd(otherEnd, msgs); 157 case EcorePackage.EFACTORY__EPACKAGE: 158 if (ePackage != null) 159 msgs = ((InternalEObject)ePackage).eInverseRemove(this, EcorePackage.EPACKAGE__EFACTORY_INSTANCE, EPackage.class, msgs); 160 return basicSetEPackage((EPackage)otherEnd, msgs); 161 default: 162 return eDynamicInverseAdd(otherEnd, featureID, baseClass, msgs); 163 } 164 } 165 if (eContainer != null) 166 msgs = eBasicRemoveFromContainer(msgs); 167 return eBasicSetContainer(otherEnd, featureID, msgs); 168 } 169 170 175 public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class baseClass, NotificationChain msgs) 176 { 177 if (featureID >= 0) 178 { 179 switch (eDerivedStructuralFeatureID(featureID, baseClass)) 180 { 181 case EcorePackage.EFACTORY__EANNOTATIONS: 182 return ((InternalEList)getEAnnotations()).basicRemove(otherEnd, msgs); 183 case EcorePackage.EFACTORY__EPACKAGE: 184 return basicSetEPackage(null, msgs); 185 default: 186 return eDynamicInverseRemove(otherEnd, featureID, baseClass, msgs); 187 } 188 } 189 return eBasicSetContainer(null, featureID, msgs); 190 } 191 192 195 public EObject create(EClass eClass) 196 { 197 if (getEPackage() != eClass.getEPackage()) 198 { 199 throw new IllegalArgumentException ("The class '" + eClass.getName() + "' is not a valid classifier"); 200 } 201 202 for (List eSuperTypes = eClass.getESuperTypes(); !eSuperTypes.isEmpty(); ) 203 { 204 EClass eSuperType = (EClass)eSuperTypes.get(0); 205 if (eSuperType.getInstanceClass() != null) 206 { 207 EObject result = eSuperType.getEPackage().getEFactoryInstance().create(eSuperType); 208 ((InternalEObject)result).eSetClass(eClass); 209 return result; 210 } 211 eSuperTypes = eSuperType.getESuperTypes(); 212 } 213 214 return basicCreate(eClass); 215 } 216 217 protected EObject basicCreate(EClass eClass) 218 { 219 EObjectImpl result = new EObjectImpl(); 220 result.eSetClass(eClass); 221 return result; 222 } 223 224 227 public Object createFromString(EDataType eDataType, String stringValue) 228 { 229 if (stringValue == null) 230 { 231 return null; 232 } 233 234 if (getEPackage() != eDataType.getEPackage()) 235 { 236 throw new IllegalArgumentException ("The datatype '" + eDataType.getName() + "' is not a valid classifier"); 237 } 238 239 if (eDataType instanceof EEnum) 240 { 241 return ((EEnum)eDataType).getEEnumLiteral(stringValue); 242 } 243 244 switch (ExtendedMetaData.INSTANCE.getWhiteSpaceFacet(eDataType)) 245 { 246 case ExtendedMetaData.REPLACE_WHITE_SPACE: 247 { 248 stringValue = replaceWhiteSpace(stringValue); 249 break; 250 } 251 case ExtendedMetaData.COLLAPSE_WHITE_SPACE: 252 { 253 stringValue = collapseWhiteSpace(stringValue); 254 break; 255 } 256 } 257 258 EDataType baseType = ExtendedMetaData.INSTANCE.getBaseType(eDataType); 259 if (baseType != null) 260 { 261 return EcoreUtil.createFromString(baseType, stringValue); 262 } 263 264 EDataType itemType = ExtendedMetaData.INSTANCE.getItemType(eDataType); 265 if (itemType != null) 266 { 267 List result = new ArrayList (); 268 for (StringTokenizer stringTokenizer = new StringTokenizer (stringValue); stringTokenizer.hasMoreTokens(); ) 269 { 270 String item = stringTokenizer.nextToken(); 271 result.add(EcoreUtil.createFromString(itemType, item)); 272 } 273 return result; 274 } 275 276 List memberTypes = ExtendedMetaData.INSTANCE.getMemberTypes(eDataType); 277 if (!memberTypes.isEmpty()) 278 { 279 for (Iterator i = memberTypes.iterator(); i.hasNext(); ) 280 { 281 EDataType memberType = (EDataType)i.next(); 282 try 283 { 284 Object result = EcoreUtil.createFromString(memberType, stringValue); 285 if (result != null) 286 { 287 return result; 288 } 289 } 290 catch (RuntimeException exception) 291 { 292 } 293 } 294 throw new IllegalArgumentException ("The value '"+stringValue+"' does not match any member types of the union datatype '" + eDataType.getName() + "'"); 295 } 296 297 Class c = EcoreUtil.wrapperClassFor(eDataType.getInstanceClass()); 298 if (c == null) return null; 299 300 if (c == Character .class) 301 { 302 char charValue = 0; 303 try 304 { 305 Integer value = new Integer (stringValue); 306 charValue = (char) value.intValue(); 307 } 308 catch (NumberFormatException e) 309 { 310 char[] carray = stringValue.toCharArray(); 311 charValue = carray[0]; 312 } 313 314 return new Character (charValue); 315 } 316 317 if (c == Date .class) 318 { 319 for (int i = 0; i < EDATE_FORMATS.length; ++i) 320 { 321 try 322 { 323 return EDATE_FORMATS[i].parse(stringValue); 324 } 325 catch (ParseException parseException) 326 { 327 } 328 } 329 throw new IllegalArgumentException ("The value '" + stringValue + "' is not a date formatted string of the form yyyy-MM-dd'T'HH:mm:ss'.'SSSZ or a valid subset thereof"); 330 } 331 332 Class stringClass = String .class; 333 Class [] signature = { stringClass }; 334 335 Constructor ctor = null; 336 try 337 { 338 ctor = c.getConstructor(signature); 339 } 340 catch (NoSuchMethodException e) 341 { 342 } 343 Exception formatException = null; 344 try 345 { 346 if (ctor != null) 347 { 348 Object [] ctorArgs = {stringValue}; 349 return ctor.newInstance(ctorArgs); 350 } 351 } 352 catch (InstantiationException e) 353 { 354 formatException = e; 355 } 356 catch (InvocationTargetException e) 357 { 358 formatException = e; 359 } 360 catch (IllegalAccessException e) 361 { 362 formatException = e; 363 } 364 365 Method valueOf = null; 366 try 367 { 368 valueOf = c.getMethod("valueOf", signature); 369 } 370 catch (NoSuchMethodException e) 371 { 372 } 373 374 try 375 { 376 if (valueOf != null) 377 { 378 Object [] valueOfArgs = {stringValue}; 379 return valueOf.invoke(null, valueOfArgs); 380 } 381 } 382 catch (IllegalArgumentException e) 383 { 384 formatException = e; 385 } 386 catch (InvocationTargetException e) 387 { 388 formatException = e; 389 } 390 catch (IllegalAccessException e) 391 { 392 formatException = e; 393 } 394 String exceptionString = (formatException != null) ? formatException.toString() : ""; 395 throw new IllegalArgumentException ("The value '" + stringValue + "' is invalid. " + exceptionString); 396 } 397 398 401 public String convertToString(EDataType eDataType, Object objectValue) 402 { 403 if (getEPackage() != eDataType.getEPackage()) 404 { 405 throw new IllegalArgumentException ("The datatype '" + eDataType.getName() + "' is not a valid classifier"); 406 } 407 408 EDataType baseType = ExtendedMetaData.INSTANCE.getBaseType(eDataType); 409 if (baseType != null) 410 { 411 return EcoreUtil.convertToString(baseType, objectValue); 412 } 413 414 EDataType itemType = ExtendedMetaData.INSTANCE.getItemType(eDataType); 415 if (itemType != null) 416 { 417 if (objectValue == null) 418 { 419 return null; 420 } 421 List list = (List )objectValue; 422 if (list.isEmpty()) 423 { 424 return ""; 425 } 426 StringBuffer result = new StringBuffer (); 427 for (Iterator i = list.iterator(); i.hasNext(); ) 428 { 429 Object item = i.next(); 430 result.append(EcoreUtil.convertToString(itemType, item)); 431 result.append(' '); 432 } 433 return result.substring(0, result.length() - 1); 434 } 435 436 List memberTypes = ExtendedMetaData.INSTANCE.getMemberTypes(eDataType); 437 if (!memberTypes.isEmpty()) 438 { 439 for (Iterator i = memberTypes.iterator(); i.hasNext(); ) 440 { 441 EDataType memberType = (EDataType)i.next(); 442 if (memberType.isInstance(objectValue)) 443 { 444 try 445 { 446 String result = EcoreUtil.convertToString(memberType, objectValue); 447 if (result != null) 448 { 449 return result; 450 } 451 } 452 catch (Exception e) 453 { 454 } 455 } 456 } 457 throw new IllegalArgumentException ("Invalid value: '"+objectValue+"' for datatype :"+eDataType.getName()); 458 } 459 460 if (objectValue == null) 461 { 462 return null; 463 } 464 else if (objectValue instanceof Character ) 465 { 466 int charInt = ((Character ) objectValue).charValue(); 467 Integer value = new Integer (charInt); 468 return value.toString(); 469 } 470 else if (objectValue.getClass() == Date .class) 471 { 472 return EDATE_FORMATS[0].format((Date )objectValue); 473 } 474 else 475 { 476 return objectValue.toString(); 477 } 478 } 479 480 protected String replaceWhiteSpace(String value) 481 { 482 return XMLTypeUtil.normalize(value, false); 483 } 484 485 protected String collapseWhiteSpace(String value) 486 { 487 return XMLTypeUtil.normalize(value, true); 488 } 489 490 private static class SafeSimpleDateFormat extends SimpleDateFormat 491 { 492 public SafeSimpleDateFormat(String pattern) 493 { 494 super(pattern); 495 } 496 497 public synchronized Date parse(String source) throws ParseException 498 { 499 return super.parse(source); 500 } 501 502 public synchronized StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) 503 { 504 return super.format(date, toAppendTo, fieldPosition); 505 } 506 } 507 508 protected static final DateFormat [] EDATE_FORMATS = 509 { 510 new SafeSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSSZ"), 511 new SafeSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSS"), 512 new SafeSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"), 513 new SafeSimpleDateFormat("yyyy-MM-dd'T'HH:mm"), 514 new SafeSimpleDateFormat("yyyy-MM-dd") 515 }; 516 517 522 public Object eGet(EStructuralFeature eFeature, boolean resolve) 523 { 524 switch (eDerivedStructuralFeatureID(eFeature)) 525 { 526 case EcorePackage.EFACTORY__EANNOTATIONS: 527 return getEAnnotations(); 528 case EcorePackage.EFACTORY__EPACKAGE: 529 return getEPackage(); 530 } 531 return eDynamicGet(eFeature, resolve); 532 } 533 534 539 public boolean eIsSet(EStructuralFeature eFeature) 540 { 541 switch (eDerivedStructuralFeatureID(eFeature)) 542 { 543 case EcorePackage.EFACTORY__EANNOTATIONS: 544 return eAnnotations != null && !eAnnotations.isEmpty(); 545 case EcorePackage.EFACTORY__EPACKAGE: 546 return ePackage != null; 547 } 548 return eDynamicIsSet(eFeature); 549 } 550 551 556 public void eSet(EStructuralFeature eFeature, Object newValue) 557 { 558 switch (eDerivedStructuralFeatureID(eFeature)) 559 { 560 case EcorePackage.EFACTORY__EANNOTATIONS: 561 getEAnnotations().clear(); 562 getEAnnotations().addAll((Collection )newValue); 563 return; 564 case EcorePackage.EFACTORY__EPACKAGE: 565 setEPackage((EPackage)newValue); 566 return; 567 } 568 eDynamicSet(eFeature, newValue); 569 } 570 571 576 public void eUnset(EStructuralFeature eFeature) 577 { 578 switch (eDerivedStructuralFeatureID(eFeature)) 579 { 580 case EcorePackage.EFACTORY__EANNOTATIONS: 581 getEAnnotations().clear(); 582 return; 583 case EcorePackage.EFACTORY__EPACKAGE: 584 setEPackage((EPackage)null); 585 return; 586 } 587 eDynamicUnset(eFeature); 588 } 589 590 } 591 | Popular Tags |