property annotations (documentation) for particular formatters v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 15 Aug 2015 10:20:39 +0200
branchv_0
changeset 206e2f24eea8543
parent 205 d6624c3b146a
child 207 2bba68ef47c1
property annotations (documentation) for particular formatters
java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/CommonProperties.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/TeXFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java	Sat Aug 15 10:04:28 2015 +0200
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java	Sat Aug 15 10:20:39 2015 +0200
     1.3 @@ -23,6 +23,9 @@
     1.4  import javax.xml.namespace.QName;
     1.5  import static info.globalcode.sql.dk.Functions.isEmpty;
     1.6  import static info.globalcode.sql.dk.Functions.toHex;
     1.7 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
     1.8 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
     1.9 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
    1.10  import java.nio.charset.Charset;
    1.11  import java.util.EmptyStackException;
    1.12  import java.util.HashMap;
    1.13 @@ -44,10 +47,12 @@
    1.14   *
    1.15   * @author Ing. František Kučera (frantovo.cz)
    1.16   */
    1.17 +@PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION)
    1.18 +@PropertyDeclaration(name = AbstractXmlFormatter.PROPERTY_INDENT, type = String.class, description = "tab or sequence of spaces used for indentation of nested elements")
    1.19 +@PropertyDeclaration(name = AbstractXmlFormatter.PROPERTY_INDENT_TEXT, type = Boolean.class, description = "whether to indent text nodes")
    1.20  public abstract class AbstractXmlFormatter extends AbstractFormatter {
    1.21  
    1.22  	private static final Logger log = Logger.getLogger(AbstractXmlFormatter.class.getName());
    1.23 -	public static final String PROPERTY_COLORFUL = "color";
    1.24  	public static final String PROPERTY_INDENT = "indent";
    1.25  	/**
    1.26  	 * Whether text with line breaks should be indented (default). Otherwise original whitespace
    1.27 @@ -66,7 +71,7 @@
    1.28  
    1.29  	public AbstractXmlFormatter(FormatterContext formatterContext) {
    1.30  		super(formatterContext);
    1.31 -		boolean colorful = formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, false);
    1.32 +		boolean colorful = formatterContext.getProperties().getBoolean(COLORFUL, false);
    1.33  		out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful);
    1.34  		indent = formatterContext.getProperties().getString(PROPERTY_INDENT, "\t");
    1.35  		indentText = formatterContext.getProperties().getBoolean(PROPERTY_INDENT_TEXT, true);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/CommonProperties.java	Sat Aug 15 10:20:39 2015 +0200
     2.3 @@ -0,0 +1,28 @@
     2.4 +/**
     2.5 + * SQL-DK
     2.6 + * Copyright © 2015 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package info.globalcode.sql.dk.formatting;
    2.22 +
    2.23 +/**
    2.24 + *
    2.25 + * @author Ing. František Kučera (frantovo.cz)
    2.26 + */
    2.27 +public class CommonProperties {
    2.28 +
    2.29 +	public static final String COLORFUL = "color";
    2.30 +	public static final String COLORFUL_DESCRIPTION = "whether the output should be printed in color (escape sequences)";
    2.31 +}
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java	Sat Aug 15 10:04:28 2015 +0200
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java	Sat Aug 15 10:20:39 2015 +0200
     3.3 @@ -18,6 +18,9 @@
     3.4  package info.globalcode.sql.dk.formatting;
     3.5  
     3.6  import info.globalcode.sql.dk.ColorfulPrintWriter;
     3.7 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
     3.8 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
     3.9 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
    3.10  
    3.11  /**
    3.12   * Formatter intended for printing one record (or few records) with many columns.
    3.13 @@ -25,17 +28,17 @@
    3.14   *
    3.15   * @author Ing. František Kučera (frantovo.cz)
    3.16   */
    3.17 +@PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION)
    3.18  public class SingleRecordFormatter extends AbstractFormatter {
    3.19  
    3.20  	public static final String NAME = "record"; // bash-completion:formatter
    3.21 -	public static final String PROPERTY_COLORFUL = "color";
    3.22  	private final ColorfulPrintWriter out;
    3.23  	private boolean firstResult = true;
    3.24  
    3.25  	public SingleRecordFormatter(FormatterContext formatterContext) {
    3.26  		super(formatterContext);
    3.27  		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    3.28 -		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
    3.29 +		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
    3.30  	}
    3.31  
    3.32  	@Override
     4.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Sat Aug 15 10:04:28 2015 +0200
     4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Sat Aug 15 10:20:39 2015 +0200
     4.3 @@ -22,6 +22,9 @@
     4.4  import static info.globalcode.sql.dk.Functions.lpad;
     4.5  import static info.globalcode.sql.dk.Functions.rpad;
     4.6  import static info.globalcode.sql.dk.Functions.repeat;
     4.7 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
     4.8 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
     4.9 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
    4.10  import java.util.List;
    4.11  import java.util.regex.Matcher;
    4.12  import java.util.regex.Pattern;
    4.13 @@ -40,13 +43,15 @@
    4.14   * @see TabularPrefetchingFormatter
    4.15   * @see TabularWrappingFormatter
    4.16   */
    4.17 +@PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION)
    4.18 +@PropertyDeclaration(name = TabularFormatter.PROPERTY_ASCII, type = Boolean.class, description = "whether to use ASCII table borders instead of unicode ones")
    4.19 +@PropertyDeclaration(name = TabularFormatter.PROPERTY_TRIM, type = Boolean.class, description = "whether to trim the values to fit the column width")
    4.20  public class TabularFormatter extends AbstractFormatter {
    4.21  
    4.22  	public static final String NAME = "tabular"; // bash-completion:formatter
    4.23  	private static final String HEADER_TYPE_PREFIX = " (";
    4.24  	private static final String HEADER_TYPE_SUFFIX = ")";
    4.25  	public static final String PROPERTY_ASCII = "ascii";
    4.26 -	public static final String PROPERTY_COLORFUL = "color";
    4.27  	public static final String PROPERTY_TRIM = "trim";
    4.28  	private static final String NBSP = " ";
    4.29  	private static final Pattern whitespaceToReplace = Pattern.compile("\\n|\\r|\\t|" + NBSP);
    4.30 @@ -67,7 +72,7 @@
    4.31  		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    4.32  		asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
    4.33  		trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
    4.34 -		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
    4.35 +		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
    4.36  	}
    4.37  
    4.38  	@Override
     5.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TeXFormatter.java	Sat Aug 15 10:04:28 2015 +0200
     5.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TeXFormatter.java	Sat Aug 15 10:20:39 2015 +0200
     5.3 @@ -19,6 +19,9 @@
     5.4  
     5.5  import info.globalcode.sql.dk.ColorfulPrintWriter;
     5.6  import info.globalcode.sql.dk.Constants;
     5.7 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
     5.8 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
     5.9 +import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
    5.10  import java.util.Collections;
    5.11  import java.util.HashMap;
    5.12  import java.util.List;
    5.13 @@ -29,10 +32,10 @@
    5.14   *
    5.15   * @author Ing. František Kučera (frantovo.cz)
    5.16   */
    5.17 +@PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION)
    5.18  public class TeXFormatter extends AbstractFormatter {
    5.19  
    5.20  	public static final String NAME = "tex"; // bash-completion:formatter
    5.21 -	public static final String PROPERTY_COLORFUL = "color";
    5.22  	private static final ColorfulPrintWriter.TerminalColor COMMAND_COLOR = ColorfulPrintWriter.TerminalColor.Magenta;
    5.23  	private static final ColorfulPrintWriter.TerminalColor OPTIONS_COLOR = ColorfulPrintWriter.TerminalColor.Yellow;
    5.24  	private static final Map<Character, String> TEX_ESCAPE_MAP;
    5.25 @@ -58,7 +61,7 @@
    5.26  
    5.27  	public TeXFormatter(FormatterContext formatterContext) {
    5.28  		super(formatterContext);
    5.29 -		boolean colorful = formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, false);
    5.30 +		boolean colorful = formatterContext.getProperties().getBoolean(COLORFUL, false);
    5.31  		out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful);
    5.32  	}
    5.33  
     6.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java	Sat Aug 15 10:04:28 2015 +0200
     6.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java	Sat Aug 15 10:20:39 2015 +0200
     6.3 @@ -22,6 +22,7 @@
     6.4  import info.globalcode.sql.dk.configuration.DatabaseDefinition;
     6.5  import static info.globalcode.sql.dk.Functions.notNull;
     6.6  import info.globalcode.sql.dk.NamedParameter;
     6.7 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
     6.8  import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
     6.9  import java.sql.Array;
    6.10  import java.sql.SQLException;
    6.11 @@ -33,13 +34,16 @@
    6.12  import javax.xml.namespace.QName;
    6.13  
    6.14  /**
    6.15 - * <p>Prints machine-readable output – XML document containing resultsets and updates count. Good
    6.16 + * <p>
    6.17 + * Prints machine-readable output – XML document containing resultsets and updates count. Good
    6.18   * choice for further processing – e.g. XSL transformation.</p>
    6.19   *
    6.20 - * <p>TODO: XSD</p>
    6.21 + * <p>
    6.22 + * TODO: XSD</p>
    6.23   *
    6.24   * @author Ing. František Kučera (frantovo.cz)
    6.25   */
    6.26 +@PropertyDeclaration(name = XmlFormatter.PROPERTY_LABELED_COLUMNS, type = Boolean.class, description = "whether to add 'label' attribute to each 'column' element")
    6.27  public class XmlFormatter extends AbstractXmlFormatter {
    6.28  
    6.29  	public static final String NAME = "xml"; // bash-completion:formatter
    6.30 @@ -158,9 +162,7 @@
    6.31  			attributes.put(qname("null"), "true");
    6.32  			printEmptyElement(qname("column"), attributes);
    6.33  		} else if (value instanceof Array) {
    6.34 -			
    6.35 -			
    6.36 -			
    6.37 +
    6.38  			Array sqlArray = (Array) value;
    6.39  			try {
    6.40  				Object[] array = (Object[]) sqlArray.getArray();
    6.41 @@ -171,13 +173,12 @@
    6.42  				log.log(Level.SEVERE, "Unable to format array", e);
    6.43  				writeColumnValue(String.valueOf(value));
    6.44  			}
    6.45 -			
    6.46 -			
    6.47 +
    6.48  		} else {
    6.49  			printTextElement(qname("column"), attributes, toString(value));
    6.50  		}
    6.51  	}
    6.52 -	
    6.53 +
    6.54  	private void printArray(Object[] array) {
    6.55  		printStartElement(qname("array"));
    6.56  		for (Object o : array) {