Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.caucho.es.parser
Class Parser

    java.lang.Object
      |
      +--com.caucho.es.parser.Parser

public class Parser
extends java.lang.Object

Parser is a factory for generating compiled Script objects.

Most applications will use the `parse(String)` interface to parse JavaScript. That method will try to load a precompiled script from the work directory before trying to parse it.

Applications will often set the script path a directory for script and include the classpath in the path. Applications will often override the work dir for a more appropriate work directory. ``

     package com.caucho.vfs.*;
     package com.caucho.es.*;

     ...

     com.caucho.es.parser.Parser parser;
     parser = new com.caucho.es.parser.Parser();

     // configure the path to search for *.js files
     MergePath scriptPath = new MergePath();
     scriptPath.addMergePath(Vfs.lookup("/home/ferg/js"));
     ClassLoader loader = Thread.currentThread().contextClassLoader();
     scriptPath.addClassPath(loader);
     parser.setScriptPath(scriptPath);

     // configure the directory storing compiled scripts
     Path workPath = Vfs.lookup("/tmp/caucho/work");
     parser.setWorkDir(workPath);

     Script script = parser.parse("test.js");

Constructor Summary
`Parser()`

Method Summary
`void` | `addImport(java.lang.String name)` Adds a package/script to be automatically imported by the script.
`java.lang.String` | `getFilename()` Returns the current filename being parsed.
`Path` | `getScriptPath()` Returns the path to search for imported javascript.
`Path` | `getWorkDir()` Returns the directory for generated *.java and *.class files.
`Script` | `parse(ReadStream is)` Alternative parsing method when the application only has an open stream to the file.
`Script` | `parse(ReadStream is, java.lang.String name, int line)` An alternative parsing method given an open stream, a filename and a line number.
`Script` | `parse(java.lang.String name)` Main application parsing method.
`Script` | `parseEval(ReadStream is, java.lang.String name, int line)` Parses a script for the JavaScript "eval" expression.
`void` | `setClassLoader(java.lang.ClassLoader loader)` Internal method to set the actual class loader.
`void` | `setClassName(java.lang.String name)` Sets the name of the generated java class.
`void` | `setFast(boolean isFast)` Sets "fast" mode, i.e.
`void` | `setLineMap(com.caucho.java.LineMap lineMap)` Sets a line number map.
`void` | `setParentLoader(java.lang.ClassLoader parentLoader)` Sets the parent class loader.
`void` | `setScriptPath(Path scriptPath)` Sets the path to search for imported javascript source files.
`void` | `setWorkDir(Path path)` Sets the directory for generated *.java and *.class files.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### Parser

    public Parser()

Method Detail

### setParentLoader

    public void setParentLoader(java.lang.ClassLoader parentLoader)

  Sets the parent class loader. If unspecified, defaults to the context classloader. Most applications will just use the default.
  Parameters:
    `parentLoader` - the classloader to be used for the script's parent.

### setClassLoader

    public void setClassLoader(java.lang.ClassLoader loader)

  Internal method to set the actual class loader. Normally, this should only be called from com.caucho.es functions.

### setScriptPath

    public void setScriptPath(Path scriptPath)

  Sets the path to search for imported javascript source files. Normally, ScriptPath will be a MergePath. If the MergePath adds the classpath, then JavaScript files can be put in the normal Java classpath.

  If the ScriptPath is not specified, it will use the current directory and the classpath from the context class loader. ``

       MergePath scriptPath = new MergePath();
       scriptPath.addMergePath(Vfs.lookup("/home/ferg/js"));

       ClassLoader loader = Thread.currentThread().contextClassLoader();
       scriptPath.addClassPath(loader);

       parser.setScriptPath(scriptPath);

  Parameters:
    `scriptPath` - path to search for imported scripts.

### getScriptPath

    public Path getScriptPath()

  Returns the path to search for imported javascript. Normally, scriptPath will be a MergePath.
  Parameters:
    `scriptPath` - path to search for imported scripts.

### addImport

    public void addImport(java.lang.String name)

  Adds a package/script to be automatically imported by the script. Each import is the equivalent of adding the following javascript: `

       package name;

  `
  Parameters:
    `name` - package or script name to be automatically imported.

### setFast

    public void setFast(boolean isFast)

  Sets "fast" mode, i.e. JavaScript 2.0. Fast mode lets the compiler make assumptions about types and classes, e.g. that class methods won't change dynamically. This lets the compiler generate more code that directly translates to Java calls.

### setLineMap

    public void setLineMap(com.caucho.java.LineMap lineMap)

  Sets a line number map. For generated files like JSP or XTP, the error messages need an extra translation to get to the original line numbers.

### setClassName

    public void setClassName(java.lang.String name)

  Sets the name of the generated java class. If unset, the parser will mangle the input name.

### setWorkDir

    public void setWorkDir(Path path)

  Sets the directory for generated *.java and *.class files. The parser will check this directory for any precompiled javascript classes. The default work-dir is /tmp/caucho on unix and c:\temp\caucho on windows.
  Parameters:
    `path` - the work directory.

### getWorkDir

    public Path getWorkDir()

  Returns the directory for generated *.java and *.class files.

### parse

    public Script parse(java.lang.String name)
                 throws ESException,
                        java.io.IOException

  Main application parsing method. The parser will try to load the compiled script. If the compiled script exists and the source file has not changed, parse will return the old script. Otherwise, it will parse and compile the javascript.
  Parameters:
    `name` - the name of the javascript source.
  Returns:
    the parsed script

### parse

    public Script parse(ReadStream is)
                 throws ESException,
                        java.io.IOException

  Alternative parsing method when the application only has an open stream to the file. Since this method will always compile a new script, it can be significantly slower than the `parse(String)` method.
  Parameters:
    `is` - a read stream to the javascript source.
  Returns:
    the parsed script.

### parse

    public Script parse(ReadStream is,
                        java.lang.String name,
                        int line)
                 throws ESException,
                        java.io.IOException

  An alternative parsing method given an open stream, a filename and a line number.
  Parameters:
    `is` - a stream to the javascript source.
    `name` - filename to use for error messages.
    `line` - initial line number.
  Returns:
    the compiled script

### parseEval

    public Script parseEval(ReadStream is,
                            java.lang.String name,
                            int line)
                     throws ESException,
                            java.io.IOException

  Parses a script for the JavaScript "eval" expression. The semantics for "eval" are slightly different from standard scripts.
  Parameters:
    `is` - stream to the eval source.
    `name` - filename to use for error messages
    `line` - initial line number to use for error messages.
  Returns:
    the compiled script.

### getFilename

    public java.lang.String getFilename()

  Returns the current filename being parsed.

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD