Class Munge
- java.lang.Object
-
- org.sonatype.plugins.munge.Munge
-
public class Munge extends java.lang.ObjectMunge: a purposely-simple Java preprocessor. It only supports conditional inclusion of source based on defined strings of the form "if[tag]", "if_not[tag]", "else[tag], and "end[tag]". Unlike traditional preprocessors, comments and formatting are all preserved for the included lines. This is on purpose, as the output of Munge will be distributed as human-readable source code.To avoid creating a separate Java dialect, the conditional tags are contained in Java comments. This allows one build to compile the source files without pre-processing, to facilitate faster incremental development. Other builds from the same source have their code contained within that comment. The format of the tags is a little verbose, so that the tags won't accidentally be used by other comment readers such as javadoc. Munge tags must be in C-style comments; C++-style comments may be used to comment code within a comment.
To demonstrate this, our sample source has 1.1 and 1.2-specific code, with 1.1 as the default build:
public void setSystemProperty(String key, String value) { /*if[JDK1.1]*/ Properties props = System.getProperties(); props.setProperty(key, value); System.setProperties(props); /*end[JDK1.1]*//*if[JDK1.2] // Use the new System method. System.setProperty(key, value); end[JDK1.2]*/ }
When the above code is directly compiled, the code bracketed by the JDK1.1 tags will be used. If the file is run through Munge with the JDK1.2 tag defined, the second code block will used instead. This code can also be written as:
Munge also performs text substitution; the Swing build uses this to convert its package references frompublic void setSystemProperty(String key, String value) { /*if[JDK1.2] // Use the new System method. System.setProperty(key, value); else[JDK1.2]*/Properties props = System.getProperties(); props.setProperty(key, value); System.setProperties(props); /*end[JDK1.2]*/ }
javax.swingtojava.awt.swing, for example. This substitution is has no knowledge of Java syntax, so only use it to convert strings which are unambiguous. Substitutions are made in the same order as the arguments are specified, so the first substitution is made over the whole file before the second one, and so on.Munge's command line takes one of the following forms:
java Munge [-D<symbol> ...] [-s <old>=<new> ...] [<in file>] [<out file>] java Munge [-D<symbol> ...] [-s <old>=<new> ...] <file> ... <directory>In the first form, if no output file is given, System.out is used. If neither input nor output file are given, System.in and System.out are used. Munge can also take an
@<cmdfile>argument. If one is specified then the given file is read for additional command line arguments.Like any preprocessor, developers must be careful not to abuse its capabilities so that their code becomes unreadable. Please use it as little as possible.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classMunge.CommandLineThis class was cut and pasted from the JDK1.2 sun.tools.util package.
-
Field Summary
Fields Modifier and Type Field Description (package private) java.lang.Stringblock(package private) intCODE(package private) java.lang.String[]commands(package private) intCOMMENT(package private) intELSE(package private) intEND(package private) intEOF(package private) interrors(package private) intIF(package private) intIF_NOT(package private) java.io.BufferedReaderin(package private) java.lang.StringinName(package private) intline(package private) static java.util.VectornewTextStrings(package private) intnumCommands(package private) static java.util.VectoroldTextStrings(package private) java.io.PrintWriterout(package private) booleanprinting(package private) java.lang.Stringsource(package private) java.util.Stackstack(package private) static java.util.Hashtablesymbols
-
Constructor Summary
Constructors Constructor Description Munge(java.lang.String inName, java.lang.String outName)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()(package private) voidcmd_else()(package private) voidcmd_end()(package private) voidcmd_if(java.lang.String version)(package private) voidcmd_if_not(java.lang.String version)(package private) intcountLines(java.lang.String s)(package private) static intcountQuotes(java.lang.String input)voiderror(java.lang.String text)(package private) static intfindCommentStart(java.lang.String source, int fromIndex)(package private) intgetCommand(java.lang.String s)booleanhasErrors()static voidmain(java.lang.String[] args)Munge's main entry point.(package private) intnextBlock()(package private) voidprint(java.lang.String s)voidprintErrorCount()voidprocess()(package private) voidprocessComment(java.lang.String comment)(package private) voidsubstitute()static voidusage()Report how this utility is used and exit.static voidusage(java.lang.String msg)
-
-
-
Field Detail
-
symbols
static java.util.Hashtable symbols
-
oldTextStrings
static java.util.Vector oldTextStrings
-
newTextStrings
static java.util.Vector newTextStrings
-
errors
int errors
-
line
int line
-
inName
java.lang.String inName
-
in
java.io.BufferedReader in
-
out
java.io.PrintWriter out
-
stack
java.util.Stack stack
-
printing
boolean printing
-
source
java.lang.String source
-
block
java.lang.String block
-
commands
final java.lang.String[] commands
-
IF
final int IF
- See Also:
- Constant Field Values
-
IF_NOT
final int IF_NOT
- See Also:
- Constant Field Values
-
ELSE
final int ELSE
- See Also:
- Constant Field Values
-
END
final int END
- See Also:
- Constant Field Values
-
numCommands
final int numCommands
- See Also:
- Constant Field Values
-
EOF
final int EOF
- See Also:
- Constant Field Values
-
COMMENT
final int COMMENT
- See Also:
- Constant Field Values
-
CODE
final int CODE
- See Also:
- Constant Field Values
-
-
Method Detail
-
getCommand
int getCommand(java.lang.String s)
-
error
public void error(java.lang.String text)
-
printErrorCount
public void printErrorCount()
-
hasErrors
public boolean hasErrors()
-
close
public void close() throws java.io.IOException- Throws:
java.io.IOException
-
cmd_if
void cmd_if(java.lang.String version)
-
cmd_if_not
void cmd_if_not(java.lang.String version)
-
cmd_else
void cmd_else()
-
cmd_end
void cmd_end() throws java.util.EmptyStackException- Throws:
java.util.EmptyStackException
-
print
void print(java.lang.String s) throws java.io.IOException- Throws:
java.io.IOException
-
countLines
int countLines(java.lang.String s)
-
processComment
void processComment(java.lang.String comment) throws java.io.IOException- Throws:
java.io.IOException
-
nextBlock
int nextBlock() throws java.io.IOException- Throws:
java.io.IOException
-
findCommentStart
static int findCommentStart(java.lang.String source, int fromIndex)
-
countQuotes
static int countQuotes(java.lang.String input)
-
substitute
void substitute()
-
process
public void process() throws java.io.IOException- Throws:
java.io.IOException
-
usage
public static void usage()
Report how this utility is used and exit.
-
usage
public static void usage(java.lang.String msg)
-
main
public static void main(java.lang.String[] args)
Munge's main entry point.
-
-