sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1413150 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/internal/util/ main/java/org/apache/sis/io/ main/java/org/apache/sis/measure/ main/java/org/apache/sis/util/ test/java/org/apache/sis/internal/util/
Date Sat, 24 Nov 2012 08:25:50 GMT
Author: desruisseaux
Date: Sat Nov 24 08:25:49 2012
New Revision: 1413150

URL: http://svn.apache.org/viewvc?rev=1413150&view=rev
Log:
Removed the internal X364.toHTML(String) method, which is probably not appropriate for this
module.
Avoid some calls to String.substring(...) since it copies the data since recent JDK,
and minor documentation cleaning.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/X364.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/TableFormatter.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/package-info.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/util/X364Test.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/X364.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/X364.java?rev=1413150&r1=1413149&r2=1413150&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/X364.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/X364.java Sat
Nov 24 08:25:49 2012
@@ -17,11 +17,16 @@
 package org.apache.sis.internal.util;
 
 import org.apache.sis.util.CharSequences;
-import org.apache.sis.util.StringBuilders;
 
 
 /**
- * Escape codes from ANSI X3.64 standard (aka ECMA-48 and ISO/IEC 6429).
+ * A limited set of color and font attributes assignable to characters at formatting time.
+ * Those attributes are used by SIS formatters for providing some syntax coloring,
+ * for example in the <code>org.apache.sis.io.wkt</code> package.
+ *
+ * <p>This enumeration is restricted to a subset of the <cite>ANSI escape codes</cite>
(a.k.a.
+ * ECMA-48, ISO/IEC 6429 and X3.64 standards) because SIS uses them mostly for syntax coloring
in
+ * console outputs. However those attributes can also occasionally be used for HTML rendering.</p>
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @since   0.3 (derived from geotk-2.4)
@@ -131,9 +136,9 @@ public enum X364 {
     }
 
     /**
-     * Returns the X3.64 escape sequence.
+     * Returns the ANSI escape sequence.
      *
-     * @return The X3.64 escape sequence.
+     * @return The ANSI escape sequence.
      */
     public String sequence() {
         if (sequence == null) {
@@ -147,86 +152,22 @@ public enum X364 {
     }
 
     /**
-     * Replaces escape codes in the given string by HTML {@code <font>} instructions.
-     * If no HTML instruction is associated to the given escape code, then the escape
-     * sequence is removed.
-     *
-     * @param  text The text with X3.64 sequences.
-     * @return The text with HTML {@code <font>} instructions.
-     */
-    public static String toHTML(final String text) {
-        final StringBuilder buffer = new StringBuilder(text);
-        StringBuilders.replace(buffer, "&", "&amp;");
-        StringBuilders.replace(buffer, "<", "&lt;");
-        StringBuilders.replace(buffer, ">", "&gt;");
-        boolean fontApplied = false;
-        StringBuilder tmp = null;
-        for (int i=buffer.indexOf(START); i>=0; i=buffer.indexOf(START, i)) {
-            int lower  = i + START.length();
-            int upper  = lower;
-            int length = buffer.length();
-            while (upper < length) {
-                if (buffer.charAt(upper++) == END) {
-                    break;
-                }
-            }
-            final int code;
-            try {
-                code = Integer.parseInt(buffer.substring(lower, upper-1));
-            } catch (NumberFormatException e) {
-                buffer.delete(i, upper);
-                continue;
-            }
-            final String color;
-            switch (code) {
-                case 31: color="red";     break;
-                case 32: color="green";   break;
-                case 33: color="olive";   break; // "yellow" is too bright.
-                case 34: color="blue";    break;
-                case 35: color="magenta"; break;
-                case 36: color="teal";    break; // "cyan" is not in HTML 4, while "teal"
is.
-                case 37: color="gray";    break;
-                case 39: // Fall through
-                case 0:  color=null; break;
-                default: {
-                    buffer.delete(i, upper);
-                    continue;
-                }
-            }
-            if (tmp == null) {
-                tmp = new StringBuilder(24);
-            }
-            if (fontApplied) {
-                tmp.append("</font>");
-                fontApplied = false;
-            }
-            if (color != null) {
-                tmp.append("<font color=\"").append(color).append("\">");
-                fontApplied = true;
-            }
-            buffer.replace(i, upper, tmp.toString());
-            tmp.setLength(0);
-        }
-        final String result = buffer.toString();
-        return result.equals(text) ? text : result;
-    }
-
-    /**
      * Removes all escape codes from the given string.
      *
-     * @param  text The string which may contains escape codes.
+     * @param  text      The string which may contains escape codes.
+     * @param  fromIndex The index from which to start the process.
+     * @param  toIndex   The index after the last character to process.
      * @return Text without the escape codes, or the given {@code text} reference if
      *         it didn't contained any escape codes.
      */
-    public static String plain(final String text) {
-        int i = text.indexOf(START);
+    public static CharSequence plain(final CharSequence text, int fromIndex, final int toIndex)
{
+        int i = CharSequences.indexOf(text, START, fromIndex, toIndex);
         if (i >= 0) {
             StringBuilder buffer = null;
-            int last = 0;
 search:     do {
                 final int start = i;
                 i += START.length();
-                final int end = text.indexOf(END, i);
+                final int end = CharSequences.indexOf(text, END, i, toIndex);
                 if (end < 0) {
                     break;
                 }
@@ -237,13 +178,13 @@ search:     do {
                     }
                 }
                 if (buffer == null) {
-                    buffer = new StringBuilder(text.length() - last);
+                    buffer = new StringBuilder(toIndex - fromIndex);
                 }
-                buffer.append(text, last, start);
-                last = ++i; // The ++ is for skipping the END character.
-            } while ((i = text.indexOf(START, i)) >= 0);
+                buffer.append(text, fromIndex, start);
+                fromIndex = ++i; // The ++ is for skipping the END character.
+            } while ((i = CharSequences.indexOf(text, START, i, toIndex)) >= 0);
             if (buffer != null) {
-                return buffer.append(text, last, text.length()).toString();
+                return buffer.append(text, fromIndex, toIndex);
             }
         }
         return text;
@@ -255,20 +196,22 @@ search:     do {
      * CharSequences.codePointCount}({@linkplain #plain plain}(text))</code> without
the
      * cost of creating a temporary string.
      *
-     * @param  text The string which may contains escape codes.
+     * @param  text      The string which may contains escape codes.
+     * @param  fromIndex The index from which to start the computation.
+     * @param  toIndex   The index after the last character to take in account.
      * @return The length of the given string without escape codes.
      */
-    public static int lengthOfPlain(final String text) {
-        int i = text.indexOf(START);
+    public static int lengthOfPlain(final CharSequence text, final int fromIndex, final int
toIndex) {
+        int i = CharSequences.indexOf(text, START, fromIndex, toIndex);
         if (i < 0) {
-            return text.codePointCount(0, text.length());
+            return CharSequences.codePointCount(text, fromIndex, toIndex);
         }
-        int last   = 0;
+        int last   = fromIndex;
         int length = 0;
 search: do {
             final int start = i;
             i += START.length();
-            final int end = text.indexOf(END, i);
+            final int end = CharSequences.indexOf(text, END, i, toIndex);
             if (end < 0) {
                 break;
             }
@@ -278,16 +221,16 @@ search: do {
                     continue search; // Not an X.364 sequence.
                 }
             }
-            length += text.codePointCount(last, start);
+            length += CharSequences.codePointCount(text, last, start);
             last = ++i; // The ++ is for skipping the END character.
-        } while ((i = text.indexOf(START, i)) >= 0);
-        length += text.codePointCount(last, text.length());
-        assert CharSequences.codePointCount(plain(text)) == length : text;
+        } while ((i = CharSequences.indexOf(text, START, i, toIndex)) >= 0);
+        length += CharSequences.codePointCount(text, last, toIndex);
+        assert CharSequences.codePointCount(plain(text, fromIndex, toIndex)) == length :
text.subSequence(fromIndex, toIndex);
         return length;
     }
 
     /**
-     * Returns {@code true} if we think that the operating system supports X3.64 sequences.
+     * Returns {@code true} if we think that the operating system supports ANSI sequences.
      * This method performs a very naive and approximative check. Result is just a hint and
      * may be wrong.
      *
@@ -296,10 +239,10 @@ search: do {
      * error stream} instead than the {@linkplain System#out standard output stream}, in
which
      * case the console information is not applicable.</p>
      *
-     * @return {@code true} if we think that the operating system supports X3.64.
+     * @return {@code true} if we think that the operating system supports ANSI codes.
      *         This method may conservatively returns {@code false} in case of doubt.
      */
-    public static boolean isSupported() {
+    public static boolean isAnsiSupported() {
         String terminal;
         try {
             terminal = System.getenv("COLORTERM");

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/TableFormatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/TableFormatter.java?rev=1413150&r1=1413149&r2=1413150&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/TableFormatter.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/TableFormatter.java Sat
Nov 24 08:25:49 2012
@@ -19,10 +19,10 @@ package org.apache.sis.io;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
-import java.util.StringTokenizer;
 import java.io.Flushable;
 import java.io.IOException;
 import org.apache.sis.util.Decorator;
+import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.internal.util.X364;
@@ -594,16 +594,23 @@ public class TableFormatter extends Filt
         if (currentColumn >= maximalColumnWidths.length) {
             maximalColumnWidths = Arrays.copyOf(maximalColumnWidths, currentColumn+1);
         }
-        int length = 0;
-        final StringTokenizer tk = new StringTokenizer(cellText, "\r\n");
-        while (tk.hasMoreTokens()) {
-            final int lg = X364.lengthOfPlain(tk.nextToken());
-            if (lg > length) {
-                length = lg;
+        int width     = 0;
+        int lineStart = 0;
+        final int length = cellText.length();
+        while (lineStart < length) {
+            final int nextLine = CharSequences.indexOfLineStart(cellText, 1, lineStart);
+            for (int i=nextLine; --i >= lineStart;) {
+                if (!Character.isISOControl(cellText.charAt(i))) {
+                    final int lg = X364.lengthOfPlain(cellText, lineStart, i+1);
+                    if (lg > width) {
+                        width = lg;
+                    }
+                }
             }
+            lineStart = nextLine;
         }
-        if (length > maximalColumnWidths[currentColumn]) {
-            maximalColumnWidths[currentColumn] = length;
+        if (width > maximalColumnWidths[currentColumn]) {
+            maximalColumnWidths[currentColumn] = width;
         }
         currentColumn++;
         buffer.setLength(0);
@@ -779,7 +786,7 @@ public class TableFormatter extends Filt
                         endOfFirstLine = next;
                     }
                     currentLine[j] = remaining;
-                    textLength = X364.lengthOfPlain(cellText);
+                    textLength = X364.lengthOfPlain(cellText, 0, cellText.length());
                     /*
                      * If the cell to write is actually a border, do a special processing
                      * in order to use the characters defined in the BOX static constant.

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=1413150&r1=1413149&r2=1413150&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 Sat Nov
24 08:25:49 2012
@@ -24,6 +24,11 @@
  * replaces all occurrence of {@code '\t'} by the amount of spaces needed for producing a
tabular
  * output.
  *
+ * {@note One of the formatter classes defined in this package is a <code>java.text.Format</code>
+ * subclass. While traditionally though as part of <code>text</code> packages,
that {@code Format}
+ * is defined in this I/O package because it can format to an <code>Appendable</code>
and for
+ * consistency with the <code>org.apache.sis.io.wkt</code> package.}
+ *
  * {@section Unicode characters}
  * Some formatters in this package make extensive use of Unicode characters. This may produce
  * unexpected results in a Windows console, unless the underlying output stream uses the
correct

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java?rev=1413150&r1=1413149&r2=1413150&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/AngleFormat.java Sat
Nov 24 08:25:49 2012
@@ -1010,8 +1010,7 @@ scan:   for (int i=0; i<length;) {
 
     /**
      * Formats an angle, latitude or longitude value as an attributed character iterator.
-     * Callers can iterator over the returned iterator and queries the attribute values as
-     * in the following example:
+     * Callers can iterate and queries the attribute values as in the following example:
      *
      * {@preformat java
      *     AttributedCharacterIterator it = angleFormat.formatToCharacterIterator(myAngle);

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java?rev=1413150&r1=1413149&r2=1413150&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/CharSequences.java Sat
Nov 24 08:25:49 2012
@@ -160,7 +160,7 @@ public final class CharSequences extends
      * @param  text The character sequence from which to get the count, or {@code null}.
      * @return The number of Unicode code points, or 0 if the argument is {@code null}.
      *
-     * @see Character#codePointCount(CharSequence, int, int)
+     * @see #codePointCount(CharSequence, int, int)
      */
     public static int codePointCount(final CharSequence text) {
         if (text == null)                  return 0;
@@ -177,6 +177,40 @@ public final class CharSequences extends
     }
 
     /**
+     * Returns the number of Unicode code points in the given characters sub-sequence,
+     * or 0 if {@code null}. Unpaired surrogates within the text count as one code
+     * point each.
+     *
+     * <p>This method performs the same work than the standard
+     * {@link Character#codePointCount(CharSequence, int, int)} method, except that it tries
+     * to delegate to the optimized methods from the {@link String}, {@link StringBuilder},
+     * {@link StringBuffer} or {@link CharBuffer} classes if possible.</p>
+     *
+     * @param  text      The character sequence from which to get the count, or {@code null}.
+     * @param  fromIndex The index from which to start the computation.
+     * @param  toIndex   The index after the last character to take in account.
+     * @return The number of Unicode code points, or 0 if the argument is {@code null}.
+     *
+     * @see Character#codePointCount(CharSequence, int, int)
+     * @see String#codePointCount(int, int)
+     * @see StringBuilder#codePointCount(int, int)
+     */
+    public static int codePointCount(final CharSequence text, final int fromIndex, final
int toIndex) {
+        if (text == null)                  return 0;
+        if (text instanceof String)        return ((String)        text).codePointCount(fromIndex,
toIndex);
+        if (text instanceof StringBuilder) return ((StringBuilder) text).codePointCount(fromIndex,
toIndex);
+        if (text instanceof StringBuffer)  return ((StringBuffer)  text).codePointCount(fromIndex,
toIndex);
+        if (text instanceof CharBuffer) {
+            final CharBuffer buffer = (CharBuffer) text;
+            if (buffer.hasArray() && !buffer.isReadOnly()) {
+                final int position = buffer.position();
+                return Character.codePointCount(buffer.array(), position + fromIndex, position
+ toIndex);
+            }
+        }
+        return Character.codePointCount(text, fromIndex, toIndex);
+    }
+
+    /**
      * Returns the number of occurrences of the {@code toSearch} string in the given {@code
text}.
      * The search is case-sensitive.
      *

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/util/X364Test.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/util/X364Test.java?rev=1413150&r1=1413149&r2=1413150&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/util/X364Test.java
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/internal/util/X364Test.java
Sat Nov 24 08:25:49 2012
@@ -18,7 +18,9 @@ package org.apache.sis.internal.util;
 
 import org.junit.Test;
 import org.apache.sis.test.TestCase;
+import org.apache.sis.test.DependsOn;
 
+import static java.lang.String.valueOf;
 import static org.junit.Assert.*;
 import static org.apache.sis.internal.util.X364.*;
 
@@ -31,6 +33,7 @@ import static org.apache.sis.internal.ut
  * @version 0.3
  * @module
  */
+@DependsOn(org.apache.sis.util.CharSequencesTest.class)
 public final strictfp class X364Test extends TestCase {
     /**
      * Tests the {@link X364#plain(String)} method.
@@ -40,13 +43,13 @@ public final strictfp class X364Test ext
         String colored, plain;
         colored = "Some plain text";
         plain   = "Some plain text";
-        assertEquals(plain, plain(colored));
-        assertEquals(plain.length(), lengthOfPlain(colored));
+        assertEquals(plain,          valueOf(plain(colored, 0, colored.length())));
+        assertEquals(plain.length(), lengthOfPlain(colored, 0, colored.length()));
 
         plain   = "With blue in the middle";
         colored = "With " + FOREGROUND_BLUE.sequence() +
                   "blue"  + FOREGROUND_DEFAULT.sequence() + " in the middle";
-        assertEquals(plain, plain(colored));
-        assertEquals(plain.length(), lengthOfPlain(colored));
+        assertEquals(plain,          valueOf(plain(colored, 0, colored.length())));
+        assertEquals(plain.length(), lengthOfPlain(colored, 0, colored.length()));
     }
 }



Mime
View raw message