# HG changeset patch # User František Kučera # Date 1439626839 -7200 # Node ID e2f24eea85438abcdc5097ceee62d70645230af7 # Parent d6624c3b146a7bfff6d971edf9143bdfa8a15e34 property annotations (documentation) for particular formatters diff -r d6624c3b146a -r e2f24eea8543 java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java Sat Aug 15 10:04:28 2015 +0200 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java Sat Aug 15 10:20:39 2015 +0200 @@ -23,6 +23,9 @@ import javax.xml.namespace.QName; import static info.globalcode.sql.dk.Functions.isEmpty; import static info.globalcode.sql.dk.Functions.toHex; +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.nio.charset.Charset; import java.util.EmptyStackException; import java.util.HashMap; @@ -44,10 +47,12 @@ * * @author Ing. František Kučera (frantovo.cz) */ +@PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION) +@PropertyDeclaration(name = AbstractXmlFormatter.PROPERTY_INDENT, type = String.class, description = "tab or sequence of spaces used for indentation of nested elements") +@PropertyDeclaration(name = AbstractXmlFormatter.PROPERTY_INDENT_TEXT, type = Boolean.class, description = "whether to indent text nodes") public abstract class AbstractXmlFormatter extends AbstractFormatter { private static final Logger log = Logger.getLogger(AbstractXmlFormatter.class.getName()); - public static final String PROPERTY_COLORFUL = "color"; public static final String PROPERTY_INDENT = "indent"; /** * Whether text with line breaks should be indented (default). Otherwise original whitespace @@ -66,7 +71,7 @@ public AbstractXmlFormatter(FormatterContext formatterContext) { super(formatterContext); - boolean colorful = formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, false); + boolean colorful = formatterContext.getProperties().getBoolean(COLORFUL, false); out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful); indent = formatterContext.getProperties().getString(PROPERTY_INDENT, "\t"); indentText = formatterContext.getProperties().getBoolean(PROPERTY_INDENT_TEXT, true); diff -r d6624c3b146a -r e2f24eea8543 java/sql-dk/src/info/globalcode/sql/dk/formatting/CommonProperties.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/CommonProperties.java Sat Aug 15 10:20:39 2015 +0200 @@ -0,0 +1,28 @@ +/** + * SQL-DK + * Copyright © 2015 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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 . + */ +package info.globalcode.sql.dk.formatting; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class CommonProperties { + + public static final String COLORFUL = "color"; + public static final String COLORFUL_DESCRIPTION = "whether the output should be printed in color (escape sequences)"; +} diff -r d6624c3b146a -r e2f24eea8543 java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java Sat Aug 15 10:04:28 2015 +0200 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java Sat Aug 15 10:20:39 2015 +0200 @@ -18,6 +18,9 @@ package info.globalcode.sql.dk.formatting; import info.globalcode.sql.dk.ColorfulPrintWriter; +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; /** * Formatter intended for printing one record (or few records) with many columns. @@ -25,17 +28,17 @@ * * @author Ing. František Kučera (frantovo.cz) */ +@PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION) public class SingleRecordFormatter extends AbstractFormatter { public static final String NAME = "record"; // bash-completion:formatter - public static final String PROPERTY_COLORFUL = "color"; private final ColorfulPrintWriter out; private boolean firstResult = true; public SingleRecordFormatter(FormatterContext formatterContext) { super(formatterContext); out = new ColorfulPrintWriter(formatterContext.getOutputStream()); - out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true)); + out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true)); } @Override diff -r d6624c3b146a -r e2f24eea8543 java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Sat Aug 15 10:04:28 2015 +0200 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Sat Aug 15 10:20:39 2015 +0200 @@ -22,6 +22,9 @@ import static info.globalcode.sql.dk.Functions.lpad; import static info.globalcode.sql.dk.Functions.rpad; import static info.globalcode.sql.dk.Functions.repeat; +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.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -40,13 +43,15 @@ * @see TabularPrefetchingFormatter * @see TabularWrappingFormatter */ +@PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION) +@PropertyDeclaration(name = TabularFormatter.PROPERTY_ASCII, type = Boolean.class, description = "whether to use ASCII table borders instead of unicode ones") +@PropertyDeclaration(name = TabularFormatter.PROPERTY_TRIM, type = Boolean.class, description = "whether to trim the values to fit the column width") public class TabularFormatter extends AbstractFormatter { public static final String NAME = "tabular"; // bash-completion:formatter private static final String HEADER_TYPE_PREFIX = " ("; private static final String HEADER_TYPE_SUFFIX = ")"; public static final String PROPERTY_ASCII = "ascii"; - public static final String PROPERTY_COLORFUL = "color"; public static final String PROPERTY_TRIM = "trim"; private static final String NBSP = " "; private static final Pattern whitespaceToReplace = Pattern.compile("\\n|\\r|\\t|" + NBSP); @@ -67,7 +72,7 @@ out = new ColorfulPrintWriter(formatterContext.getOutputStream()); asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false); trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false); - out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true)); + out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true)); } @Override diff -r d6624c3b146a -r e2f24eea8543 java/sql-dk/src/info/globalcode/sql/dk/formatting/TeXFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TeXFormatter.java Sat Aug 15 10:04:28 2015 +0200 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TeXFormatter.java Sat Aug 15 10:20:39 2015 +0200 @@ -19,6 +19,9 @@ 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; @@ -29,10 +32,10 @@ * * @author Ing. František Kučera (frantovo.cz) */ +@PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION) public class TeXFormatter extends AbstractFormatter { public static final String NAME = "tex"; // bash-completion:formatter - public static final String PROPERTY_COLORFUL = "color"; private static final ColorfulPrintWriter.TerminalColor COMMAND_COLOR = ColorfulPrintWriter.TerminalColor.Magenta; private static final ColorfulPrintWriter.TerminalColor OPTIONS_COLOR = ColorfulPrintWriter.TerminalColor.Yellow; private static final Map TEX_ESCAPE_MAP; @@ -58,7 +61,7 @@ public TeXFormatter(FormatterContext formatterContext) { super(formatterContext); - boolean colorful = formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, false); + boolean colorful = formatterContext.getProperties().getBoolean(COLORFUL, false); out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful); } diff -r d6624c3b146a -r e2f24eea8543 java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java Sat Aug 15 10:04:28 2015 +0200 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java Sat Aug 15 10:20:39 2015 +0200 @@ -22,6 +22,7 @@ import info.globalcode.sql.dk.configuration.DatabaseDefinition; import static info.globalcode.sql.dk.Functions.notNull; import info.globalcode.sql.dk.NamedParameter; +import info.globalcode.sql.dk.configuration.PropertyDeclaration; import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname; import java.sql.Array; import java.sql.SQLException; @@ -33,13 +34,16 @@ import javax.xml.namespace.QName; /** - *

Prints machine-readable output – XML document containing resultsets and updates count. Good + *

+ * Prints machine-readable output – XML document containing resultsets and updates count. Good * choice for further processing – e.g. XSL transformation.

* - *

TODO: XSD

+ *

+ * TODO: XSD

* * @author Ing. František Kučera (frantovo.cz) */ +@PropertyDeclaration(name = XmlFormatter.PROPERTY_LABELED_COLUMNS, type = Boolean.class, description = "whether to add 'label' attribute to each 'column' element") public class XmlFormatter extends AbstractXmlFormatter { public static final String NAME = "xml"; // bash-completion:formatter @@ -158,9 +162,7 @@ attributes.put(qname("null"), "true"); printEmptyElement(qname("column"), attributes); } else if (value instanceof Array) { - - - + Array sqlArray = (Array) value; try { Object[] array = (Object[]) sqlArray.getArray(); @@ -171,13 +173,12 @@ log.log(Level.SEVERE, "Unable to format array", e); writeColumnValue(String.valueOf(value)); } - - + } else { printTextElement(qname("column"), attributes, toString(value)); } } - + private void printArray(Object[] array) { printStartElement(qname("array")); for (Object o : array) {