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

Side by Side Diff: compiler/java/com/google/dart/compiler/SystemLibraryManager.java

Issue 10828394: SystemLibraryManager refactor - to separate package and sdk library functionality (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 4 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 package com.google.dart.compiler; 5 package com.google.dart.compiler;
6 6
7 import java.io.BufferedInputStream;
8 import java.io.File; 7 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.net.URI; 8 import java.net.URI;
14 import java.net.URISyntaxException; 9 import java.net.URISyntaxException;
15 import java.util.ArrayList; 10 import java.util.ArrayList;
16 import java.util.Arrays; 11 import java.util.Arrays;
17 import java.util.Collection; 12 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List; 13 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Properties;
24 14
25 15
26 /** 16 /**
27 * Manages the collection of {@link SystemLibrary}s. 17 * Manages the collection of {@link SystemLibrary}s.
28 */ 18 */
29 public class SystemLibraryManager { 19 public class SystemLibraryManager {
30 20
31 public static class NotADartShortUriException extends RuntimeException { 21 public static class NotADartShortUriException extends RuntimeException {
32 22
33 public NotADartShortUriException(String uriString) { 23 public NotADartShortUriException(String uriString) {
(...skipping 12 matching lines...) Expand all
46 public static final String DEFAULT_PLATFORM = "any"; 36 public static final String DEFAULT_PLATFORM = "any";
47 public static final File DEFAULT_SDK_PATH = new File(System.getProperty( 37 public static final File DEFAULT_SDK_PATH = new File(System.getProperty(
48 "com.google.dart.sdk", "../")); 38 "com.google.dart.sdk", "../"));
49 39
50 public static final File DEFAULT_PACKAGE_ROOT = new File("packages"); 40 public static final File DEFAULT_PACKAGE_ROOT = new File("packages");
51 public static final List<File> DEFAULT_PACKAGE_ROOTS = Arrays.asList(new File[ ] {DEFAULT_PACKAGE_ROOT}); 41 public static final List<File> DEFAULT_PACKAGE_ROOTS = Arrays.asList(new File[ ] {DEFAULT_PACKAGE_ROOT});
52 42
53 public static final String PACKAGE_SCHEME = "package"; 43 public static final String PACKAGE_SCHEME = "package";
54 public static final String PACKAGE_SCHEME_SPEC = "package:"; 44 public static final String PACKAGE_SCHEME_SPEC = "package:";
55 45
56 private static final String DART_SCHEME = "dart"; 46 public static final String DART_SCHEME = "dart";
57 private static final String DART_SCHEME_SPEC = "dart:"; 47 public static final String DART_SCHEME_SPEC = "dart:";
58 private static final String IMPORT_CONFIG = "import_%s.config"; 48
59 49
60 /** 50 /**
61 * Answer <code>true</code> if the string is a dart spec 51 * Answer <code>true</code> if the string is a dart spec
62 */ 52 */
63 public static boolean isDartSpec(String spec) { 53 public static boolean isDartSpec(String spec) {
64 return spec != null && spec.startsWith(DART_SCHEME_SPEC); 54 return spec != null && spec.startsWith(DART_SCHEME_SPEC);
65 } 55 }
66 56
67 /** 57 /**
68 * Answer <code>true</code> if the specified URI has a "dart" scheme 58 * Answer <code>true</code> if the specified URI has a "dart" scheme
69 */ 59 */
70 public static boolean isDartUri(URI uri) { 60 public static boolean isDartUri(URI uri) {
71 return uri != null && DART_SCHEME.equals(uri.getScheme()); 61 return uri != null && DART_SCHEME.equals(uri.getScheme());
72 } 62 }
73 63
74 /** 64 /**
75 * Answer <code>true</code> if the string is a package spec 65 * Answer <code>true</code> if the string is a package spec
76 */ 66 */
77 public static boolean isPackageSpec(String spec) { 67 public static boolean isPackageSpec(String spec) {
78 return spec != null && spec.startsWith(PACKAGE_SCHEME_SPEC); 68 return spec != null && spec.startsWith(PACKAGE_SCHEME_SPEC);
79 } 69 }
80 70
81 /** 71 /**
82 * Answer <code>true</code> if the specified URI has a "package" scheme 72 * Answer <code>true</code> if the specified URI has a "package" scheme
83 */ 73 */
84 public static boolean isPackageUri(URI uri) { 74 public static boolean isPackageUri(URI uri) {
85 return uri != null && PACKAGE_SCHEME.equals(uri.getScheme()); 75 return uri != null && PACKAGE_SCHEME.equals(uri.getScheme());
86 } 76 }
87 77
88 private HashMap<String, String> expansionMap; 78 private static SdkLibraryManager SDK_LIBRARY_MANAGER;
89 private Map<String, SystemLibrary> hostMap; 79
90 private final File sdkLibPath;
91 private final URI sdkLibPathUri;
92 private final String platformName;
93
94 private List<File> packageRoots = new ArrayList<File>(); 80 private List<File> packageRoots = new ArrayList<File>();
95 private List<URI> packageRootsUri = new ArrayList<URI>(); 81 private List<URI> packageRootsUri = new ArrayList<URI>();
96 82
97 private Map<URI, URI> longToShortUriMap;
98
99 private List<SystemLibrary> libraries;
100
101 public SystemLibraryManager() { 83 public SystemLibraryManager() {
102 this(DEFAULT_SDK_PATH, DEFAULT_PLATFORM); 84 this(DEFAULT_SDK_PATH, DEFAULT_PLATFORM);
103 } 85 }
104 86
105 public SystemLibraryManager(File sdkPath, String platformName) { 87 public SystemLibraryManager(File sdkPath, String platformName) {
106 this.sdkLibPath = new File(sdkPath, "lib").getAbsoluteFile(); 88 if (SDK_LIBRARY_MANAGER == null){
107 this.sdkLibPathUri = sdkLibPath.toURI(); 89 SDK_LIBRARY_MANAGER = new SdkLibraryManager(sdkPath, platformName);
108 this.platformName = platformName; 90 }
109 setLibraries(getDefaultLibraries());
110 setPackageRoots(DEFAULT_PACKAGE_ROOTS);
111 } 91 }
112 92
113 93
114 /** 94 /**
115 * Expand a relative or short URI (e.g. "dart:html") which is implementation i ndependent to its 95 * Expand a relative or short URI (e.g. "dart:html") which is implementation i ndependent to its
116 * full URI (e.g. "dart://html/com/google/dart/htmllib/html.dart"). 96 * full URI (e.g. "dart://html/com/google/dart/htmllib/html.dart").
117 * 97 *
118 * @param uri the relative URI 98 * @param uri the relative URI
119 * @return the expanded URI 99 * @return the expanded URI
120 * or the original URI if it could not be expanded 100 * or the original URI if it could not be expanded
121 * or null if the uri is of the form "dart:<libname>" but does not correspond to a system library 101 * or null if the uri is of the form "dart:<libname>" but does not correspond to a system library
122 */ 102 */
123 public URI expandRelativeDartUri(URI uri) throws AssertionError { 103 public URI expandRelativeDartUri(URI uri) throws AssertionError {
124 if (isDartUri(uri)) { 104 if (isDartUri(uri)) {
125 String host = uri.getHost(); 105 return SDK_LIBRARY_MANAGER.expandRelativeDartUri(uri);
126 if (host == null) {
127 String spec = uri.getSchemeSpecificPart();
128 String replacement = expansionMap.get(spec);
129 if (replacement != null) {
130 try {
131 uri = new URI(DART_SCHEME + ":" + replacement);
132 } catch (URISyntaxException e) {
133 throw new AssertionError();
134 }
135 } else {
136 return null;
137 }
138 }
139 } 106 }
140 if (isPackageUri(uri)){ 107 if (isPackageUri(uri)){
141 String host = uri.getHost(); 108 String host = uri.getHost();
142 if (host == null) { 109 if (host == null) {
143 String spec = uri.getSchemeSpecificPart(); 110 String spec = uri.getSchemeSpecificPart();
144 if (!spec.startsWith("//")){ 111 if (!spec.startsWith("//")){
145 try { 112 try {
146 if (spec.startsWith("/")){ 113 if (spec.startsWith("/")){
147 // TODO(keertip): fix to handle spaces 114 // TODO(keertip): fix to handle spaces
148 uri = new URI(PACKAGE_SCHEME + ":/" + spec); 115 uri = new URI(PACKAGE_SCHEME + ":/" + spec);
149 } else { 116 } else {
150 uri = new URI(PACKAGE_SCHEME + "://" + spec); 117 uri = new URI(PACKAGE_SCHEME + "://" + spec);
151 } 118 }
152 } catch (URISyntaxException e) { 119 } catch (URISyntaxException e) {
153 throw new AssertionError(); 120 throw new AssertionError();
154 } 121 }
155 } 122 }
156 } 123 }
157 } 124 }
158 return uri; 125 return uri;
159 } 126 }
160 127
161 /** 128
162 * Answer a collection of all bundled library URL specs (e.g. "dart:html").
163 *
164 * @return a collection of specs (not <code>null</code>, contains no <code>nul l</code>s)
165 */
166 public Collection<String> getAllLibrarySpecs() {
167 Collection<String> result = new ArrayList<String>(libraries.size());
168 for (SystemLibrary lib : libraries) {
169 result.add("dart:" + lib.getShortName());
170 }
171 return result;
172 }
173
174 protected InputStream getImportConfigStream() {
175 File file = new File(new File(sdkLibPath, "config"),
176 String.format(IMPORT_CONFIG, platformName));
177 if (!file.exists()) {
178 throw new InternalCompilerException("Failed to find " + file.toString()
179 + ". Is dart-sdk path correct?");
180 }
181 try {
182 return new BufferedInputStream(new FileInputStream(file));
183 } catch (FileNotFoundException e) {
184 throw new InternalCompilerException("Failed to open " + file);
185 }
186 }
187 129
188 /** 130 /**
189 * Given an absolute file URI (e.g. "file:/some/install/directory/dart-sdk/lib /core/bool.dart"), 131 * Given an absolute file URI (e.g. "file:/some/install/directory/dart-sdk/lib /core/bool.dart"),
190 * answer the corresponding dart: URI (e.g. "dart://core/bool.dart") for that file URI, 132 * answer the corresponding dart: URI (e.g. "dart://core/bool.dart") for that file URI,
191 * or <code>null</code> if the file URI does not map to a dart: URI 133 * or <code>null</code> if the file URI does not map to a dart: URI
192 * @param fileUri the file URI 134 * @param fileUri the file URI
193 * @return the dart URI or <code>null</code> 135 * @return the dart URI or <code>null</code>
194 */ 136 */
195 public URI getRelativeUri(URI fileUri) { 137 public URI getRelativeUri(URI fileUri) {
196 // TODO (danrubel): does not convert dart: libraries outside the dart-sdk/li b directory 138 // TODO (danrubel): does not convert dart: libraries outside the dart-sdk/li b directory
197 if (fileUri == null || !fileUri.getScheme().equals("file")) { 139 if (fileUri == null || !fileUri.getScheme().equals("file")) {
198 return null; 140 return null;
199 } 141 }
200 URI relativeUri = sdkLibPathUri.relativize(fileUri); 142
201 if (relativeUri.getScheme() == null) { 143 URI relativeUri = SDK_LIBRARY_MANAGER.getRelativeUri(fileUri);
202 try { 144 if (relativeUri != null){
203 return new URI(null, null, "dart://" + relativeUri.getPath(), null, null ); 145 return relativeUri;
204 } catch (URISyntaxException e) {
205 //$FALL-THROUGH$
206 }
207 } 146 }
208 147
209 for (URI rootUri : packageRootsUri){ 148 for (URI rootUri : packageRootsUri){
210 relativeUri = rootUri.relativize(fileUri); 149 relativeUri = rootUri.relativize(fileUri);
211 if (relativeUri.getScheme() == null) { 150 if (relativeUri.getScheme() == null) {
212 try { 151 try {
213 return new URI(null, null, "package://" + relativeUri.getPath(), nul l, null); 152 return new URI(null, null, "package://" + relativeUri.getPath(), nul l, null);
214 } catch (URISyntaxException e) { 153 } catch (URISyntaxException e) {
215 //$FALL-THROUGH$ 154 //$FALL-THROUGH$
216 } 155 }
(...skipping 24 matching lines...) Expand all
241 return packageRootsUri.get(0).resolve(relPath); 180 return packageRootsUri.get(0).resolve(relPath);
242 } 181 }
243 return null; 182 return null;
244 } 183 }
245 184
246 /** 185 /**
247 * Answer the original "dart:<libname>" URI for the specified resolved URI or <code>null</code> if 186 * Answer the original "dart:<libname>" URI for the specified resolved URI or <code>null</code> if
248 * it does not map to a short URI. 187 * it does not map to a short URI.
249 */ 188 */
250 public URI getShortUri(URI uri) { 189 public URI getShortUri(URI uri) {
251 URI shortUri = longToShortUriMap.get(uri); 190 URI shortUri = SDK_LIBRARY_MANAGER.getShortUri(uri);
252 if (shortUri != null){ 191 if (shortUri != null){
253 return shortUri; 192 return shortUri;
254 } 193 }
255 shortUri = getRelativeUri(uri); 194 shortUri = getRelativeUri(uri);
256 if (shortUri != null){ 195 if (shortUri != null){
257 try { 196 try {
258 return new URI(null, null, shortUri.getScheme() + ":" + shortUri.getHos t() + shortUri.getPath(),null, null); 197 return new URI(null, null, shortUri.getScheme() + ":" + shortUri.getHos t() + shortUri.getPath(),null, null);
259 } catch (URISyntaxException e) { 198 } catch (URISyntaxException e) {
260 } 199 }
261 } 200 }
(...skipping 17 matching lines...) Expand all
279 218
280 219
281 public List<File> getPackageRoots(){ 220 public List<File> getPackageRoots(){
282 return packageRoots; 221 return packageRoots;
283 } 222 }
284 223
285 224
286 public void setPackageRoots(List<File> roots){ 225 public void setPackageRoots(List<File> roots){
287 if (roots == null || roots.isEmpty()){ 226 if (roots == null || roots.isEmpty()){
288 this.packageRoots = DEFAULT_PACKAGE_ROOTS; 227 this.packageRoots = DEFAULT_PACKAGE_ROOTS;
289 } else { 228 } else {
290 packageRoots.clear(); 229 packageRoots.clear();
291 for (File file : roots){ 230 for (File file : roots){
292 packageRoots.add(file.getAbsoluteFile()); 231 packageRoots.add(file.getAbsoluteFile());
293 } 232 }
294 } 233 }
295 packageRootsUri.clear(); 234 packageRootsUri.clear();
296 for (File file : roots){ 235 for (File file : roots){
297 packageRootsUri.add(file.toURI()); 236 packageRootsUri.add(file.toURI());
298 } 237 }
299 } 238 }
300 239
301 /** 240 /**
302 * Translate the URI from dart://[host]/[pathToLib] (e.g. dart://html/html.dar t) 241 * Translate the URI from dart://[host]/[pathToLib] (e.g. dart://html/html.dar t)
303 * to a "file:" URI (e.g. "file:/some/install/directory/html.dart") 242 * to a "file:" URI (e.g. "file:/some/install/directory/html.dart")
304 * 243 *
305 * @param uri the original URI 244 * @param uri the original URI
306 * @return the translated URI, which may be <code>null</code> and may not exis t 245 * @return the translated URI, which may be <code>null</code> and may not exis t
307 * @exception RuntimeException if the URI is a "dart" scheme, 246 * @exception RuntimeException if the URI is a "dart" scheme,
308 * but does not map to a defined system library 247 * but does not map to a defined system library
309 */ 248 */
310 public URI translateDartUri(URI uri) { 249 public URI translateDartUri(URI uri) {
311 if (isDartUri(uri)) { 250 if (isDartUri(uri)) {
312 String host = uri.getHost(); 251 return SDK_LIBRARY_MANAGER.translateDartUri(uri);
313 SystemLibrary library = hostMap.get(host);
314 if (library != null) {
315 return library.translateUri(uri);
316 }
317 if (host != null) {
318 return new File(getSdkLibPath(), host).toURI().resolve("." + uri.getPath ());
319 }
320 throw new RuntimeException("No system library defined for " + uri);
321 } 252 }
322 if (isPackageUri(uri)){ 253 if (isPackageUri(uri)){
323 URI fileUri; 254 URI fileUri;
324 for (URI rootUri : packageRootsUri){ 255 for (URI rootUri : packageRootsUri){
325 fileUri = getResolvedPackageUri(uri, rootUri); 256 fileUri = getResolvedPackageUri(uri, rootUri);
326 File file = new File(fileUri); 257 File file = new File(fileUri);
327 if (file.exists()){ 258 if (file.exists()){
328 return file.toURI(); 259 return file.toURI();
329 } 260 }
330 } 261 }
(...skipping 28 matching lines...) Expand all
359 // so use uri.getAuthority to resolve 290 // so use uri.getAuthority to resolve
360 // package://third_party/dart_lang/lib/unittest/unittest.dart 291 // package://third_party/dart_lang/lib/unittest/unittest.dart
361 if (uri.getHost() != null){ 292 if (uri.getHost() != null){
362 fileUri = packageRootUri.resolve(uri.getHost() + uri.getPath()); 293 fileUri = packageRootUri.resolve(uri.getHost() + uri.getPath());
363 } else { 294 } else {
364 fileUri = packageRootUri.resolve(uri.getAuthority() + uri.getPath()); 295 fileUri = packageRootUri.resolve(uri.getAuthority() + uri.getPath());
365 } 296 }
366 return fileUri; 297 return fileUri;
367 } 298 }
368 299
300 /**
301 * Answer a collection of all bundled library URL specs (e.g. "dart:html").
302 *
303 * @return a collection of specs (not <code>null</code>, contains no <code>nul l</code>s)
304 */
305 public Collection<String> getAllLibrarySpecs() {
306 return SDK_LIBRARY_MANAGER.getAllLibrarySpecs();
307 }
308
309 protected SystemLibrary[] getDefaultLibraries() {
310 return SDK_LIBRARY_MANAGER.getDefaultLibraries();
311
312 }
313
369 public File getSdkLibPath() { 314 public File getSdkLibPath() {
370 return sdkLibPath; 315 return SDK_LIBRARY_MANAGER.getSdkLibPath();
371 } 316 }
372 317
373 /** 318
374 * Scan the directory returned by {@link #getLibrariesDir()} looking for libra ries of the form 319
375 * libraries/<name>/<name>_<platform>.dart and libraries/<name>/<name>.dart wh ere <platform> is
376 * the value initialized in the {@link SystemLibraryManager}.
377 */
378 protected SystemLibrary[] getDefaultLibraries() {
379 libraries = new ArrayList<SystemLibrary>();
380 longToShortUriMap = new HashMap<URI, URI>();
381
382 // Cycle through the import.config, extracting explicit mappings and searchi ng directories
383 URI base = this.sdkLibPathUri;
384 Properties importConfig = getImportConfig();
385 HashSet<String> explicitShortNames = new HashSet<String>();
386 for (Entry<Object, Object> entry : importConfig.entrySet()) {
387 String shortName = ((String) entry.getKey()).trim();
388 String path = ((String) entry.getValue()).trim();
389
390 File file;
391 try {
392 file = new File(base.resolve(new URI(null, null, path, null, null)).norm alize());
393 } catch (URISyntaxException e) {
394 continue;
395 }
396 if (!file.exists()) {
397 throw new InternalCompilerException("Can't find system library dart:" + shortName
398 + " at " + file);
399 }
400
401 // If the shortName ends with ":" then search the associated directory for libraries
402
403 if (shortName.endsWith(":")) {
404 if (!file.isDirectory()) {
405 continue;
406 }
407 for (File child : file.listFiles()) {
408 String host = child.getName();
409 // Do not overwrite explicit shortName to dart file mappings
410 if (explicitShortNames.contains(shortName + host)) {
411 continue;
412 }
413 if (!child.isDirectory()) {
414 continue;
415 }
416 File dartFile = new File(child, child.getName() + ".dart");
417 if (!dartFile.isFile()) {
418 // addLib() will throw an exception. In this case, we are just scann ing
419 // for libraries and don't want the error to be fatal.
420 continue;
421 }
422 addLib(shortName, host, host, child, dartFile.getName());
423 }
424 } else {
425 // Otherwise treat the entry as an explicit shortName to dart file mappi ng
426 int index = shortName.indexOf(':');
427 if (index == -1) {
428 continue;
429 }
430 explicitShortNames.add(shortName);
431 String scheme = shortName.substring(0, index + 1);
432 String name = shortName.substring(index + 1);
433 String host = file.getParentFile().getName();
434 addLib(scheme, host, name, file.getParentFile(), file.getName());
435 }
436 }
437 return libraries.toArray(new SystemLibrary[libraries.size()]);
438 }
439
440 /**
441 * Read the import.config content and return it as a collection of key/value p airs
442 */
443 protected Properties getImportConfig() {
444 Properties importConfig = new Properties();
445 InputStream stream = getImportConfigStream();
446 try {
447 importConfig.load(stream);
448 } catch (IOException ignored) {
449 } finally {
450 try {
451 stream.close();
452 } catch (IOException ignored) {
453 }
454 }
455 return importConfig;
456 }
457
458 private boolean addLib(String scheme, String host, String name, File dir, Stri ng libFileName)
459 throws AssertionError {
460 File libFile = new File(dir, libFileName);
461 if (!libFile.isFile()) {
462 throw new InternalCompilerException("Error mapping dart:" + host + ", path "
463 + libFile.getAbsolutePath() + " is not a file.");
464 }
465 SystemLibrary lib = new SystemLibrary(name, host, libFileName, dir);
466 libraries.add(lib);
467 String libSpec = scheme + name;
468 URI libUri;
469 URI expandedUri;
470 try {
471 libUri = new URI(libSpec);
472 expandedUri = new URI("dart:" + "//" + host + "/" + libFileName);
473 } catch (URISyntaxException e) {
474 throw new AssertionError(e);
475 }
476 URI resolvedUri = lib.translateUri(expandedUri);
477 longToShortUriMap.put(resolvedUri, libUri);
478 longToShortUriMap.put(expandedUri, libUri);
479 return true;
480 }
481
482 /**
483 * Register system libraries for the "dart:" protocol such that dart:[shortLib Name] (e.g.
484 * "dart:html") will automatically be expanded to dart://[host]/[pathToLib] (e .g.
485 * dart://html/html.dart)
486 */
487 private void setLibraries(SystemLibrary[] newLibraries) {
488 libraries = new ArrayList<SystemLibrary>();
489 hostMap = new HashMap<String, SystemLibrary>();
490 expansionMap = new HashMap<String, String>();
491 for (SystemLibrary library : newLibraries) {
492 String host = library.getHost();
493 SystemLibrary existingLib = hostMap.get(host);
494 if (existingLib != null) {
495 libraries.remove(existingLib);
496 }
497 libraries.add(library);
498 hostMap.put(host, library);
499 expansionMap.put(library.getShortName(),
500 "//" + host + "/" + library.getPathToLib());
501 }
502 }
503 } 320 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698