--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java Sat Jan 04 19:54:11 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java Sat Jan 04 20:08:56 2014 +0100
@@ -41,6 +41,11 @@
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
+ * will be preserved.
+ */
+ public static final String PROPERTY_INDENT_TEXT = "indentText";
private static final TerminalColor ELEMENT_COLOR = TerminalColor.Magenta;
private static final TerminalColor ATTRIBUTE_NAME_COLOR = TerminalColor.Green;
private static final TerminalColor ATTRIBUTE_VALUE_COLOR = TerminalColor.Yellow;
@@ -48,12 +53,14 @@
private Stack<QName> treePosition = new Stack<>();
private final ColorfulPrintWriter out;
private final String indent;
+ private final boolean indentText;
public AbstractXmlFormatter(FormatterContext formatterContext) {
super(formatterContext);
boolean colorful = formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, false);
out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful);
indent = formatterContext.getProperties().getString(PROPERTY_INDENT, "\t");
+ indentText = formatterContext.getProperties().getBoolean(PROPERTY_INDENT_TEXT, true);
if (!indent.matches("\\s*")) {
log.log(Level.WARNING, "Setting indent to „{0}“ is weird & freaky; in hex: {1}", new Object[]{indent, toHex(indent.getBytes())});
@@ -123,8 +130,21 @@
*/
protected void printTextElement(QName element, Map<QName, String> attributes, String text) {
printStartElement(element, attributes);
- printText(text, false);
- printEndElement(false);
+
+ String[] lines = text.split("\\n");
+
+ if (indentText && lines.length > 1) {
+ for (String line : lines) {
+ printText(line, true);
+ }
+ printEndElement(true);
+ } else {
+ /*
+ * line breaks at the end of the text will be eaten – if you need them, use indentText = false
+ */
+ printText(lines[0], false);
+ printEndElement(false);
+ }
}
protected void printEmptyElement(QName element, Map<QName, String> attributes) {
@@ -151,11 +171,7 @@
}
}
- protected void printText(String s) {
- printText(s, true);
- }
-
- private void printText(String s, boolean indent) {
+ protected void printText(String s, boolean indent) {
if (indent) {
printIndent();
}