KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > impl > EFactoryImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: EFactoryImpl.java,v 1.13 2005/06/12 13:29:22 emerks Exp $
16  */

17 package org.eclipse.emf.ecore.impl;
18
19
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.text.DateFormat JavaDoc;
24 import java.text.FieldPosition JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
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 /**
52  * <!-- begin-user-doc -->
53  * An implementation of the model object '<em><b>EFactory</b></em>'.
54  * <!-- end-user-doc -->
55  * <p>
56  * The following features are implemented:
57  * <ul>
58  * <li>{@link org.eclipse.emf.ecore.impl.EFactoryImpl#getEPackage <em>EPackage</em>}</li>
59  * </ul>
60  * </p>
61  *
62  * @generated
63  */

64 public class EFactoryImpl extends EModelElementImpl implements EFactory
65 {
66   /**
67    * The cached value of the '{@link #getEPackage() <em>EPackage</em>}' reference.
68    * <!-- begin-user-doc -->
69    * <!-- end-user-doc -->
70    * @see #getEPackage()
71    * @generated
72    * @ordered
73    */

74   protected EPackage ePackage = null;
75
76   /**
77    * <!-- begin-user-doc -->
78    * <!-- end-user-doc -->
79    * @generated
80    */

81   protected EFactoryImpl()
82   {
83     super();
84   }
85
86   /**
87    * <!-- begin-user-doc -->
88    * <!-- end-user-doc -->
89    * @generated
90    */

91   protected EClass eStaticClass()
92   {
93     return EcorePackage.eINSTANCE.getEFactory();
94   }
95
96   /**
97    * <!-- begin-user-doc -->
98    * <!-- end-user-doc -->
99    * @generated
100    */

101   public EPackage getEPackage()
102   {
103     return ePackage;
104   }
105
106   /**
107    * <!-- begin-user-doc -->
108    * <!-- end-user-doc -->
109    * @generated
110    */

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   /**
128    * <!-- begin-user-doc -->
129    * <!-- end-user-doc -->
130    * @generated
131    */

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   /**
145    * <!-- begin-user-doc -->
146    * <!-- end-user-doc -->
147    * @generated
148    */

149   public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, Class JavaDoc 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   /**
171    * <!-- begin-user-doc -->
172    * <!-- end-user-doc -->
173    * @generated
174    */

175   public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class JavaDoc 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   /**
193    * @generated modifiable
194    */

195   public EObject create(EClass eClass)
196   {
197     if (getEPackage() != eClass.getEPackage())
198     {
199       throw new IllegalArgumentException JavaDoc("The class '" + eClass.getName() + "' is not a valid classifier");
200     }
201
202     for (List JavaDoc 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   /**
225    * @generated NOT
226    */

227   public Object JavaDoc createFromString(EDataType eDataType, String JavaDoc stringValue)
228   {
229     if (stringValue == null)
230     {
231       return null;
232     }
233
234     if (getEPackage() != eDataType.getEPackage())
235     {
236       throw new IllegalArgumentException JavaDoc("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 JavaDoc result = new ArrayList JavaDoc();
268       for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(stringValue); stringTokenizer.hasMoreTokens(); )
269       {
270         String JavaDoc item = stringTokenizer.nextToken();
271         result.add(EcoreUtil.createFromString(itemType, item));
272       }
273       return result;
274     }
275
276     List JavaDoc memberTypes = ExtendedMetaData.INSTANCE.getMemberTypes(eDataType);
277     if (!memberTypes.isEmpty())
278     {
279       for (Iterator JavaDoc i = memberTypes.iterator(); i.hasNext(); )
280       {
281         EDataType memberType = (EDataType)i.next();
282         try
283         {
284           Object JavaDoc result = EcoreUtil.createFromString(memberType, stringValue);
285           if (result != null)
286           {
287             return result;
288           }
289         }
290         catch (RuntimeException JavaDoc exception)
291         {
292         }
293       }
294       throw new IllegalArgumentException JavaDoc("The value '"+stringValue+"' does not match any member types of the union datatype '" + eDataType.getName() + "'");
295     }
296
297     Class JavaDoc c = EcoreUtil.wrapperClassFor(eDataType.getInstanceClass());
298     if (c == null) return null;
299
300     if (c == Character JavaDoc.class)
301     {
302       char charValue = 0;
303       try
304       {
305         Integer JavaDoc value = new Integer JavaDoc(stringValue);
306         charValue = (char) value.intValue();
307       }
308       catch (NumberFormatException JavaDoc e)
309       {
310         char[] carray = stringValue.toCharArray();
311         charValue = carray[0];
312       }
313
314       return new Character JavaDoc(charValue);
315     }
316
317     if (c == Date JavaDoc.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 JavaDoc parseException)
326         {
327         }
328       }
329       throw new IllegalArgumentException JavaDoc("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 JavaDoc stringClass = String JavaDoc.class;
333     Class JavaDoc[] signature = { stringClass };
334
335     Constructor JavaDoc ctor = null;
336     try
337     {
338       ctor = c.getConstructor(signature);
339     }
340     catch (NoSuchMethodException JavaDoc e)
341     {
342     }
343     Exception JavaDoc formatException = null;
344     try
345     {
346       if (ctor != null)
347       {
348         Object JavaDoc[] ctorArgs = {stringValue};
349         return ctor.newInstance(ctorArgs);
350       }
351     }
352     catch (InstantiationException JavaDoc e)
353     {
354       formatException = e;
355     }
356     catch (InvocationTargetException JavaDoc e)
357     {
358       formatException = e;
359     }
360     catch (IllegalAccessException JavaDoc e)
361     {
362       formatException = e;
363     }
364     
365     Method JavaDoc valueOf = null;
366     try
367     {
368       valueOf = c.getMethod("valueOf", signature);
369     }
370     catch (NoSuchMethodException JavaDoc e)
371     {
372     }
373
374     try
375     {
376       if (valueOf != null)
377       {
378         Object JavaDoc[] valueOfArgs = {stringValue};
379         return valueOf.invoke(null, valueOfArgs);
380       }
381     }
382     catch (IllegalArgumentException JavaDoc e)
383     {
384       formatException = e;
385     }
386     catch (InvocationTargetException JavaDoc e)
387     {
388       formatException = e;
389     }
390     catch (IllegalAccessException JavaDoc e)
391     {
392       formatException = e;
393     }
394     String JavaDoc exceptionString = (formatException != null) ? formatException.toString() : "";
395     throw new IllegalArgumentException JavaDoc("The value '" + stringValue + "' is invalid. " + exceptionString);
396   }
397
398   /**
399    * @generated modifiable
400    */

401   public String JavaDoc convertToString(EDataType eDataType, Object JavaDoc objectValue)
402   {
403     if (getEPackage() != eDataType.getEPackage())
404     {
405       throw new IllegalArgumentException JavaDoc("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 JavaDoc list = (List JavaDoc)objectValue;
422       if (list.isEmpty())
423       {
424         return "";
425       }
426       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
427       for (Iterator JavaDoc i = list.iterator(); i.hasNext(); )
428       {
429         Object JavaDoc 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 JavaDoc memberTypes = ExtendedMetaData.INSTANCE.getMemberTypes(eDataType);
437     if (!memberTypes.isEmpty())
438     {
439       for (Iterator JavaDoc i = memberTypes.iterator(); i.hasNext(); )
440       {
441         EDataType memberType = (EDataType)i.next();
442         if (memberType.isInstance(objectValue))
443         {
444           try
445           {
446             String JavaDoc result = EcoreUtil.convertToString(memberType, objectValue);
447             if (result != null)
448             {
449               return result;
450             }
451           }
452           catch (Exception JavaDoc e)
453           {
454           }
455         }
456       }
457       throw new IllegalArgumentException JavaDoc("Invalid value: '"+objectValue+"' for datatype :"+eDataType.getName());
458     }
459
460     if (objectValue == null)
461     {
462       return null;
463     }
464     else if (objectValue instanceof Character JavaDoc)
465     {
466       int charInt = ((Character JavaDoc) objectValue).charValue();
467       Integer JavaDoc value = new Integer JavaDoc(charInt);
468       return value.toString();
469     }
470     else if (objectValue.getClass() == Date JavaDoc.class)
471     {
472       return EDATE_FORMATS[0].format((Date JavaDoc)objectValue);
473     }
474     else
475     {
476       return objectValue.toString();
477     }
478   }
479
480   protected String JavaDoc replaceWhiteSpace(String JavaDoc value)
481   {
482     return XMLTypeUtil.normalize(value, false);
483   }
484
485   protected String JavaDoc collapseWhiteSpace(String JavaDoc value)
486   {
487     return XMLTypeUtil.normalize(value, true);
488   }
489
490   private static class SafeSimpleDateFormat extends SimpleDateFormat JavaDoc
491   {
492     public SafeSimpleDateFormat(String JavaDoc pattern)
493     {
494       super(pattern);
495     }
496     
497     public synchronized Date JavaDoc parse(String JavaDoc source) throws ParseException JavaDoc
498     {
499       return super.parse(source);
500     }
501     
502     public synchronized StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc toAppendTo, FieldPosition JavaDoc fieldPosition)
503     {
504       return super.format(date, toAppendTo, fieldPosition);
505     }
506   }
507   
508   protected static final DateFormat JavaDoc [] 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   /**
518    * <!-- begin-user-doc -->
519    * <!-- end-user-doc -->
520    * @generated
521    */

522   public Object JavaDoc 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   /**
535    * <!-- begin-user-doc -->
536    * <!-- end-user-doc -->
537    * @generated
538    */

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   /**
552    * <!-- begin-user-doc -->
553    * <!-- end-user-doc -->
554    * @generated
555    */

556   public void eSet(EStructuralFeature eFeature, Object JavaDoc newValue)
557   {
558     switch (eDerivedStructuralFeatureID(eFeature))
559     {
560       case EcorePackage.EFACTORY__EANNOTATIONS:
561         getEAnnotations().clear();
562         getEAnnotations().addAll((Collection JavaDoc)newValue);
563         return;
564       case EcorePackage.EFACTORY__EPACKAGE:
565         setEPackage((EPackage)newValue);
566         return;
567     }
568     eDynamicSet(eFeature, newValue);
569   }
570
571   /**
572    * <!-- begin-user-doc -->
573    * <!-- end-user-doc -->
574    * @generated
575    */

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