| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 package com.google.dart.compiler; | 4 package com.google.dart.compiler; |
| 5 | 5 |
| 6 import java.io.File; | 6 import java.io.File; |
| 7 import java.net.URI; | 7 import java.net.URI; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * A {@link LibrarySource} backed by a URL. | 10 * A {@link LibrarySource} backed by a URL. |
| 11 */ | 11 */ |
| 12 public class UrlLibrarySource extends UrlSource implements LibrarySource { | 12 public class UrlLibrarySource extends UrlSource implements LibrarySource { |
| 13 |
| 13 public UrlLibrarySource(URI uri, PackageLibraryManager slm) { | 14 public UrlLibrarySource(URI uri, PackageLibraryManager slm) { |
| 14 super(uri, slm); | 15 super(uri, slm); |
| 15 } | 16 } |
| 16 | 17 |
| 17 public UrlLibrarySource(URI uri) { | 18 public UrlLibrarySource(URI uri) { |
| 18 this(uri, null); | 19 this(uri, null); |
| 19 } | 20 } |
| 20 | 21 |
| 21 public UrlLibrarySource(File file) { | 22 public UrlLibrarySource(File file) { |
| 22 super(file); | 23 super(file); |
| 23 } | 24 } |
| 24 | 25 |
| 25 @Override | 26 @Override |
| 26 public String getName() { | 27 public String getName() { |
| 27 return getUri().toString(); | 28 return getUri().toString(); |
| 28 } | 29 } |
| 29 | 30 |
| 30 @Override | 31 @Override |
| 31 public DartSource getSourceFor(final String relPath) { | |
| 32 if (relPath == null || relPath.isEmpty()) { | |
| 33 return null; | |
| 34 } | |
| 35 try { | |
| 36 // Force the creation of an escaped relative URI to deal with spaces, etc.
s | |
| 37 URI uri = getUri().resolve(new URI(null, null, relPath, null, null)).norma
lize(); | |
| 38 return new UrlDartSource(uri, relPath, this, packageLibraryManager); | |
| 39 } catch (Throwable e) { | |
| 40 return null; | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 @Override | |
| 45 public LibrarySource getImportFor(String relPath) { | 32 public LibrarySource getImportFor(String relPath) { |
| 46 if (relPath == null || relPath.isEmpty()) { | 33 if (relPath == null || relPath.isEmpty()) { |
| 47 return null; | 34 return null; |
| 48 } | 35 } |
| 49 try { | 36 try { |
| 50 // Force the creation of an escaped relative URI to deal with spaces, etc. | 37 // Force the creation of an escaped relative URI to deal with spaces, etc. |
| 51 URI uri = getUri().resolve(new URI(null, null, relPath, null, null)).norma
lize(); | 38 URI uri = getUri().resolve(new URI(null, null, relPath, null, null)).norma
lize(); |
| 52 String path = uri.getPath(); | 39 String path = uri.getPath(); |
| 53 // Resolve relative reference out of one system library into another | 40 // Resolve relative reference out of one system library into another |
| 54 if (PackageLibraryManager.isDartUri(uri)) { | 41 if (PackageLibraryManager.isDartUri(uri)) { |
| 55 if(path != null && path.startsWith("/..")) { | 42 if (path != null && path.startsWith("/..")) { |
| 56 URI fileUri = packageLibraryManager.resolveDartUri(uri); | 43 URI fileUri = packageLibraryManager.resolveDartUri(uri); |
| 57 URI shortUri = packageLibraryManager.getShortUri(fileUri); | 44 URI shortUri = packageLibraryManager.getShortUri(fileUri); |
| 58 if (shortUri != null) { | 45 if (shortUri != null) { |
| 59 uri = shortUri; | 46 uri = shortUri; |
| 60 } | 47 } |
| 61 } | 48 } |
| 62 } else if (PackageLibraryManager.isPackageUri(uri)){ | 49 } else if (PackageLibraryManager.isPackageUri(uri)) { |
| 63 URI fileUri = packageLibraryManager.resolveDartUri(uri); | 50 URI fileUri = packageLibraryManager.resolveDartUri(uri); |
| 64 if (fileUri != null){ | 51 if (fileUri != null) { |
| 65 uri = fileUri; | 52 uri = fileUri; |
| 66 } | 53 } |
| 67 } else if (path != null && !(new File(path).exists())){ | 54 } else if (!resourceExists(uri)) { |
| 68 // resolve against package root directories to find file | 55 // resolve against package root directories to find file |
| 69 uri = packageLibraryManager.findExistingFileInPackages(uri); | 56 uri = packageLibraryManager.findExistingFileInPackages(uri); |
| 70 } | 57 } |
| 71 | 58 |
| 72 return new UrlLibrarySource(uri, packageLibraryManager); | 59 return createLibrarySource(uri, packageLibraryManager); |
| 73 } catch (Throwable e) { | 60 } catch (Throwable e) { |
| 74 return null; | 61 return null; |
| 75 } | 62 } |
| 76 } | 63 } |
| 64 |
| 65 @Override |
| 66 public DartSource getSourceFor(final String relPath) { |
| 67 if (relPath == null || relPath.isEmpty()) { |
| 68 return null; |
| 69 } |
| 70 try { |
| 71 // Force the creation of an escaped relative URI to deal with spaces, etc. |
| 72 URI uri = getUri().resolve(new URI(null, null, relPath, null, null)).norma
lize(); |
| 73 return createDartSource(uri, relPath, this, packageLibraryManager); |
| 74 } catch (Throwable e) { |
| 75 return null; |
| 76 } |
| 77 } |
| 78 |
| 79 /** |
| 80 * Create a URL library source. |
| 81 * |
| 82 * (Clients can override.) |
| 83 * |
| 84 * @param uri the URI of the library |
| 85 * @param relPath relative path to the dart source |
| 86 * @param libSource the library source |
| 87 * @param packageManager the package library manager |
| 88 * @return the resulting dart source |
| 89 */ |
| 90 protected UrlDartSource createDartSource(URI uri, String relPath, UrlLibrarySo
urce libSource, PackageLibraryManager packageManager) { |
| 91 return new UrlDartSource(uri, relPath, libSource, packageManager); |
| 92 } |
| 93 |
| 94 /** |
| 95 * Create a URL library source. |
| 96 * |
| 97 * (Clients can override.) |
| 98 * |
| 99 * @param uri the URI of the library |
| 100 * @return the resulting library source |
| 101 */ |
| 102 protected UrlLibrarySource createLibrarySource(URI uri, PackageLibraryManager
packageManager) { |
| 103 return new UrlLibrarySource(uri, packageManager); |
| 104 } |
| 105 |
| 106 /** |
| 107 * Check if a resource exists at this URI. |
| 108 * |
| 109 * (Clients can override.) |
| 110 * |
| 111 * @param uri the URI to test |
| 112 * @return <code>true</code> if a resource exists at this URI, <code>false</co
de> otherwise |
| 113 */ |
| 114 protected boolean resourceExists(URI uri) { |
| 115 String path = uri.getPath(); |
| 116 return path == null || new File(path).exists(); |
| 117 } |
| 118 |
| 77 } | 119 } |
| OLD | NEW |