45 * Must be used with care – bad usage can lead to invalid XML (e.g. using undeclared namespaces). |
45 * Must be used with care – bad usage can lead to invalid XML (e.g. using undeclared namespaces). |
46 * </p> |
46 * </p> |
47 * |
47 * |
48 * @author Ing. František Kučera (frantovo.cz) |
48 * @author Ing. František Kučera (frantovo.cz) |
49 */ |
49 */ |
50 @PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION) |
50 @PropertyDeclaration(name = COLORFUL, defaultValue = "false", type = Boolean.class, description = COLORFUL_DESCRIPTION) |
51 @PropertyDeclaration(name = AbstractXmlFormatter.PROPERTY_INDENT, type = String.class, description = "tab or sequence of spaces used for indentation of nested elements") |
51 @PropertyDeclaration(name = AbstractXmlFormatter.PROPERTY_INDENT, defaultValue = AbstractXmlFormatter.PROPERTY_INDENT_DEFAULT, type = String.class, description = "tab or sequence of spaces used for indentation of nested elements") |
52 @PropertyDeclaration(name = AbstractXmlFormatter.PROPERTY_INDENT_TEXT, type = Boolean.class, description = "whether to indent text nodes") |
52 @PropertyDeclaration(name = AbstractXmlFormatter.PROPERTY_INDENT_TEXT, defaultValue = "true", type = Boolean.class, description = "whether text with line breaks should be indented; if not original whitespace will be preserved.") |
53 public abstract class AbstractXmlFormatter extends AbstractFormatter { |
53 public abstract class AbstractXmlFormatter extends AbstractFormatter { |
54 |
54 |
55 private static final Logger log = Logger.getLogger(AbstractXmlFormatter.class.getName()); |
55 private static final Logger log = Logger.getLogger(AbstractXmlFormatter.class.getName()); |
56 public static final String PROPERTY_INDENT = "indent"; |
56 public static final String PROPERTY_INDENT = "indent"; |
57 /** |
57 protected static final String PROPERTY_INDENT_DEFAULT = "\t"; |
58 * Whether text with line breaks should be indented (default). Otherwise original whitespace |
|
59 * will be preserved. |
|
60 */ |
|
61 public static final String PROPERTY_INDENT_TEXT = "indentText"; |
58 public static final String PROPERTY_INDENT_TEXT = "indentText"; |
62 private static final TerminalColor ELEMENT_COLOR = TerminalColor.Magenta; |
59 private static final TerminalColor ELEMENT_COLOR = TerminalColor.Magenta; |
63 private static final TerminalColor ATTRIBUTE_NAME_COLOR = TerminalColor.Green; |
60 private static final TerminalColor ATTRIBUTE_NAME_COLOR = TerminalColor.Green; |
64 private static final TerminalColor ATTRIBUTE_VALUE_COLOR = TerminalColor.Yellow; |
61 private static final TerminalColor ATTRIBUTE_VALUE_COLOR = TerminalColor.Yellow; |
65 private static final TerminalColor XML_DECLARATION_COLOR = TerminalColor.Red; |
62 private static final TerminalColor XML_DECLARATION_COLOR = TerminalColor.Red; |
71 |
68 |
72 public AbstractXmlFormatter(FormatterContext formatterContext) { |
69 public AbstractXmlFormatter(FormatterContext formatterContext) { |
73 super(formatterContext); |
70 super(formatterContext); |
74 boolean colorful = formatterContext.getProperties().getBoolean(COLORFUL, false); |
71 boolean colorful = formatterContext.getProperties().getBoolean(COLORFUL, false); |
75 out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful); |
72 out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful); |
76 indent = formatterContext.getProperties().getString(PROPERTY_INDENT, "\t"); |
73 indent = formatterContext.getProperties().getString(PROPERTY_INDENT, PROPERTY_INDENT_DEFAULT); |
77 indentText = formatterContext.getProperties().getBoolean(PROPERTY_INDENT_TEXT, true); |
74 indentText = formatterContext.getProperties().getBoolean(PROPERTY_INDENT_TEXT, true); |
78 |
75 |
79 if (!indent.matches("\\s*")) { |
76 if (!indent.matches("\\s*")) { |
80 log.log(Level.WARNING, "Setting indent to „{0}“ is weird & freaky; in hex: {1}", new Object[]{indent, toHex(indent.getBytes())}); |
77 log.log(Level.WARNING, "Setting indent to „{0}“ is weird & freaky; in hex: {1}", new Object[]{indent, toHex(indent.getBytes())}); |
81 } |
78 } |