Debian Codegraph @

jte-jsp-converter/src/main/java/gg/jte/convert/ConverterOutput.java

raw
1 package gg.jte.convert;
2
3 import java.util.ArrayDeque;
4 import java.util.Deque;
5
6 public class ConverterOutput {
7
8 private final StringBuilder buffer = new StringBuilder();
9 private final Deque<Boolean> trimWhitespaceStack = new ArrayDeque<>();
10 private final Deque<Boolean> insideScriptStack = new ArrayDeque<>();
11
12
13 private int indentationCount = 4;
14 private char indentationChar = ' ';
15
16 private int skipIndent;
17 private boolean trimWhitespace = false;
18 private boolean insideScript = false;
19 private boolean allowNextWhitespace = false;
20
21 public ConverterOutput append(String s) {
22 if (s != null) {
23 buffer.append(trimIndent(s));
24 }
25 return this;
26 }
27
28 public ConverterOutput prepend(String s) {
29 if (s != null) {
30 buffer.insert(0, s);
31 }
32 return this;
33 }
34
35 public boolean isTrimWhitespace() {
36 if (allowNextWhitespace) {
37 allowNextWhitespace = false;
38 return false;
39 }
40
41 return trimWhitespace;
42 }
43
44 public boolean isInsideScript() {
45 return insideScript;
46 }
47
48 public ConverterOutput trim() {
49 String trimmed = buffer.toString().trim();
50 buffer.setLength(0);
51 buffer.append(trimmed);
52 return this;
53 }
54
55 public ConverterOutput newLine(String newLinePrefix) {
56 buffer.append("\n");
57 buffer.append(newLinePrefix);
58 return this;
59 }
60
61 public int getCurrentLineCharCount() {
62 int indentationBegin = buffer.lastIndexOf("\n");
63 // Gives correct result even when indentationBegin is -1
64 return buffer.length() - indentationBegin - 1;
65 }
66
67 public ConverterOutput newLine() {
68 int indentationBegin = buffer.lastIndexOf("\n");
69 int indentationEnd = -1;
70 if (indentationBegin != -1) {
71 indentationBegin++;
72 for (int i = indentationBegin; i < buffer.length(); ++i) {
73 if (!Character.isWhitespace(buffer.charAt(i))) {
74 indentationEnd = i;
75 break;
76 }
77 }
78 }
79
80 if (indentationBegin != -1 && indentationEnd != -1) {
81 String indent = buffer.substring(indentationBegin, indentationEnd);
82 newLine(indent);
83 } else {
84 newLine("");
85 }
86
87 return this;
88 }
89
90 public ConverterOutput indent(int amount) {
91 for(int i = 0; i < amount * indentationCount; ++i) {
92 buffer.append(indentationChar);
93 }
94 return this;
95 }
96
97 public void incrementSkipIndent() {
98 skipIndent++;
99 }
100
101 public void decrementSkipIndent() {
102 skipIndent--;
103 }
104
105 private String trimIndent(String content) {
106 if (skipIndent == 0) {
107 return content;
108 }
109
110 StringBuilder result = new StringBuilder(content.length());
111
112 int trim = 0;
113
114 for (int i = 0; i < content.length(); ++i) {
115 char c = content.charAt(i);
116 if (c == '\n') {
117 trim = skipIndent * indentationCount;
118 } else if (trim > 0) {
119 if (c == indentationChar) {
120 --trim;
121 continue;
122 } else {
123 trim = 0;
124 }
125 }
126
127 result.append(c);
128 }
129
130 return result.toString();
131 }
132
133 public char getIndentationChar() {
134 return indentationChar;
135 }
136
137 public void setIndentationChar(char indentationChar) {
138 this.indentationChar = indentationChar;
139 }
140
141 public void setIndentationCount(int indentationCount) {
142 this.indentationCount = indentationCount;
143 }
144
145 public void pushTrimWhitespace(boolean trimWhitespace) {
146 trimWhitespaceStack.push(trimWhitespace);
147 this.trimWhitespace = trimWhitespace;
148 }
149
150 public void popTrimWhitespace() {
151 trimWhitespaceStack.pop();
152 if (trimWhitespaceStack.isEmpty()) {
153 trimWhitespace = false;
154 } else {
155 trimWhitespace = this.trimWhitespaceStack.peek();
156 }
157 }
158
159 public void pushInsideScript(boolean insideScript) {
160 insideScriptStack.push(ConverterOutput.this.insideScript);
161 this.insideScript = insideScript;
162 }
163
164 public void popInsideScript() {
165 insideScriptStack.pop();
166 if (insideScriptStack.isEmpty()) {
167 insideScript = false;
168 } else {
169 insideScript = this.insideScriptStack.peek();
170 }
171 }
172
173 public void setAllowNextWhitespace() {
174 allowNextWhitespace = true;
175 }
176
177 @Override
178 public String toString() {
179 return buffer.toString();
180 }
181 }