OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 | 4 |
5 import 'package:path/path.dart' as p; | 5 import 'package:path/path.dart' as p; |
6 | 6 |
7 /// The class `UriResolver` implements the rules for resolving URIs to file | 7 /// The class `UriResolver` implements the rules for resolving URIs to file |
8 /// paths. | 8 /// paths. |
9 /// | |
10 /// TODO(paulberry): Is it necessary to support the "http" scheme? | |
11 class UriResolver { | 9 class UriResolver { |
| 10 /// The URI scheme used for "package" URIs. |
| 11 static const PACKAGE_SCHEME = 'package'; |
| 12 |
| 13 /// The URI scheme used for "dart" URIs. |
| 14 static const DART_SCHEME = 'dart'; |
| 15 |
12 /// A map from package name to the file URI of the "lib" directory of the | 16 /// A map from package name to the file URI of the "lib" directory of the |
13 /// corresponding package. This is equivalent to the format returned by | 17 /// corresponding package. This is equivalent to the format returned by |
14 /// the "package_config" package's parse() function. | 18 /// the "package_config" package's parse() function. |
15 final Map<String, Uri> packages; | 19 final Map<String, Uri> packages; |
16 | 20 |
17 /// A map from SDK library name (e.g. `core` for `dart:core`) to the file URI | 21 /// A map from SDK library name (e.g. `core` for `dart:core`) to the file URI |
18 /// of the defining compilation unit of the SDK library. | 22 /// of the defining compilation unit of the SDK library. |
19 final Map<String, Uri> sdkLibraries; | 23 final Map<String, Uri> sdkLibraries; |
20 | 24 |
21 /// The path context which should be used to convert from file URIs to file | 25 /// The path context which should be used to convert from file URIs to file |
22 /// paths. | 26 /// paths. |
23 final p.Context pathContext; | 27 final p.Context pathContext; |
24 | 28 |
25 /// The URI scheme used for "package" URIs. | |
26 static const PACKAGE_SCHEME = 'package'; | |
27 | |
28 /// The URI scheme used for "dart" URIs. | |
29 static const DART_SCHEME = 'dart'; | |
30 | |
31 /// The URI scheme used for "file" URIs. | |
32 static const FILE_SCHEME = 'file'; | |
33 | |
34 UriResolver(this.packages, this.sdkLibraries, this.pathContext); | 29 UriResolver(this.packages, this.sdkLibraries, this.pathContext); |
35 | 30 |
36 /// Converts a URI to a file path. | 31 /// Converts "package:" and "dart:" URIs to the locations of the corresponding |
| 32 /// files. |
37 /// | 33 /// |
38 /// If the given URI is valid, and of a recognized form, returns the file path | 34 /// If the given URI is a "package:" or "dart:" URI, is well formed, and names |
39 /// it corresponds to. Otherwise returns `null`. It is not necessary for the | 35 /// a package or dart library that is recognized, returns the file path it |
40 /// URI to be absolute (relative URIs will be converted to relative file | 36 /// corresponds to. If the given URI is a "package:" or "dart:" URI, and is |
41 /// paths). | 37 /// ill-formed or names a package or dart library that is not recognized, |
| 38 /// returns `null`. |
| 39 /// |
| 40 /// If the given URI has any scheme other than "package:" or "dart:", it is |
| 41 /// returned unchanged. |
| 42 /// |
| 43 /// It is not necessary for the URI to be absolute (relative URIs will be |
| 44 /// converted to relative file paths). |
42 /// | 45 /// |
43 /// Note that no I/O is performed; the file path that is returned will be | 46 /// Note that no I/O is performed; the file path that is returned will be |
44 /// independent of whether or not any particular file exists on the file | 47 /// independent of whether or not any particular file exists on the file |
45 /// system. | 48 /// system. |
46 String resolve(Uri uri) { | 49 Uri resolve(Uri uri) { |
47 Uri fileUri; | 50 if (uri.scheme == DART_SCHEME || uri.scheme == PACKAGE_SCHEME) { |
48 if (uri.scheme == FILE_SCHEME) { | |
49 fileUri = uri; | |
50 } else { | |
51 var path = uri.path; | 51 var path = uri.path; |
52 var slashIndex = path.indexOf('/'); | 52 var slashIndex = path.indexOf('/'); |
53 String prefix; | 53 String prefix; |
54 String rest; | 54 String rest; |
55 if (slashIndex >= 0) { | 55 if (slashIndex >= 0) { |
56 prefix = path.substring(0, slashIndex); | 56 prefix = path.substring(0, slashIndex); |
57 rest = path.substring(slashIndex + 1); | 57 rest = path.substring(slashIndex + 1); |
58 } else { | 58 } else { |
59 prefix = path; | 59 prefix = path; |
60 rest = ''; | 60 rest = ''; |
61 } | 61 } |
62 Uri libUri; | 62 Uri libUri; |
63 if (uri.scheme == PACKAGE_SCHEME) { | 63 if (uri.scheme == PACKAGE_SCHEME) { |
64 if (slashIndex < 0) return null; | 64 if (slashIndex < 0) return null; |
65 libUri = packages[prefix]; | 65 libUri = packages[prefix]; |
66 } else if (uri.scheme == DART_SCHEME) { | 66 } else if (uri.scheme == DART_SCHEME) { |
67 libUri = sdkLibraries[prefix]; | 67 libUri = sdkLibraries[prefix]; |
68 } | 68 } |
69 if (libUri == null) return null; | 69 if (libUri == null) return null; |
70 fileUri = libUri.resolve(rest); | 70 return libUri.resolve(rest); |
71 if (fileUri.scheme != FILE_SCHEME) return null; | 71 } else { |
| 72 return uri; |
72 } | 73 } |
73 return pathContext.fromUri(fileUri); | |
74 } | 74 } |
75 } | 75 } |
OLD | NEW |