com.google.javascript.jscomp
Class SourceFile

java.lang.Object
  extended by com.google.javascript.jscomp.SourceFile
All Implemented Interfaces:
StaticSourceFile, Serializable
Direct Known Subclasses:
JSSourceFile

public class SourceFile
extends Object
implements StaticSourceFile, Serializable

An abstract representation of a source file that provides access to language-neutral features. The source file can be loaded from various locations, such as from disk or from a preloaded string.

See Also:
Serialized Form

Nested Class Summary
static class SourceFile.Builder
          A builder interface for source files.
static interface SourceFile.Generator
          A JavaScript source code provider.
 
Constructor Summary
SourceFile(String fileName)
          Construct a new abstract source file.
 
Method Summary
static SourceFile.Builder builder()
          Create a new builder for source files.
 void clearCachedSource()
           
static SourceFile fromCode(String fileName, String code)
           
static SourceFile fromCode(String fileName, String originalPath, String code)
           
static SourceFile fromFile(File file)
           
static SourceFile fromFile(File file, Charset c)
           
static SourceFile fromFile(String fileName)
           
static SourceFile fromFile(String fileName, Charset c)
           
static SourceFile fromGenerator(String fileName, SourceFile.Generator generator)
           
static SourceFile fromInputStream(String fileName, InputStream s)
           
static SourceFile fromInputStream(String fileName, String originalPath, InputStream s)
           
static SourceFile fromReader(String fileName, Reader r)
           
 String getCode()
          Gets all the code in this source file.
 Reader getCodeReader()
          Gets a reader for the code in this source file.
 String getLine(int lineNumber)
          Gets the source line for the indicated line number.
 int getLineOffset(int lineno)
          Returns the offset of the given line number relative to the file start.
 String getName()
          Returns a unique name for the source file.
 String getOriginalPath()
           
 Region getRegion(int lineNumber)
          Get a region around the indicated line number.
 boolean isExtern()
          Returns whether this is an extern.
 void setOriginalPath(String originalPath)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SourceFile

public SourceFile(String fileName)
Construct a new abstract source file.

Parameters:
fileName - The file name of the source file. It does not necessarily need to correspond to a real path. But it should be unique. Will appear in warning messages emitted by the compiler.
Method Detail

getLineOffset

public int getLineOffset(int lineno)
Description copied from interface: StaticSourceFile
Returns the offset of the given line number relative to the file start. Line number should be 1-based. If the source file doesn't have line information, it should return Integer.MIN_VALUE. The negative offsets will make it more obvious what happened.

Specified by:
getLineOffset in interface StaticSourceFile
Parameters:
lineno - the line of the input to get the absolute offset of.
Returns:
the absolute offset of the start of the provided line.

getCode

public String getCode()
               throws IOException
Gets all the code in this source file.

Throws:
IOException

getCodeReader

public Reader getCodeReader()
                     throws IOException
Gets a reader for the code in this source file.

Throws:
IOException

getOriginalPath

public String getOriginalPath()

setOriginalPath

public void setOriginalPath(String originalPath)

clearCachedSource

public void clearCachedSource()

getName

public String getName()
Returns a unique name for the source file.

Specified by:
getName in interface StaticSourceFile

isExtern

public boolean isExtern()
Returns whether this is an extern.

Specified by:
isExtern in interface StaticSourceFile

getLine

public String getLine(int lineNumber)
Gets the source line for the indicated line number.

Parameters:
lineNumber - the line number, 1 being the first line of the file.
Returns:
The line indicated. Does not include the newline at the end of the file. Returns null if it does not exist, or if there was an IO exception.

getRegion

public Region getRegion(int lineNumber)
Get a region around the indicated line number. The exact definition of a region is implementation specific, but it must contain the line indicated by the line number. A region must not start or end by a carriage return.

Parameters:
lineNumber - the line number, 1 being the first line of the file.
Returns:
The line indicated. Returns null if it does not exist, or if there was an IO exception.

toString

public String toString()
Overrides:
toString in class Object

fromFile

public static SourceFile fromFile(String fileName,
                                  Charset c)

fromFile

public static SourceFile fromFile(String fileName)

fromFile

public static SourceFile fromFile(File file,
                                  Charset c)

fromFile

public static SourceFile fromFile(File file)

fromCode

public static SourceFile fromCode(String fileName,
                                  String code)

fromCode

public static SourceFile fromCode(String fileName,
                                  String originalPath,
                                  String code)

fromInputStream

public static SourceFile fromInputStream(String fileName,
                                         InputStream s)
                                  throws IOException
Throws:
IOException

fromInputStream

public static SourceFile fromInputStream(String fileName,
                                         String originalPath,
                                         InputStream s)
                                  throws IOException
Throws:
IOException

fromReader

public static SourceFile fromReader(String fileName,
                                    Reader r)
                             throws IOException
Throws:
IOException

fromGenerator

public static SourceFile fromGenerator(String fileName,
                                       SourceFile.Generator generator)

builder

public static SourceFile.Builder builder()
Create a new builder for source files.