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

Side by Side Diff: compiler/java/com/google/dart/compiler/SdkLibraryManager.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
« no previous file with comments | « no previous file | compiler/java/com/google/dart/compiler/SystemLibraryManager.java » ('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) 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
3 // BSD-style license that can be found in the LICENSE file.
4
5 package com.google.dart.compiler;
6
7 import java.io.BufferedInputStream;
8 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;
14 import java.net.URISyntaxException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Properties;
23
24 /**
25 * A Library manager that manages system libraries
26 */
27 public class SdkLibraryManager {
28
29 private static final String IMPORT_CONFIG = "import_%s.config";
30
31 private HashMap<String, String> expansionMap;
32 private Map<String, SystemLibrary> hostMap;
33 private final File sdkLibPath;
34 private final URI sdkLibPathUri;
35 private final String platformName;
36
37 private Map<URI, URI> longToShortUriMap;
38
39 private List<SystemLibrary> libraries;
40
41 public SdkLibraryManager(File sdkPath, String platformName) {
42 this.sdkLibPath = new File(sdkPath, "lib").getAbsoluteFile();
43 this.sdkLibPathUri = sdkLibPath.toURI();
44 this.platformName = platformName;
45 setLibraries(getDefaultLibraries());
46
47 }
48
49 public URI expandRelativeDartUri(URI uri) throws AssertionError {
50 String host = uri.getHost();
51 if (host == null) {
52 String spec = uri.getSchemeSpecificPart();
53 String replacement = expansionMap.get(spec);
54 if (replacement != null) {
55 try {
56 uri = new URI(SystemLibraryManager.DART_SCHEME + ":" + replacement);
57 } catch (URISyntaxException e) {
58 throw new AssertionError();
59 }
60 } else {
61 return null;
62 }
63 }
64 return uri;
65 }
66
67 public URI getRelativeUri(URI fileUri) {
68
69 if (fileUri == null || !fileUri.getScheme().equals("file")){
70 return null;
71 }
72
73 URI relativeUri = sdkLibPathUri.relativize(fileUri);
74 if (relativeUri.getScheme() == null) {
75 try {
76 return new URI(null, null, "dart://" + relativeUri.getPath(), null, null );
77 } catch (URISyntaxException e) {
78 //$FALL-THROUGH$
79 }
80 }
81 return null;
82 }
83
84 public URI getShortUri(URI uri) {
85 URI shortUri = longToShortUriMap.get(uri);
86 if (shortUri != null){
87 return shortUri;
88 }
89 shortUri = getRelativeUri(uri);
90 if (shortUri != null){
91 try {
92 return new URI(null, null, shortUri.getScheme() + ":" + shortUri.getHos t() + shortUri.getPath(),null, null);
93 } catch (URISyntaxException e) {
94 }
95 }
96 return null;
97 }
98
99 public URI translateDartUri(URI uri) {
100
101 String host = uri.getHost();
102 SystemLibrary library = hostMap.get(host);
103 if (library != null) {
104 return library.translateUri(uri);
105 }
106 if (host != null) {
107 return new File(getSdkLibPath(), host).toURI().resolve("." + uri.getPath ());
108 }
109 throw new RuntimeException("No system library defined for " + uri);
110
111 }
112
113
114 public Collection<String> getAllLibrarySpecs() {
115 Collection<String> result = new ArrayList<String>(libraries.size());
116 for (SystemLibrary lib : libraries) {
117 result.add("dart:" + lib.getShortName());
118 }
119 return result;
120 }
121
122
123 /**
124 * Scan the directory returned by {@link #getLibrariesDir()} looking for libra ries of the form
125 * libraries/<name>/<name>_<platform>.dart and libraries/<name>/<name>.dart wh ere <platform> is
126 * the value initialized in the {@link SdkLibraryManager}.
127 */
128 protected SystemLibrary[] getDefaultLibraries() {
129 libraries = new ArrayList<SystemLibrary>();
130 longToShortUriMap = new HashMap<URI, URI>();
131
132 // Cycle through the import.config, extracting explicit mappings and searchi ng directories
133 URI base = this.sdkLibPathUri;
134 Properties importConfig = getImportConfig();
135 HashSet<String> explicitShortNames = new HashSet<String>();
136 for (Entry<Object, Object> entry : importConfig.entrySet()) {
137 String shortName = ((String) entry.getKey()).trim();
138 String path = ((String) entry.getValue()).trim();
139
140 File file;
141 try {
142 file = new File(base.resolve(new URI(null, null, path, null, null)).norm alize());
143 } catch (URISyntaxException e) {
144 continue;
145 }
146 if (!file.exists()) {
147 throw new InternalCompilerException("Can't find system library dart:" + shortName
148 + " at " + file);
149 }
150
151 // If the shortName ends with ":" then search the associated directory for libraries
152
153 if (shortName.endsWith(":")) {
154 if (!file.isDirectory()) {
155 continue;
156 }
157 for (File child : file.listFiles()) {
158 String host = child.getName();
159 // Do not overwrite explicit shortName to dart file mappings
160 if (explicitShortNames.contains(shortName + host)) {
161 continue;
162 }
163 if (!child.isDirectory()) {
164 continue;
165 }
166 File dartFile = new File(child, child.getName() + ".dart");
167 if (!dartFile.isFile()) {
168 // addLib() will throw an exception. In this case, we are just scann ing
169 // for libraries and don't want the error to be fatal.
170 continue;
171 }
172 addLib(shortName, host, host, child, dartFile.getName());
173 }
174 } else {
175 // Otherwise treat the entry as an explicit shortName to dart file mappi ng
176 int index = shortName.indexOf(':');
177 if (index == -1) {
178 continue;
179 }
180 explicitShortNames.add(shortName);
181 String scheme = shortName.substring(0, index + 1);
182 String name = shortName.substring(index + 1);
183 String host = file.getParentFile().getName();
184 addLib(scheme, host, name, file.getParentFile(), file.getName());
185 }
186 }
187 return libraries.toArray(new SystemLibrary[libraries.size()]);
188 }
189
190 /**
191 * Read the import.config content and return it as a collection of key/value p airs
192 */
193 protected Properties getImportConfig() {
194 Properties importConfig = new Properties();
195 InputStream stream = getImportConfigStream();
196 try {
197 importConfig.load(stream);
198 } catch (IOException ignored) {
199 } finally {
200 try {
201 stream.close();
202 } catch (IOException ignored) {
203 }
204 }
205 return importConfig;
206 }
207
208 protected InputStream getImportConfigStream() {
209 File file = new File(new File(sdkLibPath, "config"),
210 String.format(IMPORT_CONFIG, platformName));
211 if (!file.exists()) {
212 throw new InternalCompilerException("Failed to find " + file.toString()
213 + ". Is dart-sdk path correct?");
214 }
215 try {
216 return new BufferedInputStream(new FileInputStream(file));
217 } catch (FileNotFoundException e) {
218 throw new InternalCompilerException("Failed to open " + file);
219 }
220 }
221
222
223 private boolean addLib(String scheme, String host, String name, File dir, Stri ng libFileName)
224 throws AssertionError {
225 File libFile = new File(dir, libFileName);
226 if (!libFile.isFile()) {
227 throw new InternalCompilerException("Error mapping dart:" + host + ", path "
228 + libFile.getAbsolutePath() + " is not a file.");
229 }
230 SystemLibrary lib = new SystemLibrary(name, host, libFileName, dir);
231 libraries.add(lib);
232 String libSpec = scheme + name;
233 URI libUri;
234 URI expandedUri;
235 try {
236 libUri = new URI(libSpec);
237 expandedUri = new URI("dart:" + "//" + host + "/" + libFileName);
238 } catch (URISyntaxException e) {
239 throw new AssertionError(e);
240 }
241 URI resolvedUri = lib.translateUri(expandedUri);
242 longToShortUriMap.put(resolvedUri, libUri);
243 longToShortUriMap.put(expandedUri, libUri);
244 return true;
245 }
246
247 /**
248 * Register system libraries for the "dart:" protocol such that dart:[shortLib Name] (e.g.
249 * "dart:html") will automatically be expanded to dart://[host]/[pathToLib] (e .g.
250 * dart://html/html.dart)
251 */
252 private void setLibraries(SystemLibrary[] newLibraries) {
253 libraries = new ArrayList<SystemLibrary>();
254 hostMap = new HashMap<String, SystemLibrary>();
255 expansionMap = new HashMap<String, String>();
256 for (SystemLibrary library : newLibraries) {
257 String host = library.getHost();
258 SystemLibrary existingLib = hostMap.get(host);
259 if (existingLib != null) {
260 libraries.remove(existingLib);
261 }
262 libraries.add(library);
263 hostMap.put(host, library);
264 expansionMap.put(library.getShortName(),
265 "//" + host + "/" + library.getPathToLib());
266 }
267 }
268
269 public File getSdkLibPath() {
270 return sdkLibPath;
271 }
272
273 }
OLDNEW
« no previous file with comments | « no previous file | compiler/java/com/google/dart/compiler/SystemLibraryManager.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698