| 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 resolver.package; | |
| 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 fromEncoding(UriKind kind, Uri uri) { | |
| 42 if (kind == UriKind.PACKAGE_URI) { | |
| 43 Resource resource = resourceProvider.getResource(uri.path); | |
| 44 if (resource is File) { | |
| 45 return resource.createSource(kind); | |
| 46 } | |
| 47 } | |
| 48 return null; | |
| 49 } | |
| 50 | |
| 51 @override | |
| 52 Source resolveAbsolute(Uri uri) { | |
| 53 if (!isPackageUri(uri)) { | |
| 54 return null; | |
| 55 } | |
| 56 // Prepare path. | |
| 57 String path = uri.path; | |
| 58 // Prepare path components. | |
| 59 String pkgName; | |
| 60 String relPath; | |
| 61 int index = path.indexOf('/'); | |
| 62 if (index == -1 || index == 0) { | |
| 63 return null; | |
| 64 } else { | |
| 65 // <pkgName>/<relPath> | |
| 66 pkgName = path.substring(0, index); | |
| 67 relPath = path.substring(index + 1); | |
| 68 } | |
| 69 // Try to find an existing file. | |
| 70 List<Folder> packageDirs = packageMap[pkgName]; | |
| 71 if (packageDirs != null) { | |
| 72 for (Folder packageDir in packageDirs) { | |
| 73 if (packageDir.exists) { | |
| 74 Resource result = packageDir.getChild(relPath); | |
| 75 if (result is File && result.exists) { | |
| 76 return result.createSource(UriKind.PACKAGE_URI); | |
| 77 } | |
| 78 } | |
| 79 } | |
| 80 } | |
| 81 // Return a NonExistingSource instance. | |
| 82 // This helps provide more meaningful error messages to users | |
| 83 // (a missing file error, as opposed to an invalid URI error). | |
| 84 // TODO(scheglov) move NonExistingSource to "source.dart" | |
| 85 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); | |
| 86 } | |
| 87 | |
| 88 /** | |
| 89 * Returns `true` if [uri] is a `package` URI. | |
| 90 */ | |
| 91 static bool isPackageUri(Uri uri) { | |
| 92 return uri.scheme == PACKAGE_SCHEME; | |
| 93 } | |
| 94 } | |
| OLD | NEW |