1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java Sat Jan 04 19:54:11 2014 +0100
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java Sat Jan 04 20:08:56 2014 +0100
1.3 @@ -41,6 +41,11 @@
1.4 private static final Logger log = Logger.getLogger(AbstractXmlFormatter.class.getName());
1.5 public static final String PROPERTY_COLORFUL = "color";
1.6 public static final String PROPERTY_INDENT = "indent";
1.7 + /**
1.8 + * Whether text with line breaks should be indented (default). Otherwise original whitespace
1.9 + * will be preserved.
1.10 + */
1.11 + public static final String PROPERTY_INDENT_TEXT = "indentText";
1.12 private static final TerminalColor ELEMENT_COLOR = TerminalColor.Magenta;
1.13 private static final TerminalColor ATTRIBUTE_NAME_COLOR = TerminalColor.Green;
1.14 private static final TerminalColor ATTRIBUTE_VALUE_COLOR = TerminalColor.Yellow;
1.15 @@ -48,12 +53,14 @@
1.16 private Stack<QName> treePosition = new Stack<>();
1.17 private final ColorfulPrintWriter out;
1.18 private final String indent;
1.19 + private final boolean indentText;
1.20
1.21 public AbstractXmlFormatter(FormatterContext formatterContext) {
1.22 super(formatterContext);
1.23 boolean colorful = formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, false);
1.24 out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful);
1.25 indent = formatterContext.getProperties().getString(PROPERTY_INDENT, "\t");
1.26 + indentText = formatterContext.getProperties().getBoolean(PROPERTY_INDENT_TEXT, true);
1.27
1.28 if (!indent.matches("\\s*")) {
1.29 log.log(Level.WARNING, "Setting indent to „{0}“ is weird & freaky; in hex: {1}", new Object[]{indent, toHex(indent.getBytes())});
1.30 @@ -123,8 +130,21 @@
1.31 */
1.32 protected void printTextElement(QName element, Map<QName, String> attributes, String text) {
1.33 printStartElement(element, attributes);
1.34 - printText(text, false);
1.35 - printEndElement(false);
1.36 +
1.37 + String[] lines = text.split("\\n");
1.38 +
1.39 + if (indentText && lines.length > 1) {
1.40 + for (String line : lines) {
1.41 + printText(line, true);
1.42 + }
1.43 + printEndElement(true);
1.44 + } else {
1.45 + /*
1.46 + * line breaks at the end of the text will be eaten – if you need them, use indentText = false
1.47 + */
1.48 + printText(lines[0], false);
1.49 + printEndElement(false);
1.50 + }
1.51 }
1.52
1.53 protected void printEmptyElement(QName element, Map<QName, String> attributes) {
1.54 @@ -151,11 +171,7 @@
1.55 }
1.56 }
1.57
1.58 - protected void printText(String s) {
1.59 - printText(s, true);
1.60 - }
1.61 -
1.62 - private void printText(String s, boolean indent) {
1.63 + protected void printText(String s, boolean indent) {
1.64 if (indent) {
1.65 printIndent();
1.66 }