de.enough.polish.preprocess
Class Preprocessor

java.lang.Object
  extended by de.enough.polish.preprocess.Preprocessor

public class Preprocessor
extends Object

Preprocesses source code.

Copyright Enough Software 2004 - 2012

Author:
Robert Virkus, robert@enough.de

Field Summary
static int CHANGED
          A value indicating that the file has been changed.
static Pattern DIRECTIVE_PATTERN
           
static String ENVIRONMENT_KEY
           
static int NOT_CHANGED
          A value indicating that the file has not been changed.
static int SKIP_FILE
          A value indicating that the file should be skipped altogether.
static int SKIP_REST
          A value indicating that the rest of the current file should not be preprocessed.
protected static Pattern SYSTEM_PRINT_PATTERN
           
 
Constructor Summary
Preprocessor(PolishProject project, Environment environment, File destinationDir, boolean backup, boolean indent, boolean replacePropertiesWithoutDirective, String newExt)
          Creates a new Preprocessor - usually for a specific device or a device group.
 
Method Summary
 void addCustomPreprocessor(CustomPreprocessor preprocessor)
          Adds a custom preprocesor to this preprocessor.
 void addSymbol(String name)
          Deprecated. use environment.addSymbol() instead
 void addToPreprocessQueue(String fileName)
          Adds the file to the preprocessing queue
 void addVariable(String name, String value)
          Deprecated. use environment.addVariable() instead
 void addVariables(Map<String,String> additionalVars)
          Deprecated. use environment.addVariables() instead
protected  boolean checkIfCondition(String argument, String className, StringList lines)
          Checks whether an if-condition is true.
 void clearCustomPreprocessors()
           
static boolean containsDirective(String line)
          Checks if the given line contains a directive.
 BooleanEvaluator getBooleanEvaluator()
          Deprecated. use environment.getBooleanEvaluator() instead
 CssAttributesManager getCssAttributesManager()
           
 Environment getEnvironment()
           
protected  String getErrorStart(String className, StringList lines)
          Creates the start of an error message:
 StyleSheet getStyleSheet()
          Retrieves the style sheet.
 TextFileManager getTextFileManager()
           
 String getVariable(String name)
          Deprecated. use environment.getVariable() instead
 Map<String,String> getVariables()
          Deprecated. use environment.getVariable() instead
 boolean hasSymbol(String symbol)
          Deprecated. use environment.hasSymbol() instead
 boolean isInPreprocessQueue(String fileName)
          Determines whether the given file is in the preprocess queue.
 void notifyDevice(Device device, boolean usesPolishGui)
          Notifies this preprocessor about a new device for which code is preprocessed.
 void notifyDeviceEnd(Device device, boolean usesPolishGui)
          Notifies this preprocessor about the end of preprocessing for the given device.
 void notifyLocale(Locale locale)
          Notifies this preprocessor about a new locale for which code is preprocessed.
 void notifyPolishPackageStart()
          Notifies the processor that from now on source code from the J2ME Polish package is processed.
 boolean preprocess(File sourceDir, String fileName)
          Preprocesses the given file and saves it to the destination directory.
 int preprocess(String className, StringList lines)
          Preprocesses the given source code.
 String[] preprocess(String resourceName, StringList list, boolean removePreprocessingComments)
          Preprocesses the given string array.
protected  int processSingleLine(String className, StringList lines, String line, String trimmedLine)
          Checks a single line for a preprocessing directive.
 void removeFromPreprocessQueue(String fileName)
          Removes the file from the preprocessing queue
 void removeSymbol(String name)
          Deprecated. use environment.removeSymbol() instead
 void removeVariable(String name)
          Deprecated. use environment.removeVariable() instead
 boolean replacePropertiesWithoutDirective()
          Determines whether properties should be replaced without using the //#= directive.
 void reset()
          Resets this preprocessor.
 void setCssAttributesManager(CssAttributesManager manager)
           
 void setCustomPreprocessors(CustomPreprocessor[] customPreprocessors)
          Sets the custom preprocessors which allow a finer grained control of the preprocessing phase.
 void setReplacePropertiesWithoutDirective(boolean replacePropertiesWithoutDirective)
          Specifies whether properties should be replaced without using the //#= directive.
 void setSyleSheet(StyleSheet styleSheet, Device device)
          Sets the style sheet.
 void setSymbols(HashMap<String,Boolean> symbols)
          Deprecated. use Environment for such settings now
 void setTargetDir(String path)
          Sets the direcotry to which the preprocessed files should be copied to.
 void setTextFileManager(TextFileManager textFileManager)
           
 void setUsePolishGui(boolean usePolishGui)
          Deprecated. use environment add/removeSymbol now
 void setVariables(HashMap<String,String> variables)
          Deprecated. use Environment for such settings now
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHANGED

public static final int CHANGED
A value indicating that the file has been changed. CHANGED has the value 2.

See Also:
Constant Field Values

NOT_CHANGED

public static final int NOT_CHANGED
A value indicating that the file has not been changed. NOT_CHANGED has the value 4.

See Also:
Constant Field Values

SKIP_FILE

public static final int SKIP_FILE
A value indicating that the file should be skipped altogether. This can be when e.g. a MIDP2-emulating class should not be copied to a MIDP2-system. An example are the de.enough.polish.ui.game-classes. SKIP_FILE has the value 8.

See Also:
Constant Field Values

SKIP_REST

public static final int SKIP_REST
A value indicating that the rest of the current file should not be preprocessed. SKIP_REST has the value 16.

See Also:
Constant Field Values

DIRECTIVE_PATTERN

public static final Pattern DIRECTIVE_PATTERN

SYSTEM_PRINT_PATTERN

protected static final Pattern SYSTEM_PRINT_PATTERN

ENVIRONMENT_KEY

public static final String ENVIRONMENT_KEY
See Also:
Constant Field Values
Constructor Detail

Preprocessor

public Preprocessor(PolishProject project,
                    Environment environment,
                    File destinationDir,
                    boolean backup,
                    boolean indent,
                    boolean replacePropertiesWithoutDirective,
                    String newExt)
Creates a new Preprocessor - usually for a specific device or a device group.

Parameters:
project - the project settings
environment - environment settings
destinationDir - the destination directory for the preprocessed files
backup - true when the found source files should be backuped
indent - true when comments should be intended
replacePropertiesWithoutDirective - true when ${name} properties should be replaced without using the //#= directive
newExt - the new extension for preprocessed files
Method Detail

setCustomPreprocessors

public void setCustomPreprocessors(CustomPreprocessor[] customPreprocessors)
Sets the custom preprocessors which allow a finer grained control of the preprocessing phase.

Parameters:
customPreprocessors - an array of custom preprocessors

addCustomPreprocessor

public void addCustomPreprocessor(CustomPreprocessor preprocessor)
Adds a custom preprocesor to this preprocessor.

Parameters:
preprocessor - the additional preprocessor

clearCustomPreprocessors

public void clearCustomPreprocessors()

notifyPolishPackageStart

public void notifyPolishPackageStart()
Notifies the processor that from now on source code from the J2ME Polish package is processed. This will last until the notifyDevice(...)-method is called.


notifyDevice

public void notifyDevice(Device device,
                         boolean usesPolishGui)
Notifies this preprocessor about a new device for which code is preprocessed.

Parameters:
device - the new device
usesPolishGui - true when the J2ME Polish GUI is used for the new device

notifyLocale

public void notifyLocale(Locale locale)
Notifies this preprocessor about a new locale for which code is preprocessed.

Parameters:
locale - the new locale, can be null

notifyDeviceEnd

public void notifyDeviceEnd(Device device,
                            boolean usesPolishGui)
Notifies this preprocessor about the end of preprocessing for the given device.

Parameters:
device - the new device
usesPolishGui - true when the J2ME Polish GUI is used for the new device

setTargetDir

public void setTargetDir(String path)
Sets the direcotry to which the preprocessed files should be copied to.

Parameters:
path - The target path.

setSymbols

public void setSymbols(HashMap<String,Boolean> symbols)
Deprecated. use Environment for such settings now

Sets the symbols. Any old settings will be discarded.

Parameters:
symbols - All new symbols, defined in a HashMap.
Throws:
BuildException - when an invalid symbol is defined (currently only "false" is checked);

setUsePolishGui

public void setUsePolishGui(boolean usePolishGui)
Deprecated. use environment add/removeSymbol now

Turns the support for the J2ME Polish GUI on or off.

Parameters:
usePolishGui - true when the GUI is supported, false otherwise

addSymbol

public void addSymbol(String name)
Deprecated. use environment.addSymbol() instead

Adds a single symbol to the list.

Parameters:
name - The name of the symbol.

removeSymbol

public void removeSymbol(String name)
Deprecated. use environment.removeSymbol() instead

Removes a symbol from the list of defined symbols.

Parameters:
name - The name of the symbol.

setVariables

public void setVariables(HashMap<String,String> variables)
Deprecated. use Environment for such settings now

Sets the variables, any old settings will be lost.

Parameters:
variables - the variables.

addVariable

public void addVariable(String name,
                        String value)
Deprecated. use environment.addVariable() instead

Adds a variable to the list of existing variables. When a variable with the given name already exists, it will be overwritten.

Parameters:
name - The name of the variable.
value - The value of the variable.

removeVariable

public void removeVariable(String name)
Deprecated. use environment.removeVariable() instead

Removes a variable from this preprocessor

Parameters:
name - the variable name

addVariables

public void addVariables(Map<String,String> additionalVars)
Deprecated. use environment.addVariables() instead

Adds all the variables to the existing variables. When a variable already exists, it will be overwritten.

Parameters:
additionalVars - A map of additional variables.

getBooleanEvaluator

public BooleanEvaluator getBooleanEvaluator()
Deprecated. use environment.getBooleanEvaluator() instead

Retrieves the evaluator for boolean expressions

Returns:
the boolean evaluator with the symbols and variables of the current device
See Also:
Environment.getBooleanEvaluator()

preprocess

public boolean preprocess(File sourceDir,
                          String fileName)
                   throws FileNotFoundException,
                          IOException,
                          BuildException
Preprocesses the given file and saves it to the destination directory.

Parameters:
sourceDir - the directory containing the source file
fileName - the name (and path)of the source file
Returns:
true when the file was preprocessed or changed
Throws:
FileNotFoundException - when the file was not found
IOException - when the file could not be read or written
BuildException - when the preprocessing fails

reset

public void reset()
Resets this preprocessor. The internal state is reset to allow new preprocessing of other files.


preprocess

public String[] preprocess(String resourceName,
                           StringList list,
                           boolean removePreprocessingComments)
Preprocesses the given string array.

Parameters:
resourceName - the name of the resource file.
list - a list of strings the text.
Returns:
the preprocessed text.
Throws:
BuildException - when the preprocessing fails.

preprocess

public int preprocess(String className,
                      StringList lines)
               throws BuildException
Preprocesses the given source code.

Parameters:
className - the name of the source file.
lines - the source code. Changes are made directly in the code.
Returns:
true when changes were made.
Throws:
BuildException - when the preprocessing fails.

processSingleLine

protected int processSingleLine(String className,
                                StringList lines,
                                String line,
                                String trimmedLine)
                         throws BuildException
Checks a single line for a preprocessing directive.

Parameters:
className - the name of the source file
lines - the source code lines
line - the specific line
trimmedLine - the same line but without spaces at the beginning or end
Returns:
either NOT_PROCESSED when no first level directive was found, CHANGED when a directive was found and changes were made or DIRECTIVE_FOUND when a valid first level directive was found but no changes were made
Throws:
BuildException - when there was a syntax error in the directives

checkIfCondition

protected boolean checkIfCondition(String argument,
                                   String className,
                                   StringList lines)
                            throws BuildException
Checks whether an if-condition is true.

Parameters:
argument - the if-expression e.g. "(symbol1 || symbol2) && symbol3"
className - the name of the file
lines - the list of source code
Returns:
true when the argument results in true
Throws:
BuildException - when there is an syntax error in the expression

containsDirective

public static final boolean containsDirective(String line)
Checks if the given line contains a directive.

Parameters:
line - the line which should be tested
Returns:
true when the given line includes a preprocessing directive.

setSyleSheet

public void setSyleSheet(StyleSheet styleSheet,
                         Device device)
Sets the style sheet.

Parameters:
device - the current device
styleSheet - the new style sheet

getStyleSheet

public StyleSheet getStyleSheet()
Retrieves the style sheet.

Returns:
the style sheet.

hasSymbol

public boolean hasSymbol(String symbol)
Deprecated. use environment.hasSymbol() instead

Determines whether the given symbol is defined.

Parameters:
symbol - the symbol
Returns:
true when the symbol is defined

getVariable

public String getVariable(String name)
Deprecated. use environment.getVariable() instead

Retrieves the value of a variable.

Parameters:
name - the name of the variable
Returns:
the value of the variable

isInPreprocessQueue

public boolean isInPreprocessQueue(String fileName)
Determines whether the given file is in the preprocess queue.

Parameters:
fileName - the name of the file, e.g. "de/enough/polish/ui/Screen.java"
Returns:
true when the given file is in the queue

addToPreprocessQueue

public void addToPreprocessQueue(String fileName)
Adds the file to the preprocessing queue

Parameters:
fileName - the name of the file, e.g. de/enough/polish/ui/Screen.java

removeFromPreprocessQueue

public void removeFromPreprocessQueue(String fileName)
Removes the file from the preprocessing queue

Parameters:
fileName - the name of the file

getVariables

public Map<String,String> getVariables()
Deprecated. use environment.getVariable() instead

Retrieves all defined variables. Changes take effect on the preprocessor.

Returns:
all defined variables

getErrorStart

protected String getErrorStart(String className,
                               StringList lines)
Creates the start of an error message:

Parameters:
className - the name of the current class
lines - the source code of that class
Returns:
a typical error-message start like "MyClass.java line [12]: "

setTextFileManager

public void setTextFileManager(TextFileManager textFileManager)

getTextFileManager

public TextFileManager getTextFileManager()

setCssAttributesManager

public void setCssAttributesManager(CssAttributesManager manager)
Parameters:
manager -

getCssAttributesManager

public CssAttributesManager getCssAttributesManager()

getEnvironment

public Environment getEnvironment()

replacePropertiesWithoutDirective

public boolean replacePropertiesWithoutDirective()
Determines whether properties should be replaced without using the //#= directive.

Returns:
Returns true when properties should be replaced without using the //#= directive.

setReplacePropertiesWithoutDirective

public void setReplacePropertiesWithoutDirective(boolean replacePropertiesWithoutDirective)
Specifies whether properties should be replaced without using the //#= directive.

Parameters:
replacePropertiesWithoutDirective - true when properties should be replaced without using the //#= directive.