sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1781968 - in /sis/branches/JDK8/core: sis-feature/src/main/java/org/apache/sis/feature/ sis-metadata/src/main/java/org/apache/sis/io/wkt/ sis-referencing/src/main/java/org/apache/sis/geometry/ sis-referencing/src/main/java/org/apache/sis/p...
Date Tue, 07 Feb 2017 03:27:06 GMT
Author: desruisseaux
Date: Tue Feb  7 03:27:06 2017
New Revision: 1781968

URL: http://svn.apache.org/viewvc?rev=1781968&view=rev
Log:
Change CompoundFormat.parse(CharSequence text, ParsePosition pos) javadoc for making it consistent
with all implementation except TreeFormat,
and update TreeFormat implementation for making it consistent with new specification. JIRA:
SIS-348

Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/geometry/CoordinateFormat.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java?rev=1781968&r1=1781967&r2=1781968&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
[UTF-8] Tue Feb  7 03:27:06 2017
@@ -501,7 +501,7 @@ header: for (int i=0; ; i++) {
     @Override
     public Object parse(final CharSequence text, final ParsePosition pos) throws ParseException
{
         throw new ParseException(Errors.getResources(displayLocale)
-                .getString(Errors.Keys.UnsupportedOperation_1, "parse"), 0);
+                .getString(Errors.Keys.UnsupportedOperation_1, "parse"), pos.getIndex());
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java?rev=1781968&r1=1781967&r2=1781968&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
[UTF-8] Tue Feb  7 03:27:06 2017
@@ -700,10 +700,12 @@ public class WKTFormat extends CompoundF
     /**
      * Creates an object from the given character sequence.
      * The parsing begins at the index given by the {@code pos} argument.
+     * After successful parsing, {@link ParsePosition#getIndex()} gives the position after
the last parsed character.
+     * In case of error, {@link ParseException#getErrorOffset()} gives the position of the
first illegal character.
      *
      * @param  wkt  the character sequence for the object to parse.
      * @param  pos  the position where to start the parsing.
-     * @return the parsed object.
+     * @return the parsed object (never {@code null}).
      * @throws ParseException if an error occurred while parsing the WKT.
      */
     @Override

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/geometry/CoordinateFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/geometry/CoordinateFormat.java?rev=1781968&r1=1781967&r2=1781968&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/geometry/CoordinateFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/geometry/CoordinateFormat.java
[UTF-8] Tue Feb  7 03:27:06 2017
@@ -565,25 +565,16 @@ public class CoordinateFormat extends Co
     /**
      * Parses a coordinate from the given character sequence.
      * This method presumes that the coordinate reference system is the {@linkplain #getDefaultCRS()
default CRS}.
-     * The parsing begins at the index given by the {@code pos} argument. If parsing succeeds,
then:
-     *
-     * <ul>
-     *   <li>The {@code pos} {@linkplain ParsePosition#getIndex() index}
-     *       is updated to the index after the last ordinate value.</li>
-     *   <li>The parsed coordinate is returned.</li>
-     * </ul>
-     *
-     * If parsing fails, then:
-     *
-     * <ul>
-     *   <li>The {@code pos} index is left unchanged</li>
-     *   <li>The {@code pos} {@linkplain ParsePosition#getErrorIndex() error index}
-     *       is set to the beginning of the unparsable ordinate value.</li>
-     * </ul>
+     * The parsing begins at the {@linkplain ParsePosition#getIndex() index} given by the
{@code pos} argument.
+     * If parsing succeeds, then the {@code pos} index is updated to the index after the
last ordinate value and
+     * the parsed coordinate is returned. Otherwise (if parsing fails), the {@code pos} index
is left unchanged,
+     * the {@code pos} {@linkplain ParsePosition#getErrorIndex() error index} is set to the
index of the first
+     * unparsable character and an exception is thrown with a similar {@linkplain ParseException#getErrorOffset()
+     * error index}.
      *
      * @param  text  the character sequence for the coordinate to parse.
      * @param  pos   the index where to start the parsing.
-     * @return the parsed coordinate.
+     * @return the parsed coordinate (never {@code null}).
      * @throws ParseException if an error occurred while parsing the coordinate.
      */
     @Override

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java?rev=1781968&r1=1781967&r2=1781968&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterFormat.java
[UTF-8] Tue Feb  7 03:27:06 2017
@@ -1012,7 +1012,7 @@ public class ParameterFormat extends Tab
     @Override
     public Object parse(final CharSequence text, final ParsePosition pos) throws ParseException
{
         throw new ParseException(Errors.getResources(displayLocale)
-                .getString(Errors.Keys.UnsupportedOperation_1, "parse"), 0);
+                .getString(Errors.Keys.UnsupportedOperation_1, "parse"), pos.getIndex());
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java?rev=1781968&r1=1781967&r2=1781968&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
[UTF-8] Tue Feb  7 03:27:06 2017
@@ -209,25 +209,28 @@ public abstract class CompoundFormat<T>
      *   <li>The {@code pos} index is left unchanged</li>
      *   <li>The {@code pos} {@linkplain ParsePosition#getErrorIndex() error index}
      *       is set to the beginning of the unparsable character sequence.</li>
-     *   <li>A {@code ParseException} is thrown with an
-     *       {@linkplain ParseException#getErrorOffset() error offset} relative to the above-cited
-     *       {@code pos} error index. Consequently the exact error location is <var>{@code
pos}
-     *       error index</var> + <var>{@code ParseException} error offset</var>.</li>
+     *   <li>One of the following actions is taken (at implementation choice):
+     *     <ul>
+     *       <li>this method returns {@code null}, or</li>
+     *       <li>a {@code ParseException} is thrown with an {@linkplain ParseException#getErrorOffset()
error offset}
+     *           set to the index of the first unparsable character. This is usually the
same information than the
+     *           above-cited {@code pos} error index, but implementations are free to adopt
a slightly different policy.</li>
+     *     </ul>
+     *   </li>
      * </ul>
      *
      * <div class="note"><b>Example:</b>
      * if parsing of the {@code "30.0 40,0"} coordinate fails on the coma in the last number,
then the {@code pos}
-     * error index will be set to 5 (the beginning of the {@code "40.0"} character sequence)
while the
-     * {@link ParseException} error offset will be set to 2 (the coma position relative the
beginning
-     * of the {@code "40.0"} character sequence).</div>
-     *
-     * This error offset policy is a consequence of the compound nature of {@code CompoundFormat},
-     * since the exception may have been produced by a call to {@link Format#parseObject(String)}
-     * on one of the {@linkplain #getFormat(Class) sub-formats} used by this {@code CompoundFormat}.
+     * {@linkplain ParsePosition#getErrorIndex() error index} may be set to 5 (the beginning
of the {@code "40.0"}
+     * character sequence) or to 7 (the coma position), depending on the implementation.</div>
+     *
+     * Most implementations never return {@code null}. However some implementations may choose
to return {@code null}
+     * if they can determine that the given text is not a supported format and reserve {@code
ParseException} for the
+     * cases where the text seems to be the expected format but contains a malformed element.
      *
      * @param  text  the character sequence for the object to parse.
      * @param  pos   the position where to start the parsing.
-     * @return the parsed object.
+     * @return the parsed object, or {@code null} if the text is not recognized.
      * @throws ParseException if an error occurred while parsing the object.
      */
     public abstract T parse(CharSequence text, ParsePosition pos) throws ParseException;
@@ -253,8 +256,6 @@ public abstract class CompoundFormat<T>
      * </ul>
      *
      * The default implementation delegates to {@link #parse(CharSequence, ParsePosition)}.
-     * In case of failure, the {@linkplain ParseException exception error offset} is added
-     * to the {@code pos} error index.
      *
      * @param  text  the string representation of the object to parse.
      * @param  pos   the position where to start the parsing.
@@ -265,7 +266,9 @@ public abstract class CompoundFormat<T>
         try {
             return parse(text, pos);
         } catch (ParseException e) {
-            pos.setErrorIndex(Math.max(pos.getIndex(), pos.getErrorIndex()) + e.getErrorOffset());
+            if (pos.getErrorIndex() < 0) {
+                pos.setErrorIndex(e.getErrorOffset());
+            }
             return null;
         }
     }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java?rev=1781968&r1=1781967&r2=1781968&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java
[UTF-8] Tue Feb  7 03:27:06 2017
@@ -31,6 +31,7 @@ import org.opengis.util.InternationalStr
 import org.apache.sis.io.TableAppender;
 import org.apache.sis.io.TabularFormat;
 import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.resources.Vocabulary;
 import org.apache.sis.util.collection.BackingStoreException;
 
@@ -48,7 +49,7 @@ import static java.lang.Math.*;
  *
  * @author  Martin Desruisseaux (MPO, IRD, Geomatys)
  * @since   0.3
- * @version 0.3
+ * @version 0.8
  * @module
  */
 public class StatisticsFormat extends TabularFormat<Statistics> {
@@ -209,14 +210,15 @@ public class StatisticsFormat extends Ta
     }
 
     /**
-     * Not yet implemented.
+     * Not yet supported.
      *
      * @return currently never return.
-     * @throws ParseException currently never thrown.
+     * @throws ParseException currently always thrown.
      */
     @Override
     public Statistics parse(CharSequence text, ParsePosition pos) throws ParseException {
-        throw new UnsupportedOperationException();
+        throw new ParseException(Errors.getResources(getLocale())
+                .getString(Errors.Keys.UnsupportedOperation_1, "parse"), pos.getIndex());
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java?rev=1781968&r1=1781967&r2=1781968&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
[UTF-8] Tue Feb  7 03:27:06 2017
@@ -324,7 +324,7 @@ public class TreeTableFormat extends Tab
 
     /**
      * Creates a tree from the given character sequence,
-     * or returns {@code null} if an error occurred while parsing the characters.
+     * or returns {@code null} if the given text does not look like a tree for this method.
      * This method can parse the trees created by the {@code format(…)} methods
      * defined in this class.
      *
@@ -338,12 +338,23 @@ public class TreeTableFormat extends Tab
      *     </ol>
      *   </li>
      *   <li>The number of spaces and drawing characters before the node values determines
the node
-     *       indentation. This indentation doesn't need to be a factor of the {@link #getIndentation()}
+     *       indentation. This indentation does not need to be a factor of the {@link #getIndentation()}
      *       value, but must be consistent across all the parsed tree.</li>
      *   <li>The indentation determines the parent of each node.</li>
      *   <li>Parsing stops at first empty line (ignoring whitespaces), or at the end
of the given text.</li>
      * </ul>
      *
+     * <div class="section">Error index</div>
+     * If the given text does not seem to be a tree table, then this method returns {@code
null}.
+     * Otherwise if parsing started but failed, then:
+     *
+     * <ul>
+     *   <li>{@link ParsePosition#getErrorIndex()} will give the index at the beginning
+     *       of line or beginning of cell where the error occurred, and</li>
+     *   <li>{@link ParseException#getErrorOffset()} will give either the same value,
+     *       or a slightly more accurate value inside the cell.</li>
+     * </ul>
+     *
      * @param  text  the character sequence for the tree to parse.
      * @param  pos   the position where to start the parsing.
      * @return the parsed tree, or {@code null} if the given character sequence can not be
parsed.
@@ -403,26 +414,31 @@ public class TreeTableFormat extends Tab
              * text are not parsed (the value is left to null).
              */
             final TreeTable.Node node = new DefaultTreeTable.Node(table);
-            try {
-                matcher.region(indexOfValue, endOfLine);
-                for (int ci=0; ci<columns.length; ci++) {
-                    final boolean found = matcher.find();
-                    int endOfColumn = found ? matcher.start() : endOfLine;
-                    indexOfValue   = CharSequences.skipLeadingWhitespaces (text, indexOfValue,
endOfColumn);
-                    int endOfValue = CharSequences.skipTrailingWhitespaces(text, indexOfValue,
endOfColumn);
-                    if (endOfValue > indexOfValue) {
-                        parseValue(node, columns[ci], formats[ci], text.subSequence(indexOfValue,
endOfValue).toString());
+            matcher.region(indexOfValue, endOfLine);
+            for (int ci=0; ci<columns.length; ci++) {
+                final boolean found = matcher.find();
+                int endOfColumn = found ? matcher.start() : endOfLine;
+                indexOfValue   = CharSequences.skipLeadingWhitespaces (text, indexOfValue,
endOfColumn);
+                int endOfValue = CharSequences.skipTrailingWhitespaces(text, indexOfValue,
endOfColumn);
+                if (endOfValue > indexOfValue) {
+                    final String valueText = text.subSequence(indexOfValue, endOfValue).toString();
+                    try {
+                        parseValue(node, columns[ci], formats[ci], valueText);
+                    } catch (ParseException | ClassCastException e) {
+                        pos.setErrorIndex(indexOfValue);                                
   // See method javadoc.
+                        if (e instanceof ParseException) {
+                            indexOfValue += ((ParseException) e).getErrorOffset();
+                        }
+                        throw new LocalizedParseException(getDisplayLocale(), Errors.Keys.UnparsableStringForClass_2,
+                                new Object[] {columns[ci].getElementType(), valueText}, indexOfValue).initCause(e);
                     }
-                    if (!found) break;
-                    /*
-                     * The end of this column will be the beginning of the next column,
-                     * after skipping the last character of the column separator.
-                     */
-                    indexOfValue = matcher.end();
                 }
-            } catch (ParseException e) {
-                pos.setErrorIndex(indexOfValue);
-                throw e;
+                if (!found) break;
+                /*
+                 * The end of this column will be the beginning of the next column,
+                 * after skipping the last character of the column separator.
+                 */
+                indexOfValue = matcher.end();
             }
             /*
              * If this is the first node created so far, it will be the root.
@@ -441,7 +457,7 @@ public class TreeTableFormat extends Tab
                     if (--indentationLevel < 0) {
                         pos.setErrorIndex(indexOfLineStart);
                         throw new LocalizedParseException(getDisplayLocale(),
-                                Errors.Keys.NodeHasNoParent_1, new Object[] {node}, 0);
+                                Errors.Keys.NodeHasNoParent_1, new Object[] {node}, indexOfLineStart);
                     }
                     lastNode = lastNode.getParent();
                 }
@@ -455,7 +471,7 @@ public class TreeTableFormat extends Tab
                     if (parent == null) {
                         pos.setErrorIndex(indexOfLineStart);
                         throw new LocalizedParseException(getDisplayLocale(),
-                                Errors.Keys.NodeHasNoParent_1, new Object[] {node}, 0);
+                                Errors.Keys.NodeHasNoParent_1, new Object[] {node}, indexOfLineStart);
                     }
                     parent.getChildren().add(node);
                 } else if (i > p) {
@@ -485,12 +501,16 @@ public class TreeTableFormat extends Tab
      * Parses the given string using a format appropriate for the type of values in
      * the given column, and stores the value in the given node.
      *
+     * <p>This work is done in a separated method instead than inlined in the
+     * {@code parse(…)} method because of the {@code <V>} parametric value.</p>
+     *
      * @param  V        the type of values in the given column.
      * @param  node     the node in which to set the value.
      * @param  column   the column in which to set the value.
      * @param  format   the format to use for parsing the value, or {@code null}.
      * @param  text     the textual representation of the value.
      * @throws ParseException if an error occurred while parsing.
+     * @throws ClassCastException if the parsed value is not of the expected type.
      */
     private <V> void parseValue(final TreeTable.Node node, final TableColumn<V>
column,
             final Format format, final String text) throws ParseException



Mime
View raw message