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).

Formatter instances are created by WKTFormat and given to the Formattable­Object​.format­To(Formatter) method of the object to format. Formatter provides the following services:

  • A series of append(…) methods to be invoked by the format­To(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:
  • Constructor Details

    • 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, or WKTFormat​.SINGLE_LINE for formatting the whole WKT on a single line.
  • Method Details

    • getConvention

      public final Convention getConvention()
      Returns the convention to use for formatting the WKT. The default is Convention​.WKT2.
      Returns:
      the convention (never null).
      See Also:
    • 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:
      Returns:
      the mapper between Java character sequences and the characters to write in WKT.
      Since:
      0.6
      See Also:
    • 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​.set­Name­Authority(Citation) method. This is not necessarily the authority who created the object to format.

      Example

      The EPSG name of the EPSG:6326 datum 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:
    • getLocale

      public final Locale getLocale()
      Returns the locale to use for localizing International­String instances. This is not the locale for formatting dates and numbers.
      Specified by:
      get­Locale in interface Localized
      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 any append(…) method call will cause the next element to appear on the next line.

      This method has no effect in any of the following cases:

    • indent

      public void indent(int amount)
      Increases or decreases the indentation. A value of +1 increases the indentation by the amount of spaces specified at construction time, and a value of -1 reduces 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 the Keyword­Style value. This method can be used by Formattable­Object​.format­To(Formatter) implementations for choosing the return value.
      Parameters:
      short­Keyword - the keyword to return if the style is Keyword­Style​.SHORT.
      long­Keyword - the keyword to return if the style is Keyword­Style​.LONG.
      Returns:
      the short or long keyword depending on the keyword style setting.
      Since:
      0.6
      See Also:
    • append

      public void append(FormattableObject object)
      Appends the given Formattable­Object. This method performs the following steps:
      • Invoke object.format­To(this).
      • Prepend the keyword returned by the above method call (e.g. "GEOCS").
      • If the given object is an instance of Identified­Object, then append complementary information:
      Complementary WKT elements
      WKT 2 elementWKT 1 elementFor types
      Anchor[…] Datum
      Scope[…] ReferenceSystem, Datum, Coordinate­Operation
      Area[…] ReferenceSystem, Datum, Coordinate­Operation
      BBox[…] ReferenceSystem, Datum, Coordinate­Operation
      VerticalExtent[…] ReferenceSystem, Datum, Coordinate­Operation
      TimeExtent[…] ReferenceSystem, Datum, Coordinate­Operation
      Id[…]Authority[…]IdentifiedObject
      Remarks[…] ReferenceSystem, Coordinate­Operation
      Parameters:
      object - the formattable object to append to the WKT, or null if none.
    • append

      public void append(InternationalString scope, Extent area)
      Appends the usage (scope and domain of validity) of an object. The arguments are the components of an Default­Object­Domain. The given extent is decomposed in horizontal, vertical and temporal components. The horizontal component uses the default number of fraction digits recommended by ISO 19162.

      Usage element

      In a WKT string, the given elements should be enclosed in an USAGE[…] element according the ISO 19162:2019 standard but not according the previous version (2015). The USAGE[…] enclosing element shall be provided when needed by the caller.
      Parameters:
      scope - description of domain of usage, or null if none.
      area - area for which the object is valid, or null if none.
      Since:
      1.4
    • append

      public void append(GeographicBoundingBox bbox, int fractionDigits)
      Appends the given geographic bounding box in a BBOX[…] 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 precision

      The ISO 19162 standards recommends to format those values with only 2 decimal digits. This is because Geographic­Bounding­Box does not specify the datum, so this box is an approximated information only.
      Parameters:
      bbox - the geographic bounding box to append to the WKT, or null.
      fraction­Digits - 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 a PARAM_MT[…] element.
      Parameters:
      transform - the transform object to append to the WKT, or null if 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, or null if none.
      type - the key of the colors to apply if syntax coloring is enabled, or null if 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 "north­East"). 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, or null if 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, or null if 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, or null if none.
      fraction­Digits - the number of fraction digits for each column in a row, or null for 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 than fraction­Digits​.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 a Unit[…] element or one of the specialized elements. Specialized elements are Angle­Unit, Length­Unit, Scale­Unit, Parametric­Unit and Time­Unit. By default, specialized unit keywords are used with the WKT 2 convention.

      Example

      append(Units​.KILOMETRE) will append "Length­Unit["km", 1000]" to the WKT.
      Parameters:
      unit - the unit to append to the WKT, or null if none.
      See Also:
    • 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 to­String() representation.
      Parameters:
      value - the value to append to the WKT, or null.
    • delegateTo

      public String delegateTo(Object other) throws UnformattableObjectException
      Delegates the formatting to another Formattable­Object implementation. Invoking this method is equivalent to first verifying the other class, then delegating as below:
      return other.formatTo(this);
      
      This method is useful for Formattable­Object which 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 Formattable­Object​.format­To(Formatter).
      Throws:
      Unformattable­Object­Exception
      Since:
      0.5
    • getEnclosingElement

      public FormattableObject getEnclosingElement(int depth)
      Returns the enclosing WKT element, or null if 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)
      Returns true if the element at the given depth specified a contextual unit. This method returns true if the formattable object given by get­Enclosing­Element(depth) has invoked add­Contextual­Unit(Unit) with a non-null unit at least once.

      Usage note

      The main purpose of this method is to allow AXIS[…] 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 add­Contextual­Unit(…) 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 quantity Q. The given unit will apply to all WKT elements containing a value of quantity Q without their own UNIT[…] element, until the restore­Contextual­Unit(Unit, Unit) method is invoked.

      If the given unit is null, then this method does nothing and returns null.

      Special case

      If the WKT conventions are WKT1_COMMON_UNITS, then this method ignores the given unit and returns null. See Convention​.WKT1_COMMON_UNITS javadoc for more information.
      Type Parameters:
      Q - the unit quantity.
      Parameters:
      unit - the contextual unit to add, or null if none.
      Returns:
      the previous contextual unit for quantity Q, or null if none.
    • restoreContextualUnit

      public void restoreContextualUnit(Unit<?> unit, Unit<?> previous)
      Restores the contextual unit to its previous state before the call to add­Contextual­Unit(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 to add­Contextual­Unit(unit) (can be null).
      previous - the value returned by add­Contextual­Unit(unit) (can be null).
      Throws:
      Illegal­State­Exception - 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 of the given one, or unit if there is no replacement. This method searches for a unit specified by add­Contextual­Unit(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, or null.
      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()
      Returns true if the WKT written by this formatter is not strictly compliant to the WKT specification. This method returns true if set­Invalid­WKT(Identified­Object, Exception) has been invoked at least once. The action to take regarding invalid WKT is caller-dependent. For example, Formattable­Object​.to­String() will accepts loose WKT formatting and ignore this flag, while Formattable­Object​.to­WKT() requires strict WKT formatting and will thrown an exception if this flag is set.
      Returns:
      true if 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 of Formattable­Object​.format­To(Formatter) when the object to format is more complex than what the WKT specification allows. Applications can test is­Invalid­WKT() later for checking WKT validity.
      Parameters:
      unformattable - the object that cannot be formatted,
      cause - the cause for the failure to format, or null if 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 to set­Invalid­WKT(Identified­Object, Exception) when the problematic object is not an instance of Identified­Object.
      Parameters:
      unformattable - the class of the object that cannot be formatted,
      cause - the cause for the failure to format, or null if the cause is not an exception.
    • toWKT

      public String toWKT()
      Returns the WKT formatted by this object.
      Returns:
      the WKT formatted by this formatter.
    • toString

      public String toString()
      Returns a string representation of this formatter for debugging purpose.
      Overrides:
      to­String in class Object
      Returns:
      a string representation of this formatter.