001 package escjava.vcGeneration;
002
003 import java.io.*;
004
005 public class PrettyPrinter {
006
007 private final String TAB;
008 private final String LBR;
009 private final String RBR;
010 private final String NL;
011
012
013 String commentMark = "";
014
015 /*@ non_null @*/Writer out = null;
016
017 /*@ non_null @*/StringBuffer indentation = null;
018
019 public PrettyPrinter(Writer out, String tab, String lbr, String rbr, String nl) {
020 this.out = out;
021 indentation = new StringBuffer();
022 TAB = tab;
023 LBR = lbr;
024 RBR = rbr;
025 NL = nl;
026 }
027
028 /**
029 * just append the parameter, N stands for normal
030 */
031 public/*@ non_null @*/Writer appendN(/*@ non_null @*/String s) throws IOException {
032 if (!commentMark.equals(""))
033 s.replaceAll(NL,NL+commentMark);
034 out.write(s);
035 return out;
036 }
037
038 /**
039 * append indentation and parameter
040 */
041 public/*@ non_null @*/Writer append(/*@ non_null @*/String s) throws IOException {
042 if (!commentMark.equals(""))
043 s.replaceAll(NL,NL+commentMark);
044 out.write(indentation.toString());
045 out.write(s);
046
047 return out;
048 }
049
050 /**
051 * This function goes to next line, increases indentation by two spaces
052 * and add a '('
053 * If you want to change the indentation style do it here.
054 */
055 public/*@ non_null @*/Writer appendI(/*@ non_null @*/String s) throws IOException {
056 if (!commentMark.equals(""))
057 s.replaceAll(NL,NL+commentMark);
058 if (indentation.length() != 0) { // not first call
059 out.write(NL + commentMark);
060 out.write(indentation.toString());
061 }
062 else
063 out.write(commentMark);
064
065 indentation.append(TAB);
066
067 out.write(LBR + s);
068 return out;
069 }
070
071 /**
072 * This function increases indentation by two spaces
073 * and add a '('
074 * If you want to change the indentation style do it here.
075 */
076 public/*@ non_null @*/Writer appendIwNl(/*@ non_null @*/String s) throws IOException {
077 if (!commentMark.equals(""))
078 s.replaceAll(NL,NL+commentMark);
079 indentation.append(TAB);
080 out.write(indentation.toString());
081 out.write(LBR + s);
082
083 return out;
084 }
085
086 /**
087 * This function goes to new line, add a ')' and
088 * reduces indentation by two spaces
089 * If you want to change the indentation style do it here.
090 */
091 public/*@ non_null @*/Writer reduceI() throws IOException {
092 out.write(NL + commentMark);
093 indentation = indentation.delete(0, TAB.length());
094 out.write(indentation + RBR);
095
096 return out;
097 }
098
099 /**
100 * This function add a ')' and
101 * reduces indentation by two spaces
102 * If you want to change the indentation style do it here.
103 */
104 public/*@ non_null @*/Writer reduceIwNl() throws IOException {
105 out.write(RBR);
106 indentation = indentation.delete(0, TAB.length());
107
108 return out;
109 }
110
111 public/*@ non_null @*/String toString() {
112 return out.toString();
113 }
114
115 /**
116 * This adds a user-specified comment mark string to the beginning of each
117 * line. It can be removed by wither recalling setC with the empty
118 * string or calling removeC. It immediately adds one instance of the
119 * comment marker.
120 */
121 public /*@ non_null @*/Writer beginC(/*@ non_null @*/String s) throws IOException {
122 commentMark = s;
123 out.write(s);
124 return out;
125 }
126
127 /**
128 * This function removes the comment mark set by setC() and appends a
129 * newline;
130 */
131 public/*@ non_null @*/Writer endC() throws IOException {
132 commentMark = "";
133 out.write(NL);
134 return out;
135 }
136
137
138 }