org.wurbelizer
Class AbstractWurbiler

java.lang.Object
  extended by org.wurbelizer.AbstractWurbiler
All Implemented Interfaces:
Wurbiler
Direct Known Subclasses:
DefaultWurbiler

public class AbstractWurbiler
extends Object
implements Wurbiler

Provides basic functionality of a wurblet compiler. Translates an input stream (usually a file with extension *.wrbl) into a an output stream and a fixed source list. The generated java-output implements the interface Wurblet. The fixed source part of the wurblet is stored in a List of Strings which usually is stored in serialized form in a separate file and will be deserialized by the wurbler when running the wurblet.

By default the input stream starts at the source-level. The backslash is recognized as wurblet-level is entered via @[ and left via ]@. Wurblet-levels again may contain other wurblet-levels, i.e. they may be nested resulting in a wurblet generating a wurblet which in turn generates java source or even another wurblet.

As a shorthand for @[out.print(object);]@ the wurbiler understands the notation @(object)@. This may be even used in a string (e.g.: "This is line @(line)@\n");).

The backslash '\' is recognized as an escape char. If followed by a '@', the backslash will be consumed and the @ passed to the source. Other pairs of escapes will be passed unprocessed (e.g. '\n' or whatever).

Commands may also be embedded at the source-level via @{command par1 par2 ... parN}@. The following commands are supported:

@{include <pathname>}@: includes a file in the current input stream. Includes may be nested to any depth. Loops are detected. the include pathname allows $-variables that will be replaced at compile time.

@{package <packagename>}@: sets the package-name for the generated class.

@{import <classpath>}@: adds to the extra imports.

@{extends <parentclass>}@ sets the parent-class (default is AbstractWurblet).

@{implements <if1> ... <ifN>}@ implements interfaces.

@{indent <spaces>}@: sets the fixed source indentation. "auto" enables autoindent., i.e. @{indent auto}@. Autoindent is the default.

@{args <arg1> <arg2> ...}@: presets the wurblet arguments (default is from container, i.e. wurbler) notice that @{args}@ without any arguments enables container args again. This is especially useful for @{including} serveral wurblets in a single source-file. Thus, if the wurblet needs args, it's a good practice to include an @{args}-line in the wurblet-code.

@{to <outputstream>}@: sets the output stream (this is the stream the wurblet will write the generated code to, not the output writer of the compiled wurblet!). @{to out} resets to the default stream, which is equivalent to @{to}.


Constructor Summary
AbstractWurbiler(Reader in, String inName, Writer out, List<String> sourceList)
          Creates a compiler instance.
 
Method Summary
 boolean addInterface(String iface)
          Adds an interface.
 int compile()
          Compiles the wurbelizable.
 String[] getArgs()
          Gets the presetted wurblet arguments.
 String[] getImports()
          Gets the list of imports.
 int getIndent()
          Gets the source indentation.
 Collection<String> getInterfaces()
          Gets the interface list.
 Logger getLogger()
          Gets the logger for warnings and errors.
 String getOutName()
          Gets the outname.
 String getPackageName()
          Gets the package name.
 String getParentClass()
          Gets the name of the parent class.
 Reader getReader()
          Gets the input reader.
 List<String> getSourceList()
          Gets the list of fixed source-strings.
 Writer getWriter()
          Gets the output writer.
 boolean isAutoIndent()
          Gets the autoindent feature.
 void logError(String msg)
          Logs an error message.
 void logWarning(String msg)
          Logs a warning.
 void setArgs(String[] args)
          Presets the wurblet arguments.
 void setAutoIndent(boolean autoIndent)
          Sets the autoindent feature.
 void setImports(String[] imps)
          Sets the imports.
 void setIndent(int indent)
          Sets the indentation for code generating the source.
 void setLogger(Logger logger)
          Sets the logger for warnings and errors.
 void setOutName()
          Sets outname to the default.
 void setOutName(String outName)
          Sets the name of the output stream in the generated source.
 void setPackageName(String packageName)
          Sets the package-name.
 void setParentClass(String parentClass)
          Sets the parent class.
 void setSourceList(List<String> sourceList)
          Presets the source list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractWurbiler

public AbstractWurbiler(Reader in,
                        String inName,
                        Writer out,
                        List<String> sourceList)
Creates a compiler instance.

Parameters:
in - the input stream
inName - the name of the input stream (for diagnostics only)
out - the output stream
sourceList - the fixed source list (null to create it)
Method Detail

getWriter

public Writer getWriter()
Gets the output writer.

Returns:
the output writer

getReader

public Reader getReader()
Gets the input reader.

Returns:
the input reader

compile

public int compile()
            throws IOException
Compiles the wurbelizable.

Specified by:
compile in interface Wurbiler
Returns:
the number of errors, 0 = no errors (warnings are not counted)
Throws:
IOException - if the compiled output could not be created

getSourceList

public List<String> getSourceList()
Gets the list of fixed source-strings.

Specified by:
getSourceList in interface Wurbiler
Returns:
the list of strings

setSourceList

public void setSourceList(List<String> sourceList)
Presets the source list.

Specified by:
setSourceList in interface Wurbiler
Parameters:
sourceList - the source list, null if compile() should initialize it

getIndent

public int getIndent()
Gets the source indentation.

Specified by:
getIndent in interface Wurbiler
Returns:
the current indentation

setIndent

public void setIndent(int indent)
Sets the indentation for code generating the source.

Specified by:
setIndent in interface Wurbiler
Parameters:
indent - the new indentation

setAutoIndent

public void setAutoIndent(boolean autoIndent)
Sets the autoindent feature.

Specified by:
setAutoIndent in interface Wurbiler
Parameters:
autoIndent - true to enable autoindent, false to disable.

isAutoIndent

public boolean isAutoIndent()
Gets the autoindent feature.

Specified by:
isAutoIndent in interface Wurbiler
Returns:
true if autoindent is enabled (default)

setOutName

public void setOutName(String outName)
Sets the name of the output stream in the generated source.

Specified by:
setOutName in interface Wurbiler
Parameters:
outName - the name of the output stream, null sets to the default value "out".

setOutName

public void setOutName()
Sets outname to the default.

Specified by:
setOutName in interface Wurbiler

getOutName

public String getOutName()
Gets the outname.

Specified by:
getOutName in interface Wurbiler
Returns:
the name of the output stream

getLogger

public Logger getLogger()
Gets the logger for warnings and errors.

Specified by:
getLogger in interface Wurbiler
Returns:
the logger.

setLogger

public void setLogger(Logger logger)
Sets the logger for warnings and errors.

Specified by:
setLogger in interface Wurbiler
Parameters:
logger - the logger.

logError

public void logError(String msg)
Logs an error message.

Parameters:
msg - the error message

logWarning

public void logWarning(String msg)
Logs a warning.

Parameters:
msg - the warning message

getPackageName

public String getPackageName()
Gets the package name.

Specified by:
getPackageName in interface Wurbiler
Returns:
the package name, null if default package

setPackageName

public void setPackageName(String packageName)
Sets the package-name.

Specified by:
setPackageName in interface Wurbiler
Parameters:
packageName - the package name.

getParentClass

public String getParentClass()
Gets the name of the parent class.

Specified by:
getParentClass in interface Wurbiler
Returns:
the name of the parent class, null if default

setParentClass

public void setParentClass(String parentClass)
Sets the parent class.

Specified by:
setParentClass in interface Wurbiler
Parameters:
parentClass - the name of the parent class

addInterface

public boolean addInterface(String iface)
Adds an interface.

Parameters:
iface - the name of the interface
Returns:
true if added, false if already in list of interfaces.

getInterfaces

public Collection<String> getInterfaces()
Gets the interface list.

Returns:
the list of interfaces

getImports

public String[] getImports()
Gets the list of imports.

Specified by:
getImports in interface Wurbiler
Returns:
the imports, null if no imports

setImports

public void setImports(String[] imps)
Sets the imports.

Specified by:
setImports in interface Wurbiler
Parameters:
imps - the imports

getArgs

public String[] getArgs()
Gets the presetted wurblet arguments.

Specified by:
getArgs in interface Wurbiler
Returns:
the wurblet args, null = default

setArgs

public void setArgs(String[] args)
Presets the wurblet arguments. The default (null) means that the args are determined at runtime from the wurbler, i.e. command-line, @wurblet-directive.

Specified by:
setArgs in interface Wurbiler
Parameters:
args - the wurblet args


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de