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

/**
 * SQL-DK
 * Copyright © 2014 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk.formatting;

import info.globalcode.sql.dk.ColorfulPrintWriter;
import info.globalcode.sql.dk.Constants;
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Outputs result sets in (La)TeX format.
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
@PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION)
public class TeXFormatter extends AbstractFormatter {

	public static final String NAME = "tex"; // bash-completion:formatter
	private static final ColorfulPrintWriter.TerminalColor COMMAND_COLOR = ColorfulPrintWriter.TerminalColor.Magenta;
	private static final ColorfulPrintWriter.TerminalColor OPTIONS_COLOR = ColorfulPrintWriter.TerminalColor.Yellow;
	private static final Map<Character, String> TEX_ESCAPE_MAP;
	private final ColorfulPrintWriter out;

	static {
		Map<Character, String> replacements = new HashMap<>();

		replacements.put('\\', "\\textbackslash{}");
		replacements.put('{', "\\{{}");
		replacements.put('}', "\\}{}");
		replacements.put('_', "\\_{}");
		replacements.put('^', "\\textasciicircum{}");
		replacements.put('#', "\\#{}");
		replacements.put('&', "\\&{}");
		replacements.put('$', "\\${}");
		replacements.put('%', "\\%{}");
		replacements.put('~', "\\textasciitilde{}");
		replacements.put('-', "{-}");

		TEX_ESCAPE_MAP = Collections.unmodifiableMap(replacements);
	}

	public TeXFormatter(FormatterContext formatterContext) {
		super(formatterContext);
		boolean colorful = formatterContext.getProperties().getBoolean(COLORFUL, false);
		out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful);
	}

	@Override
	public void writeStartBatch() {
		super.writeStartBatch();

		printCommand("documentclass", "a4paper,twoside", "article", true);
		printCommand("usepackage", "T1", "fontenc", true);
		printCommand("usepackage", "utf8x", "inputenc", true);
		printCommand("usepackage", "pdfauthor={" + Constants.WEBSITE + "}, bookmarks=true,unicode,colorlinks=true,linkcolor=black,urlcolor=blue,citecolor=blue", "hyperref", true);
		printBegin("document");
	}

	@Override
	public void writeEndBatch() {
		super.writeEndBatch();
		printEnd("document");
	}

	@Override
	public void writeColumnValue(Object value) {
		super.writeColumnValue(value);
		// TODO: arrays, numbers, booleans, nulls etc.:
		out.print(escapeTex(toString(value)));

		if (!isCurrentColumnLast()) {
			printColumnSeparator();
		}
	}

	@Override
	public void writeEndRow() {
		super.writeEndRow();
		printEndRow();
	}

	@Override
	public void writeStartResultSet(ColumnsHeader header) {
		super.writeStartResultSet(header);
		printCommand("begin", null, "tabular", false);

		List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();

		StringBuilder columnAlignments = new StringBuilder();
		for (ColumnDescriptor cd : columnDescriptors) {
			if (cd.isNumeric() || cd.isBoolean()) {
				columnAlignments.append('r');
			} else {
				columnAlignments.append('l');
			}
		}

		printCommand(null, null, columnAlignments.toString(), true);
		printCommand("hline", null, null, true);

		for (ColumnDescriptor cd : columnDescriptors) {
			printCommand("textbf", null, cd.getLabel(), false);
			if (cd.isLastColumn()) {
				printEndRow();
			} else {
				printColumnSeparator();
			}
		}

		printCommand("hline", null, null, true);
	}

	@Override
	public void writeEndResultSet() {
		super.writeEndResultSet();
		printCommand("hline", null, null, true);
		printEnd("tabular");
	}

	private String escapeTex(String text) {
		if (text == null) {
			return null;
		} else {
			StringBuilder result = new StringBuilder(text.length() * 2);

			for (char ch : text.toCharArray()) {
				String replacement = TEX_ESCAPE_MAP.get(ch);
				result.append(replacement == null ? ch : replacement);
			}

			return result.toString();
		}
	}

	protected String toString(Object value) {
		return String.valueOf(value);
	}

	private void printColumnSeparator() {
		out.print(COMMAND_COLOR, " & ");
	}

	private void printEndRow() {
		out.println(COMMAND_COLOR, " \\\\");
		out.flush();
	}

	/**
	 *
	 * @param command will not be escaped – should contain just a valid TeX command name
	 * @param options will not be escaped – should be properly formatted to be printed inside [
	 * and ]
	 * @param value will be escaped
	 * @param println whether to print line end and flush
	 */
	private void printCommand(String command, String options, String value, boolean println) {

		if (command != null) {
			out.print(COMMAND_COLOR, "\\" + command);
		}

		if (options != null) {
			out.print(COMMAND_COLOR, "[");
			out.print(OPTIONS_COLOR, options);
			out.print(COMMAND_COLOR, "]");
		}

		if (value != null) {
			out.print(COMMAND_COLOR, "{");
			out.print(escapeTex(value));
			out.print(COMMAND_COLOR, "}");
		}

		if (println) {
			out.println();
			out.flush();
		}
	}

	private void printBegin(String environment) {
		printCommand("begin", null, environment, true);
	}

	private void printEnd(String environment) {
		printCommand("end", null, environment, true);
	}

}