Class AbstractJessopScriptBuilder

java.lang.Object
com.randomnoun.common.jessop.AbstractJessopScriptBuilder
All Implemented Interfaces:
JessopScriptBuilder
Direct Known Subclasses:
JavaJessopScriptBuilder, JavascriptJessopScriptBuilder, LispJessopScriptBuilder, LuaJessopScriptBuilder, Python2JessopScriptBuilder, RubyJessopScriptBuilder

public abstract class AbstractJessopScriptBuilder extends Object implements JessopScriptBuilder
This is an abstract class that supports generic support for creating template scripts from jessop source.

This class is responsible for processing jessop declarations (e.g. <%@ jessop language="javascript" engine="rhino" %>), and switching to the correct language JessopScriptBuilder implementation.

Note that having multiple languages in the same script file is not yet supported by jessop. The declaration (if it exists) should therefore only appear once, and be the first thing that appears in a jessop source file.

If the declaration is missing, then the default JavascriptJessopScriptBuilder is used, using the 'rhino' engine.

Author:
knoxg
  • Field Details

  • Constructor Details

  • Method Details

    • setPrintWriter

      public void setPrintWriter(PrintWriter pw)
      Description copied from interface: JessopScriptBuilder
      Sets the printWriter that this class will write to during emit() methods
      Specified by:
      setPrintWriter in interface JessopScriptBuilder
      Parameters:
      pw - the printWriter that this class will write to during emit() methods
    • setTokeniserAndDeclarations

      Description copied from interface: JessopScriptBuilder
      Sets the tokeniser and declaration that are in effect at the start of parsing. This allows this ScriptBuilder to switch ScriptBuilders.
      Specified by:
      setTokeniserAndDeclarations in interface JessopScriptBuilder
      Parameters:
      t - tokeniser that is processing the jessop source
      declarations - jessop declarations that are in effect
    • getDeclarations

      Description copied from interface: JessopScriptBuilder
      Returns the value of any jessop declarations that are in effect.

      Used to maintain declarations when switching JessopScriptBuilders, and to retrieve declarations after the script is built.

      Specified by:
      getDeclarations in interface JessopScriptBuilder
    • emitDeclaration

      public void emitDeclaration(int line, String s) throws ScriptException
      Description copied from interface: JessopScriptBuilder
      Called by the tokeniser and requests that this JessopScriptBuilder process a <%@ ... %> directive.

      The exact semantics of these declarations can be defined per-language, but 'jessop' declarations should be processed by the AbstractJessopScriptBuilder, i.e. declarations that are in the form <%@ jessop language="xxx" %>

      Ideally, there's only one declaration, at the top of the source script.

      Specified by:
      emitDeclaration in interface JessopScriptBuilder
      Parameters:
      line - jessop source line number that begins this directive
      s - the contents of the <%@ ... %> directive
      Throws:
      ScriptException
    • emitText

      public abstract void emitText(int line, String s)
      Description copied from interface: JessopScriptBuilder
      Called by the tokeniser and requests that this JessopScriptBuilder generate code to generate some text output.

      This method should escape the output and generate code that outputs this text in the implementation language.

      Specified by:
      emitText in interface JessopScriptBuilder
      Parameters:
      line - jessop source line number that begins this text output
      s - the text to output; may include newlines.
    • emitExpression

      public abstract void emitExpression(int line, String s)
      Description copied from interface: JessopScriptBuilder
      Called by the tokeniser and requests that this JessopScriptBuilder generate code to generate some evaluated output. i.e. process a <%= ... %> expression.

      This method should generate the code to evaluate and output the contents of the <%= ... %> expression.

      This method should attempt to preserve line numbers between the jessop source file and the generated code file.

      Specified by:
      emitExpression in interface JessopScriptBuilder
      Parameters:
      line - jessop source line number that begins this text output.
      s - the contents of the <%= ... %> expression.
    • emitScriptlet

      public abstract void emitScriptlet(int line, String s)
      Description copied from interface: JessopScriptBuilder
      Called by the tokeniser and requests that this JessopScriptBuilder copy the included code into the generated script. i.e. process a <% ... %> scriptlet. (The 'scriptlet' term is the term used in the JSP specification)

      This method should generate the same code that is contained within the <% ... %> scriptlet.

      This method should attempt to preserve line numbers between the jessop source file and the generated code file.

      This scriptlet may not be syntactically correct by itself and may require further scriptlets to function, e.g.

       <% if (a > b) { %>
       something
       <% } %>
       
      Specified by:
      emitScriptlet in interface JessopScriptBuilder
      Parameters:
      line - jessop source line number that begins this scriptlet.
      s - the contents of the <%= ... %> expression.
    • getDefaultExceptionConverterClassName

      Description copied from interface: JessopScriptBuilder
      Returns the name of the default exceptionConverter class that should be used for the default script engine, or null if no converter is required.

      Changing the engine in the jessop declaration will reset the the converter to null.

      Specified by:
      getDefaultExceptionConverterClassName in interface JessopScriptBuilder
      Returns:
      the name of the default exceptionConverter class that should be used for the default script engine
    • getDefaultBindingsConverterClassName

      Description copied from interface: JessopScriptBuilder
      Returns the name of the default bindingsConverter class that should be used for the default script engine, or null if no converter is required.

      Changing the engine in the jessop declaration will reset the the converter to null.

      Specified by:
      getDefaultBindingsConverterClassName in interface JessopScriptBuilder
      Returns:
      the name of the default bindingsConverter class that should be used for the default script engine
    • suppressEol

      protected String suppressEol(String s, boolean suppressEol)
      Conditionally remove the first newline from the supplied string.

      This method is used to perform suppressEol declaration processing.

      When the suppressEol declaration is true, and the text to be emitted by the output script immediately follows a scriptlet and begins with a newline (or whitespace followed by a newline), then we want to remove that (whitespace and) newline.

      If there are non-whitespace characters before the first newline, then it is not suppressed.

      Parameters:
      s - text which is to be emitted by the output script
      suppressEol - if true, remove the beginning whitespace and newline, if it exists.
      Returns:
      the supplied string, with the first newline conditionally removed