1 17 package org.eclipse.emf.ecore.resource.impl; 18 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.net.URL ; 29 import java.net.URLConnection ; 30 import java.util.Map ; 31 32 import org.eclipse.core.resources.IContainer; 33 import org.eclipse.core.resources.IFile; 34 import org.eclipse.core.resources.IFolder; 35 import org.eclipse.core.resources.IResource; 36 import org.eclipse.core.resources.IWorkspaceRoot; 37 import org.eclipse.core.runtime.CoreException; 38 import org.eclipse.core.runtime.IProgressMonitor; 39 import org.eclipse.core.runtime.Path; 40 import org.eclipse.emf.common.archive.ArchiveURLConnection; 41 import org.eclipse.emf.common.util.URI; 42 import org.eclipse.emf.ecore.plugin.EcorePlugin; 43 import org.eclipse.emf.ecore.resource.Resource; 44 import org.eclipse.emf.ecore.resource.URIConverter; 45 46 47 59 public class URIConverterImpl implements URIConverter 60 { 61 65 public static class PlatformResourceOutputStream extends ByteArrayOutputStream 66 { 67 protected IFile file; 68 protected boolean force; 69 protected boolean keepHistory; 70 protected IProgressMonitor progressMonitor; 71 protected boolean previouslyFlushed; 72 73 public PlatformResourceOutputStream(IFile file, boolean force, boolean keepHistory, IProgressMonitor progressMonitor) 74 { 75 this.file = file; 76 this.force = force; 77 this.keepHistory = keepHistory; 78 this.progressMonitor = progressMonitor; 79 } 80 81 protected void createContainer(IContainer container) throws IOException 82 { 83 if (!container.exists()) 84 { 85 if (container.getType() == IResource.FOLDER) 86 { 87 createContainer(container.getParent()); 88 try 89 { 90 ((IFolder)container).create(force, keepHistory, progressMonitor); 91 } 92 catch (CoreException exception) 93 { 94 throw new ResourceImpl.IOWrappedException(exception); 95 } 96 } 97 } 98 } 99 100 public void close() throws IOException 101 { 102 flush(); 103 super.close(); 104 } 105 106 public void flush() throws IOException 107 { 108 super.flush(); 109 110 if (previouslyFlushed) 111 { 112 if (count == 0) 113 { 114 return; 115 } 116 } 117 else 118 { 119 createContainer(file.getParent()); 120 } 121 122 byte[] contents = toByteArray(); 123 InputStream inputStream = new ByteArrayInputStream (contents, 0, contents.length); 124 125 try 126 { 127 if (previouslyFlushed) 128 { 129 file.appendContents(inputStream, force, false, progressMonitor); 130 } 131 else if (!file.exists()) 132 { 133 file.create(inputStream, false, null); 134 previouslyFlushed = true; 135 } 136 else 137 { 138 if (!file.isLocal(IResource.DEPTH_ONE) || !file.isSynchronized(IResource.DEPTH_ONE)) 139 { 140 file.refreshLocal(IResource.DEPTH_ONE, progressMonitor); 141 } 142 file.setContents(inputStream, force, keepHistory, progressMonitor); 143 previouslyFlushed = true; 144 } 145 reset(); 146 } 147 catch (CoreException exception) 148 { 149 throw new Resource.IOWrappedException(exception); 150 } 151 } 152 } 153 154 157 public static class WorkbenchHelper 158 { 159 170 public static OutputStream createPlatformResourceOutputStream(String platformResourcePath) throws IOException 171 { 172 IFile file = workspaceRoot.getFile(new Path(platformResourcePath)); 173 return new PlatformResourceOutputStream(file, false, true, null); 174 } 175 176 186 public static InputStream createPlatformResourceInputStream(String platformResourcePath) throws IOException 187 { 188 IFile file = workspaceRoot.getFile(new Path(platformResourcePath)); 189 try 190 { 191 if (!file.isLocal(IResource.DEPTH_ONE) || !file.isSynchronized(IResource.DEPTH_ONE)) 192 { 193 file.refreshLocal(IResource.DEPTH_ONE, null); 194 } 195 return file.getContents(); 196 } 197 catch (CoreException exception) 198 { 199 throw new Resource.IOWrappedException(exception); 200 } 201 } 202 } 203 204 207 protected static IWorkspaceRoot workspaceRoot = EcorePlugin.getWorkspaceRoot(); 208 209 211 214 public interface URIMap extends Map 215 { 216 221 URI getURI(URI uri); 222 } 223 224 227 protected URIMap uriMap; 228 229 232 public URIConverterImpl() 233 { 234 } 246 247 253 protected boolean isArchiveScheme(String scheme) 254 { 255 return "archive".equals(scheme); 256 } 257 258 283 public OutputStream createOutputStream(URI uri) throws IOException 284 { 285 URI converted = normalize(uri); 286 if (converted.isFile()) 287 { 288 String filePath = converted.toFileString(); 289 return createFileOutputStream(filePath); 290 } 291 else 292 { 293 String scheme = converted.scheme(); 294 if (isArchiveScheme(scheme)) 295 { 296 return createArchiveOutputStream(converted); 297 } 298 else if ("platform".equals(scheme) && converted.segmentCount() > 1 && "resource".equals(converted.segment(0))) 299 { 300 StringBuffer platformResourcePath = new StringBuffer (); 301 for (int i = 1, size = converted.segmentCount(); i < size; ++i) 302 { 303 platformResourcePath.append('/'); 304 platformResourcePath.append(URI.decode(converted.segment(i))); 305 } 306 return createPlatformResourceOutputStream(platformResourcePath.toString()); 307 } 308 else 309 { 310 return createURLOutputStream(converted); 311 } 312 } 313 } 314 315 323 protected OutputStream createFileOutputStream(String filePath) throws IOException 324 { 325 File file = new File (filePath); 326 String parent = file.getParent(); 327 if (parent != null) 328 { 329 new File (parent).mkdirs(); 330 } 331 OutputStream outputStream = new FileOutputStream (file); 332 return outputStream; 333 } 334 335 341 protected OutputStream createArchiveOutputStream(URI archiveURI) throws IOException 342 { 343 return createArchive(archiveURI).getOutputStream(); 344 } 345 346 360 protected OutputStream createPlatformResourceOutputStream(String platformResourcePath) throws IOException 361 { 362 if (workspaceRoot != null) 364 { 365 return WorkbenchHelper.createPlatformResourceOutputStream(platformResourcePath); 366 } 367 else 368 { 370 URI resolvedLocation = EcorePlugin.resolvePlatformResourcePath(platformResourcePath); 371 if (resolvedLocation != null) 372 { 373 return createOutputStream(resolvedLocation); 374 } 375 376 throw new IOException ("The path '" + platformResourcePath + "' is unmapped"); 377 } 378 } 379 380 385 protected OutputStream createURLOutputStream(URI uri) throws IOException 386 { 387 URL url = new URL (uri.toString()); 388 URLConnection urlConnection = url.openConnection(); 389 urlConnection.setDoOutput(true); 390 return urlConnection.getOutputStream(); 391 } 392 393 418 public InputStream createInputStream(URI uri) throws IOException 419 { 420 URI converted = normalize(uri); 421 if (converted.isFile()) 422 { 423 String filePath = converted.toFileString(); 424 return createFileInputStream(filePath); 425 } 426 else 427 { 428 String scheme = converted.scheme(); 429 if (isArchiveScheme(scheme)) 430 { 431 return createArchiveInputStream(converted); 432 } 433 else if ("platform".equals(scheme) && converted.segmentCount() > 1 && "resource".equals(converted.segment(0))) 434 { 435 436 StringBuffer platformResourcePath = new StringBuffer (); 437 for (int i = 1, size = converted.segmentCount(); i < size; ++i) 438 { 439 platformResourcePath.append('/'); 440 platformResourcePath.append(URI.decode(converted.segment(i))); 441 } 442 return createPlatformResourceInputStream(platformResourcePath.toString()); 443 } 444 else 445 { 446 return createURLInputStream(converted); 447 } 448 } 449 } 450 451 459 protected InputStream createFileInputStream(String filePath) throws IOException 460 { 461 File file = new File (filePath); 462 InputStream inputStream = new FileInputStream (file); 463 return inputStream; 464 } 465 466 469 protected class Archive extends ArchiveURLConnection 470 { 471 public Archive(URI uri) 472 { 473 super(uri.toString()); 474 } 475 476 protected boolean emulateArchiveScheme() 477 { 478 return false; 479 } 480 481 protected boolean useZipFile() 482 { 483 return true; 484 } 485 486 protected InputStream createInputStream(String nestedURL) throws IOException 487 { 488 return URIConverterImpl.this.createInputStream(URI.createURI(nestedURL)); 489 } 490 491 protected OutputStream createOutputStream(String nestedURL) throws IOException 492 { 493 return URIConverterImpl.this.createOutputStream(URI.createURI(nestedURL)); 494 } 495 } 496 497 protected Archive createArchive(URI uri) 498 { 499 return new Archive(uri); 500 } 501 502 509 protected InputStream createArchiveInputStream(URI archiveURI) throws IOException 510 { 511 return createArchive(archiveURI).getInputStream(); 512 } 513 514 528 protected InputStream createPlatformResourceInputStream(String platformResourcePath) throws IOException 529 { 530 if (workspaceRoot != null) 532 { 533 return WorkbenchHelper.createPlatformResourceInputStream(platformResourcePath); 534 } 535 else 536 { 538 URI resolvedLocation = EcorePlugin.resolvePlatformResourcePath(platformResourcePath); 539 if (resolvedLocation != null) 540 { 541 return createInputStream(resolvedLocation); 542 } 543 544 throw new IOException ("The path '" + platformResourcePath + "' is unmapped"); 545 } 546 } 547 548 553 protected InputStream createURLInputStream(URI uri) throws IOException 554 { 555 URL url = new URL (uri.toString()); 556 URLConnection urlConnection = url.openConnection(); 557 return urlConnection.getInputStream(); 558 } 559 560 570 public URI normalize(URI uri) 571 { 572 String fragment = uri.fragment(); 573 URI result = 574 fragment == null ? 575 getInternalURIMap().getURI(uri) : 576 getInternalURIMap().getURI(uri.trimFragment()).appendFragment(fragment); 577 String scheme = result.scheme(); 578 if (scheme == null) 579 { 580 if (workspaceRoot != null) 582 { 583 if (result.hasAbsolutePath()) 584 { 585 result = URI.createPlatformResourceURI(result.trimFragment().toString()); 586 if (fragment != null) 587 { 588 result = result.appendFragment(fragment); 589 } 590 } 591 } 592 else 593 { 595 if (result.hasAbsolutePath()) 596 { 597 result = URI.createURI("file:" + result); 598 } 599 else 600 { 601 result = URI.createFileURI(new File (result.trimFragment().toString()).getAbsolutePath()); 602 if (fragment != null) 603 { 604 result = result.appendFragment(fragment); 605 } 606 } 607 } 608 } 609 610 if (result.equals(uri)) 611 { 612 return uri; 613 } 614 else 615 { 616 return normalize(result); 617 } 618 } 619 620 623 public Map getURIMap() 624 { 625 return getInternalURIMap(); 626 } 627 628 632 protected URIMap getInternalURIMap() 633 { 634 if (uriMap == null) 635 { 636 URIMappingRegistryImpl mappingRegistryImpl = 637 new URIMappingRegistryImpl() 638 { 639 protected URI delegatedGetURI(URI uri) 640 { 641 return URIMappingRegistryImpl.INSTANCE.getURI(uri); 642 } 643 }; 644 645 uriMap = (URIMap)mappingRegistryImpl.map(); 646 } 647 648 return uriMap; 649 } 650 } 651 | Popular Tags |