java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/TeXFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Mar 2019 20:15:24 +0100
branchv_0
changeset 238 4a1864c3e867
parent 207 java/sql-dk/src/info/globalcode/sql/dk/formatting/TeXFormatter.java@2bba68ef47c1
child 250 aae5009bd0af
permissions -rw-r--r--
mavenized: sql-dk
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@206
    22
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
franta-hg@206
    23
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
franta-hg@206
    24
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
franta-hg@174
    25
import java.util.Collections;
franta-hg@174
    26
import java.util.HashMap;
franta-hg@174
    27
import java.util.List;
franta-hg@174
    28
import java.util.Map;
franta-hg@174
    29
franta-hg@174
    30
/**
franta-hg@174
    31
 * Outputs result sets in (La)TeX format.
franta-hg@174
    32
 *
franta-hg@174
    33
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@174
    34
 */
franta-hg@207
    35
@PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION)
franta-hg@174
    36
public class TeXFormatter extends AbstractFormatter {
franta-hg@174
    37
franta-hg@174
    38
	public static final String NAME = "tex"; // bash-completion:formatter
franta-hg@174
    39
	private static final ColorfulPrintWriter.TerminalColor COMMAND_COLOR = ColorfulPrintWriter.TerminalColor.Magenta;
franta-hg@174
    40
	private static final ColorfulPrintWriter.TerminalColor OPTIONS_COLOR = ColorfulPrintWriter.TerminalColor.Yellow;
franta-hg@174
    41
	private static final Map<Character, String> TEX_ESCAPE_MAP;
franta-hg@174
    42
	private final ColorfulPrintWriter out;
franta-hg@174
    43
franta-hg@174
    44
	static {
franta-hg@174
    45
		Map<Character, String> replacements = new HashMap<>();
franta-hg@174
    46
franta-hg@174
    47
		replacements.put('\\', "\\textbackslash{}");
franta-hg@174
    48
		replacements.put('{', "\\{{}");
franta-hg@174
    49
		replacements.put('}', "\\}{}");
franta-hg@174
    50
		replacements.put('_', "\\_{}");
franta-hg@174
    51
		replacements.put('^', "\\textasciicircum{}");
franta-hg@174
    52
		replacements.put('#', "\\#{}");
franta-hg@174
    53
		replacements.put('&', "\\&{}");
franta-hg@174
    54
		replacements.put('$', "\\${}");
franta-hg@174
    55
		replacements.put('%', "\\%{}");
franta-hg@174
    56
		replacements.put('~', "\\textasciitilde{}");
franta-hg@174
    57
		replacements.put('-', "{-}");
franta-hg@174
    58
franta-hg@174
    59
		TEX_ESCAPE_MAP = Collections.unmodifiableMap(replacements);
franta-hg@174
    60
	}
franta-hg@174
    61
franta-hg@174
    62
	public TeXFormatter(FormatterContext formatterContext) {
franta-hg@174
    63
		super(formatterContext);
franta-hg@206
    64
		boolean colorful = formatterContext.getProperties().getBoolean(COLORFUL, false);
franta-hg@174
    65
		out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful);
franta-hg@174
    66
	}
franta-hg@174
    67
franta-hg@174
    68
	@Override
franta-hg@174
    69
	public void writeStartBatch() {
franta-hg@174
    70
		super.writeStartBatch();
franta-hg@174
    71
franta-hg@174
    72
		printCommand("documentclass", "a4paper,twoside", "article", true);
franta-hg@174
    73
		printCommand("usepackage", "T1", "fontenc", true);
franta-hg@174
    74
		printCommand("usepackage", "utf8x", "inputenc", true);
franta-hg@174
    75
		printCommand("usepackage", "pdfauthor={" + Constants.WEBSITE + "}, bookmarks=true,unicode,colorlinks=true,linkcolor=black,urlcolor=blue,citecolor=blue", "hyperref", true);
franta-hg@174
    76
		printBegin("document");
franta-hg@174
    77
	}
franta-hg@174
    78
franta-hg@174
    79
	@Override
franta-hg@174
    80
	public void writeEndBatch() {
franta-hg@174
    81
		super.writeEndBatch();
franta-hg@174
    82
		printEnd("document");
franta-hg@174
    83
	}
franta-hg@174
    84
franta-hg@174
    85
	@Override
franta-hg@174
    86
	public void writeColumnValue(Object value) {
franta-hg@174
    87
		super.writeColumnValue(value);
franta-hg@174
    88
		// TODO: arrays, numbers, booleans, nulls etc.:
franta-hg@174
    89
		out.print(escapeTex(toString(value)));
franta-hg@174
    90
franta-hg@174
    91
		if (!isCurrentColumnLast()) {
franta-hg@174
    92
			printColumnSeparator();
franta-hg@174
    93
		}
franta-hg@174
    94
	}
franta-hg@174
    95
franta-hg@174
    96
	@Override
franta-hg@174
    97
	public void writeEndRow() {
franta-hg@174
    98
		super.writeEndRow();
franta-hg@174
    99
		printEndRow();
franta-hg@174
   100
	}
franta-hg@174
   101
franta-hg@174
   102
	@Override
franta-hg@174
   103
	public void writeStartResultSet(ColumnsHeader header) {
franta-hg@174
   104
		super.writeStartResultSet(header);
franta-hg@174
   105
		printCommand("begin", null, "tabular", false);
franta-hg@174
   106
franta-hg@174
   107
		List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();
franta-hg@174
   108
franta-hg@174
   109
		StringBuilder columnAlignments = new StringBuilder();
franta-hg@174
   110
		for (ColumnDescriptor cd : columnDescriptors) {
franta-hg@174
   111
			if (cd.isNumeric() || cd.isBoolean()) {
franta-hg@174
   112
				columnAlignments.append('r');
franta-hg@174
   113
			} else {
franta-hg@174
   114
				columnAlignments.append('l');
franta-hg@174
   115
			}
franta-hg@174
   116
		}
franta-hg@174
   117
franta-hg@174
   118
		printCommand(null, null, columnAlignments.toString(), true);
franta-hg@174
   119
		printCommand("hline", null, null, true);
franta-hg@174
   120
franta-hg@174
   121
		for (ColumnDescriptor cd : columnDescriptors) {
franta-hg@174
   122
			printCommand("textbf", null, cd.getLabel(), false);
franta-hg@174
   123
			if (cd.isLastColumn()) {
franta-hg@174
   124
				printEndRow();
franta-hg@174
   125
			} else {
franta-hg@174
   126
				printColumnSeparator();
franta-hg@174
   127
			}
franta-hg@174
   128
		}
franta-hg@174
   129
franta-hg@174
   130
		printCommand("hline", null, null, true);
franta-hg@174
   131
	}
franta-hg@174
   132
franta-hg@174
   133
	@Override
franta-hg@174
   134
	public void writeEndResultSet() {
franta-hg@174
   135
		super.writeEndResultSet();
franta-hg@174
   136
		printCommand("hline", null, null, true);
franta-hg@174
   137
		printEnd("tabular");
franta-hg@174
   138
	}
franta-hg@174
   139
franta-hg@174
   140
	private String escapeTex(String text) {
franta-hg@174
   141
		if (text == null) {
franta-hg@174
   142
			return null;
franta-hg@174
   143
		} else {
franta-hg@174
   144
			StringBuilder result = new StringBuilder(text.length() * 2);
franta-hg@174
   145
franta-hg@174
   146
			for (char ch : text.toCharArray()) {
franta-hg@174
   147
				String replacement = TEX_ESCAPE_MAP.get(ch);
franta-hg@174
   148
				result.append(replacement == null ? ch : replacement);
franta-hg@174
   149
			}
franta-hg@174
   150
franta-hg@174
   151
			return result.toString();
franta-hg@174
   152
		}
franta-hg@174
   153
	}
franta-hg@174
   154
franta-hg@174
   155
	protected String toString(Object value) {
franta-hg@174
   156
		return String.valueOf(value);
franta-hg@174
   157
	}
franta-hg@174
   158
franta-hg@174
   159
	private void printColumnSeparator() {
franta-hg@174
   160
		out.print(COMMAND_COLOR, " & ");
franta-hg@174
   161
	}
franta-hg@174
   162
franta-hg@174
   163
	private void printEndRow() {
franta-hg@174
   164
		out.println(COMMAND_COLOR, " \\\\");
franta-hg@174
   165
		out.flush();
franta-hg@174
   166
	}
franta-hg@174
   167
franta-hg@174
   168
	/**
franta-hg@174
   169
	 *
franta-hg@174
   170
	 * @param command will not be escaped – should contain just a valid TeX command name
franta-hg@174
   171
	 * @param options will not be escaped – should be properly formatted to be printed inside [
franta-hg@174
   172
	 * and ]
franta-hg@174
   173
	 * @param value will be escaped
franta-hg@174
   174
	 * @param println whether to print line end and flush
franta-hg@174
   175
	 */
franta-hg@174
   176
	private void printCommand(String command, String options, String value, boolean println) {
franta-hg@174
   177
franta-hg@174
   178
		if (command != null) {
franta-hg@174
   179
			out.print(COMMAND_COLOR, "\\" + command);
franta-hg@174
   180
		}
franta-hg@174
   181
franta-hg@174
   182
		if (options != null) {
franta-hg@174
   183
			out.print(COMMAND_COLOR, "[");
franta-hg@174
   184
			out.print(OPTIONS_COLOR, options);
franta-hg@174
   185
			out.print(COMMAND_COLOR, "]");
franta-hg@174
   186
		}
franta-hg@174
   187
franta-hg@174
   188
		if (value != null) {
franta-hg@174
   189
			out.print(COMMAND_COLOR, "{");
franta-hg@174
   190
			out.print(escapeTex(value));
franta-hg@174
   191
			out.print(COMMAND_COLOR, "}");
franta-hg@174
   192
		}
franta-hg@174
   193
franta-hg@174
   194
		if (println) {
franta-hg@174
   195
			out.println();
franta-hg@174
   196
			out.flush();
franta-hg@174
   197
		}
franta-hg@174
   198
	}
franta-hg@174
   199
franta-hg@174
   200
	private void printBegin(String environment) {
franta-hg@174
   201
		printCommand("begin", null, environment, true);
franta-hg@174
   202
	}
franta-hg@174
   203
franta-hg@174
   204
	private void printEnd(String environment) {
franta-hg@174
   205
		printCommand("end", null, environment, true);
franta-hg@174
   206
	}
franta-hg@174
   207
franta-hg@174
   208
}