You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2014/01/31 23:28:05 UTC
svn commit: r1563306 - in /sis/branches/JDK7/core:
sis-referencing/src/main/java/org/apache/sis/io/wkt/
sis-referencing/src/test/java/org/apache/sis/io/wkt/
sis-referencing/src/test/java/org/apache/sis/referencing/
sis-utility/src/main/java/org/apache/...
Author: desruisseaux
Date: Fri Jan 31 22:28:05 2014
New Revision: 1563306
URL: http://svn.apache.org/r1563306
Log:
Define WKTFormat as a subclass of CompoundFormat, in order to give control on the creation of the
NumberFormat or DateFormat to be used for the WKT values.
Modified:
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/SymbolsTest.java
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/Assert.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java?rev=1563306&r1=1563305&r2=1563306&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/FormattableObject.java [UTF-8] Fri Jan 31 22:28:05 2014
@@ -158,9 +158,7 @@ public class FormattableObject {
if (formatter == null) {
formatter = new Formatter();
}
- formatter.indentation = indentation;
- formatter.colors = colorize ? Colors.CONSOLE : null;
- formatter.setConvention(convention, null);
+ formatter.configure(convention, null, colorize ? Colors.CONSOLE : null, indentation);
final String wkt;
try {
/*
Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java?rev=1563306&r1=1563305&r2=1563306&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java [UTF-8] Fri Jan 31 22:28:05 2014
@@ -55,12 +55,15 @@ import org.apache.sis.referencing.operat
/**
* Formats {@linkplain FormattableObject formattable objects} as <cite>Well Known Text</cite> (WKT).
- * Each {@code Formatter} instance if created for a given {@linkplain Symbols set of symbols}.
- * For example in order to format an object with {@linkplain Symbols#CURLY_BRACKETS curly brackets}
- * instead of square ones and the whole text on the same line (no indentation), use the following:
+ * Each {@code Formatter} instance is created for a given {@linkplain Convention convention},
+ * {@linkplain Symbols set of symbols} and indentation.
+ *
+ * {@link Example}
+ * For formatting an object with {@linkplain Symbols#CURLY_BRACKETS curly brackets} instead of
+ * square ones and the whole text on the same line (no indentation), one can use the following:
*
* {@preformat java
- * Formatter formatter = new Formatter(Symbols.CURLY_BRACKETS, null, WKTFormat.SINGLE_LINE);
+ * Formatter formatter = new Formatter(Convention.DEFAULT, Symbols.CURLY_BRACKETS, WKTFormat.SINGLE_LINE);
* formatter.append(theObject);
* String wkt = formatter.toString();
*
@@ -68,6 +71,7 @@ import org.apache.sis.referencing.operat
* formatter.clear();
* }
*
+ * {@section Thread safety}
* Formatters are not synchronized. It is recommended to create separated formatter instances for each thread.
* If multiple threads access a formatter concurrently, then the formatter must be synchronized externally.
*
@@ -107,30 +111,29 @@ public class Formatter {
* If non-null, the terminal must be ANSI X3.64 compatible.
* The default value is {@code null}.
*
- * @see WKTFormat#getColors()
- * @see WKTFormat#setColors(Colors)
+ * @see #configure(Convention, Citation, Colors, byte)
*/
- Colors colors;
+ private Colors colors;
/**
* The preferred convention for objects or parameter names.
* This field should never be {@code null}.
*
- * @see WKTFormat#getConvention()
- * @see WKTFormat#setConvention(Convention)
+ * @see #configure(Convention, Citation, Colors, byte)
*/
private Convention convention;
/**
* The preferred authority for objects or parameter names.
*
- * @see WKTFormat#getNameAuthority()
- * @see WKTFormat#setNameAuthority(Citation)
+ * @see #configure(Convention, Citation, Colors, byte)
*/
private Citation authority;
/**
* The unit for writing length, or {@code null} for the "natural" unit of each WKT element.
+ *
+ * @see #setLinearUnit(Unit)
*/
private Unit<Length> linearUnit;
@@ -138,6 +141,8 @@ public class Formatter {
* The unit for writing angles, or {@code null} for the "natural" unit of each WKT element.
* This value is set for example by {@code "GEOGCS"}, which force its enclosing {@code "PRIMEM"}
* to take the same units than itself.
+ *
+ * @see #setAngularUnit(Unit)
*/
private Unit<Angle> angularUnit;
@@ -157,22 +162,28 @@ public class Formatter {
private final FieldPosition dummy = new FieldPosition(0);
/**
- * The buffer in which to format. Consider this field as private and final. The only method to change
- * the value of this field is {@link WKTFormat#format(Object, StringBuffer, FieldPosition)}.
+ * The buffer in which to format. Consider this field as final. The only method to change
+ * (indirectly) the value of this field is {@link WKTFormat#format(Object, Appendable)}.
+ *
+ * @see #setBuffer(StringBuffer)
*/
- StringBuffer buffer;
+ private StringBuffer buffer;
/**
* The starting point in the buffer. Always 0, except when used by
- * {@link WKTFormat#format(Object, StringBuffer, FieldPosition)}.
+ * {@link WKTFormat#format(Object, Appendable)}.
+ *
+ * @see #setBuffer(StringBuffer)
*/
- int bufferBase;
+ private int bufferBase;
/**
* The amount of spaces to use in indentation, or {@value org.apache.sis.io.wkt.WKTFormat#SINGLE_LINE}
* if indentation is disabled.
+ *
+ * @see #configure(Convention, Citation, Colors, byte)
*/
- byte indentation;
+ private byte indentation;
/**
* The amount of space to write on the left side of each line. This amount is increased
@@ -215,7 +226,7 @@ public class Formatter {
* Creates a new formatter instance with the default symbols, no syntax coloring and the default indentation.
*/
public Formatter() {
- this(Convention.DEFAULT, Symbols.DEFAULT, null, WKTFormat.DEFAULT_INDENTATION);
+ this(Convention.DEFAULT, Symbols.getDefault(), WKTFormat.DEFAULT_INDENTATION);
}
/**
@@ -223,36 +234,59 @@ public class Formatter {
*
* @param convention The convention to use.
* @param symbols The symbols.
- * @param colors The syntax coloring, or {@code null} if none.
* @param indentation The amount of spaces to use in indentation for WKT formatting,
* or {@link WKTFormat#SINGLE_LINE} for formatting the whole WKT on a single line.
*/
- public Formatter(final Convention convention, final Symbols symbols, final Colors colors, final int indentation) {
+ public Formatter(final Convention convention, final Symbols symbols, final int indentation) {
ArgumentChecks.ensureNonNull("convention", convention);
ArgumentChecks.ensureNonNull("symbols", symbols);
ArgumentChecks.ensureBetween("indentation", WKTFormat.SINGLE_LINE, Byte.MAX_VALUE, indentation);
this.convention = convention;
+ this.authority = convention.getNameAuthority();
this.symbols = symbols.immutable();
this.indentation = (byte) indentation;
this.numberFormat = symbols.createNumberFormat();
this.unitFormat = UnitFormat.getInstance(symbols.getLocale());
this.buffer = new StringBuffer();
- if (colors != null) {
- this.colors = colors.immutable();
- }
}
/**
- * Constructor for private use by {@link WKTFormat#format} only.
- * This constructor helps to share some objects with {@link Parser}.
+ * Constructor for private use by {@link WKTFormat#getFormatter()} only. This allows to use the number
+ * format created by {@link WKTFormat#createFormat(Class)}, which may be overridden by the user.
*/
Formatter(final Symbols symbols, final NumberFormat numberFormat) {
this.convention = Convention.DEFAULT;
+ this.authority = Convention.DEFAULT.getNameAuthority();
this.symbols = symbols;
this.indentation = WKTFormat.DEFAULT_INDENTATION;
this.numberFormat = numberFormat; // No clone needed.
this.unitFormat = UnitFormat.getInstance(symbols.getLocale());
- // Do not set the buffer. It will be set by WKTFormat.format(...).
+ // Do not set the buffer. It will be set by WKTFormat.format(…).
+ }
+
+ /**
+ * Sets the destination buffer. Used by {@link WKTFormat#format(Object, Appendable)} only.
+ */
+ final void setBuffer(final StringBuffer buffer) {
+ this.buffer = buffer;
+ bufferBase = (buffer != null) ? buffer.length() : 0;
+ }
+
+ /**
+ * Sets the convention, authority, colors and indentation to use for formatting WKT elements.
+ * This method does not validate the argument — validation must be done by the caller.
+ *
+ * @param convention The convention, or {@code null} for the default value.
+ * @param authority The authority, or {@code null} for inferring it from the convention.
+ * @param colors The syntax coloring, or {@code null} if none.
+ * @param indentation The amount of spaces to use in indentation for WKT formatting,
+ * or {@link WKTFormat#SINGLE_LINE}.
+ */
+ final void configure(Convention convention, final Citation authority, final Colors colors, final byte indentation) {
+ this.convention = convention;
+ this.authority = (authority != null) ? authority : convention.getNameAuthority();
+ this.colors = colors;
+ this.indentation = indentation;
}
/**
@@ -268,17 +302,6 @@ public class Formatter {
}
/**
- * Sets the convention and the authority to use for formatting WKT elements.
- *
- * @param convention The convention, or {@code null} for the default value.
- * @param authority The authority, or {@code null} for inferring it from the convention.
- */
- final void setConvention(Convention convention, final Citation authority) {
- this.convention = convention;
- this.authority = (authority != null) ? authority : convention.getNameAuthority();
- }
-
- /**
* Returns the preferred name for the specified object.
* If the specified object contains a name from the preferred authority, then this name is returned.
* Otherwise, the first name found is returned.
Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java?rev=1563306&r1=1563305&r2=1563306&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java [UTF-8] Fri Jan 31 22:28:05 2014
@@ -31,24 +31,21 @@ import static org.apache.sis.util.Argume
/**
* The set of symbols to use for <cite>Well Known Text</cite> (WKT) parsing and formatting.
- * Newly created {@code Symbols} instances use the following defaults:
+ * This class allows to specify the following properties:
*
- * <ul>
- * <li>An English locale for {@linkplain java.text.DecimalFormatSymbols decimal format symbols}.</li>
- * <li>Square brackets, as in {@code DATUM["WGS84"]}. An alternative allowed by the WKT
- * specification is curly brackets as in {@code DATUM("WGS84")}.</li>
- * <li>English quotation mark ({@code '"'}). SIS also accepts {@code '“'} opening quote and {@code '”'}
- * closing quote for more readable {@link String} constants in Java code, but the later are not legal WKT.</li>
- * <li>Coma separator followed by a space ({@code ", "}).</li>
- * </ul>
+ * <table class="sis">
+ * <tr><th>Property</th> <th>Standard value</th> <th>Legal alternative</th></tr>
+ * <tr><td>Locale for number format</td> <td>{@link Locale#ROOT}</td> <td></td></tr>
+ * <tr><td>Bracket symbols</td> <td>{@code [} … {@code ]}</td> <td>{@code (} … {@code )}</td></tr>
+ * <tr><td>Quote symbols</td> <td>{@code "} … {@code "}</td> <td></td></tr>
+ * <tr><td>Separator</td> <td>{@code ,}</td> <td></td></tr>
+ * </table>
*
- * {@section Relationship between <code>Symbols</code> locale and <code>WKTFormat</code> locale}
- * The {@link WKTFormat#getLocale()} property specifies the language to use when formatting
- * {@link org.opengis.util.InternationalString} instances. This can be set to any value.
- * On the contrary, the {@code Locale} property of this {@code Symbols} class controls
- * the decimal format symbols and is very rarely set to an other locale than {@link Locale#ROOT}.
+ * The two constants defined in this class, namely {@link #SQUARE_BRACKETS} and {@link #CURLY_BRACKETS},
+ * define the symbols for ISO 19162 compliant WKT formatting. Parsing can be made tolerant to alternative
+ * forms by optionally specifying additional bracket and quote symbols.
*
- * @author Martin Desruisseaux (IRD)
+ * @author Martin Desruisseaux (IRD, Geomatys)
* @since 0.4 (derived from geotk-2.1)
* @version 0.4
* @module
@@ -77,12 +74,6 @@ public class Symbols implements Localize
new int[] {'(', ')', '[', ']'}, SQUARE_BRACKETS.quotes);
/**
- * The default set of symbols, as documented in the class javadoc.
- * This is currently set to {@link #SQUARE_BRACKETS}.
- */
- public static final Symbols DEFAULT = SQUARE_BRACKETS;
-
- /**
* The locale of {@linkplain java.text.DecimalFormatSymbols decimal format symbols} or other symbols.
*
* @see #getLocale()
@@ -139,10 +130,20 @@ public class Symbols implements Localize
private String separator;
/**
- * Creates a new set of WKT symbols initialized to the {@linkplain #DEFAULT default} values.
+ * Creates a new set of WKT symbols initialized to the default values.
+ * Newly created {@code Symbols} instances use the following defaults:
+ *
+ * <ul>
+ * <li>{@link Locale#ROOT} for {@linkplain java.text.DecimalFormatSymbols decimal format symbols}.</li>
+ * <li>Square brackets by default, as in {@code DATUM["WGS84"]}, but accepting also curly brackets as in
+ * {@code DATUM("WGS84")}. Both are legal WKT.</li>
+ * <li>English quotation mark ({@code '"'}) by default, but accepting also {@code '“'} … {@code '”'} quotes
+ * for more readable {@link String} constants in Java code.</li>
+ * <li>Coma separator followed by a space ({@code ", "}).</li>
+ * </ul>
*/
public Symbols() {
- this(DEFAULT);
+ this(SQUARE_BRACKETS);
}
/**
@@ -239,11 +240,28 @@ public class Symbols implements Localize
}
/**
+ * Returns the default set of symbols.
+ * This is currently set to {@link #SQUARE_BRACKETS}.
+ *
+ * @return The default set of symbols.
+ */
+ public static Symbols getDefault() {
+ return SQUARE_BRACKETS;
+ }
+
+ /**
* Returns the locale of {@linkplain java.text.DecimalFormatSymbols decimal format symbols} or other symbols.
- * The default value is {@link Locale#ROOT}. Note that this is not the same locale than the {@link WKTFormat}
- * one, which is used for choosing the language of international strings.
+ * The default value is {@link Locale#ROOT}.
+ *
+ * {@section Relationship between <code>Symbols</code> locale and <code>WKTFormat</code> locale}
+ * The {@code WKTFormat.getLocale()} property specifies the language to use when formatting
+ * {@link org.opengis.util.InternationalString} instances. This can be set to any value.
+ * On the contrary, the {@code Locale} property of this {@code Symbols} class controls
+ * the decimal format symbols and is very rarely set to an other locale than {@link Locale#ROOT}.
*
* @return The symbols locale.
+ *
+ * @see WKTFormat#getLocale(Locale.Category)
*/
@Override
public final Locale getLocale() {
Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java?rev=1563306&r1=1563305&r2=1563306&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java [UTF-8] Fri Jan 31 22:28:05 2014
@@ -16,14 +16,18 @@
*/
package org.apache.sis.io.wkt;
+import java.util.Locale;
+import java.util.TimeZone;
import java.text.Format;
-import java.text.FieldPosition;
+import java.text.NumberFormat;
import java.text.ParsePosition;
+import java.io.IOException;
import org.opengis.metadata.citation.Citation;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.referencing.IdentifiedObject;
import org.opengis.referencing.operation.Matrix;
import org.opengis.referencing.operation.MathTransform;
+import org.apache.sis.io.CompoundFormat;
import org.apache.sis.util.ArgumentChecks;
import org.apache.sis.util.resources.Errors;
@@ -75,7 +79,7 @@ import org.apache.sis.util.resources.Err
* @version 0.4
* @module
*/
-public class WKTFormat extends Format {
+public class WKTFormat extends CompoundFormat<Object> {
/**
* For cross-version compatibility.
*/
@@ -97,7 +101,7 @@ public class WKTFormat extends Format {
* The same object is also referenced in the {@linkplain #parser} and {@linkplain #formatter}.
* It appears here for serialization purpose.
*/
- private Symbols symbols = Symbols.DEFAULT;
+ private Symbols symbols;
/**
* The colors to use for this formatter, or {@code null} for no syntax coloring.
@@ -110,7 +114,7 @@ public class WKTFormat extends Format {
* The convention to use. The same object is also referenced in the {@linkplain #formatter}.
* It appears here for serialization purpose.
*/
- private Convention convention = Convention.DEFAULT;
+ private Convention convention;
/**
* The preferred authority for objects or parameter names. A {@code null} value
@@ -123,18 +127,58 @@ public class WKTFormat extends Format {
* The same value is also stored in the {@linkplain #formatter}.
* It appears here for serialization purpose.
*/
- private byte indentation = DEFAULT_INDENTATION;
+ private byte indentation;
/**
* A formatter using the same symbols than the {@linkplain #parser}.
- * Will be created by the {@link #format} method when first needed.
+ * Will be created by the {@link #format(Object, Appendable)} method when first needed.
*/
private transient Formatter formatter;
/**
- * Constructs a format using the default factories.
+ * Creates a format for the given locale and timezone. The given locale will be used for
+ * {@link org.opengis.util.InternationalString} localization; this is <strong>not</strong>
+ * the locale for number format.
+ *
+ * @param locale The locale for the new {@code Format}, or {@code null} for {@code Locale.ROOT}.
+ * @param timezone The timezone, or {@code null} for UTC.
+ */
+ public WKTFormat(final Locale locale, final TimeZone timezone) {
+ super(locale, timezone);
+ convention = Convention.DEFAULT;
+ symbols = Symbols.getDefault();
+ indentation = DEFAULT_INDENTATION;
+ }
+
+ /**
+ * Returns the locale for the given category. This method implements the following mapping:
+ *
+ * <ul>
+ * <li>{@link Locale.Category#FORMAT} — the value of {@link Symbols#getLocale()},
+ * normally fixed to {@link Locale#ROOT}, used for number formatting.</li>
+ * <li>{@link Locale.Category#DISPLAY} — the {@code locale} given at construction time,
+ * used for {@code InternationalString} localization.</li>
+ * </ul>
+ *
+ * @param category The category for which a locale is desired.
+ * @return The locale for the given category (never {@code null}).
*/
- public WKTFormat() {
+ @Override
+ public Locale getLocale(final Locale.Category category) {
+ if (category == Locale.Category.FORMAT) {
+ return symbols.getLocale();
+ }
+ return super.getLocale(category);
+ }
+
+ /**
+ * Returns the kind of objects formatted by this class.
+ *
+ * @return {@code Object.class}
+ */
+ @Override
+ public Class<?> getValueType() {
+ return Object.class;
}
/**
@@ -174,17 +218,18 @@ public class WKTFormat extends Format {
* This property applies only when formatting text.
*
* <p>Newly created {@code WKTFormat}s have no syntax coloring. If a non-null argument like
- * {@link Colors#CONSOLE} is given to this method, then the {@link #format(Object) format(…)}
+ * {@link Colors#CONSOLE} is given to this method, then the {@link #format(Object, Appendable) format(…)}
* method tries to highlight most of the elements that are relevant to
* {@link org.apache.sis.util.Utilities#equalsIgnoreMetadata(Object, Object)}.</p>
*
* @param colors The colors for syntax coloring, or {@code null} if none.
*/
- public void setColors(final Colors colors) {
- this.colors = colors;
- if (formatter != null) {
- formatter.colors = colors;
+ public void setColors(Colors colors) {
+ if (colors != null) {
+ colors = colors.immutable();
}
+ this.colors = colors;
+ updateFormatter(formatter);
}
/**
@@ -243,13 +288,13 @@ public class WKTFormat extends Format {
}
/**
- * Updates the formatter convention and authority according the current state of this
- * {@code WKTFormat}. The authority may be null, in which case it will be inferred from
- * the convention when first needed.
+ * Updates the formatter convention, authority, colors and indentation according the current state of this
+ * {@code WKTFormat}. The authority may be null, in which case it will be inferred from the convention when
+ * first needed.
*/
private void updateFormatter(final Formatter formatter) {
if (formatter != null) {
- formatter.setConvention(convention, authority);
+ formatter.configure(convention, authority, colors, indentation);
}
}
@@ -270,11 +315,9 @@ public class WKTFormat extends Format {
* @param indentation The new indentation to use.
*/
public void setIndentation(final int indentation) {
- ArgumentChecks.ensureBetween("indentation", WKTFormat.SINGLE_LINE, Byte.MAX_VALUE, indentation);
+ ArgumentChecks.ensureBetween("indentation", SINGLE_LINE, Byte.MAX_VALUE, indentation);
this.indentation = (byte) indentation;
- if (formatter != null) {
- formatter.indentation = this.indentation;
- }
+ updateFormatter(formatter);
}
/**
@@ -285,7 +328,7 @@ public class WKTFormat extends Format {
* @return The parsed object, or {@code null} in case of failure.
*/
@Override
- public Object parseObject(final String text, final ParsePosition position) {
+ public Object parse(final CharSequence text, final ParsePosition position) {
throw new UnsupportedOperationException("Not supported yet.");
}
@@ -295,7 +338,7 @@ public class WKTFormat extends Format {
private Formatter getFormatter() {
Formatter formatter = this.formatter;
if (formatter == null) {
- formatter = new Formatter(Convention.DEFAULT, symbols, colors, indentation);
+ formatter = new Formatter(symbols, (NumberFormat) createFormat(Number.class));
updateFormatter(formatter);
this.formatter = formatter;
}
@@ -309,21 +352,22 @@ public class WKTFormat extends Format {
*
* @param object The object to format.
* @param toAppendTo Where the text is to be appended.
- * @param pos An identification of a field in the formatted text.
- * @return The given {@code toAppendTo} buffer.
+ * @throws IOException If an error occurred while writing to {@code toAppendTo}.
*
* @see #getWarning()
*/
@Override
- public StringBuffer format(final Object object,
- final StringBuffer toAppendTo,
- final FieldPosition pos)
- {
+ public void format(final Object object, final Appendable toAppendTo) throws IOException {
+ final StringBuffer buffer;
+ if (toAppendTo instanceof StringBuffer) {
+ buffer = (StringBuffer) toAppendTo;
+ } else {
+ buffer = new StringBuffer(500);
+ }
final Formatter formatter = getFormatter();
try {
formatter.clear();
- formatter.buffer = toAppendTo;
- formatter.bufferBase = toAppendTo.length();
+ formatter.setBuffer(buffer);
if (object instanceof FormattableObject) {
formatter.append((FormattableObject) object);
} else if (object instanceof IdentifiedObject) {
@@ -342,19 +386,56 @@ public class WKTFormat extends Format {
throw new ClassCastException(Errors.format(
Errors.Keys.IllegalArgumentClass_2, "object", object.getClass()));
}
- return toAppendTo;
} finally {
- formatter.buffer = null;
+ formatter.setBuffer(null);
+ }
+ if (buffer != toAppendTo) {
+ toAppendTo.append(buffer);
+ }
+ }
+
+ /**
+ * Creates a new format to use for parsing and formatting values of the given type.
+ * This method is invoked the first time that a format is needed for the given type.
+ * The {@code valueType} can be one of the following classes:
+ *
+ * <table class="sis">
+ * <tr><th>Value type</th> <th>Format to create</th></tr>
+ * <tr><td>{@link Number}</td> <td>{@link NumberFormat}</td></tr>
+ * <tr><td>{@link Date}</td> <td>{@link DateFormat}</td></tr>
+ * <tr><td>{@link Angle}</td> <td>{@link AngleFormat}</td></tr>
+ * </table>
+ *
+ * @param valueType The base type of values to parse or format.
+ * @return The format to use for parsing of formatting values of the given type, or {@code null} if none.
+ */
+ @Override
+ protected Format createFormat(final Class<?> valueType) {
+ if (valueType == Number.class) {
+ return symbols.createNumberFormat();
}
+ return super.createFormat(valueType);
}
/**
- * If a warning occurred during the last WKT {@linkplain #format formatting}, returns the warning.
- * Otherwise returns {@code null}. The warning is cleared every time a new object is formatted.
+ * If a warning occurred during the last WKT {@linkplain #format(Object, Appendable) formatting}, returns
+ * the warning. Otherwise returns {@code null}. The warning is cleared every time a new object is formatted.
*
* @return The last warning, or {@code null} if none.
*/
public String getWarning() {
return (formatter != null) ? formatter.getErrorMessage() : null;
}
+
+ /**
+ * Returns a clone of this format.
+ *
+ * @return A clone of this format.
+ */
+ @Override
+ public WKTFormat clone() {
+ final WKTFormat clone = (WKTFormat) super.clone();
+ clone.formatter = null; // Do not share the formatter.
+ return clone;
+ }
}
Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/SymbolsTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/SymbolsTest.java?rev=1563306&r1=1563305&r2=1563306&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/SymbolsTest.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/SymbolsTest.java [UTF-8] Fri Jan 31 22:28:05 2014
@@ -53,12 +53,12 @@ public final strictfp class SymbolsTest
* the standard {@code '"'} quotation mark after we tested the given string.
*/
private static void assertContainsAxis(final String message, final boolean expected, final String wkt) {
- assertEquals(message, expected, Symbols.DEFAULT.containsAxis(wkt));
+ assertEquals(message, expected, Symbols.getDefault().containsAxis(wkt));
final StringBuilder buffer = new StringBuilder(wkt);
StringBuilders.replace(buffer, '“', '"');
StringBuilders.replace(buffer, '”', '"');
assertFalse(wkt.contentEquals(buffer));
- assertEquals(message, expected, Symbols.DEFAULT.containsAxis(buffer));
+ assertEquals(message, expected, Symbols.getDefault().containsAxis(buffer));
}
/**
Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/Assert.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/Assert.java?rev=1563306&r1=1563305&r2=1563306&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/Assert.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/Assert.java [UTF-8] Fri Jan 31 22:28:05 2014
@@ -58,7 +58,7 @@ public strictfp class Assert extends org
* This formatter uses the {@code “…”} quotation marks instead of {@code "…"}
* for easier readability of {@link String} constants in Java code.
*/
- private static final WKTFormat WKT_FORMAT = new WKTFormat();
+ private static final WKTFormat WKT_FORMAT = new WKTFormat(null, null);
static {
final Symbols s = new Symbols();
s.setPairedQuotes("“”", "\"\"");
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java?rev=1563306&r1=1563305&r2=1563306&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java [UTF-8] Fri Jan 31 22:28:05 2014
@@ -102,14 +102,13 @@ public abstract class CompoundFormat<T>
/**
* Creates a new format for the given locale. The given locale can be {@code null} or
* {@link Locale#ROOT} if this format shall parse and format "unlocalized" strings.
- * See {@link #getLocale()} for more information on {@code ROOT} locale.
+ * See {@link #getLocale()} for more information about the {@code ROOT} locale.
*
- * @param locale The locale to use for numbers, dates and angles formatting,
- * or {@code null} for the {@linkplain Locale#ROOT root locale}.
+ * @param locale The locale for the new {@code Format}, or {@code null} for {@code Locale.ROOT}.
* @param timezone The timezone, or {@code null} for UTC.
*/
protected CompoundFormat(final Locale locale, final TimeZone timezone) {
- this.locale = (locale != null) ? locale : Locale.ROOT;
+ this.locale = (locale != null) ? locale : Locale.ROOT;
this.timezone = timezone;
}
@@ -119,11 +118,11 @@ public abstract class CompoundFormat<T>
* is implementation-dependent, but some typical examples are:
*
* <ul>
- * <li>Format {@link Number}s using {@code toString()} instead than {@code NumberFormat}.</li>
- * <li>Format {@link Date}s using the ISO pattern instead than the English one.</li>
+ * <li>Format {@link Number} instances using {@code toString()} instead than {@code NumberFormat}.</li>
+ * <li>Format {@link Date} instances using the ISO pattern instead than the English one.</li>
* </ul>
*
- * @return The locale used for this format, or {@link Locale#ROOT} for unlocalized format.
+ * @return The locale of this {@code Format}, or {@code Locale.ROOT} for unlocalized format.
*/
@Override
public Locale getLocale() {
@@ -131,6 +130,34 @@ public abstract class CompoundFormat<T>
}
/**
+ * Returns the locale for the given category. Subclasses may override this method in order to assign
+ * different roles to the different locale categories. A typical (but not mandatory) mapping is:
+ *
+ * <ul>
+ * <li>{@link Locale.Category#FORMAT} specifies the locale to use for numbers, dates and angles formatting.</li>
+ * <li>{@link Locale.Category#DISPLAY} specifies the locale to use for {@link org.opengis.util.CodeList} labels
+ * and {@link org.opengis.util.InternationalString} contents.</li>
+ * </ul>
+ *
+ * {@example The ISO 19162 (<cite>Well Known Text</cite>) standard requires a number format similar to the one
+ * defined by <code>Locale.ROOT</code> while it allows informative texts (remarks, <i>etc.</i>) to be
+ * formatted in the user's locale. Consequently <code>WKTFormat</code> fixes (usually) the locale for
+ * <code>Category.FORMAT</code> to <code>Locale.ROOT</code> and let <code>Category.DISPLAY</code> be
+ * any locale.}
+ *
+ * For subclasses that do not override this method, the default implementation returns {@link #getLocale()}.
+ *
+ * @param category The category for which a locale is desired.
+ * @return The locale for the given category (never {@code null}).
+ *
+ * @since 0.4
+ */
+ public Locale getLocale(final Locale.Category category) {
+ ArgumentChecks.ensureNonNull("category", category);
+ return getLocale();
+ }
+
+ /**
* Returns the timezone used by this format.
*
* @return The timezone used for this format, or UTC for unlocalized format.
@@ -144,6 +171,13 @@ public abstract class CompoundFormat<T>
* The returned type may be a subclass of {@code <T>} if the format is configured in a way
* that restrict the kind value to be parsed.
*
+ * {@example
+ * <ul>
+ * <li><code>StatisticsFormat</code> unconditionally returns <code>Statistics.class</code>.</li>
+ * <li><code>TreeTableFormat</code> unconditionally returns <code>TreeTable.class</code>.</li>
+ * </ul>
+ * }
+ *
* @return The base type of values parsed and formatted by this {@code Format} instance.
*/
public abstract Class<? extends T> getValueType();
@@ -257,7 +291,7 @@ public abstract class CompoundFormat<T>
} while (Character.isSpaceChar(c) || Character.isISOControl(c));
pos.setErrorIndex(i);
}
- throw new LocalizedParseException(getLocale(), getValueType(), text, pos);
+ throw new LocalizedParseException(getLocale(Locale.Category.DISPLAY), getValueType(), text, pos);
}
/**
@@ -379,7 +413,7 @@ public abstract class CompoundFormat<T>
* documented in this method javadoc. But actually it is not, since the call to
* DefaultFormat.getInstance(…) will indirectly perform this kind of comparison.
*/
- final Locale locale = getLocale();
+ final Locale locale = getLocale(Locale.Category.FORMAT);
if (Number.class.isAssignableFrom(valueType)) {
if (Locale.ROOT.equals(locale)) {
return DefaultFormat.getInstance(valueType);
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java?rev=1563306&r1=1563305&r2=1563306&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/math/StatisticsFormat.java [UTF-8] Fri Jan 31 22:28:05 2014
@@ -123,6 +123,28 @@ public class StatisticsFormat extends Ta
}
/**
+ * Returns the locale for the given category. This method implements the following mapping:
+ *
+ * <ul>
+ * <li>{@link Locale.Category#DISPLAY} — the {@code headerLocale} given at construction time.</li>
+ * <li>{@link Locale.Category#FORMAT} — the {@code locale} given at construction time,
+ * used for all values below the header row.</li>
+ * </ul>
+ *
+ * @param category The category for which a locale is desired.
+ * @return The locale for the given category (never {@code null}).
+ *
+ * @since 0.4
+ */
+ @Override
+ public Locale getLocale(final Locale.Category category) {
+ if (category == Locale.Category.DISPLAY) {
+ return headerLocale;
+ }
+ return super.getLocale(category);
+ }
+
+ /**
* Returns the kind of objects formatted by this class.
*
* @return {@code Statistics.class}
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java?rev=1563306&r1=1563305&r2=1563306&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java [UTF-8] Fri Jan 31 22:28:05 2014
@@ -190,6 +190,8 @@ public class TreeTableFormat extends Tab
/**
* Returns the type of object formatted by this class, which is {@link TreeTable}.
+ *
+ * @return {@inheritDoc}
*/
@Override
public final Class<TreeTable> getValueType() {
@@ -282,6 +284,13 @@ public class TreeTableFormat extends Tab
}
/**
+ * Returns the locale to use for code lists, international strings and exception messages.
+ */
+ final Locale getDisplayLocale() {
+ return getLocale(Locale.Category.DISPLAY);
+ }
+
+ /**
* Returns the formats to use for parsing and formatting the values of each column.
* The returned array may contain {@code null} elements, which means that the values
* in that column can be stored as {@code String}s.
@@ -420,7 +429,7 @@ public class TreeTableFormat extends Tab
*/
if (--indentationLevel < 0) {
pos.setErrorIndex(indexOfLineStart);
- throw new LocalizedParseException(getLocale(),
+ throw new LocalizedParseException(getDisplayLocale(),
Errors.Keys.NodeHasNoParent_1, new Object[] {node}, 0);
}
lastNode = lastNode.getParent();
@@ -434,7 +443,7 @@ public class TreeTableFormat extends Tab
final TreeTable.Node parent = lastNode.getParent();
if (parent == null) {
pos.setErrorIndex(indexOfLineStart);
- throw new LocalizedParseException(getLocale(),
+ throw new LocalizedParseException(getDisplayLocale(),
Errors.Keys.NodeHasNoParent_1, new Object[] {node}, 0);
}
parent.getChildren().add(node);
@@ -618,11 +627,11 @@ public class TreeTableFormat extends Tab
}
text = format.format(value);
} else if (value instanceof InternationalString) {
- text = ((InternationalString) value).toString(getLocale());
+ text = ((InternationalString) value).toString(getDisplayLocale());
} else if (value instanceof CharSequence) {
text = value.toString();
} else if (value instanceof CodeList<?>) {
- text = Types.getCodeTitle((CodeList<?>) value).toString(getLocale());
+ text = Types.getCodeTitle((CodeList<?>) value).toString(getDisplayLocale());
} else if (value instanceof Enum<?>) {
text = CharSequences.upperCaseToSentence(((Enum<?>) value).name());
} else {
@@ -705,7 +714,7 @@ public class TreeTableFormat extends Tab
for (int i=0; i<level; i++) {
out.append(getTreeSymbols(true, isLast[i]));
}
- final Locale locale = getLocale();
+ final Locale locale = getDisplayLocale();
out.append('(').append(Vocabulary.getResources(locale)
.getString(Vocabulary.Keys.CycleOmitted).toLowerCase(locale))
.append(')').append(lineSeparator);