| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.annotations.VisibleForTesting; |
| 20 |
import com.google.common.base.Charsets; |
| 21 |
import com.google.common.io.CharStreams; |
| 22 |
import com.google.common.io.Files; |
| 23 |
import com.google.javascript.rhino.jstype.StaticSourceFile; |
| 24 |
|
| 25 |
import java.io.File; |
| 26 |
import java.io.FileReader; |
| 27 |
import java.io.IOException; |
| 28 |
import java.io.InputStream; |
| 29 |
import java.io.InputStreamReader; |
| 30 |
import java.io.Reader; |
| 31 |
import java.io.Serializable; |
| 32 |
import java.io.StringReader; |
| 33 |
import java.nio.charset.Charset; |
| 34 |
import java.util.Arrays; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class SourceFile implements StaticSourceFile, Serializable { |
| 44 |
private static final long serialVersionUID = 1L; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public interface Generator { |
| 50 |
public String getCode(); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private static final int SOURCE_EXCERPT_REGION_LENGTH = 5; |
| 58 |
|
| 59 |
private final String fileName; |
| 60 |
private boolean isExternFile = false; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private String originalPath = null; |
| 68 |
|
| 69 |
|
| 70 |
private int[] lineOffsets = null; |
| 71 |
|
| 72 |
private String code = null; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public SourceFile(String fileName) { |
| 82 |
if (fileName == null || fileName.isEmpty()) { |
| 83 |
throw new IllegalArgumentException("a source must have a name"); |
| 84 |
} |
| 85 |
this.fileName = fileName; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public int getLineOffset(int lineno) { |
| 90 |
findLineOffsets(); |
| 91 |
if (lineno < 1 || lineno > lineOffsets.length) { |
| 92 |
throw new IllegalArgumentException( |
| 93 |
"Expected line number between 1 and " + lineOffsets.length + |
| 94 |
"\nActual: " + lineno); |
| 95 |
} |
| 96 |
return lineOffsets[lineno - 1]; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
int getNumLines() { |
| 101 |
findLineOffsets(); |
| 102 |
return lineOffsets.length; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
private void findLineOffsets() { |
| 107 |
if (lineOffsets != null) { |
| 108 |
return; |
| 109 |
} |
| 110 |
try { |
| 111 |
String[] sourceLines = getCode().split("\n"); |
| 112 |
lineOffsets = new int[sourceLines.length]; |
| 113 |
for (int ii = 1; ii < sourceLines.length; ++ii) { |
| 114 |
lineOffsets[ii] = |
| 115 |
lineOffsets[ii - 1] + sourceLines[ii - 1].length() + 1; |
| 116 |
} |
| 117 |
} catch (IOException e) { |
| 118 |
lineOffsets = new int[1]; |
| 119 |
lineOffsets[0] = 0; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public String getCode() throws IOException { |
| 132 |
return code; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public Reader getCodeReader() throws IOException { |
| 139 |
return new StringReader(getCode()); |
| 140 |
} |
| 141 |
|
| 142 |
@VisibleForTesting |
| 143 |
String getCodeNoCache() { |
| 144 |
return code; |
| 145 |
} |
| 146 |
|
| 147 |
private void setCode(String sourceCode) { |
| 148 |
code = sourceCode; |
| 149 |
} |
| 150 |
|
| 151 |
public String getOriginalPath() { |
| 152 |
return originalPath != null ? originalPath : fileName; |
| 153 |
} |
| 154 |
|
| 155 |
public void setOriginalPath(String originalPath) { |
| 156 |
this.originalPath = originalPath; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public void clearCachedSource() { |
| 164 |
|
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
boolean hasSourceInMemory() { |
| 169 |
return code != null; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
@Override |
| 174 |
public String getName() { |
| 175 |
return fileName; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
@Override |
| 180 |
public boolean isExtern() { |
| 181 |
return isExternFile; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void setIsExtern(boolean newVal) { |
| 186 |
isExternFile = newVal; |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public int getLineOfOffset(int offset) { |
| 191 |
findLineOffsets(); |
| 192 |
int search = Arrays.binarySearch(lineOffsets, offset); |
| 193 |
if (search >= 0) { |
| 194 |
return search + 1; |
| 195 |
} else { |
| 196 |
int insertionPoint = -1 * (search + 1); |
| 197 |
return Math.min(insertionPoint - 1, lineOffsets.length - 1) + 1; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public int getColumnOfOffset(int offset) { |
| 203 |
int line = getLineOfOffset(offset); |
| 204 |
return offset - lineOffsets[line - 1]; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public String getLine(int lineNumber) { |
| 216 |
findLineOffsets(); |
| 217 |
if (lineNumber > lineOffsets.length) { |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
if (lineNumber < 1) { |
| 222 |
lineNumber = 1; |
| 223 |
} |
| 224 |
|
| 225 |
int pos = lineOffsets[lineNumber - 1]; |
| 226 |
String js = ""; |
| 227 |
try { |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
js = getCode(); |
| 232 |
} catch (IOException e) { |
| 233 |
return null; |
| 234 |
} |
| 235 |
|
| 236 |
if (js.indexOf('\n', pos) == -1) { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
if (pos >= js.length()) { |
| 241 |
return null; |
| 242 |
} else { |
| 243 |
return js.substring(pos, js.length()); |
| 244 |
} |
| 245 |
} else { |
| 246 |
return js.substring(pos, js.indexOf('\n', pos)); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public Region getRegion(int lineNumber) { |
| 260 |
String js = ""; |
| 261 |
try { |
| 262 |
js = getCode(); |
| 263 |
} catch (IOException e) { |
| 264 |
return null; |
| 265 |
} |
| 266 |
int pos = 0; |
| 267 |
int startLine = Math.max(1, |
| 268 |
lineNumber - (SOURCE_EXCERPT_REGION_LENGTH + 1) / 2 + 1); |
| 269 |
for (int n = 1; n < startLine; n++) { |
| 270 |
int nextpos = js.indexOf('\n', pos); |
| 271 |
if (nextpos == -1) { |
| 272 |
break; |
| 273 |
} |
| 274 |
pos = nextpos + 1; |
| 275 |
} |
| 276 |
int end = pos; |
| 277 |
int endLine = startLine; |
| 278 |
for (int n = 0; n < SOURCE_EXCERPT_REGION_LENGTH; n++, endLine++) { |
| 279 |
end = js.indexOf('\n', end); |
| 280 |
if (end == -1) { |
| 281 |
break; |
| 282 |
} |
| 283 |
end++; |
| 284 |
} |
| 285 |
if (lineNumber >= endLine) { |
| 286 |
return null; |
| 287 |
} |
| 288 |
if (end == -1) { |
| 289 |
int last = js.length() - 1; |
| 290 |
if (js.charAt(last) == '\n') { |
| 291 |
return |
| 292 |
new SimpleRegion(startLine, endLine, js.substring(pos, last)); |
| 293 |
} else { |
| 294 |
return new SimpleRegion(startLine, endLine, js.substring(pos)); |
| 295 |
} |
| 296 |
} else { |
| 297 |
return new SimpleRegion(startLine, endLine, js.substring(pos, end)); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
@Override |
| 302 |
public String toString() { |
| 303 |
return fileName; |
| 304 |
} |
| 305 |
|
| 306 |
public static SourceFile fromFile(String fileName, Charset c) { |
| 307 |
return builder().withCharset(c).buildFromFile(fileName); |
| 308 |
} |
| 309 |
|
| 310 |
public static SourceFile fromFile(String fileName) { |
| 311 |
return builder().buildFromFile(fileName); |
| 312 |
} |
| 313 |
|
| 314 |
public static SourceFile fromFile(File file, Charset c) { |
| 315 |
return builder().withCharset(c).buildFromFile(file); |
| 316 |
} |
| 317 |
|
| 318 |
public static SourceFile fromFile(File file) { |
| 319 |
return builder().buildFromFile(file); |
| 320 |
} |
| 321 |
|
| 322 |
public static SourceFile fromCode(String fileName, String code) { |
| 323 |
return builder().buildFromCode(fileName, code); |
| 324 |
} |
| 325 |
|
| 326 |
public static SourceFile fromCode(String fileName, |
| 327 |
String originalPath, String code) { |
| 328 |
return builder().withOriginalPath(originalPath) |
| 329 |
.buildFromCode(fileName, code); |
| 330 |
} |
| 331 |
|
| 332 |
public static SourceFile fromInputStream(String fileName, InputStream s) |
| 333 |
throws IOException { |
| 334 |
return builder().buildFromInputStream(fileName, s); |
| 335 |
} |
| 336 |
|
| 337 |
public static SourceFile fromInputStream(String fileName, |
| 338 |
String originalPath, InputStream s) throws IOException { |
| 339 |
return builder().withOriginalPath(originalPath) |
| 340 |
.buildFromInputStream(fileName, s); |
| 341 |
} |
| 342 |
|
| 343 |
public static SourceFile fromReader(String fileName, Reader r) |
| 344 |
throws IOException { |
| 345 |
return builder().buildFromReader(fileName, r); |
| 346 |
} |
| 347 |
|
| 348 |
public static SourceFile fromGenerator(String fileName, |
| 349 |
Generator generator) { |
| 350 |
return builder().buildFromGenerator(fileName, generator); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
public static Builder builder() { |
| 355 |
return new Builder(); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public static class Builder { |
| 365 |
private Charset charset = Charsets.UTF_8; |
| 366 |
private String originalPath = null; |
| 367 |
|
| 368 |
public Builder() {} |
| 369 |
|
| 370 |
|
| 371 |
public Builder withCharset(Charset charset) { |
| 372 |
this.charset = charset; |
| 373 |
return this; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
public Builder withOriginalPath(String originalPath) { |
| 378 |
this.originalPath = originalPath; |
| 379 |
return this; |
| 380 |
} |
| 381 |
|
| 382 |
public SourceFile buildFromFile(String fileName) { |
| 383 |
return buildFromFile(new File(fileName)); |
| 384 |
} |
| 385 |
|
| 386 |
public SourceFile buildFromFile(File file) { |
| 387 |
return new OnDisk(file, originalPath, charset); |
| 388 |
} |
| 389 |
|
| 390 |
public SourceFile buildFromCode(String fileName, String code) { |
| 391 |
return new Preloaded(fileName, originalPath, code); |
| 392 |
} |
| 393 |
|
| 394 |
public SourceFile buildFromInputStream(String fileName, InputStream s) |
| 395 |
throws IOException { |
| 396 |
return buildFromCode(fileName, |
| 397 |
CharStreams.toString(new InputStreamReader(s, charset))); |
| 398 |
} |
| 399 |
|
| 400 |
public SourceFile buildFromReader(String fileName, Reader r) |
| 401 |
throws IOException { |
| 402 |
return buildFromCode(fileName, CharStreams.toString(r)); |
| 403 |
} |
| 404 |
|
| 405 |
public SourceFile buildFromGenerator(String fileName, |
| 406 |
Generator generator) { |
| 407 |
return new Generated(fileName, originalPath, generator); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
static class Preloaded extends SourceFile { |
| 419 |
private static final long serialVersionUID = 1L; |
| 420 |
|
| 421 |
Preloaded(String fileName, String originalPath, String code) { |
| 422 |
super(fileName); |
| 423 |
super.setOriginalPath(originalPath); |
| 424 |
super.setCode(code); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
static class Generated extends SourceFile { |
| 433 |
private static final long serialVersionUID = 1L; |
| 434 |
private final Generator generator; |
| 435 |
|
| 436 |
|
| 437 |
Generated(String fileName, String originalPath, Generator generator) { |
| 438 |
super(fileName); |
| 439 |
super.setOriginalPath(originalPath); |
| 440 |
this.generator = generator; |
| 441 |
} |
| 442 |
|
| 443 |
@Override |
| 444 |
public synchronized String getCode() throws IOException { |
| 445 |
String cachedCode = super.getCode(); |
| 446 |
|
| 447 |
if (cachedCode == null) { |
| 448 |
cachedCode = generator.getCode(); |
| 449 |
super.setCode(cachedCode); |
| 450 |
} |
| 451 |
return cachedCode; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
@Override |
| 457 |
public void clearCachedSource() { |
| 458 |
super.setCode(null); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
static class OnDisk extends SourceFile { |
| 468 |
private static final long serialVersionUID = 1L; |
| 469 |
private final File file; |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
private String inputCharset = Charsets.UTF_8.name(); |
| 475 |
|
| 476 |
OnDisk(File file, String originalPath, Charset c) { |
| 477 |
super(file.getPath()); |
| 478 |
this.file = file; |
| 479 |
super.setOriginalPath(originalPath); |
| 480 |
if (c != null) { |
| 481 |
this.setCharset(c); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
@Override |
| 486 |
public synchronized String getCode() throws IOException { |
| 487 |
String cachedCode = super.getCode(); |
| 488 |
|
| 489 |
if (cachedCode == null) { |
| 490 |
cachedCode = Files.toString(file, this.getCharset()); |
| 491 |
super.setCode(cachedCode); |
| 492 |
} |
| 493 |
return cachedCode; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
@Override |
| 500 |
public Reader getCodeReader() throws IOException { |
| 501 |
if (hasSourceInMemory()) { |
| 502 |
return super.getCodeReader(); |
| 503 |
} else { |
| 504 |
|
| 505 |
return new FileReader(file); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
@Override |
| 512 |
public void clearCachedSource() { |
| 513 |
super.setCode(null); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
public void setCharset(Charset c) { |
| 523 |
inputCharset = c.name(); |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
public Charset getCharset() { |
| 533 |
return Charset.forName(inputCharset); |
| 534 |
} |
| 535 |
} |
| 536 |
} |