java/sql-dk/src/info/globalcode/sql/dk/formatting/TeXFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 16 May 2015 23:58:06 +0200
branchv_0
changeset 191 862d0a8747ac
parent 174 3c6d560a1d14
child 206 e2f24eea8543
permissions -rw-r--r--
avoid NullPointerException (value = null) while duplicating to java.util.Properties
franta-hg@174
     1
/**
franta-hg@174
     2
 * SQL-DK
franta-hg@174
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@174
     4
 *
franta-hg@174
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@174
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@174
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@174
     8
 * (at your option) any later version.
franta-hg@174
     9
 *
franta-hg@174
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@174
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@174
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@174
    13
 * GNU General Public License for more details.
franta-hg@174
    14
 *
franta-hg@174
    15
 * You should have received a copy of the GNU General Public License
franta-hg@174
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@174
    17
 */
franta-hg@174
    18
package info.globalcode.sql.dk.formatting;
franta-hg@174
    19
franta-hg@174
    20
import info.globalcode.sql.dk.ColorfulPrintWriter;
franta-hg@174
    21
import info.globalcode.sql.dk.Constants;
franta-hg@174
    22
import java.util.Collections;
franta-hg@174
    23
import java.util.HashMap;
franta-hg@174
    24
import java.util.List;
franta-hg@174
    25
import java.util.Map;
franta-hg@174
    26
franta-hg@174
    27
/**
franta-hg@174
    28
 * Outputs result sets in (La)TeX format.
franta-hg@174
    29
 *
franta-hg@174
    30
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@174
    31
 */
franta-hg@174
    32
public class TeXFormatter extends AbstractFormatter {
franta-hg@174
    33
franta-hg@174
    34
	public static final String NAME = "tex"; // bash-completion:formatter
franta-hg@174
    35
	public static final String PROPERTY_COLORFUL = "color";
franta-hg@174
    36
	private static final ColorfulPrintWriter.TerminalColor COMMAND_COLOR = ColorfulPrintWriter.TerminalColor.Magenta;
franta-hg@174
    37
	private static final ColorfulPrintWriter.TerminalColor OPTIONS_COLOR = ColorfulPrintWriter.TerminalColor.Yellow;
franta-hg@174
    38
	private static final Map<Character, String> TEX_ESCAPE_MAP;
franta-hg@174
    39
	private final ColorfulPrintWriter out;
franta-hg@174
    40
franta-hg@174
    41
	static {
franta-hg@174
    42
		Map<Character, String> replacements = new HashMap<>();
franta-hg@174
    43
franta-hg@174
    44
		replacements.put('\\', "\\textbackslash{}");
franta-hg@174
    45
		replacements.put('{', "\\{{}");
franta-hg@174
    46
		replacements.put('}', "\\}{}");
franta-hg@174
    47
		replacements.put('_', "\\_{}");
franta-hg@174
    48
		replacements.put('^', "\\textasciicircum{}");
franta-hg@174
    49
		replacements.put('#', "\\#{}");
franta-hg@174
    50
		replacements.put('&', "\\&{}");
franta-hg@174
    51
		replacements.put('$', "\\${}");
franta-hg@174
    52
		replacements.put('%', "\\%{}");
franta-hg@174
    53
		replacements.put('~', "\\textasciitilde{}");
franta-hg@174
    54
		replacements.put('-', "{-}");
franta-hg@174
    55
franta-hg@174
    56
		TEX_ESCAPE_MAP = Collections.unmodifiableMap(replacements);
franta-hg@174
    57
	}
franta-hg@174
    58
franta-hg@174
    59
	public TeXFormatter(FormatterContext formatterContext) {
franta-hg@174
    60
		super(formatterContext);
franta-hg@174
    61
		boolean colorful = formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, false);
franta-hg@174
    62
		out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful);
franta-hg@174
    63
	}
franta-hg@174
    64
franta-hg@174
    65
	@Override
franta-hg@174
    66
	public void writeStartBatch() {
franta-hg@174
    67
		super.writeStartBatch();
franta-hg@174
    68
franta-hg@174
    69
		printCommand("documentclass", "a4paper,twoside", "article", true);
franta-hg@174
    70
		printCommand("usepackage", "T1", "fontenc", true);
franta-hg@174
    71
		printCommand("usepackage", "utf8x", "inputenc", true);
franta-hg@174
    72
		printCommand("usepackage", "pdfauthor={" + Constants.WEBSITE + "}, bookmarks=true,unicode,colorlinks=true,linkcolor=black,urlcolor=blue,citecolor=blue", "hyperref", true);
franta-hg@174
    73
		printBegin("document");
franta-hg@174
    74
	}
franta-hg@174
    75
franta-hg@174
    76
	@Override
franta-hg@174
    77
	public void writeEndBatch() {
franta-hg@174
    78
		super.writeEndBatch();
franta-hg@174
    79
		printEnd("document");
franta-hg@174
    80
	}
franta-hg@174
    81
franta-hg@174
    82
	@Override
franta-hg@174
    83
	public void writeColumnValue(Object value) {
franta-hg@174
    84
		super.writeColumnValue(value);
franta-hg@174
    85
		// TODO: arrays, numbers, booleans, nulls etc.:
franta-hg@174
    86
		out.print(escapeTex(toString(value)));
franta-hg@174
    87
franta-hg@174
    88
		if (!isCurrentColumnLast()) {
franta-hg@174
    89
			printColumnSeparator();
franta-hg@174
    90
		}
franta-hg@174
    91
	}
franta-hg@174
    92
franta-hg@174
    93
	@Override
franta-hg@174
    94
	public void writeEndRow() {
franta-hg@174
    95
		super.writeEndRow();
franta-hg@174
    96
		printEndRow();
franta-hg@174
    97
	}
franta-hg@174
    98
franta-hg@174
    99
	@Override
franta-hg@174
   100
	public void writeStartResultSet(ColumnsHeader header) {
franta-hg@174
   101
		super.writeStartResultSet(header);
franta-hg@174
   102
		printCommand("begin", null, "tabular", false);
franta-hg@174
   103
franta-hg@174
   104
		List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();
franta-hg@174
   105
franta-hg@174
   106
		StringBuilder columnAlignments = new StringBuilder();
franta-hg@174
   107
		for (ColumnDescriptor cd : columnDescriptors) {
franta-hg@174
   108
			if (cd.isNumeric() || cd.isBoolean()) {
franta-hg@174
   109
				columnAlignments.append('r');
franta-hg@174
   110
			} else {
franta-hg@174
   111
				columnAlignments.append('l');
franta-hg@174
   112
			}
franta-hg@174
   113
		}
franta-hg@174
   114
franta-hg@174
   115
		printCommand(null, null, columnAlignments.toString(), true);
franta-hg@174
   116
		printCommand("hline", null, null, true);
franta-hg@174
   117
franta-hg@174
   118
		for (ColumnDescriptor cd : columnDescriptors) {
franta-hg@174
   119
			printCommand("textbf", null, cd.getLabel(), false);
franta-hg@174
   120
			if (cd.isLastColumn()) {
franta-hg@174
   121
				printEndRow();
franta-hg@174
   122
			} else {
franta-hg@174
   123
				printColumnSeparator();
franta-hg@174
   124
			}
franta-hg@174
   125
		}
franta-hg@174
   126
franta-hg@174
   127
		printCommand("hline", null, null, true);
franta-hg@174
   128
	}
franta-hg@174
   129
franta-hg@174
   130
	@Override
franta-hg@174
   131
	public void writeEndResultSet() {
franta-hg@174
   132
		super.writeEndResultSet();
franta-hg@174
   133
		printCommand("hline", null, null, true);
franta-hg@174
   134
		printEnd("tabular");
franta-hg@174
   135
	}
franta-hg@174
   136
franta-hg@174
   137
	private String escapeTex(String text) {
franta-hg@174
   138
		if (text == null) {
franta-hg@174
   139
			return null;
franta-hg@174
   140
		} else {
franta-hg@174
   141
			StringBuilder result = new StringBuilder(text.length() * 2);
franta-hg@174
   142
franta-hg@174
   143
			for (char ch : text.toCharArray()) {
franta-hg@174
   144
				String replacement = TEX_ESCAPE_MAP.get(ch);
franta-hg@174
   145
				result.append(replacement == null ? ch : replacement);
franta-hg@174
   146
			}
franta-hg@174
   147
franta-hg@174
   148
			return result.toString();
franta-hg@174
   149
		}
franta-hg@174
   150
	}
franta-hg@174
   151
franta-hg@174
   152
	protected String toString(Object value) {
franta-hg@174
   153
		return String.valueOf(value);
franta-hg@174
   154
	}
franta-hg@174
   155
franta-hg@174
   156
	private void printColumnSeparator() {
franta-hg@174
   157
		out.print(COMMAND_COLOR, " & ");
franta-hg@174
   158
	}
franta-hg@174
   159
franta-hg@174
   160
	private void printEndRow() {
franta-hg@174
   161
		out.println(COMMAND_COLOR, " \\\\");
franta-hg@174
   162
		out.flush();
franta-hg@174
   163
	}
franta-hg@174
   164
franta-hg@174
   165
	/**
franta-hg@174
   166
	 *
franta-hg@174
   167
	 * @param command will not be escaped – should contain just a valid TeX command name
franta-hg@174
   168
	 * @param options will not be escaped – should be properly formatted to be printed inside [
franta-hg@174
   169
	 * and ]
franta-hg@174
   170
	 * @param value will be escaped
franta-hg@174
   171
	 * @param println whether to print line end and flush
franta-hg@174
   172
	 */
franta-hg@174
   173
	private void printCommand(String command, String options, String value, boolean println) {
franta-hg@174
   174
franta-hg@174
   175
		if (command != null) {
franta-hg@174
   176
			out.print(COMMAND_COLOR, "\\" + command);
franta-hg@174
   177
		}
franta-hg@174
   178
franta-hg@174
   179
		if (options != null) {
franta-hg@174
   180
			out.print(COMMAND_COLOR, "[");
franta-hg@174
   181
			out.print(OPTIONS_COLOR, options);
franta-hg@174
   182
			out.print(COMMAND_COLOR, "]");
franta-hg@174
   183
		}
franta-hg@174
   184
franta-hg@174
   185
		if (value != null) {
franta-hg@174
   186
			out.print(COMMAND_COLOR, "{");
franta-hg@174
   187
			out.print(escapeTex(value));
franta-hg@174
   188
			out.print(COMMAND_COLOR, "}");
franta-hg@174
   189
		}
franta-hg@174
   190
franta-hg@174
   191
		if (println) {
franta-hg@174
   192
			out.println();
franta-hg@174
   193
			out.flush();
franta-hg@174
   194
		}
franta-hg@174
   195
	}
franta-hg@174
   196
franta-hg@174
   197
	private void printBegin(String environment) {
franta-hg@174
   198
		printCommand("begin", null, environment, true);
franta-hg@174
   199
	}
franta-hg@174
   200
franta-hg@174
   201
	private void printEnd(String environment) {
franta-hg@174
   202
		printCommand("end", null, environment, true);
franta-hg@174
   203
	}
franta-hg@174
   204
franta-hg@174
   205
}