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