SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.formatting;
20 import info.globalcode.sql.dk.Parameter;
21 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
22 import java.util.EmptyStackException;
23 import java.util.EnumSet;
24 import java.util.List;
25 import java.util.Stack;
29 * <li>ensures integrity – if methods are called in correct order and context</li>
30 * <li>provides default implmentations of methods that does not produce any output for given
34 * @author Ing. František Kučera (frantovo.cz)
36 public abstract class AbstractFormatter implements Formatter {
38 private Stack<State> state = new Stack<>();
39 private FormatterContext formatterContext;
40 private ColumnsHeader currentColumnsHeader;
41 private String currentQuery;
42 private int currentColumnsCount;
43 private int currentRowCount;
45 public AbstractFormatter(FormatterContext formatterContext) {
46 this.formatterContext = formatterContext;
47 state.push(State.ROOT);
62 protected enum State {
73 * Go down in hierarchy.
74 * Pushes new state and verifies the old one.
76 * @param current the new state – currently entering
77 * @param expected expected previous states (any of them is valid)
78 * @return previous state
79 * @throws IllegalStateException if previous state was not one from expected
81 private State pushState(State current, EnumSet expected) {
82 State previous = state.peek();
84 if (expected.contains(previous)) {
88 throw new IllegalStateException("Formatter was in wrong state: " + previous + " when it should be in one of: " + expected);
92 protected State peekState(EnumSet expected) {
93 State current = state.peek();
95 if (expected.contains(current)) {
98 throw new IllegalStateException("Formatter is in wrong state: " + current + " when it should be in one of: " + expected);
104 * Go up in hierarchy.
105 * Pops the superior state/branch.
107 * @param expected expected superior state
108 * @return the superior state
109 * @throws IllegalStateException if superior state was not one from expected or if there is no
110 * more superior state (we are at root level)
112 private State popState(EnumSet expected) {
115 State superior = state.peek();
116 if (expected.contains(superior)) {
119 throw new IllegalStateException("Formatter had wrong superior state: " + superior + " when it should be in one of: " + expected);
121 } catch (EmptyStackException e) {
122 throw new IllegalStateException("Formatter was already at root level – there is nothing above that.", e);
127 public void writeStartBatch() {
128 pushState(State.BATCH, EnumSet.of(State.ROOT));
132 public void writeEndBatch() {
133 popState(EnumSet.of(State.ROOT));
137 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
138 pushState(State.DATABASE, EnumSet.of(State.BATCH));
142 public void writeEndDatabase() {
143 popState(EnumSet.of(State.BATCH));
147 public void writeStartStatement() {
148 pushState(State.STATEMENT, EnumSet.of(State.DATABASE));
152 public void writeEndStatement() {
153 popState(EnumSet.of(State.DATABASE));
157 public void writeStartResultSet(ColumnsHeader header) {
158 pushState(State.RESULT_SET, EnumSet.of(State.STATEMENT));
160 currentColumnsHeader = header;
164 public void writeEndResultSet() {
165 popState(EnumSet.of(State.STATEMENT));
166 currentColumnsHeader = null;
170 public void writeQuery(String sql) {
171 peekState(EnumSet.of(State.STATEMENT));
173 if (currentColumnsHeader == null) {
176 throw new IllegalStateException("Query string '" + sql + "' must be set before columns header – was already set: " + currentColumnsHeader);
181 public void writeParameters(List<? extends Parameter> parameters) {
182 peekState(EnumSet.of(State.STATEMENT));
184 if (currentColumnsHeader != null) {
185 throw new IllegalStateException("Parameters '" + parameters + "' must be set before columns header – was already set: " + currentColumnsHeader);
188 if (currentQuery == null && parameters != null) {
189 throw new IllegalStateException("Parameters '" + parameters + "' must be set after query – was not yet set.");
194 public void writeStartRow() {
195 pushState(State.ROW, EnumSet.of(State.RESULT_SET));
196 currentColumnsCount = 0;
201 public void writeEndRow() {
202 popState(EnumSet.of(State.RESULT_SET));
206 public void writeColumnValue(Object value) {
207 peekState(EnumSet.of(State.ROW));
208 currentColumnsCount++;
210 int declaredCount = currentColumnsHeader.getColumnCount();
211 if (currentColumnsCount > declaredCount) {
212 throw new IllegalStateException("Current columns count is " + currentColumnsCount + " which is more than declared " + declaredCount + " in header.");
217 public void writeUpdatesResult(int updatedRowsCount) {
218 peekState(EnumSet.of(State.STATEMENT));
222 public void close() throws FormatterException {
225 public FormatterContext getFormatterContext() {
226 return formatterContext;
229 protected ColumnsHeader getCurrentColumnsHeader() {
230 return currentColumnsHeader;
234 * @return column number, 1 = first
236 protected int getCurrentColumnsCount() {
237 return currentColumnsCount;
240 protected boolean isCurrentColumnFirst() {
241 return currentColumnsCount == 1;
244 protected boolean isCurrentColumnLast() {
245 return currentColumnsCount == currentColumnsHeader.getColumnCount();
249 * @return row number, 1 = first
251 protected int getCurrentRowCount() {
252 return currentRowCount;