Package fmpp

Class TemplateEnvironment

java.lang.Object
fmpp.TemplateEnvironment

public class TemplateEnvironment extends Object
The runtime FMPP environment of an executing template.
  • Method Details

    • getCurrentInstance

      public static TemplateEnvironment getCurrentInstance()
      Returns the TemplateEnvironment object used for the template currently being executed by FMPP in the current thread. The return value of the method is undefined if no such template execution is in progress. In practice it means that it can be safely called from a Java method that is (indirectly) invoked by the executing template. For example, in a TemplateTransformModel that is used in the template with <@...>.
    • getFreemarkerEnvironment

      public freemarker.core.Environment getFreemarkerEnvironment()
      Returns the FreeMarker environment currently in use. The FreeMarker environment can be used to set/get variables, among others.
      Throws:
      IllegalStateException - if the FreeMarker environment is not available.
    • getEngine

      public Engine getEngine()
      Returns the FMPP engine object in use.
    • getXmlDocument

      public Object getXmlDocument()
      Returns the processed XML document (pp.doc) as Document. This will return non-null if, and only if the current processing mode is "renderXml".
      See Also:
    • getWrappedXmlDocument

      public freemarker.template.TemplateNodeModel getWrappedXmlDocument()
      The same as getXmlDocument(), but returns the document as NodeModel.
    • getData

      public Object getData(String name)
      Similar to Engine.getData(java.lang.String), but it also sees file processing specific variables (local data).
      Parameters:
      name - the name of the variable.
      Returns:
      the value of the variable, or null if no variable with the given name exists.
    • getSourceFile

      public File getSourceFile()
      Returns the source file.
    • getTemplate

      public freemarker.template.Template getTemplate()
      Retuns the FreeMarker Template object for the source file.
    • getOutputFile

      public File getOutputFile() throws IOException
      Returns the output file. Note that this value can change during the execution of template.
      Throws:
      IOException
    • getHomePath

      public String getHomePath() throws IOException
      Returns URL-style path of the output root relative to the current output file.
      Throws:
      IOException
    • getOutputEncoding

      public String getOutputEncoding() throws IOException
      Returns the output encoding. Note that this value can change during the execution of template.
      Throws:
      IOException
    • changeOutputFile

      public void changeOutputFile(String name) throws IOException
      It does the same as the directive in the pp hash.
      Throws:
      IOException
    • changeOutputFile

      public void changeOutputFile(String name, boolean append) throws IOException
      It does the same as the directive in the pp hash.
      Throws:
      IOException
    • renameOutputFile

      public void renameOutputFile(String name) throws IOException
      It does the same as the directive in the pp hash.
      Throws:
      IOException
    • dropOutputFile

      public void dropOutputFile() throws IOException
      It does the same as the directive in the pp hash.
      Throws:
      IOException
    • restartOutputFile

      public void restartOutputFile() throws IOException
      It does the same as the directive in the pp hash.
      Throws:
      IOException
    • beginNestedOutputFile

      public void beginNestedOutputFile(String name) throws IOException
      It does the same as the begin tag of the corresponding directive of the pp hash.
      Throws:
      IOException
    • beginNestedOutputFile

      public void beginNestedOutputFile(String name, boolean append) throws IOException
      It does the same as the begin tag of the corresponding directive of the pp hash.
      Throws:
      IOException
    • endNestedOutputFile

      public void endNestedOutputFile() throws IOException
      It does the same as the end tag of the corresponding directive of the pp hash.
      Throws:
      IOException
    • setOutputEncoding

      public void setOutputEncoding(String encoding) throws IOException
      It does the same as the directive in the pp hash.
      Throws:
      IOException
    • warning

      public void warning(String message)
      It does the same as the directive in the pp hash.
    • resolveSourcePath

      public File resolveSourcePath(String path) throws IOException
      Resolves a source path to a File object. Use this for your custom transforms that wants the path of a source file as parameter. When it tries to find the file, paths as foo.jpg will be interpreted relatively to the current source file, while paths like /img/foo.jpg will be interpreted relatively to source root directory.

      Note that an IOException will be thrown if the file is outside the source root directory.

      Parameters:
      path - the path in UN*X or native format.
      Returns:
      File object that points to the file.
      Throws:
      IOException
    • resolveOutputPath

      public File resolveOutputPath(String path) throws IOException
      Resolves a output path to a File object. This follows the same logic as resolveSourcePath(java.lang.String), but it uses the output file and the output root directory as appropriate.
      Throws:
      IOException
    • toSourceRootRelativePath

      public String toSourceRootRelativePath(String path) throws IOException
      Returns the path relative to the source root.
      Parameters:
      path - the path in UN*X or native format. The virtual root directory will be the source root, not the real root directory of the host system.
      Returns:
      the source root relative path in UN*X format. It does not start with slash.
      Throws:
      IOException
    • toSourceRootRelativePath

      public String toSourceRootRelativePath(File f) throws IOException
      Converts a file object to a source root relative UN*X style path.
      Throws:
      IOException
    • toOutputRelatitvePath

      public String toOutputRelatitvePath(String path) throws IOException
      Same as toSourceRootRelativePath(String) but with the output file and output root directory.
      Throws:
      IOException
    • toOutputRootRelativePath

      public String toOutputRootRelativePath(File f) throws IOException
      Convets a file object to an output root relative UN*X style path.
      Throws:
      IOException
    • getPathTo

      public String getPathTo(String dst) throws IOException
      Calculates the path of another output file relatively to current output file, in UN*X format.
      Parameters:
      dst - the path of the other output file in UN*X or native format. The (virtual) root directory will be the output root directory, not the real root directory of the host system.
      Returns:
      the path of dst relatively to the current output file, in UN*X format. It never starts with slash. It ends with slash if and only if dst ends with slash, except if the return value would be a single slash then, in which case the result will be an empty string instead.
      Throws:
      IOException
    • getSourcePathTo

      public String getSourcePathTo(String dst) throws IOException
      Same as getPathTo(java.lang.String) but with the source file and source root directory.
      Throws:
      IOException