1 17 package org.eclipse.emf.ecore.impl; 18 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 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 52 public abstract class EModelElementImpl extends EObjectImpl implements EModelElement 53 { 54 62 protected EList eAnnotations = null; 63 64 69 protected EModelElementImpl() 70 { 71 super(); 72 } 73 74 77 protected static final int EFROZEN = ELAST_EOBJECT_FLAG << 1; 78 79 82 protected static final int ELAST_EMODEL_ELEMENT_FLAG = EFROZEN; 83 84 protected void freeze() 85 { 86 eFlags |= EFROZEN; 87 for (Iterator i = eContents().iterator(); i.hasNext(); ) 88 { 89 Object 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 107 protected EClass eStaticClass() 108 { 109 return EcorePackage.eINSTANCE.getEModelElement(); 110 } 111 112 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 131 public EAnnotation getEAnnotation(String source) 132 { 133 if (source == null) 134 { 135 for (Iterator 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 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 164 public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, Class 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 186 public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, Class 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 206 public Object 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 221 public void eSet(EStructuralFeature eFeature, Object newValue) 222 { 223 switch (eDerivedStructuralFeatureID(eFeature)) 224 { 225 case EcorePackage.EMODEL_ELEMENT__EANNOTATIONS: 226 getEAnnotations().clear(); 227 getEAnnotations().addAll((Collection )newValue); 228 return; 229 } 230 eDynamicSet(eFeature, newValue); 231 } 232 233 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 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 eURIFragmentSegment(EStructuralFeature eStructuralFeature, EObject eObject) 265 { 266 if (eObject instanceof ENamedElement) 267 { 268 ENamedElement eNamedElement = (ENamedElement)eObject; 269 String name = eNamedElement.getName(); 270 if (name != null) 271 { 272 int count = 0; 273 for (Iterator i = eContents().iterator(); i.hasNext(); ) 274 { 275 Object 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 source = eAnnotation.getSource(); 299 if (source != null) 300 { 301 int count = 0; 302 for (Iterator i = eContents().iterator(); i.hasNext(); ) 303 { 304 Object 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 result = new StringBuffer (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 uriFragmentSegment) 335 { 336 if (uriFragmentSegment.length() > 0) 337 { 338 char firstCharacter = uriFragmentSegment.charAt(0); 341 if (firstCharacter != '@') 342 { 343 if (firstCharacter == '%') 346 { 347 int index = uriFragmentSegment.lastIndexOf("%"); 350 if (index != -1) 351 { 352 String source = URI.decode(uriFragmentSegment.substring(1, index)); 355 356 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 exception) 367 { 368 throw new WrappedException(exception); 369 } 370 } 371 372 for (Iterator i = eContents().iterator(); i.hasNext(); ) 375 { 376 Object 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 int index = uriFragmentSegment.lastIndexOf("."); 393 String 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 exception) 402 { 403 name = uriFragmentSegment; 406 } 407 } 408 409 for (Iterator i = eContents().iterator(); i.hasNext(); ) 412 { 413 Object 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 |