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 47d61999fe4381bce7b7ca40b4283ace983a1222
Author: Duncan Grant <duncan.gr...@cloudsoft.io>
AuthorDate: Mon Jan 18 22:13:35 2021 +0000

    Skip comments
---
 .../internal/winrm/winrm4j/PrettyXmlWriter.java    | 57 +++++++++++--------
 .../winrm/winrm4j/PrettyXmlWriterTest.java         | 65 +++++++++++++++++-----
 2 files changed, 85 insertions(+), 37 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 4c26646..381caa9 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
@@ -25,8 +25,9 @@ public class PrettyXmlWriter extends Writer {
     private Writer wrappedWriter;
     private boolean tagClosed = Boolean.FALSE;
     private int indentLevel = 0;
-    private boolean newLine = true;
+    private boolean newLine = Boolean.TRUE;
     private char lastChar = '\n';
+    private boolean isComment = Boolean.FALSE;
 
     public PrettyXmlWriter(Writer writer) {
         super(writer);
@@ -37,34 +38,46 @@ public class PrettyXmlWriter extends Writer {
     public void write(char[] cbuf, int off, int len) throws IOException {
         for (int i = off; i < off + len; i++) {
             char c = cbuf[i];
-            if (c == '<') {
-                lastChar = '<';
-                if(!newLine) {
-                    indentLevel--;
-                } else if (i + 1 < off + len && cbuf[i + 1] == '/') {
-                    indentLevel--;
-                    printIndent();
-                    indentLevel--;
+            if (isComment) {
+                if (c == '\n') {
+                    isComment = false;
+                    writeNewLine();
                 } else {
-                    printIndent();
+                    writeChar(c);
                 }
-            }
-            writeChar(c);
-            if ('>' == c || tagClosed) {
-                if (i + 1 < off + len) {
-                    if (cbuf[i + 1] == '<') {
-                        writeNewLine();
-                        if (lastChar != '/') {
-                            indentLevel++;
+            } else if (newLine && c == '#') {
+                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 = true;
+                    tagClosed = false;
                 }
-            } else {
-                tagClosed = false;
+                lastChar = c;
             }
-            lastChar = c;
         }
     }
 
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 7e28e35..4ffb2bc 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
@@ -1,8 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.apache.brooklyn.util.core.internal.winrm.winrm4j;
 
-import org.testng.Assert;
 import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
 
 import java.io.IOException;
@@ -13,14 +29,25 @@ import static org.testng.Assert.*;
 
 public class PrettyXmlWriterTest {
 
-    public static final String XML = "<?xml version=\"1.0\" ?><Objs 
xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"; 
Version=\"1.1.0.1\"><Obj S=\"progress\" RefId=\"0\"><TN 
RefId=\"0\"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64
 N=\"SourceId\">1</I64><PR N=\"Record\"><AV>Preparing modules for first 
use.</AV><AI>0</AI><Nil/><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD/></PR></MS></Obj></Objs>";
-    private RecordingWriter writer;
+    public static final String XML = "<Objs 
xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"; 
Version=\"1.1.0.1\"><Obj S=\"progress\" RefId=\"0\"><TN 
RefId=\"0\"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64
 N=\"SourceId\">1</I64><PR N=\"Record\"><AV>Preparing modules for first 
use.</AV><AI>0</AI><Nil/><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD/></PR></MS></Obj></Objs>";
+    public static final String REAL_XML = "#< CLIXML\n" +
+            "<Objs Version=\"1.1.0.1\" 
xmlns=\"http://schemas.microsoft.com/powershell/2004/04\";><Obj S=\"progress\" 
RefId=\"0\"><TN 
RefId=\"0\"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64
 N=\"SourceId\">1</I64><PR N=\"Record\"><AV>Preparing modules for first 
use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> 
</SD></PR></MS></Obj><Obj S=\"progress\" RefId=\"1\"><TNRef RefId=\"0\" 
/><MS><I64 N=\"SourceId\">1</I64><PR N=\ [...]
+    private RecordingWriter recordingWriter;
     private PrettyXmlWriter prettyXmlWriter;
 
     @BeforeMethod
     public void setUp() {
-        writer = new RecordingWriter();
-        prettyXmlWriter = new PrettyXmlWriter(writer);
+        recordingWriter = new RecordingWriter();
+        prettyXmlWriter = new PrettyXmlWriter(recordingWriter);
+    }
+
+    @Test
+    public void testREAL() throws IOException {
+        prettyXmlWriter.write(REAL_XML, 0, REAL_XML.length());
+        prettyXmlWriter.flush();
+
+        String s = recordingWriter.out.toString();
+        System.out.println(s);
     }
 
     @Test
@@ -28,7 +55,7 @@ public class PrettyXmlWriterTest {
         prettyXmlWriter.write(XML, 0, XML.length());
 
         int newLines = countNewLines();
-        assertEquals(newLines,21);
+        assertEquals(newLines,20);
     }
 
     @Test
@@ -57,7 +84,7 @@ public class PrettyXmlWriterTest {
         String xml = "<t1><t2>";
         prettyXmlWriter.write(xml, 0, xml.length());
 
-        assertEquals(writer.out.toString(), "<t1>\n\t<t2>");
+        assertEquals(recordingWriter.out.toString(), "<t1>\n\t<t2>");
     }
 
     @Test
@@ -65,7 +92,7 @@ public class PrettyXmlWriterTest {
         String xml = "<t1><t2><t3>";
         prettyXmlWriter.write(xml, 0, xml.length());
 
-        assertEquals(writer.out.toString(), "<t1>\n\t<t2>\n\t\t<t3>");
+        assertEquals(recordingWriter.out.toString(), "<t1>\n\t<t2>\n\t\t<t3>");
     }
 
     @Test
@@ -73,7 +100,7 @@ public class PrettyXmlWriterTest {
         String xml = "<t1><t2></t2>";
         prettyXmlWriter.write(xml, 0, xml.length());
 
-        assertEquals(writer.out.toString(), "<t1>\n\t<t2>\n\t</t2>");
+        assertEquals(recordingWriter.out.toString(), "<t1>\n\t<t2>\n\t</t2>");
     }
 
     @Test
@@ -81,7 +108,7 @@ public class PrettyXmlWriterTest {
         String xml = "<t1><t2></t2></t1>";
         prettyXmlWriter.write(xml, 0, xml.length());
 
-        assertEquals(writer.out.toString(), "<t1>\n\t<t2>\n\t</t2>\n</t1>");
+        assertEquals(recordingWriter.out.toString(), 
"<t1>\n\t<t2>\n\t</t2>\n</t1>");
     }
 
     @Test
@@ -89,7 +116,7 @@ public class PrettyXmlWriterTest {
         String xml = "<t1><t2><t3>Some Text</t3></t2></t1>";
         prettyXmlWriter.write(xml, 0, xml.length());
 
-        assertEquals(writer.out.toString(), "<t1>\n\t<t2>\n\t\t<t3>Some 
Text</t3>\n\t</t2>\n</t1>");
+        assertEquals(recordingWriter.out.toString(), 
"<t1>\n\t<t2>\n\t\t<t3>Some Text</t3>\n\t</t2>\n</t1>");
     }
 
     @Test
@@ -97,12 +124,20 @@ public class PrettyXmlWriterTest {
         String xml = "<t1><t2><t3/></t2></t1>";
         prettyXmlWriter.write(xml, 0, xml.length());
 
-        assertEquals(writer.out.toString(), 
"<t1>\n\t<t2>\n\t\t<t3/>\n\t</t2>\n</t1>");
+        assertEquals(recordingWriter.out.toString(), 
"<t1>\n\t<t2>\n\t\t<t3/>\n\t</t2>\n</t1>");
+    }
+
+    @Test
+    public void testIgnoreComment() throws IOException {
+       String xml = "#< CLIXML\n<t1><t2><t3/></t2></t1>";
+        prettyXmlWriter.write(xml, 0, xml.length());
+
+        assertEquals(recordingWriter.out.toString(), "#< 
CLIXML\n<t1>\n\t<t2>\n\t\t<t3/>\n\t</t2>\n</t1>");
     }
 
     private int countNewLines() {
         int newLines = 0;
-        String s = writer.out.toString();
+        String s = recordingWriter.out.toString();
         for (char c : s.toCharArray()) {
             if(c=='\n') newLines++;
         }
@@ -111,7 +146,7 @@ public class PrettyXmlWriterTest {
 
     static class RecordingWriter extends Writer {
 
-        StringBuffer out = new StringBuffer();
+        StringBuilder out = new StringBuilder();
 
         @Override
         public void write(int c) throws IOException {

Reply via email to