dol: initial dol commit
[jump.git] / dol / src / dol / util / CodePrintString.java
1 /* $Id: CodePrintString.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.util;
3
4 /**
5  * Class to print code that is correctly indented.
6  */
7 public class CodePrintString {
8
9     /**
10      * Default constructor.
11      * 
12      * @param stringBuffer string buffer where the result is stored
13      */
14     public CodePrintString(StringBuffer stringBuffer) {
15         _code = stringBuffer;
16     }
17
18     /**
19      * Print a line without indentation.
20      */
21     public void print(String string) {
22         _code.append(string);
23     }
24
25     /**
26      * Add a line break to the current line. Results in an empty
27      * line if the current line is empty.
28      */
29    public void println() {
30        _code.append(System.getProperty("line.separator"));
31    }
32
33    /**
34      * Print a line without indentation and add a line break.
35      * @param string string to print
36      */
37     public void println(String string) {
38         _code.append(string + System.getProperty("line.separator"));
39     }
40
41     /**
42      * Print spaces according to current level of indentation.
43      */
44     public void printPrefix() {
45         _code.append(_prefix);
46     }
47
48     /**
49      * Print a line at the current level of indentation.
50      * @param string string to print
51      */
52     public void printPrefix(String string) {
53         _code.append(_prefix + string);
54     }
55
56     /**
57      * Print a line at the current level of indentation and a line break.
58      * @param string string to print
59      */
60     public void printPrefixln(String string) {
61         _code.append(_prefix + string
62                 + System.getProperty("line.separator"));
63     }
64
65     /**
66      * Print the opening tag for the given XML tag name and increase
67      * the indentation.
68      * @param tagName name of the XML tag
69      */
70     public void printOpeningTag(String tagName) {
71         printPrefix();
72         print("<" + tagName);
73         prefixInc();
74     }
75
76     /**
77      * Decrease the indentation and print the closing tag for the given
78      * XML tag name. 
79      * @param tagName name of the XML tag
80      */
81     public void printClosingTag(String tagName) {
82         prefixDec();
83         printPrefixln("</" + tagName + ">");
84     }
85
86     /**
87      * Decrement the indentation.
88      */
89     public void prefixDec() {
90         if( _prefix.length() >= _offset.length() ) {
91             _prefix = _prefix.substring(_offset.length());
92         }
93     }
94
95     /**
96      * Increment the indentation.
97      */
98     public void prefixInc() {
99         _prefix += _offset;
100     }
101
102     /**
103      * Get the formatted output string.
104      * @return output string
105      */
106     public String toString()  {
107         return _code.toString();
108     }
109
110     /**
111      * Test the CodePrintString implementation.
112      */
113     public static void main(String[] args) {
114         StringBuffer buffer = new StringBuffer();
115         CodePrintString ps = new CodePrintString(buffer);
116         ps.printOpeningTag("tag");
117         ps.println(">");
118         ps.printPrefixln("<abc/>");
119         ps.printClosingTag("tag");
120         System.out.println(ps.toString());
121     }
122
123     protected static String _offset = "    ";
124     protected String _prefix = "";
125     protected StringBuffer _code = null;
126 }
127
128
129