This is an automated email from the ASF dual-hosted git repository. duncangrant pushed a commit to branch prettyPrintWinrmXml in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
commit 516afd31af67302b4f2839bedef95db46fe7b027 Author: Duncan Grant <duncan.gr...@cloudsoft.io> AuthorDate: Mon Jan 18 22:53:01 2021 +0000 Refactor and comment --- .../internal/winrm/winrm4j/PrettyXmlWriter.java | 75 ++++++++++++++-------- .../winrm/winrm4j/PrettyXmlWriterTest.java | 3 +- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/software/winrm/src/main/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/PrettyXmlWriter.java b/software/winrm/src/main/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/PrettyXmlWriter.java index 381caa9..2b126fe 100644 --- a/software/winrm/src/main/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/PrettyXmlWriter.java +++ b/software/winrm/src/main/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/PrettyXmlWriter.java @@ -22,7 +22,7 @@ import java.io.IOException; import java.io.Writer; public class PrettyXmlWriter extends Writer { - private Writer wrappedWriter; + private final Writer wrappedWriter; private boolean tagClosed = Boolean.FALSE; private int indentLevel = 0; private boolean newLine = Boolean.TRUE; @@ -39,6 +39,7 @@ public class PrettyXmlWriter extends Writer { for (int i = off; i < off + len; i++) { char c = cbuf[i]; if (isComment) { + // Currently in a comment (weird MS comment not xml comment) if (c == '\n') { isComment = false; writeNewLine(); @@ -46,39 +47,57 @@ public class PrettyXmlWriter extends Writer { writeChar(c); } } else if (newLine && c == '#') { + // MS CLI XML uses this to start a comment isComment = true; writeChar(c); } else { - if (c == '<') { - lastChar = '<'; - if (!newLine) { - indentLevel--; - } else if (i + 1 < off + len && cbuf[i + 1] == '/') { - indentLevel--; - printIndent(); - indentLevel--; - } else { - printIndent(); - } - } - writeChar(c); - if ('>' == c || tagClosed) { - if (i + 1 < off + len) { - if (cbuf[i + 1] == '<') { - writeNewLine(); - if (lastChar != '/') { - indentLevel++; - } - } - } else { - tagClosed = true; - } - } else { - tagClosed = false; + //Not a comment - treat as XML + handleMeaningfulChar(cbuf, i, c, off + len - 1); + } + } + } + + private void handleMeaningfulChar(char[] cbuf, int i, char c, int end) throws IOException { + if (tagClosed) { + // Tag was closed on last call to write so need a new line + writeNewLine(); + tagClosed = false; + } + if (c == '<') { + // Start if a tag + if (!newLine) { + // Assume closing tag following text - e.g. <t>some text</t> + indentLevel--; + } else if (i < end && cbuf[i + 1] == '/') { + // Closing tag + indentLevel--; + printIndent(); + indentLevel--; + } else { + // + printIndent(); + } + } + writeChar(c); + if ('>' == c ) { + if (i < end) { + if (cbuf[i + 1] == '<') { + writeNewLine(); + increaseIndentIfNotSelfClosingTag(); } - lastChar = c; + } else { + // We've closed a tag but don't know what the next character is + tagClosed = true; + increaseIndentIfNotSelfClosingTag(); } } + lastChar = c; + } + + private void increaseIndentIfNotSelfClosingTag() { + if (lastChar != '/') { + indentLevel++; + } } private void writeNewLine() throws IOException { diff --git a/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/PrettyXmlWriterTest.java b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/PrettyXmlWriterTest.java index 4ffb2bc..1a6409a 100644 --- a/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/PrettyXmlWriterTest.java +++ b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/PrettyXmlWriterTest.java @@ -75,8 +75,7 @@ public class PrettyXmlWriterTest { prettyXmlWriter.write(xmlPart1, 0, xmlPart1.length()); prettyXmlWriter.write(xmlPart2, 0, xmlPart2.length()); - int newLines = countNewLines(); - assertEquals(newLines,3); + assertEquals(recordingWriter.out.toString(), "<t1>\n\t<t2>fdskljfds</t2>\n\t<t3>djskdsjk</t3>\n</t1>"); } @Test