| 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:analysis_server/src/resource.dart'; |
| 8 import 'package:analyzer/src/generated/source_io.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 directory containing |
| 23 * the package. |
| 24 */ |
| 25 final Map<String, 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 path of the directory |
| 36 * 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_SELF_URI || 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 Folder packageDir = packageMap[pkgName]; |
| 71 if (packageDir != null && packageDir.exists) { |
| 72 Resource result = packageDir.getChild(relPath); |
| 73 if (result is File && result.exists) { |
| 74 return result.createSource(UriKind.PACKAGE_URI); |
| 75 } |
| 76 } |
| 77 // Return a NonExistingSource instance. |
| 78 // This helps provide more meaningful error messages to users |
| 79 // (a missing file error, as opposed to an invalid URI error). |
| 80 // TODO(scheglov) move NonExistingSource to "source.dart" |
| 81 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); |
| 82 } |
| 83 |
| 84 /** |
| 85 * Returns `true` if [uri] is a `package` URI. |
| 86 */ |
| 87 static bool isPackageUri(Uri uri) { |
| 88 return uri.scheme == PACKAGE_SCHEME; |
| 89 } |
| 90 } |
| OLD | NEW |