OLD | NEW |
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 package com.google.dart.compiler.end2end.inc; | 4 package com.google.dart.compiler.end2end.inc; |
5 | 5 |
6 import com.google.common.collect.Maps; | 6 import com.google.common.collect.Maps; |
7 import com.google.dart.compiler.DartSource; | 7 import com.google.dart.compiler.DartSource; |
8 import com.google.dart.compiler.LibrarySource; | 8 import com.google.dart.compiler.LibrarySource; |
9 import com.google.dart.compiler.Source; | 9 import com.google.dart.compiler.Source; |
10 import com.google.dart.compiler.UrlDartSource; | 10 import com.google.dart.compiler.UrlDartSource; |
11 | 11 |
12 import java.io.FileNotFoundException; | 12 import java.io.FileNotFoundException; |
13 import java.io.IOException; | 13 import java.io.IOException; |
14 import java.io.Reader; | 14 import java.io.Reader; |
15 import java.io.StringReader; | 15 import java.io.StringReader; |
16 import java.net.URI; | 16 import java.net.URI; |
17 import java.util.Map; | 17 import java.util.Map; |
18 | 18 |
19 /** | 19 /** |
20 * {@link LibrarySource} which provides content for all {@link Source}s from mem
ory. | 20 * {@link LibrarySource} which provides content for all {@link Source}s from mem
ory. |
21 */ | 21 */ |
22 public class MemoryLibrarySource implements LibrarySource { | 22 public class MemoryLibrarySource implements LibrarySource { |
23 public static final String IO_EXCEPTION_CONTENT = "simulate-IOException"; | 23 public static final String IO_EXCEPTION_CONTENT = "simulate-IOException"; |
24 private final String libName; | 24 private final String libName; |
25 private final Map<String, DartSource> sourceMap; | |
26 private final Map<String, String> sourceContentMap; | 25 private final Map<String, String> sourceContentMap; |
27 private final Map<String, Long> sourceLastModifiedMap; | 26 private final Map<String, Long> sourceLastModifiedMap; |
28 | 27 |
29 public MemoryLibrarySource(String libName) { | 28 public MemoryLibrarySource(String libName) { |
30 this.libName = libName; | 29 this.libName = libName; |
31 sourceMap = Maps.newHashMap(); | |
32 sourceContentMap = Maps.newHashMap(); | 30 sourceContentMap = Maps.newHashMap(); |
33 sourceLastModifiedMap = Maps.newHashMap(); | 31 sourceLastModifiedMap = Maps.newHashMap(); |
34 } | 32 } |
35 | 33 |
36 private MemoryLibrarySource(String libName, MemoryLibrarySource parent) { | 34 private MemoryLibrarySource(String libName, MemoryLibrarySource parent) { |
37 this.libName = libName; | 35 this.libName = libName; |
38 sourceMap = parent.sourceMap; | |
39 sourceContentMap = parent.sourceContentMap; | 36 sourceContentMap = parent.sourceContentMap; |
40 sourceLastModifiedMap = parent.sourceLastModifiedMap; | 37 sourceLastModifiedMap = parent.sourceLastModifiedMap; |
41 } | 38 } |
42 | 39 |
43 @Override | 40 @Override |
44 public boolean exists() { | 41 public boolean exists() { |
45 return true; | 42 return true; |
46 } | 43 } |
47 | 44 |
48 @Override | 45 @Override |
(...skipping 22 matching lines...) Expand all Loading... |
71 if (IO_EXCEPTION_CONTENT.equals(content)) { | 68 if (IO_EXCEPTION_CONTENT.equals(content)) { |
72 throw new IOException("simulated"); | 69 throw new IOException("simulated"); |
73 } | 70 } |
74 if (content == null) { | 71 if (content == null) { |
75 throw new FileNotFoundException(libName); | 72 throw new FileNotFoundException(libName); |
76 } | 73 } |
77 return new StringReader(content); | 74 return new StringReader(content); |
78 } | 75 } |
79 | 76 |
80 @Override | 77 @Override |
81 public LibrarySource getImportFor(String relPath) throws IOException { | 78 public LibrarySource getImportFor(final String relPath) throws IOException { |
82 if (!sourceContentMap.containsKey(relPath)) { | |
83 return null; | |
84 } | |
85 return new MemoryLibrarySource(relPath, this); | 79 return new MemoryLibrarySource(relPath, this); |
86 } | 80 } |
87 | 81 |
88 @Override | 82 @Override |
89 public DartSource getSourceFor(final String relPath) { | 83 public DartSource getSourceFor(final String relPath) { |
90 DartSource result; | |
91 // check cache | |
92 { | |
93 result = sourceMap.get(relPath); | |
94 if (result != null) { | |
95 return result; | |
96 } | |
97 } | |
98 // prepare content | |
99 final String content = sourceContentMap.get(relPath); | 84 final String content = sourceContentMap.get(relPath); |
100 final Long sourceLastModified = sourceLastModifiedMap.get(relPath); | 85 final Long sourceLastModified = sourceLastModifiedMap.get(relPath); |
101 // may be does not exist | |
102 if (content == null) { | |
103 return null; | |
104 } | |
105 // Return fake UrlDateSource with in-memory content. | 86 // Return fake UrlDateSource with in-memory content. |
106 final URI uri = URI.create(relPath); | 87 final URI uri = URI.create(relPath); |
107 result = new UrlDartSource(uri, relPath, this) { | 88 return new UrlDartSource(uri, relPath, this) { |
108 @Override | 89 @Override |
109 public String getName() { | 90 public String getName() { |
110 return relPath; | 91 return relPath; |
111 } | 92 } |
112 | 93 |
113 @Override | 94 @Override |
114 public URI getUri() { | 95 public URI getUri() { |
115 return uri; | 96 return uri; |
116 } | 97 } |
117 | 98 |
118 @Override | 99 @Override |
119 public boolean exists() { | 100 public boolean exists() { |
120 return content != null; | 101 return content != null; |
121 } | 102 } |
122 | 103 |
123 @Override | 104 @Override |
124 public long getLastModified() { | 105 public long getLastModified() { |
125 return sourceLastModified.longValue(); | 106 return sourceLastModified.longValue(); |
126 } | 107 } |
127 | 108 |
128 @Override | 109 @Override |
129 public Reader getSourceReader() throws IOException { | 110 public Reader getSourceReader() throws IOException { |
130 return new StringReader(content); | 111 return new StringReader(content); |
131 } | 112 } |
132 }; | 113 }; |
133 sourceMap.put(relPath, result); | |
134 return result; | |
135 } | 114 } |
136 | 115 |
137 /** | 116 /** |
138 * Sets the given content for the source. | 117 * Sets the given content for the source. |
139 */ | 118 */ |
140 public void setContent(String relPath, String content) { | 119 public void setContent(String relPath, String content) { |
141 sourceMap.remove(relPath); | |
142 sourceContentMap.put(relPath, content); | 120 sourceContentMap.put(relPath, content); |
143 sourceLastModifiedMap.put(relPath, System.currentTimeMillis()); | 121 sourceLastModifiedMap.put(relPath, System.currentTimeMillis()); |
144 } | 122 } |
145 } | 123 } |
OLD | NEW |