1 17 package org.eclipse.emf.ecore.resource.impl; 18 19 20 import java.io.IOException ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.ListIterator ; 26 import java.util.Map ; 27 28 import org.eclipse.emf.common.notify.AdapterFactory; 29 import org.eclipse.emf.common.notify.NotificationChain; 30 import org.eclipse.emf.common.notify.impl.NotifierImpl; 31 import org.eclipse.emf.common.notify.impl.NotifyingListImpl; 32 import org.eclipse.emf.common.util.BasicEList; 33 import org.eclipse.emf.common.util.EList; 34 import org.eclipse.emf.common.util.TreeIterator; 35 import org.eclipse.emf.common.util.URI; 36 import org.eclipse.emf.common.util.WrappedException; 37 import org.eclipse.emf.ecore.EObject; 38 import org.eclipse.emf.ecore.EPackage; 39 import org.eclipse.emf.ecore.impl.EPackageRegistryImpl; 40 import org.eclipse.emf.ecore.resource.Resource; 41 import org.eclipse.emf.ecore.resource.ResourceSet; 42 import org.eclipse.emf.ecore.resource.URIConverter; 43 import org.eclipse.emf.ecore.util.EcoreUtil; 44 import org.eclipse.emf.ecore.util.InternalEList; 45 46 47 66 public class ResourceSetImpl extends NotifierImpl implements ResourceSet 67 { 68 72 protected EList resources; 73 74 78 protected EList adapterFactories; 79 80 84 protected Map loadOptions; 85 86 90 protected Resource.Factory.Registry resourceFactoryRegistry; 91 92 96 protected URIConverter uriConverter; 97 98 102 protected EPackage.Registry packageRegistry; 103 104 108 protected Map uriResourceMap; 109 110 113 public ResourceSetImpl() 114 { 115 } 116 117 122 public Map getURIResourceMap() 123 { 124 return uriResourceMap; 125 } 126 127 136 public void setURIResourceMap(Map uriResourceMap) 137 { 138 this.uriResourceMap = uriResourceMap; 139 } 140 141 144 public EList getResources() 145 { 146 if (resources == null) 147 { 148 resources = new ResourcesEList(); 149 } 150 return resources; 151 } 152 153 156 public TreeIterator getAllContents() 157 { 158 TreeIterator result = EcoreUtil.getAllContents(Collections.singleton(this)); 159 result.next(); 160 return result; 161 } 162 163 166 public EList getAdapterFactories() 167 { 168 if (adapterFactories == null) 169 { 170 adapterFactories = 171 new BasicEList() 172 { 173 protected boolean useEquals() 174 { 175 return false; 176 } 177 178 protected boolean isUnique() 179 { 180 return true; 181 } 182 183 protected Object [] newData(int capacity) 184 { 185 return new AdapterFactory [capacity]; 186 } 187 }; 188 } 189 return adapterFactories; 190 } 191 192 195 public Map getLoadOptions() 196 { 197 if (loadOptions == null) 198 { 199 loadOptions = new HashMap (); 200 } 201 202 return loadOptions; 203 } 204 205 208 public EObject getEObject(URI uri, boolean loadOnDemand) 209 { 210 Resource resource = getResource(uri.trimFragment(), loadOnDemand); 211 if (resource != null) 212 { 213 return resource.getEObject(uri.fragment()); 214 } 215 else 216 { 217 return null; 218 } 219 } 220 221 231 protected Resource demandCreateResource(URI uri) 232 { 233 return createResource(uri); 234 } 235 236 247 protected void demandLoad(Resource resource) throws IOException 248 { 249 resource.load(getLoadOptions()); 250 } 251 252 260 protected void demandLoadHelper(Resource resource) 261 { 262 try 263 { 264 demandLoad(resource); 265 } 266 catch (Resource.IOWrappedException exception) 267 { 268 throw new WrappedException(exception.getWrappedException()); 269 } 270 catch (IOException exception) 271 { 272 throw new WrappedException(exception); 273 } 274 } 275 276 286 protected Resource delegatedGetResource(URI uri, boolean loadOnDemand) 287 { 288 EPackage ePackage = getPackageRegistry().getEPackage(uri.toString()); 289 return ePackage == null ? null : ePackage.eResource(); 290 } 291 292 295 public Resource getResource(URI uri, boolean loadOnDemand) 296 { 297 Map map = getURIResourceMap(); 298 if (map != null) 299 { 300 Resource resource = (Resource)map.get(uri); 301 if (resource != null) 302 { 303 if (loadOnDemand && !resource.isLoaded()) 304 { 305 demandLoadHelper(resource); 306 } 307 return resource; 308 } 309 } 310 311 URIConverter theURIConverter = getURIConverter(); 312 URI normalizedURI = theURIConverter.normalize(uri); 313 for (Iterator i = getResources().iterator(); i.hasNext(); ) 314 { 315 Resource resource = (Resource)i.next(); 316 if (theURIConverter.normalize(resource.getURI()).equals(normalizedURI)) 317 { 318 if (loadOnDemand && !resource.isLoaded()) 319 { 320 demandLoadHelper(resource); 321 } 322 323 if (map != null) 324 { 325 map.put(uri, resource); 326 } 327 return resource; 328 } 329 } 330 331 Resource delegatedResource = delegatedGetResource(uri, loadOnDemand); 332 if (delegatedResource != null) 333 { 334 if (map != null) 335 { 336 map.put(uri, delegatedResource); 337 } 338 return delegatedResource; 339 } 340 341 if (loadOnDemand) 342 { 343 Resource resource = demandCreateResource(uri); 344 if (resource == null) 345 { 346 throw new RuntimeException ("Cannot create a resource for '" + uri + "'; a registered resource factory is needed"); 347 } 348 349 demandLoadHelper(resource); 350 351 if (map != null) 352 { 353 map.put(uri, resource); 354 } 355 return resource; 356 } 357 358 return null; 359 } 360 361 364 public Resource createResource(URI uri) 365 { 366 Resource.Factory resourceFactory = getResourceFactoryRegistry().getFactory(uri); 367 if (resourceFactory != null) 368 { 369 Resource result = resourceFactory.createResource(uri); 370 getResources().add(result); 371 return result; 372 } 373 else 374 { 375 return null; 376 } 377 } 378 379 382 public Resource.Factory.Registry getResourceFactoryRegistry() 383 { 384 if (resourceFactoryRegistry == null) 385 { 386 resourceFactoryRegistry = 387 new ResourceFactoryRegistryImpl() 388 { 389 public Resource.Factory delegatedGetFactory(URI uri) 390 { 391 return Resource.Factory.Registry.INSTANCE.getFactory(uri); 392 } 393 }; 394 } 395 return resourceFactoryRegistry; 396 } 397 398 401 public void setResourceFactoryRegistry(Resource.Factory.Registry resourceFactoryRegistry) 402 { 403 this.resourceFactoryRegistry = resourceFactoryRegistry; 404 } 405 406 409 public URIConverter getURIConverter() 410 { 411 if (uriConverter == null) 412 { 413 uriConverter = new URIConverterImpl(); 414 } 415 return uriConverter; 416 } 417 418 421 public void setURIConverter(URIConverter uriConverter) 422 { 423 this.uriConverter = uriConverter; 424 } 425 426 429 public EPackage.Registry getPackageRegistry() 430 { 431 if (packageRegistry == null) 432 { 433 packageRegistry = new EPackageRegistryImpl(EPackage.Registry.INSTANCE); 434 } 435 return packageRegistry; 436 } 437 438 441 public void setPackageRegistry(EPackage.Registry packageRegistry) 442 { 443 this.packageRegistry = packageRegistry; 444 } 445 446 447 450 protected class ResourcesEList extends NotifyingListImpl implements InternalEList 451 { 452 protected boolean isNotificationRequired() 453 { 454 return ResourceSetImpl.this.eNotificationRequired(); 455 } 456 457 protected Object [] newData(int capacity) 458 { 459 return new Resource [capacity]; 460 } 461 462 public Object getNotifier() 463 { 464 return ResourceSetImpl.this; 465 } 466 467 public int getFeatureID() 468 { 469 return RESOURCE_SET__RESOURCES; 470 } 471 472 protected boolean useEquals() 473 { 474 return false; 475 } 476 477 protected boolean hasInverse() 478 { 479 return true; 480 } 481 482 protected boolean isUnique() 483 { 484 return true; 485 } 486 487 protected NotificationChain inverseAdd(Object object, NotificationChain notifications) 488 { 489 Resource.Internal resource = (Resource.Internal)object; 490 return resource.basicSetResourceSet(ResourceSetImpl.this, notifications); 491 } 492 493 protected NotificationChain inverseRemove(Object object, NotificationChain notifications) 494 { 495 Resource.Internal resource = (Resource.Internal)object; 496 Map map = getURIResourceMap(); 497 if (map != null) 498 { 499 for (Iterator i = map.values().iterator(); i.hasNext();) 500 { 501 if (resource == i.next()) 502 { 503 i.remove(); 504 } 505 } 506 } 507 return resource.basicSetResourceSet(null, notifications); 508 } 509 510 public Iterator basicIterator() 511 { 512 return super.basicIterator(); 513 } 514 515 public ListIterator basicListIterator() 516 { 517 return super.basicListIterator(); 518 } 519 520 public ListIterator basicListIterator(int index) 521 { 522 return super.basicListIterator(index); 523 } 524 525 public List basicList() 526 { 527 return super.basicList(); 528 } 529 } 530 531 535 public String toString() 536 { 537 return 538 getClass().getName() + '@' + Integer.toHexString(hashCode()) + 539 " resources=" + (resources == null ? "[]" : resources.toString()); 540 } 541 } 542 | Popular Tags |