KickJava   Java API By Example, From Geeks To Geeks.

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


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: EModelElementImpl.java,v 1.5 2005/06/08 06:20:10 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.impl;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.eclipse.emf.common.notify.NotificationChain;
24 import org.eclipse.emf.common.util.EList;
25 import org.eclipse.emf.common.util.URI;
26 import org.eclipse.emf.common.util.WrappedException;
27 import org.eclipse.emf.ecore.EAnnotation;
28 import org.eclipse.emf.ecore.EClass;
29 import org.eclipse.emf.ecore.EModelElement;
30 import org.eclipse.emf.ecore.ENamedElement;
31 import org.eclipse.emf.ecore.EObject;
32 import org.eclipse.emf.ecore.EStructuralFeature;
33 import org.eclipse.emf.ecore.EcorePackage;
34 import org.eclipse.emf.ecore.InternalEObject;
35 import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
36 import org.eclipse.emf.ecore.util.InternalEList;
37
38
39 /**
40  * <!-- begin-user-doc -->
41  * An implementation of the model object '<em><b>EModel Element</b></em>'.
42  * <!-- end-user-doc -->
43  * <p>
44  * The following features are implemented:
45  * <ul>
46  * <li>{@link org.eclipse.emf.ecore.impl.EModelElementImpl#getEAnnotations <em>EAnnotations</em>}</li>
47  * </ul>
48  * </p>
49  *
50  * @generated
51  */

52 public abstract class EModelElementImpl extends EObjectImpl implements EModelElement
53 {
54   /**
55    * The cached value of the '{@link #getEAnnotations() <em>EAnnotations</em>}' containment reference list.
56    * <!-- begin-user-doc -->
57    * <!-- end-user-doc -->
58    * @see #getEAnnotations()
59    * @generated
60    * @ordered
61    */

62   protected EList eAnnotations = null;
63
64   /**
65    * <!-- begin-user-doc -->
66    * <!-- end-user-doc -->
67    * @generated
68    */

69   protected EModelElementImpl()
70   {
71     super();
72   }
73
74   /**
75    * The bit of {@link #eFlags} that is used to represent being {@link #freeze() frozen}.
76    */

77   protected static final int EFROZEN = ELAST_EOBJECT_FLAG << 1;
78
79   /**
80    * The last bit used by this class; derived classes may use bit values higher than this.
81    */

82   protected static final int ELAST_EMODEL_ELEMENT_FLAG = EFROZEN;
83
84   protected void freeze()
85   {
86     eFlags |= EFROZEN;
87     for (Iterator JavaDoc i = eContents().iterator(); i.hasNext(); )
88     {
89       Object JavaDoc child = i.next();
90       if (child instanceof EModelElementImpl)
91       {
92         ((EModelElementImpl)child).freeze();
93       }
94     }
95   }
96
97   protected boolean isFrozen()
98   {
99     return (eFlags & EFROZEN) != 0;
100   }
101
102   /**
103    * <!-- begin-user-doc -->
104    * <!-- end-user-doc -->
105    * @generated
106    */

107   protected EClass eStaticClass()
108   {
109     return EcorePackage.eINSTANCE.getEModelElement();
110   }
111
112   /**
113    * <!-- begin-user-doc -->
114    * <!-- end-user-doc -->
115    * @generated
116    */

117   public EList getEAnnotations()
118   {
119     if (eAnnotations == null)
120     {
121       eAnnotations = new EObjectContainmentWithInverseEList(EAnnotation.class, this, EcorePackage.EMODEL_ELEMENT__EANNOTATIONS, EcorePackage.EANNOTATION__EMODEL_ELEMENT);
122     }
123     return eAnnotations;
124   }
125
126   /**
127    * <!-- begin-user-doc -->
128    * <!-- end-user-doc -->
129    * @generated NOT
130    */

131   public EAnnotation getEAnnotation(String JavaDoc source)
132   {
133     if (source == null)
134     {
135       for (Iterator JavaDoc i = getEAnnotations().iterator(); i.hasNext(); )
136       {
137         EAnnotation eAnnotation = (EAnnotation)i.next();
138         if (eAnnotation.getSource() == null)
139         {
140           return eAnnotation;
141         }
142       }
143     }
144     else
145     {
146       for (Iterator JavaDoc i = getEAnnotations().iterator(); i.hasNext(); )
147       {
148         EAnnotation eAnnotation = (EAnnotation)i.next();
149         if (source.equals(eAnnotation.getSource()))
150         {
151           return eAnnotation;
152         }
153       }
154     }
155
156     return null;
157   }
158
159   /**
160    * <!-- begin-user-doc -->
161    * <!-- end-user-doc -->
162    * @generated
163    */

164   public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, Class JavaDoc baseClass, NotificationChain msgs)
165   {
166     if (featureID >= 0)
167     {
168       switch (eDerivedStructuralFeatureID(featureID, baseClass))
169       {
170         case EcorePackage.EMODEL_ELEMENT__EANNOTATIONS:
171           return ((InternalEList)getEAnnotations()).basicAdd(otherEnd, msgs);
172         default:
173           return eDynamicInverseAdd(otherEnd, featureID, baseClass, msgs);
174       }
175     }
176     if (eContainer != null)
177       msgs = eBasicRemoveFromContainer(msgs);
178     return eBasicSetContainer(otherEnd, featureID, msgs);
179   }
180
181   /**
182    * <!-- begin-user-doc -->
183    * <!-- end-user-doc -->
184    * @generated
185    */

186   public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class JavaDoc baseClass, NotificationChain msgs)
187   {
188     if (featureID >= 0)
189     {
190       switch (eDerivedStructuralFeatureID(featureID, baseClass))
191       {
192         case EcorePackage.EMODEL_ELEMENT__EANNOTATIONS:
193           return ((InternalEList)getEAnnotations()).basicRemove(otherEnd, msgs);
194         default:
195           return eDynamicInverseRemove(otherEnd, featureID, baseClass, msgs);
196       }
197     }
198     return eBasicSetContainer(null, featureID, msgs);
199   }
200
201   /**
202    * <!-- begin-user-doc -->
203    * <!-- end-user-doc -->
204    * @generated
205    */

206   public Object JavaDoc eGet(EStructuralFeature eFeature, boolean resolve)
207   {
208     switch (eDerivedStructuralFeatureID(eFeature))
209     {
210       case EcorePackage.EMODEL_ELEMENT__EANNOTATIONS:
211         return getEAnnotations();
212     }
213     return eDynamicGet(eFeature, resolve);
214   }
215
216   /**
217    * <!-- begin-user-doc -->
218    * <!-- end-user-doc -->
219    * @generated
220    */

221   public void eSet(EStructuralFeature eFeature, Object JavaDoc newValue)
222   {
223     switch (eDerivedStructuralFeatureID(eFeature))
224     {
225       case EcorePackage.EMODEL_ELEMENT__EANNOTATIONS:
226         getEAnnotations().clear();
227         getEAnnotations().addAll((Collection JavaDoc)newValue);
228         return;
229     }
230     eDynamicSet(eFeature, newValue);
231   }
232
233   /**
234    * <!-- begin-user-doc -->
235    * <!-- end-user-doc -->
236    * @generated
237    */

238   public void eUnset(EStructuralFeature eFeature)
239   {
240     switch (eDerivedStructuralFeatureID(eFeature))
241     {
242       case EcorePackage.EMODEL_ELEMENT__EANNOTATIONS:
243         getEAnnotations().clear();
244         return;
245     }
246     eDynamicUnset(eFeature);
247   }
248
249   /**
250    * <!-- begin-user-doc -->
251    * <!-- end-user-doc -->
252    * @generated
253    */

254   public boolean eIsSet(EStructuralFeature eFeature)
255   {
256     switch (eDerivedStructuralFeatureID(eFeature))
257     {
258       case EcorePackage.EMODEL_ELEMENT__EANNOTATIONS:
259         return eAnnotations != null && !eAnnotations.isEmpty();
260     }
261     return eDynamicIsSet(eFeature);
262   }
263
264   public String JavaDoc eURIFragmentSegment(EStructuralFeature eStructuralFeature, EObject eObject)
265   {
266     if (eObject instanceof ENamedElement)
267     {
268       ENamedElement eNamedElement = (ENamedElement)eObject;
269       String JavaDoc name = eNamedElement.getName();
270       if (name != null)
271       {
272         int count = 0;
273         for (Iterator JavaDoc i = eContents().iterator(); i.hasNext(); )
274         {
275           Object JavaDoc otherEObject = i.next();
276           if (otherEObject == eObject)
277           {
278             break;
279           }
280           if (otherEObject instanceof ENamedElement)
281           {
282             ENamedElement otherENamedElement = (ENamedElement)otherEObject;
283             if (name.equals(otherENamedElement.getName()))
284             {
285               ++count;
286             }
287           }
288         }
289         return
290           count > 0 ?
291             name + "." + count :
292             name;
293       }
294     }
295     else if (eObject instanceof EAnnotation)
296     {
297       EAnnotation eAnnotation = (EAnnotation)eObject;
298       String JavaDoc source = eAnnotation.getSource();
299       if (source != null)
300       {
301         int count = 0;
302         for (Iterator JavaDoc i = eContents().iterator(); i.hasNext(); )
303         {
304           Object JavaDoc otherEObject = i.next();
305           if (otherEObject == eObject)
306           {
307             break;
308           }
309           if (otherEObject instanceof EAnnotation)
310           {
311             EAnnotation otherEAnnotation = (EAnnotation)otherEObject;
312             if (source.equals(otherEAnnotation.getSource()))
313             {
314               ++count;
315             }
316           }
317         }
318         
319         StringBuffer JavaDoc result = new StringBuffer JavaDoc(source.length() + 5);
320         result.append('%');
321         result.append(URI.encodeSegment(source, false));
322         result.append('%');
323         if (count > 0)
324         {
325           result.append('.');
326           result.append(count);
327         }
328         return result.toString();
329       }
330     }
331     return super.eURIFragmentSegment(eStructuralFeature, eObject);
332   }
333   
334   public EObject eObjectForURIFragmentSegment(String JavaDoc uriFragmentSegment)
335   {
336     if (uriFragmentSegment.length() > 0)
337     {
338       // Is the first character a special character, i.e., something other than '@'?
339
//
340
char firstCharacter = uriFragmentSegment.charAt(0);
341       if (firstCharacter != '@')
342       {
343         // Is it the start of a source URI of an annotation?
344
//
345
if (firstCharacter == '%')
346         {
347           // Find the closing '%'
348
//
349
int index = uriFragmentSegment.lastIndexOf("%");
350           if (index != -1)
351           {
352             // Decode all encoded characters.
353
//
354
String JavaDoc source = URI.decode(uriFragmentSegment.substring(1, index));
355             
356             // Check for a count, i.e., a '.' followed by a number.
357
//
358
int count = 0;
359             ++index;
360             if (uriFragmentSegment.length() > index && uriFragmentSegment.charAt(index) == '.')
361             {
362               try
363               {
364                 count = Integer.parseInt(uriFragmentSegment.substring(index + 1));
365               }
366               catch (NumberFormatException JavaDoc exception)
367               {
368                 throw new WrappedException(exception);
369               }
370             }
371             
372             // Look for the annotation with the matching source.
373
//
374
for (Iterator JavaDoc i = eContents().iterator(); i.hasNext(); )
375             {
376               Object JavaDoc object = i.next();
377               if (object instanceof EAnnotation)
378               {
379                 EAnnotation eAnnotation = (EAnnotation)object;
380                 if (source.equals(eAnnotation.getSource()) && count-- == 0)
381                 {
382                   return eAnnotation;
383                 }
384               }
385             }
386           }
387         }
388         else
389         {
390           // Look for trailing count.
391
//
392
int index = uriFragmentSegment.lastIndexOf(".");
393           String JavaDoc name = index == -1 ? uriFragmentSegment : uriFragmentSegment.substring(0, index);
394           int count = 0;
395           if (index != -1)
396           {
397             try
398             {
399               count = Integer.parseInt(uriFragmentSegment.substring(index + 1));
400             }
401             catch (NumberFormatException JavaDoc exception)
402             {
403               // Interpret it as part of the name.
404
//
405
name = uriFragmentSegment;
406             }
407           }
408     
409           // Look for a matching named element.
410
//
411
for (Iterator JavaDoc i = eContents().iterator(); i.hasNext(); )
412           {
413             Object JavaDoc object = i.next();
414             if (object instanceof ENamedElement)
415             {
416               ENamedElement eNamedElement = (ENamedElement)object;
417               if (name.equals(eNamedElement.getName()) && count-- == 0)
418               {
419                 return eNamedElement;
420               }
421             }
422           }
423         }
424   
425         return null;
426       }
427     }
428     
429     return super.eObjectForURIFragmentSegment(uriFragmentSegment);
430   }
431
432 }
433
Popular Tags