You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2010/02/10 23:46:30 UTC

svn commit: r908711 - in /ofbiz/trunk/framework/base: build.xml src/org/ofbiz/base/util/IndentingWriter.java src/org/ofbiz/base/util/test/IndentingWriterTests.java testdef/basetests.xml

Author: doogie
Date: Wed Feb 10 22:46:29 2010
New Revision: 908711

URL: http://svn.apache.org/viewvc?rev=908711&view=rev
Log:
A writer that allows calling code to add non-important spaces, tabs, and
new lines, then this extra formatting can easily be turned off.

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/IndentingWriter.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/IndentingWriterTests.java
Modified:
    ofbiz/trunk/framework/base/build.xml
    ofbiz/trunk/framework/base/testdef/basetests.xml

Modified: ofbiz/trunk/framework/base/build.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/build.xml?rev=908711&r1=908710&r2=908711&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/build.xml (original)
+++ ofbiz/trunk/framework/base/build.xml Wed Feb 10 22:46:29 2010
@@ -38,6 +38,7 @@
     </path>
 
     <filelist id="test.classes" dir="${src.dir}">
+        <file name="org/ofbiz/base/util/test/IndentingWriterTests.java"/>
         <file name="org/ofbiz/base/conversion/test/MiscTests.java"/>
         <file name="org/ofbiz/base/util/test/UtilIOTests.java"/>
         <file name="org/ofbiz/base/test/BaseUnitTests.java"/>

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/IndentingWriter.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/IndentingWriter.java?rev=908711&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/IndentingWriter.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/IndentingWriter.java Wed Feb 10 22:46:29 2010
@@ -0,0 +1,120 @@
+/*******************************************************************************
+ * 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.ofbiz.base.util;
+
+import java.io.IOException;
+import java.io.FilterWriter;
+import java.io.Writer;
+
+public class IndentingWriter extends FilterWriter {
+    protected final StringBuilder indent = new StringBuilder();
+    protected final boolean doSpace;
+    protected final boolean doNewline;
+    protected boolean lastWasNewline;
+
+    public IndentingWriter(Writer out, boolean doSpace, boolean doNewline) {
+        super(out);
+        this.doSpace = doSpace;
+        this.doNewline = doNewline;
+    }
+
+    public IndentingWriter(Writer out) {
+        this(out, true, true);
+    }
+
+    public IndentingWriter newline() throws IOException {
+        lastWasNewline = true;
+        if (doNewline) super.write('\n');
+        return this;
+    }
+
+    protected void checkAfterNewline() throws IOException {
+        if (lastWasNewline) {
+            if (doSpace) {
+                if (doNewline) {
+                    super.write(indent.toString(), 0, indent.length());
+                } else {
+                    super.write(' ');
+                }
+            }
+            lastWasNewline = false;
+        }
+    }
+
+    public IndentingWriter push() {
+        indent.append(' ');
+        return this;
+    }
+
+    public IndentingWriter pop() {
+        indent.setLength(indent.length() - 1);
+        return this;
+    }
+
+    public IndentingWriter space() throws IOException {
+        checkAfterNewline();
+        if (doSpace) super.write(' ');
+        return this;
+    }
+
+    public void write(char[] buf) throws IOException {
+        write(buf, 0, buf.length);
+    }
+
+    public void write(char[] buf, int offset, int length) throws IOException {
+        int i;
+        for (i = offset; i < length; i++) {
+            if (buf[i] == '\n') {
+                checkAfterNewline();
+                super.write(buf, offset, i - offset + 1);
+                offset = i + 1;
+                lastWasNewline = true;
+            }
+        }
+        checkAfterNewline();
+        super.write(buf, offset, i - offset);
+    }
+
+    public void write(int c) throws IOException {
+        checkAfterNewline();
+        super.write(c);
+        if (c == '\n') {
+            lastWasNewline = true;
+            checkAfterNewline();
+        }
+    }
+
+    public void write(String s) throws IOException {
+        write(s, 0, s.length());
+    }
+
+    public void write(String s, int offset, int length) throws IOException {
+        int i;
+        for (i = offset; i < length; i++) {
+            if (s.charAt(i) == '\n') {
+                checkAfterNewline();
+                super.write(s, offset, i - offset + 1);
+                offset = i + 1;
+                lastWasNewline = true;
+            }
+        }
+        checkAfterNewline();
+        super.write(s, offset, i - offset);
+    }
+}

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/IndentingWriterTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/IndentingWriterTests.java?rev=908711&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/IndentingWriterTests.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/IndentingWriterTests.java Wed Feb 10 22:46:29 2010
@@ -0,0 +1,67 @@
+/*******************************************************************************
+ * 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.ofbiz.base.util.test;
+
+import java.io.StringWriter;
+
+import org.ofbiz.base.util.IndentingWriter;
+import org.ofbiz.base.test.GenericTestCaseBase;
+
+public class IndentingWriterTests extends GenericTestCaseBase {
+    public IndentingWriterTests(String name) {
+        super(name);
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    private static void doTest(String label, boolean doSpace, boolean doNewline, String wanted) throws Exception {
+        StringWriter sw = new StringWriter();
+        IndentingWriter iw;
+        if (!doSpace || !doNewline) {
+            iw = new IndentingWriter(sw, doSpace, doNewline);
+        } else {
+            iw = new IndentingWriter(sw);
+        }
+        iw.write((int) 'a');
+        iw.push();
+        iw.write("b\nm");
+        iw.newline();
+        iw.write(new char[] {'1', '\n', '2'});
+        iw.space();
+        iw.write((int) '\n');
+        iw.pop();
+        iw.write("e");
+        iw.close();
+        System.err.println("wanted(" + wanted.replaceAll(" ", "-") + ") got(" + sw.toString().replaceAll(" ", "-") + ")");
+        assertEquals(label, wanted, sw.toString());
+    }
+
+    public void testIndentingWriter() throws Exception {
+        doTest("IndentingWriter doSpace:doNewline", true, true, "ab\n m\n 1\n 2 \n e");
+        doTest("IndentingWriter doNewline", false, true, "ab\nm\n1\n2\ne");
+        doTest("IndentingWriter doSpace", true, false, "ab\n m 1\n 2 \n e");
+        doTest("IndentingWriter", false, false, "ab\nm1\n2\ne");
+    }
+}

Modified: ofbiz/trunk/framework/base/testdef/basetests.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/testdef/basetests.xml?rev=908711&r1=908710&r2=908711&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/testdef/basetests.xml (original)
+++ ofbiz/trunk/framework/base/testdef/basetests.xml Wed Feb 10 22:46:29 2010
@@ -21,6 +21,7 @@
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/test-suite.xsd">
     <test-case case-name="basetests">
+        <junit-test-suite class-name="org.ofbiz.base.util.test.IndentingWriterTests"/>
         <junit-test-suite class-name="org.ofbiz.base.conversion.test.MiscTests.java"/>
         <junit-test-suite class-name="org.ofbiz.base.util.test.UtilIOTests"/>
         <junit-test-suite class-name="org.ofbiz.base.test.BaseUnitTests"/>