Top Description Fields Constructors Methods
org.hsqldb.util.preprocessor

public Class Preprocessor

extends Object
Class Inheritance
Imports
java.io.File, .IOException, .UnsupportedEncodingException, java.util.Stack

Simple text document preprocessor.

Aims specifically at transforming the HSQLDB codebase to one of a small number of specific build targets, while keeping complexity and external dependencies to a minimum, yet providing an environment that is sufficiently powerful to solve most easily imaginable preprocessing scenarios. Supports the following (case-sensitive) directives:

where BOOLEXPR is:
( IDENT
| IDENT ( EQ |  LT | LTE | GT | GTE ) VALUE
| BOOLEXPR { OR | XOR | AND } BOOLEXPR
| NOT BOOLEXPR
| LPAREN BOOLEXPR RPAREN )
and VALUE is :
( STRING
| NUMBER
| IDENT )
and lexicographic elements are :
ASSIGN     : '='
EQ         : '=='
LT         : '<'
LTE        : '<='
GT         : '>'
GTE        : '>='
OR         : ('|' | '||')
XOR        : '^'
AND        : ('&' | '&&')
NOT        : '!'
DQUOTE     : '"'
LPAREN     : '('
RPAREN     : ')'
DOT        : '.'
DIGIT      : ['0'..'9']
EOL        : ('\n' | '\r' | '\n\r')
SPACE      : (' ' | '\t')
NON_DQUOTE : { ANY_UNICODE_CHARACTER_EXCEPT_DQUOTE_OR_EOL } -- see the unicode spec
NON_SPACE  : { ANY_UNICODE_CHARACTER_EXCEPT_SPACE_OR_EOL }  -- see the unicode spec
WS         : { JAVA_WS } -- see java.lang.Character
NON_WS     : { ANY_UNICODE_CHARACTER_EXCEPT_WS_OR_EOL }
STRING     : DQUOTE NON_DQUOTE* DQUOTE
NUMBER     : DIGIT+ (DOT DIGIT*)?
IDENT      : JAVA_IDENT_START JAVA_IDENT_PART*              -- see java.lang.Character
FILEPATH   : NON_SPACE (ANY_UNICODE_CHARACTER* NON_WS)?     -- i.e. trailing SPACE elements are ignored
The lexicographic definitions above use the BNF conventions :
'?' : zero or one
'*' : zero or more
'+' : one or more
Directives may be arbitrarily indented; there is an option (INDENT) to set or unset directive indentation on output. There is also an option (FILTER) to remove directive lines from output. See Option for other preprocessor options.

'//#ifxxx' directives may be nested to arbitrary depth, may be chained with an arbitrary number of '//#elifxxx' directives, may be optionally followed by a single '//#else' directive, and must be terminated by a single '//#endif' directive.

Each '//#include' directive must be terminated by an '//#endinclude' directive; lines between '//#include' and '//#endinclude' are replaced by the content retrieved from the specified FILEPATH.

Included files are preprocessed in a nested scope that inherits the defined symbols of the including scope. Directive lines in included files are always excluded from output.

Design Notes

There are many better/more sophisticated preprocessors/templating engines out there. FreeMaker and Velocity come to mind immediately. Another--the NetBeans MIDP preprocessor--was the direct inspiration for this class.

Other options were rejected because the work of creating this class appeared to be less than dealing with the complexity and dependency issues of hooking up to external libraries. The NetBeans preprocessor, in particular, was rejected because it was not immediately evident how to invoke it independently from the IDE, how to make it available to non-MIDP projects from within the IDE or how to isolate the correct OpenIDE jars to allow stand-alone operation.

Author
Campbell Burnet (campbell-burnet@users dot sourceforge.net)
Since
1.8.1
Version
2.7.0

Field Summary

Modifier and TypeField and Description
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
private final Defines
private final Document
private final String
private final String
private final int
private final IResolver
private final Stack<E>
private int
pack-priv static final Integer[]

Constructor Summary

AccessConstructor and Description
private
Preprocessor(String documentPath, String encoding, int options, IResolver resolver, String predefined)

private

Method Summary

Modifier and TypeMethod and Description
private boolean
private void
private Document
private static void
log(Object toLog)

private boolean
public static void
preprocessBatch(File
under which input files are located
sourceDir
,
File
under which output files are to be written
targetDir
,
String[]
to be preprocessed
fileNames
,
String
to use for output file names
altExt
,
String
with which to write output files
encoding
,
int
used to control preprocessing
options
,
String
CSV list of symbol definition expressions
defines
,
IResolver
with which to perform property and path expansions
resolver
)

Preprocesses the specified list of files.

public static void
preprocessFile(File
under which the input file is located
sourceDir
,
File
under which the output file is to be written
targetDir
,
String
to be preprocessed
fileName
,
String
to use for output file name
altExt
,
String
with which to write output file
encoding
,
int
used to control preprocessing
options
,
String
CSV list of symbol definition expressions
defines
,
IResolver
with which to perform property and path expansions
resolver
)

Preprocesses a single file.

private void
private void
private void
private void
processElseIf(boolean condition)

private void
private void
processIf(boolean condition)

private int
processInclude(int lineCount, Line line)

private Line
private String
private void
private void
private void
private File
private String
toSourceLine(final Line line)

private static String
private static String
translatePath(File dir, String fileName, String ext)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONDITION_ARMEDback to summary
pack-priv static final int CONDITION_ARMED
CONDITION_IN_TRUEback to summary
pack-priv static final int CONDITION_IN_TRUE
CONDITION_NONEback to summary
pack-priv static final int CONDITION_NONE
CONDITION_TRIGGEREDback to summary
pack-priv static final int CONDITION_TRIGGERED
definesback to summary
private final Defines defines
documentback to summary
private final Document document
documentPathback to summary
private final String documentPath
encodingback to summary
private final String encoding
optionsback to summary
private final int options
resolverback to summary
private final IResolver resolver
stackback to summary
private final Stack<E> stack
stateback to summary
private int state
STATESback to summary
pack-priv static final Integer[] STATES
Annotations
@SuppressWarnings:UnnecessaryBoxing

Constructor Detail

Preprocessorback to summary
private Preprocessor(String documentPath, String encoding, int options, IResolver resolver, String predefined) throws PreprocessorException
Preprocessorback to summary
private Preprocessor(Preprocessor other, Document include)

Method Detail

isHidingLinesback to summary
private boolean isHidingLines()
loadDocumentback to summary
private void loadDocument() throws PreprocessorException
loadIncludeback to summary
private Document loadInclude(String path) throws PreprocessorException
logback to summary
private static void log(Object toLog)
preprocessback to summary
private boolean preprocess() throws PreprocessorException
preprocessBatchback to summary
public static void preprocessBatch(File sourceDir, File targetDir, String[] fileNames, String altExt, String encoding, int options, String defines, IResolver resolver) throws PreprocessorException

Preprocesses the specified list of files.

Parameters
sourceDir:File

under which input files are located

targetDir:File

under which output files are to be written

fileNames:String[]

to be preprocessed

altExt:String

to use for output file names

encoding:String

with which to write output files

options:int

used to control preprocessing

defines:String

CSV list of symbol definition expressions

resolver:IResolver

with which to perform property and path expansions

Exceptions
PreprocessorException:
if an error occurs while loading, preprocessing or saving the result of preprocessing one of the specified input files
preprocessFileback to summary
public static void preprocessFile(File sourceDir, File targetDir, String fileName, String altExt, String encoding, int options, String defines, IResolver resolver) throws PreprocessorException

Preprocesses a single file.

Parameters
sourceDir:File

under which the input file is located

targetDir:File

under which the output file is to be written

fileName:String

to be preprocessed

altExt:String

to use for output file name

encoding:String

with which to write output file

options:int

used to control preprocessing

defines:String

CSV list of symbol definition expressions

resolver:IResolver

with which to perform property and path expansions

Exceptions
PreprocessorException:
if an error occurs while loading, preprocessing or saving the result of preprocessing the specified input file
preprocessImplback to summary
private void preprocessImpl() throws PreprocessorException
processDirectiveback to summary
private void processDirective(Line line) throws PreprocessorException
processElseback to summary
private void processElse() throws PreprocessorException
processElseIfback to summary
private void processElseIf(boolean condition) throws PreprocessorException
processEndIfback to summary
private void processEndIf() throws PreprocessorException
processIfback to summary
private void processIf(boolean condition)
processIncludeback to summary
private int processInclude(int lineCount, Line line) throws PreprocessorException
resolveLineback to summary
private Line resolveLine(String line) throws PreprocessorException
resolvePathback to summary
private String resolvePath(String path)
saveDocumentback to summary
private void saveDocument(Object target) throws PreprocessorException
statePopback to summary
private void statePop()
Annotations
@SuppressWarnings:UnnecessaryUnboxing
statePushback to summary
private void statePush()
Annotations
@SuppressWarnings:unchecked
toCanonicalOrAbsoluteFileback to summary
private File toCanonicalOrAbsoluteFile(String path)
toSourceLineback to summary
private String toSourceLine(final Line line)
translateFileExtensionback to summary
private static String translateFileExtension(String fileName, String ext)
translatePathback to summary
private static String translatePath(File dir, String fileName, String ext)