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