X-Git-Url: http://sraa.de/git/?a=blobdiff_plain;f=dol%2Fsrc%2Fdol%2Futil%2FCodePrintString.java;fp=dol%2Fsrc%2Fdol%2Futil%2FCodePrintString.java;h=b7ab7805c83228a4c922e5a051d58c390b96cdc6;hb=8c411cf24ed0eb889191aaeafd8fa1e69081df42;hp=0000000000000000000000000000000000000000;hpb=dea7a4fb1ed110d3ce6e6d9255103d724bd66c0e;p=jump.git diff --git a/dol/src/dol/util/CodePrintString.java b/dol/src/dol/util/CodePrintString.java new file mode 100644 index 0000000..b7ab780 --- /dev/null +++ b/dol/src/dol/util/CodePrintString.java @@ -0,0 +1,129 @@ +/* $Id: CodePrintString.java 1 2010-02-24 13:03:05Z haidw $ */ +package dol.util; + +/** + * Class to print code that is correctly indented. + */ +public class CodePrintString { + + /** + * Default constructor. + * + * @param stringBuffer string buffer where the result is stored + */ + public CodePrintString(StringBuffer stringBuffer) { + _code = stringBuffer; + } + + /** + * Print a line without indentation. + */ + public void print(String string) { + _code.append(string); + } + + /** + * Add a line break to the current line. Results in an empty + * line if the current line is empty. + */ + public void println() { + _code.append(System.getProperty("line.separator")); + } + + /** + * Print a line without indentation and add a line break. + * @param string string to print + */ + public void println(String string) { + _code.append(string + System.getProperty("line.separator")); + } + + /** + * Print spaces according to current level of indentation. + */ + public void printPrefix() { + _code.append(_prefix); + } + + /** + * Print a line at the current level of indentation. + * @param string string to print + */ + public void printPrefix(String string) { + _code.append(_prefix + string); + } + + /** + * Print a line at the current level of indentation and a line break. + * @param string string to print + */ + public void printPrefixln(String string) { + _code.append(_prefix + string + + System.getProperty("line.separator")); + } + + /** + * Print the opening tag for the given XML tag name and increase + * the indentation. + * @param tagName name of the XML tag + */ + public void printOpeningTag(String tagName) { + printPrefix(); + print("<" + tagName); + prefixInc(); + } + + /** + * Decrease the indentation and print the closing tag for the given + * XML tag name. + * @param tagName name of the XML tag + */ + public void printClosingTag(String tagName) { + prefixDec(); + printPrefixln(""); + } + + /** + * Decrement the indentation. + */ + public void prefixDec() { + if( _prefix.length() >= _offset.length() ) { + _prefix = _prefix.substring(_offset.length()); + } + } + + /** + * Increment the indentation. + */ + public void prefixInc() { + _prefix += _offset; + } + + /** + * Get the formatted output string. + * @return output string + */ + public String toString() { + return _code.toString(); + } + + /** + * Test the CodePrintString implementation. + */ + public static void main(String[] args) { + StringBuffer buffer = new StringBuffer(); + CodePrintString ps = new CodePrintString(buffer); + ps.printOpeningTag("tag"); + ps.println(">"); + ps.printPrefixln(""); + ps.printClosingTag("tag"); + System.out.println(ps.toString()); + } + + protected static String _offset = " "; + protected String _prefix = ""; + protected StringBuffer _code = null; +} + + +