Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: pkg/front_end/lib/src/base/uri_resolver.dart

Issue 2577503002: Implement URI resolution in the front end. (Closed)
Patch Set: Add TODO Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/front_end/test/src/base/uri_resolver_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'package:path/path.dart' as p;
6
7 /// The class `UriResolver` implements the rules for resolving URIs to file
8 /// paths.
9 ///
10 /// TODO(paulberry): Is it necessary to support the "http" scheme?
11 class UriResolver {
12 /// 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
14 /// the "package_config" package's parse() function.
15 final Map<String, Uri> packages;
16
17 /// 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.
19 final Map<String, Uri> sdkLibraries;
20
21 /// The path context which should be used to convert from file URIs to file
22 /// paths.
23 final p.Context pathContext;
24
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);
35
36 /// Converts a URI to a file path.
37 ///
38 /// If the given URI is valid, and of a recognized form, returns the file path
39 /// it corresponds to. Otherwise returns `null`. It is not necessary for the
40 /// URI to be absolute (relative URIs will be converted to relative file
41 /// paths).
42 ///
43 /// 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
45 /// system.
46 String resolve(Uri uri) {
47 Uri fileUri;
48 if (uri.scheme == FILE_SCHEME) {
49 fileUri = uri;
50 } else {
51 var path = uri.path;
52 var slashIndex = path.indexOf('/');
53 String prefix;
54 String rest;
55 if (slashIndex >= 0) {
56 prefix = path.substring(0, slashIndex);
57 rest = path.substring(slashIndex + 1);
58 } else {
59 prefix = path;
60 rest = '';
61 }
62 Uri libUri;
63 if (uri.scheme == PACKAGE_SCHEME) {
64 if (slashIndex < 0) return null;
65 libUri = packages[prefix];
66 } else if (uri.scheme == DART_SCHEME) {
67 libUri = sdkLibraries[prefix];
68 }
69 if (libUri == null) return null;
70 fileUri = libUri.resolve(rest);
71 if (fileUri.scheme != FILE_SCHEME) return null;
72 }
73 return pathContext.fromUri(fileUri);
74 }
75 }
OLDNEW
« no previous file with comments | « no previous file | pkg/front_end/test/src/base/uri_resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698