OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library source.package_map_resolver; |
| 6 |
| 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/src/generated/source.dart'; |
| 9 |
| 10 |
| 11 /** |
| 12 * A [UriResolver] implementation for the `package:` scheme that uses a map of |
| 13 * package names to their directories. |
| 14 */ |
| 15 class PackageMapUriResolver extends UriResolver { |
| 16 /** |
| 17 * The name of the `package` scheme. |
| 18 */ |
| 19 static const String PACKAGE_SCHEME = "package"; |
| 20 |
| 21 /** |
| 22 * A table mapping package names to the path of the directories containing |
| 23 * the package. |
| 24 */ |
| 25 final Map<String, List<Folder>> packageMap; |
| 26 |
| 27 /** |
| 28 * The [ResourceProvider] for this resolver. |
| 29 */ |
| 30 final ResourceProvider resourceProvider; |
| 31 |
| 32 /** |
| 33 * Create a new [PackageMapUriResolver]. |
| 34 * |
| 35 * [packageMap] is a table mapping package names to the paths of the |
| 36 * directories containing the package |
| 37 */ |
| 38 PackageMapUriResolver(this.resourceProvider, this.packageMap); |
| 39 |
| 40 @override |
| 41 Source resolveAbsolute(Uri uri) { |
| 42 if (!isPackageUri(uri)) { |
| 43 return null; |
| 44 } |
| 45 // Prepare path. |
| 46 String path = uri.path; |
| 47 // Prepare path components. |
| 48 String pkgName; |
| 49 String relPath; |
| 50 int index = path.indexOf('/'); |
| 51 if (index == -1 || index == 0) { |
| 52 return null; |
| 53 } else { |
| 54 // <pkgName>/<relPath> |
| 55 pkgName = path.substring(0, index); |
| 56 relPath = path.substring(index + 1); |
| 57 } |
| 58 // Try to find an existing file. |
| 59 List<Folder> packageDirs = packageMap[pkgName]; |
| 60 if (packageDirs != null) { |
| 61 for (Folder packageDir in packageDirs) { |
| 62 if (packageDir.exists) { |
| 63 Resource result = packageDir.getChild(relPath); |
| 64 if (result is File && result.exists) { |
| 65 return result.createSource(uri); |
| 66 } |
| 67 } |
| 68 } |
| 69 } |
| 70 // Return a NonExistingSource instance. |
| 71 // This helps provide more meaningful error messages to users |
| 72 // (a missing file error, as opposed to an invalid URI error). |
| 73 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); |
| 74 } |
| 75 |
| 76 @override |
| 77 Uri restoreAbsolute(Source source) { |
| 78 String sourcePath = source.fullName; |
| 79 for (String pkgName in packageMap.keys) { |
| 80 List<Folder> pkgFolders = packageMap[pkgName]; |
| 81 for (Folder pkgFolder in pkgFolders) { |
| 82 String pkgFolderPath = pkgFolder.path; |
| 83 if (sourcePath.startsWith(pkgFolderPath)) { |
| 84 String relPath = sourcePath.substring(pkgFolderPath.length); |
| 85 return new Uri(path: '${PACKAGE_SCHEME}:$pkgName$relPath'); |
| 86 } |
| 87 } |
| 88 } |
| 89 return null; |
| 90 } |
| 91 |
| 92 /** |
| 93 * Returns `true` if [uri] is a `package` URI. |
| 94 */ |
| 95 static bool isPackageUri(Uri uri) { |
| 96 return uri.scheme == PACKAGE_SCHEME; |
| 97 } |
| 98 } |
OLD | NEW |