You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2009/01/18 23:32:17 UTC

svn commit: r735565 - in /webservices/commons/trunk/modules/tcpmon: ./ modules/tcpmon-core/ modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/ modules/tcpmon-core/src/test/ modules/tcpmon-core/src/test/java/ modules/tcpmon-core...

Author: veithen
Date: Sun Jan 18 14:32:16 2009
New Revision: 735565

URL: http://svn.apache.org/viewvc?rev=735565&view=rev
Log:
Improved XmlFormatFilter.

Added:
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/TestUtil.java   (with props)
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilterTest.java   (with props)
Modified:
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml
    webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilter.java
    webservices/commons/trunk/modules/tcpmon/pom.xml

Modified: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml?rev=735565&r1=735564&r2=735565&view=diff
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml (original)
+++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/pom.xml Sun Jan 18 14:32:16 2009
@@ -36,6 +36,11 @@
             <artifactId>geronimo-activation_1.1_spec</artifactId>
             <version>1.0.2</version>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

Modified: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilter.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilter.java?rev=735565&r1=735564&r2=735565&view=diff
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilter.java (original)
+++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/main/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilter.java Sun Jan 18 14:32:16 2009
@@ -21,6 +21,7 @@
  */
 public class XmlFormatFilter implements StreamFilter {
     private final int tabWidth;
+    private boolean firstIndent = true;
     private int nextIndent = -1;
     private int previousIndent = -1;
     
@@ -30,32 +31,29 @@
 
     public void invoke(Stream stream) {
         try {
-            boolean inXML = false;
             while (stream.available() > 0) {
-                int thisIndent = -1;
+                boolean doIndent = false;
                 if (stream.get(0) == '<' && stream.get(1) != '/') {
                     previousIndent = nextIndent++;
-                    thisIndent = nextIndent;
-                    inXML = true;
+                    doIndent = true;
                 } else if (stream.get(0) == '<' && stream.get(1) == '/') {
-                    if (previousIndent > nextIndent) {
-                        thisIndent = nextIndent;
-                    }
+                    doIndent = previousIndent > nextIndent;
                     previousIndent = nextIndent--;
-                    inXML = true;
-                } else if (stream.get(0) == '/' && stream.get(1) == '>') {
+                } else if ((stream.get(0) == '/' || stream.get(0) == '?')
+                        && stream.get(1) == '>') {
                     previousIndent = nextIndent--;
-                    inXML = true;
                 }
-                if (thisIndent != -1) {
-                    if (thisIndent > 0) {
+                if (doIndent) {
+                    if (firstIndent) {
+                        firstIndent = false;
+                    } else {
                         stream.insert((byte) '\n');
                     }
-                    for (int i = tabWidth * thisIndent; i > 0; i--) {
+                    for (int i = tabWidth * nextIndent; i > 0; i--) {
                         stream.insert((byte) ' ');
                     }
                 }
-                if (!inXML || (stream.get(0) != '\n' && stream.get(0) != '\r')) {
+                if (stream.get(0) != '\n' && stream.get(0) != '\r') {
                     stream.skip();
                 } else {
                     stream.discard();

Added: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/TestUtil.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/TestUtil.java?rev=735565&view=auto
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/TestUtil.java (added)
+++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/TestUtil.java Sun Jan 18 14:32:16 2009
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.ws.commons.tcpmon.core.filter;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+public class TestUtil {
+    private TestUtil() {}
+    
+    public static byte[] filter(StreamFilter filter, byte[] in) {
+        Pipeline pipeline = new Pipeline();
+        pipeline.addFilter(filter);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        pipeline.addFilter(new Tee(baos));
+        ByteArrayInputStream bais = new ByteArrayInputStream(in);
+        try {
+            while (pipeline.readFrom(bais) != -1) {
+                // Just loop
+            }
+        } catch (IOException ex) {
+            throw new Error("Unexpected IOException when reading from ByteArrayInputStream", ex);
+        }
+        return baos.toByteArray();
+    }
+    
+    public static String filter(StreamFilter filter, String in, String charsetName) throws UnsupportedEncodingException {
+        return new String(filter(filter, in.getBytes(charsetName)), charsetName);
+    }
+    
+    public static String filter(StreamFilter filter, String in) {
+        try {
+            return filter(filter, in, "utf-8");
+        } catch (UnsupportedEncodingException ex) {
+            throw new Error(ex);
+        }
+    }
+}

Propchange: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/TestUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilterTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilterTest.java?rev=735565&view=auto
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilterTest.java (added)
+++ webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilterTest.java Sun Jan 18 14:32:16 2009
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.ws.commons.tcpmon.core.filter;
+
+import junit.framework.TestCase;
+
+public class XmlFormatFilterTest extends TestCase {
+    private static void assertFormat(String expected, String in) {
+        assertEquals(expected, TestUtil.filter(new XmlFormatFilter(2), in));
+    }
+    
+    public void test1() {
+        assertFormat("<root>\n  <a/>\n</root>", "<root><a/></root>");
+    }
+
+    public void test2() {
+        assertFormat("<?xml version=\"1.0\"?>\n<root>\n  <a/>\n</root>",
+                     "<?xml version=\"1.0\"?><root><a/></root>");
+    }
+
+    public void test3() {
+        assertFormat("<root>\n  <a>test</a>\n</root>", "<root><a>test</a></root>");
+    }
+}

Propchange: webservices/commons/trunk/modules/tcpmon/modules/tcpmon-core/src/test/java/org/apache/ws/commons/tcpmon/core/filter/XmlFormatFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/tcpmon/pom.xml
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/tcpmon/pom.xml?rev=735565&r1=735564&r2=735565&view=diff
==============================================================================
--- webservices/commons/trunk/modules/tcpmon/pom.xml (original)
+++ webservices/commons/trunk/modules/tcpmon/pom.xml Sun Jan 18 14:32:16 2009
@@ -58,6 +58,15 @@
             scm:svn:https://svn.apache.org/repos/asf/webservices/commons/trunk/modules/tcpmon</developerConnection>
         <url>http://svn.apache.org/viewcvs.cgi/webservices/commons/trunk/modules/tcpmon</url>
     </scm>
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>junit</groupId>
+                <artifactId>junit</artifactId>
+                <version>3.8.2</version>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
     <modules>
         <module>modules/tcpmon-core</module>
         <module>modules/tcpmon-ui</module>