Package org.apache.sis.io.wkt
Class Formatter
- Object
-
- Formatter
-
- All Implemented Interfaces:
Localized
public class Formatter extends Object implements Localized
Provides support methods for formatting a Well Known Text (WKT).Formatterinstances are created byWKTFormatand given to theFormattableObject.formatTo(Formatter)method of the object to format.Formatterprovides the following services:- A series of
append(…)methods to be invoked by theformatTo(Formatter)implementations. - Contextual information. In particular, the contextual units depend on the enclosing WKT element.
- A flag for declaring the object unformattable.
- Since:
- 0.4
- See Also:
- WKT 2 specification, Legacy WKT 1
Defined in the
sis-referencingmodule
-
-
Constructor Summary
Constructors Constructor Description Formatter()Creates a new formatter instance with the default configuration.Formatter(Convention convention, Symbols symbols, int indentation)Creates a new formatter instance with the specified convention, symbols and indentation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <Q extends Quantity<Q>>
Unit<Q>addContextualUnit(Unit<Q> unit)Adds a unit to use for the next measurements of the quantityQ.voidappend(boolean value)Appends a boolean value.voidappend(double number)Appends an floating point value.voidappend(long number)Appends an integer value.voidappend(String text, ElementKind type)Appends a character string between quotes.voidappend(Date date)Appends a date.voidappend(Unit<?> unit)Appends a unit in aUnit[…]element or one of the specialized elements.voidappend(FormattableObject object)Appends the givenFormattableObject.voidappend(Vector[] rows, int... fractionDigits)Appends rows of numbers.voidappend(GeographicBoundingBox bbox, int fractionDigits)Appends the given geographic bounding box in aBBOX[…]element.voidappend(MathTransform transform)Appends the given math transform, typically (but not necessarily) in aPARAM_MT[…]element.voidappend(CodeList<?> code)Appends an enumeration or code list value.voidappendAny(Object value)Appends an object or an array of objects.StringdelegateTo(Object other)Delegates the formatting to anotherFormattableObjectimplementation.ConventiongetConvention()Returns the convention to use for formatting the WKT.FormattableObjectgetEnclosingElement(int depth)Returns the enclosing WKT element, ornullif element being formatted is the root.LocalegetLocale()Returns the locale to use for localizingInternationalStringinstances.CitationgetNameAuthority()Returns the preferred authority for choosing the projection and parameter names.TransliteratorgetTransliterator()Returns a mapper between Java character sequences and the characters to write in WKT.booleanhasContextualUnit(int depth)Returnstrueif the element at the given depth specified a contextual unit.voidindent(int amount)Increases or decreases the indentation.booleanisInvalidWKT()Returnstrueif the WKT written by this formatter is not strictly compliant to the WKT specification.voidnewLine()Request a line separator before the next element to format.voidrestoreContextualUnit(Unit<?> unit, Unit<?> previous)Restores the contextual unit to its previous state before the call toaddContextualUnit(Unit).voidsetInvalidWKT(Class<?> unformattable, Exception cause)Marks the current WKT representation of the given class as not strictly compliant with the WKT specification.voidsetInvalidWKT(IdentifiedObject unformattable, Exception cause)Marks the current WKT representation of the given object as not strictly compliant with the WKT specification.StringshortOrLong(String shortKeyword, String longKeyword)Selects a short or long keyword depending on theKeywordStylevalue.<Q extends Quantity<Q>>
Unit<Q>toContextualUnit(Unit<Q> unit)Returns the unit to use instead than the given one, orunitif there is no replacement.StringtoString()Returns a string representation of this formatter for debugging purpose.StringtoWKT()Returns the WKT formatted by this object.
-
-
-
Constructor Detail
-
Formatter
public Formatter()
Creates a new formatter instance with the default configuration.
-
Formatter
public Formatter(Convention convention, Symbols symbols, int indentation)
Creates a new formatter instance with the specified convention, symbols and indentation.- Parameters:
convention- the convention to use.symbols- the symbols.indentation- the amount of spaces to use in indentation for WKT formatting, orWKTFormat.SINGLE_LINEfor formatting the whole WKT on a single line.
-
-
Method Detail
-
getConvention
public final Convention getConvention()
Returns the convention to use for formatting the WKT. The default isConvention.WKT2.- Returns:
- the convention (never
null). - See Also:
WKTFormat.setConvention(Convention),FormattableObject.toString(Convention)
-
getTransliterator
public final Transliterator getTransliterator()
Returns a mapper between Java character sequences and the characters to write in WKT. The intent is to specify how to write characters that are not allowed in WKT strings according ISO 19162 specification. Return values can be:Transliterator.DEFAULTfor performing replacements like "é" → "e" in all WKT elements exceptREMARKS["…"].Transliterator.IDENTITYfor preserving non-ASCII characters.- Any other user-supplied mapping.
- Returns:
- the mapper between Java character sequences and the characters to write in WKT.
- Since:
- 0.6
- See Also:
WKTFormat.setTransliterator(Transliterator)
-
getNameAuthority
public final Citation getNameAuthority()
Returns the preferred authority for choosing the projection and parameter names.The preferred authority can be set by the
WKTFormat.setNameAuthority(Citation)method. This is not necessarily the authority who created the object to format.Example: The EPSG name of theEPSG:6326datum is "World Geodetic System 1984". However if the preferred authority is OGC, then the formatted datum name will rather look like "WGS84" (the exact string depends on the object aliases).- Returns:
- the authority for projection and parameter names.
- See Also:
WKTFormat.getNameAuthority(),IdentifiedObjects.getName(IdentifiedObject, Citation)
-
getLocale
public final Locale getLocale()
Returns the locale to use for localizingInternationalStringinstances. This is not the locale for formatting dates and numbers.- Specified by:
getLocalein interfaceLocalized- Returns:
- the locale to use for localizing international strings.
-
newLine
public void newLine()
Request a line separator before the next element to format. Invoking this method before anyappend(…)method call will cause the next element to appear on the next line.This method has no effect in any of the following cases:
- This method has already been invoked before the next
append(…). - The indentation is
WKTFormat.SINGLE_LINE.
- This method has already been invoked before the next
-
indent
public void indent(int amount)
Increases or decreases the indentation. A value of+1increases the indentation by the amount of spaces specified at construction time, and a value of-1reduces it by the same amount.- Parameters:
amount- +1 for increasing the indentation, or -1 for decreasing it, or 0 for no-op.
-
shortOrLong
public String shortOrLong(String shortKeyword, String longKeyword)
Selects a short or long keyword depending on theKeywordStylevalue. This method can be used byFormattableObject.formatTo(Formatter)implementations for choosing the return value.- Parameters:
shortKeyword- the keyword to return if the style isKeywordStyle.SHORT.longKeyword- the keyword to return if the style isKeywordStyle.LONG.- Returns:
- the short or long keyword depending on the keyword style setting.
- Since:
- 0.6
- See Also:
WKTFormat.setKeywordStyle(KeywordStyle)
-
append
public void append(FormattableObject object)
Appends the givenFormattableObject. This method performs the following steps:- Invoke
object.formatTo(this). - Prepend the keyword returned by the above method call (e.g.
"GEOCS"). - If the given object is an instance of
IdentifiedObject, then append complementary information:
Complementary WKT elements WKT 2 element WKT 1 element For types Anchor[…]DatumScope[…]ReferenceSystem,Datum,CoordinateOperationArea[…]ReferenceSystem,Datum,CoordinateOperationBBox[…]ReferenceSystem,Datum,CoordinateOperationVerticalExtent[…]ReferenceSystem,Datum,CoordinateOperationTimeExtent[…]ReferenceSystem,Datum,CoordinateOperationId[…]Authority[…]IdentifiedObjectRemarks[…]ReferenceSystem,CoordinateOperation- Parameters:
object- the formattable object to append to the WKT, ornullif none.
- Invoke
-
append
public void append(GeographicBoundingBox bbox, int fractionDigits)
Appends the given geographic bounding box in aBBOX[…]element. Longitude and latitude values will be formatted in decimal degrees. Longitudes are relative to the Greenwich meridian, with values increasing toward East. Latitudes values are increasing toward North.Numerical precisionThe ISO 19162 standards recommends to format those values with only 2 decimal digits. This is becauseGeographicBoundingBoxdoes not specify the datum, so this box is an approximated information only.- Parameters:
bbox- the geographic bounding box to append to the WKT, ornull.fractionDigits- the number of fraction digits to use. The recommended value is 2.
-
append
public void append(MathTransform transform)
Appends the given math transform, typically (but not necessarily) in aPARAM_MT[…]element.- Parameters:
transform- the transform object to append to the WKT, ornullif none.
-
append
public void append(String text, ElementKind type)
Appends a character string between quotes. The element separator will be written before the text if needed.- Parameters:
text- the string to format to the WKT, ornullif none.type- the key of the colors to apply if syntax coloring is enabled, ornullif none.
-
append
public void append(CodeList<?> code)
Appends an enumeration or code list value. The element separator will be written before the code list if needed.For the WKT 2 format, this method uses the ISO name if available (for example
"northEast"). For the WKT 1 format, this method uses the programmatic name instead (for example"NORTH_EAST").- Parameters:
code- the code list to append to the WKT, ornullif none.
-
append
public void append(Date date)
Appends a date. The element separator will be written before the date if needed.- Parameters:
date- the date to append to the WKT, ornullif none.
-
append
public void append(boolean value)
Appends a boolean value. The element separator will be written before the boolean if needed.- Parameters:
value- the boolean to append to the WKT.
-
append
public void append(long number)
Appends an integer value. The element separator will be written before the number if needed.- Parameters:
number- the integer to append to the WKT.
-
append
public void append(double number)
Appends an floating point value. The element separator will be written before the number if needed.- Parameters:
number- the floating point value to append to the WKT.
-
append
public void append(Vector[] rows, int... fractionDigits)
Appends rows of numbers. Each number is separated by a space, and each row is separated by a comma. Rows usually have all the same length, but this is not mandatory. This method can be used for formatting geometries or matrix.- Parameters:
rows- the rows to append, ornullif none.fractionDigits- the number of fraction digits for each column in a row, ornullfor default. A precision can be specified for each column because those columns are often different dimensions of a Coordinate Reference System (CRS), each with their own units of measurement. If a row contains more numbers thanfractionDigits.length, then the last value in this array is repeated for all remaining row numbers.- Since:
- 1.0
-
append
public void append(Unit<?> unit)
Appends a unit in aUnit[…]element or one of the specialized elements. Specialized elements areAngleUnit,LengthUnit,ScaleUnit,ParametricUnitandTimeUnit. By default, specialized unit keywords are used with the WKT 2 convention.Example:append(Units.KILOMETRE)will append "LengthUnit["km", 1000]" to the WKT.- Parameters:
unit- the unit to append to the WKT, ornullif none.- See Also:
- WKT 2 specification §7.4
-
appendAny
public void appendAny(Object value)
Appends an object or an array of objects. This method performs the following choices:- If the given value is
null, then this method appends the "null" string (without quotes). - Otherwise if the given value is an array, then this method appends the opening sequence symbol, formats all elements by invoking this method recursively, then appends the closing sequence symbol.
- Otherwise if the value type is assignable to the argument type of one of the
append(…)methods in this class, then the formatting will be delegated to that method. - Otherwise the given value is appended as a quoted text with its
toString()representation.
- Parameters:
value- the value to append to the WKT, ornull.
- If the given value is
-
delegateTo
public String delegateTo(Object other) throws UnformattableObjectException
Delegates the formatting to anotherFormattableObjectimplementation. Invoking this method is equivalent to first verifying theotherclass, then delegating as below:
This method is useful forreturn other.formatTo(this);
FormattableObjectwhich are wrapper around another object. It allows to delegate the WKT formatting to the wrapped object.- Parameters:
other- the object to format with this formatter.- Returns:
- the value returned by
FormattableObject.formatTo(Formatter). - Throws:
UnformattableObjectException- Since:
- 0.5
-
getEnclosingElement
public FormattableObject getEnclosingElement(int depth)
Returns the enclosing WKT element, ornullif element being formatted is the root. This method can be invoked by child elements having some aspects that depend on the enclosing element.- Parameters:
depth- 1 for the immediate parent, 2 for the parent of the parent, etc.- Returns:
- the parent element at the given depth, or
null.
-
hasContextualUnit
public boolean hasContextualUnit(int depth)
Returnstrueif the element at the given depth specified a contextual unit. This method returnstrueif the formattable object given bygetEnclosingElement(depth)has invokedaddContextualUnit(Unit)with a non-null unit at least once.Note: The main purpose of this method is to allowAXIS[…]elements to determine if they should inherit the unit specified by the enclosing CRS, or if they should specify their unit explicitly.- Parameters:
depth- 1 for the immediate parent, 2 for the parent of the parent, etc.- Returns:
- whether the parent element at the given depth has invoked
addContextualUnit(…)at least once.
-
addContextualUnit
public <Q extends Quantity<Q>> Unit<Q> addContextualUnit(Unit<Q> unit)
Adds a unit to use for the next measurements of the quantityQ. The given unit will apply to all WKT elements containing a value of quantityQwithout their ownUNIT[…]element, until therestoreContextualUnit(Unit, Unit)method is invoked.If the given unit is null, then this method does nothing and returns
null.Special caseIf the WKT conventions areWKT1_COMMON_UNITS, then this method ignores the given unit and returnsnull. SeeConvention.WKT1_COMMON_UNITSjavadoc for more information.- Type Parameters:
Q- the unit quantity.- Parameters:
unit- the contextual unit to add, ornullif none.- Returns:
- the previous contextual unit for quantity
Q, ornullif none.
-
restoreContextualUnit
public void restoreContextualUnit(Unit<?> unit, Unit<?> previous)
Restores the contextual unit to its previous state before the call toaddContextualUnit(Unit). This method is used in the following pattern:final Unit<?> previous = formatter.addContextualUnit(unit); // ... format some WKT elements here. formatter.restoreContextualUnit(unit, previous);
- Parameters:
unit- the value given in argument toaddContextualUnit(unit)(can benull).previous- the value returned byaddContextualUnit(unit)(can benull).- Throws:
IllegalStateException- if this method has not been invoked in the pattern documented above.- Since:
- 0.6
-
toContextualUnit
public <Q extends Quantity<Q>> Unit<Q> toContextualUnit(Unit<Q> unit)
Returns the unit to use instead than the given one, orunitif there is no replacement. This method searches for a unit specified byaddContextualUnit(Unit)which is compatible with the given unit.- Type Parameters:
Q- the quantity of the unit.- Parameters:
unit- the unit to replace by the contextual unit, ornull.- Returns:
- a contextual unit compatible with the given unit, or
unit(which may be null) if no contextual unit has been found.
-
isInvalidWKT
public boolean isInvalidWKT()
Returnstrueif the WKT written by this formatter is not strictly compliant to the WKT specification. This method returnstrueifsetInvalidWKT(IdentifiedObject, Exception)has been invoked at least once. The action to take regarding invalid WKT is caller-dependent. For exampleFormattableObject.toString()will accepts loose WKT formatting and ignore this flag, whileFormattableObject.toWKT()requires strict WKT formatting and will thrown an exception if this flag is set.- Returns:
trueif the WKT is invalid.
-
setInvalidWKT
public void setInvalidWKT(IdentifiedObject unformattable, Exception cause)
Marks the current WKT representation of the given object as not strictly compliant with the WKT specification. This method can be invoked by implementations ofFormattableObject.formatTo(Formatter)when the object to format is more complex than what the WKT specification allows. Applications can testisInvalidWKT()later for checking WKT validity.- Parameters:
unformattable- the object that can not be formatted,cause- the cause for the failure to format, ornullif the cause is not an exception.
-
setInvalidWKT
public void setInvalidWKT(Class<?> unformattable, Exception cause)
Marks the current WKT representation of the given class as not strictly compliant with the WKT specification. This method can be used as an alternative tosetInvalidWKT(IdentifiedObject, Exception)when the problematic object is not an instance ofIdentifiedObject.- Parameters:
unformattable- the class of the object that can not be formatted,cause- the cause for the failure to format, ornullif the cause is not an exception.
-
toWKT
public String toWKT()
Returns the WKT formatted by this object.- Returns:
- the WKT formatted by this formatter.
-
-