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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: compiler/java/com/google/dart/compiler/SystemLibraryManager.java
===================================================================
--- compiler/java/com/google/dart/compiler/SystemLibraryManager.java (revision 10981)
+++ compiler/java/com/google/dart/compiler/SystemLibraryManager.java (working copy)
@@ -4,23 +4,13 @@
package com.google.dart.compiler;
-import java.io.BufferedInputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Properties;
/**
@@ -53,10 +43,10 @@
public static final String PACKAGE_SCHEME = "package";
public static final String PACKAGE_SCHEME_SPEC = "package:";
- private static final String DART_SCHEME = "dart";
- private static final String DART_SCHEME_SPEC = "dart:";
- private static final String IMPORT_CONFIG = "import_%s.config";
+ public static final String DART_SCHEME = "dart";
+ public static final String DART_SCHEME_SPEC = "dart:";
+
/**
* Answer <code>true</code> if the string is a dart spec
*/
@@ -85,29 +75,19 @@
return uri != null && PACKAGE_SCHEME.equals(uri.getScheme());
}
- private HashMap<String, String> expansionMap;
- private Map<String, SystemLibrary> hostMap;
- private final File sdkLibPath;
- private final URI sdkLibPathUri;
- private final String platformName;
-
+ private static SdkLibraryManager SDK_LIBRARY_MANAGER;
+
private List<File> packageRoots = new ArrayList<File>();
private List<URI> packageRootsUri = new ArrayList<URI>();
- private Map<URI, URI> longToShortUriMap;
-
- private List<SystemLibrary> libraries;
-
public SystemLibraryManager() {
this(DEFAULT_SDK_PATH, DEFAULT_PLATFORM);
}
public SystemLibraryManager(File sdkPath, String platformName) {
- this.sdkLibPath = new File(sdkPath, "lib").getAbsoluteFile();
- this.sdkLibPathUri = sdkLibPath.toURI();
- this.platformName = platformName;
- setLibraries(getDefaultLibraries());
- setPackageRoots(DEFAULT_PACKAGE_ROOTS);
+ if (SDK_LIBRARY_MANAGER == null){
+ SDK_LIBRARY_MANAGER = new SdkLibraryManager(sdkPath, platformName);
+ }
}
@@ -122,20 +102,7 @@
*/
public URI expandRelativeDartUri(URI uri) throws AssertionError {
if (isDartUri(uri)) {
- String host = uri.getHost();
- if (host == null) {
- String spec = uri.getSchemeSpecificPart();
- String replacement = expansionMap.get(spec);
- if (replacement != null) {
- try {
- uri = new URI(DART_SCHEME + ":" + replacement);
- } catch (URISyntaxException e) {
- throw new AssertionError();
- }
- } else {
- return null;
- }
- }
+ return SDK_LIBRARY_MANAGER.expandRelativeDartUri(uri);
}
if (isPackageUri(uri)){
String host = uri.getHost();
@@ -158,33 +125,8 @@
return uri;
}
- /**
- * Answer a collection of all bundled library URL specs (e.g. "dart:html").
- *
- * @return a collection of specs (not <code>null</code>, contains no <code>null</code>s)
- */
- public Collection<String> getAllLibrarySpecs() {
- Collection<String> result = new ArrayList<String>(libraries.size());
- for (SystemLibrary lib : libraries) {
- result.add("dart:" + lib.getShortName());
- }
- return result;
- }
+
- protected InputStream getImportConfigStream() {
- File file = new File(new File(sdkLibPath, "config"),
- String.format(IMPORT_CONFIG, platformName));
- if (!file.exists()) {
- throw new InternalCompilerException("Failed to find " + file.toString()
- + ". Is dart-sdk path correct?");
- }
- try {
- return new BufferedInputStream(new FileInputStream(file));
- } catch (FileNotFoundException e) {
- throw new InternalCompilerException("Failed to open " + file);
- }
- }
-
/**
* Given an absolute file URI (e.g. "file:/some/install/directory/dart-sdk/lib/core/bool.dart"),
* answer the corresponding dart: URI (e.g. "dart://core/bool.dart") for that file URI,
@@ -197,13 +139,10 @@
if (fileUri == null || !fileUri.getScheme().equals("file")) {
return null;
}
- URI relativeUri = sdkLibPathUri.relativize(fileUri);
- if (relativeUri.getScheme() == null) {
- try {
- return new URI(null, null, "dart://" + relativeUri.getPath(), null, null);
- } catch (URISyntaxException e) {
- //$FALL-THROUGH$
- }
+
+ URI relativeUri = SDK_LIBRARY_MANAGER.getRelativeUri(fileUri);
+ if (relativeUri != null){
+ return relativeUri;
}
for (URI rootUri : packageRootsUri){
@@ -248,7 +187,7 @@
* it does not map to a short URI.
*/
public URI getShortUri(URI uri) {
- URI shortUri = longToShortUriMap.get(uri);
+ URI shortUri = SDK_LIBRARY_MANAGER.getShortUri(uri);
if (shortUri != null){
return shortUri;
}
@@ -286,16 +225,16 @@
public void setPackageRoots(List<File> roots){
if (roots == null || roots.isEmpty()){
this.packageRoots = DEFAULT_PACKAGE_ROOTS;
- } else {
+ } else {
packageRoots.clear();
for (File file : roots){
packageRoots.add(file.getAbsoluteFile());
- }
+ }
}
packageRootsUri.clear();
for (File file : roots){
packageRootsUri.add(file.toURI());
- }
+ }
}
/**
@@ -309,15 +248,7 @@
*/
public URI translateDartUri(URI uri) {
if (isDartUri(uri)) {
- String host = uri.getHost();
- SystemLibrary library = hostMap.get(host);
- if (library != null) {
- return library.translateUri(uri);
- }
- if (host != null) {
- return new File(getSdkLibPath(), host).toURI().resolve("." + uri.getPath());
- }
- throw new RuntimeException("No system library defined for " + uri);
+ return SDK_LIBRARY_MANAGER.translateDartUri(uri);
}
if (isPackageUri(uri)){
URI fileUri;
@@ -366,138 +297,24 @@
return fileUri;
}
- public File getSdkLibPath() {
- return sdkLibPath;
- }
-
/**
- * Scan the directory returned by {@link #getLibrariesDir()} looking for libraries of the form
- * libraries/<name>/<name>_<platform>.dart and libraries/<name>/<name>.dart where <platform> is
- * the value initialized in the {@link SystemLibraryManager}.
+ * Answer a collection of all bundled library URL specs (e.g. "dart:html").
+ *
+ * @return a collection of specs (not <code>null</code>, contains no <code>null</code>s)
*/
+ public Collection<String> getAllLibrarySpecs() {
+ return SDK_LIBRARY_MANAGER.getAllLibrarySpecs();
+ }
+
protected SystemLibrary[] getDefaultLibraries() {
- libraries = new ArrayList<SystemLibrary>();
- longToShortUriMap = new HashMap<URI, URI>();
-
- // Cycle through the import.config, extracting explicit mappings and searching directories
- URI base = this.sdkLibPathUri;
- Properties importConfig = getImportConfig();
- HashSet<String> explicitShortNames = new HashSet<String>();
- for (Entry<Object, Object> entry : importConfig.entrySet()) {
- String shortName = ((String) entry.getKey()).trim();
- String path = ((String) entry.getValue()).trim();
-
- File file;
- try {
- file = new File(base.resolve(new URI(null, null, path, null, null)).normalize());
- } catch (URISyntaxException e) {
- continue;
- }
- if (!file.exists()) {
- throw new InternalCompilerException("Can't find system library dart:" + shortName
- + " at " + file);
- }
-
- // If the shortName ends with ":" then search the associated directory for libraries
-
- if (shortName.endsWith(":")) {
- if (!file.isDirectory()) {
- continue;
- }
- for (File child : file.listFiles()) {
- String host = child.getName();
- // Do not overwrite explicit shortName to dart file mappings
- if (explicitShortNames.contains(shortName + host)) {
- continue;
- }
- if (!child.isDirectory()) {
- continue;
- }
- File dartFile = new File(child, child.getName() + ".dart");
- if (!dartFile.isFile()) {
- // addLib() will throw an exception. In this case, we are just scanning
- // for libraries and don't want the error to be fatal.
- continue;
- }
- addLib(shortName, host, host, child, dartFile.getName());
- }
- } else {
- // Otherwise treat the entry as an explicit shortName to dart file mapping
- int index = shortName.indexOf(':');
- if (index == -1) {
- continue;
- }
- explicitShortNames.add(shortName);
- String scheme = shortName.substring(0, index + 1);
- String name = shortName.substring(index + 1);
- String host = file.getParentFile().getName();
- addLib(scheme, host, name, file.getParentFile(), file.getName());
- }
- }
- return libraries.toArray(new SystemLibrary[libraries.size()]);
+ return SDK_LIBRARY_MANAGER.getDefaultLibraries();
+
}
-
- /**
- * Read the import.config content and return it as a collection of key/value pairs
- */
- protected Properties getImportConfig() {
- Properties importConfig = new Properties();
- InputStream stream = getImportConfigStream();
- try {
- importConfig.load(stream);
- } catch (IOException ignored) {
- } finally {
- try {
- stream.close();
- } catch (IOException ignored) {
- }
- }
- return importConfig;
+
+ public File getSdkLibPath() {
+ return SDK_LIBRARY_MANAGER.getSdkLibPath();
}
-
- private boolean addLib(String scheme, String host, String name, File dir, String libFileName)
- throws AssertionError {
- File libFile = new File(dir, libFileName);
- if (!libFile.isFile()) {
- throw new InternalCompilerException("Error mapping dart:" + host + ", path "
- + libFile.getAbsolutePath() + " is not a file.");
- }
- SystemLibrary lib = new SystemLibrary(name, host, libFileName, dir);
- libraries.add(lib);
- String libSpec = scheme + name;
- URI libUri;
- URI expandedUri;
- try {
- libUri = new URI(libSpec);
- expandedUri = new URI("dart:" + "//" + host + "/" + libFileName);
- } catch (URISyntaxException e) {
- throw new AssertionError(e);
- }
- URI resolvedUri = lib.translateUri(expandedUri);
- longToShortUriMap.put(resolvedUri, libUri);
- longToShortUriMap.put(expandedUri, libUri);
- return true;
- }
-
- /**
- * Register system libraries for the "dart:" protocol such that dart:[shortLibName] (e.g.
- * "dart:html") will automatically be expanded to dart://[host]/[pathToLib] (e.g.
- * dart://html/html.dart)
- */
- private void setLibraries(SystemLibrary[] newLibraries) {
- libraries = new ArrayList<SystemLibrary>();
- hostMap = new HashMap<String, SystemLibrary>();
- expansionMap = new HashMap<String, String>();
- for (SystemLibrary library : newLibraries) {
- String host = library.getHost();
- SystemLibrary existingLib = hostMap.get(host);
- if (existingLib != null) {
- libraries.remove(existingLib);
- }
- libraries.add(library);
- hostMap.put(host, library);
- expansionMap.put(library.getShortName(),
- "//" + host + "/" + library.getPathToLib());
- }
- }
+
+
+
}

Powered by Google App Engine
This is Rietveld 408576698