sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1403412 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/io/ test/java/org/apache/sis/io/ test/java/org/apache/sis/test/
Date Mon, 29 Oct 2012 16:43:03 GMT
Author: desruisseaux
Date: Mon Oct 29 16:43:03 2012
New Revision: 1403412

URL: http://svn.apache.org/viewvc?rev=1403412&view=rev
Log:
Merged IndentedLineFormatter with LineWrapFormatter.

Removed:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/IndentedLineFormatter.java
Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/LineWrapFormatter.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/package-info.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/io/IndentedLineFormatterTest.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Assert.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/LineWrapFormatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/LineWrapFormatter.java?rev=1403412&r1=1403411&r2=1403412&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/LineWrapFormatter.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/LineWrapFormatter.java Mon
Oct 29 16:43:03 2012
@@ -117,6 +117,17 @@ public class LineWrapFormatter extends F
     private boolean skipLF;
 
     /**
+     * {@code true} if the next character will be at the beginning of a new line.
+     * This flag is set to {@code true} only for "real" new lines, as a result of
+     * line separator found in the input given to this formatter. The "generated"
+     * new lines (resulting from line wrap) will invoke {@link #onLineBegin(boolean)}
+     * directly without the help of this temporary variable.
+     *
+     * @see #transfer(int)
+     */
+    private boolean isNewLine = true;
+
+    /**
      * {@code true} if an escape sequence is in progress. The escape sequence will stop
      * after the first non-digit character other than {@link X364#AFTER_ESCAPE}.
      */
@@ -310,6 +321,20 @@ public class LineWrapFormatter extends F
     }
 
     /**
+     * Writes the given amount of characters from the {@linkplain #buffer},
+     * then removes those characters from the buffer. This method does not
+     * adjust {@link #printableLength}; it is caller responsibility to do so.
+     */
+    private void transfer(final int length) throws IOException {
+        if (isNewLine) {
+            isNewLine = false;
+            onLineBegin(false);
+        }
+        out.append(buffer, 0, length);
+        buffer.delete(0, length);
+    }
+
+    /**
      * Writes the specified code point.
      *
      * @throws IOException If an I/O error occurs.
@@ -326,19 +351,21 @@ public class LineWrapFormatter extends F
          *   4) Write the line separator.
          */
         if (Characters.isLineOrParagraphSeparator(c)) {
-            buffer.setLength(printableLength); // Reduce the amount of work for StringBuilder.deleteCharAt(int).
-            deleteSoftHyphen();
-            out.append(buffer);
-            buffer.setLength(0);
-            printableLength  = 0;
-            codePointCount   = 0;
-            isEscapeSequence = false; // Handle line-breaks as "end of escape sequence".
             final boolean skip;
             switch (c) {
                 case '\r': skip = false;  skipLF = true;  break;
                 case '\n': skip = skipLF; skipLF = false; break;
                 default:   skip = false;  skipLF = false; break;
             }
+            if (!skip) {
+                buffer.setLength(printableLength); // Reduce the amount of work for StringBuilder.deleteCharAt(int).
+                deleteSoftHyphen();
+                transfer(printableLength);
+                printableLength  = 0;
+                codePointCount   = 0;
+                isEscapeSequence = false; // Handle line-breaks as "end of escape sequence".
+                isNewLine        = true;
+            }
             if (!isEndOfLineReplaced) {
                 appendCodePoint(c); // Forward EOL sequences "as-is".
             } else if (!skip) {
@@ -351,12 +378,15 @@ public class LineWrapFormatter extends F
          * If the character to write is a whitespace, then write any pending characters from
          * the buffer to the underlying appendable since we know that those characters didn't
          * exceeded the line length limit.
+         *
+         * We use Character.isWhitespace(…) instead of Character.isSpaceChar(…)
because
+         * the former returns 'true' tabulations (which we want), and returns 'false'
+         * for non-breaking spaces (which we also want).
          */
         if (Character.isWhitespace(c)) {
             if (printableLength != 0) {
                 deleteSoftHyphen();
-                out.append(buffer, 0, printableLength);
-                buffer.delete(0, printableLength);
+                transfer(printableLength);
                 printableLength = 0;
             }
             if (c != '\t') {
@@ -415,8 +445,7 @@ searchHyp:  for (int i=buffer.length(); 
                     }
                     case Characters.HYPHEN:
                     case Characters.SOFT_HYPHEN: {
-                        out.append(buffer, 0, i);
-                        buffer.delete(0, i);
+                        transfer(i);
                         break searchHyp;
                     }
                 }
@@ -438,6 +467,7 @@ searchHyp:  for (int i=buffer.length(); 
             }
             printableLength = buffer.length();
             codePointCount  = buffer.codePointCount(0, printableLength);
+            onLineBegin(true);
         }
     }
 
@@ -505,4 +535,21 @@ searchHyp:  for (int i=buffer.length(); 
         buffer.setLength(0);
         IO.flush(out);
     }
+
+    /**
+     * Invoked when a new line is beginning. The default implementation does nothing,
+     * but subclasses can override this method for example in order to insert a margin
+     * on the left side before each line.
+     *
+     * <p>If an implementation wishes to write characters, it shall do so by writing
+     * directly to {@link #out}, <strong>not</strong> by invoking the {@code
append}
+     * methods of this class.</p>
+     *
+     * @param  isContinuation {@code true} if the new line is the continuation of the previous
+     *         line after a "line wrap", or {@code false} if a line or paragraph separator
has
+     *         been explicitly sent to this formatter.
+     * @throws IOException if an error occurred while writing to {@link #out}.
+     */
+    protected void onLineBegin(boolean isContinuation) throws IOException {
+    }
 }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/package-info.java?rev=1403412&r1=1403411&r2=1403412&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/package-info.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/package-info.java Mon Oct
29 16:43:03 2012
@@ -19,8 +19,8 @@
  * Extensions to standard Java I/O ({@link java.io.Reader}, {@link java.io.Writer},
  * {@link java.lang.Appendable}).
  * Many classes defined in this package are filters applying on-the-fly formatting while
writing
- * text to the output device. For example {@link org.apache.sis.io.IndentedLineFormatter}
adds
- * indentation at the beginning of every new line, and {@link org.apache.sis.io.TableFormatter}
+ * text to the output device. For example {@link org.apache.sis.io.LineFormatter} can wrap
lines
+ * to some maximal line length (e.g. 80 characters), and {@link org.apache.sis.io.TableFormatter}
  * replaces all occurrence of {@code '\t'} by the amount of spaces needed for producing a
tabular
  * output.
  *

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/io/IndentedLineFormatterTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/io/IndentedLineFormatterTest.java?rev=1403412&r1=1403411&r2=1403412&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/io/IndentedLineFormatterTest.java
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/io/IndentedLineFormatterTest.java
Mon Oct 29 16:43:03 2012
@@ -24,7 +24,8 @@ import static org.junit.Assert.*;
 
 
 /**
- * Tests the {@link IndentedLineFormatter} implementation.
+ * Tests the {@link LineWrapFormatter} implementation
+ * when used for inserting a margin before every line.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-3.00)
@@ -37,7 +38,12 @@ public final strictfp class IndentedLine
      * Creates a new test case.
      */
     public IndentedLineFormatterTest() {
-        formatter = new IndentedLineFormatter(formatter, 4);
+        formatter = new LineWrapFormatter(formatter) {
+            @Override
+            protected void onLineBegin(boolean isContinuation) throws IOException {
+                out.append("    ");
+            }
+        };
     }
 
     /**
@@ -46,9 +52,6 @@ public final strictfp class IndentedLine
     @Override
     void run(final String lineSeparator) throws IOException {
         final Appendable out = formatter;
-        if (out instanceof IndentedLineFormatter) {
-            assertEquals("getMargin", "    ", ((IndentedLineFormatter) out).getMargin());
-        }
 
         // Extract from Arthur RIMBAUD (1854-1891), "Le bateau ivre"
         assertSame(out, out.append("Comme je descendais des Fleuves impassibles,\r"

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Assert.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Assert.java?rev=1403412&r1=1403411&r2=1403412&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Assert.java (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Assert.java Mon Oct 29
16:43:03 2012
@@ -79,7 +79,13 @@ public strictfp class Assert extends org
      * @param actual   The actual string.
      */
     public static void assertMultilinesEquals(final String message, final CharSequence expected,
final CharSequence actual) {
-        assertArrayEquals(message, CharSequences.split(expected, '\n'), CharSequences.split(actual,
'\n'));
+        final CharSequence[] a1 = CharSequences.split(expected, '\n');
+        final CharSequence[] a2 = CharSequences.split(actual,   '\n');
+        final int length = Math.min(a1.length, a2.length);
+        for (int i=0; i<length; i++) {
+            assertEquals("Line " + (i+1) + ':', a1[i], a2[i]);
+        }
+        assertArrayEquals(message, a1, a2);
     }
 
     /**



Mime
View raw message