dol: initial dol commit
[jump.git] / dol / src / dol / util / CodePrintString.java
diff --git a/dol/src/dol/util/CodePrintString.java b/dol/src/dol/util/CodePrintString.java
new file mode 100644 (file)
index 0000000..b7ab780
--- /dev/null
@@ -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("</" + tagName + ">");
+    }
+
+    /**
+     * 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("<abc/>");
+        ps.printClosingTag("tag");
+        System.out.println(ps.toString());
+    }
+
+    protected static String _offset = "    ";
+    protected String _prefix = "";
+    protected StringBuffer _code = null;
+}
+
+
+