();
/**
* The indentation value to give to {@link #toWKT(int)} method for formatting the complete
* object on a single line.
*
* @since 2.6
*/
public static final int SINGLE_LINE = 0;
/**
* Default constructor.
*/
protected Formattable() {
}
/**
* Returns a string representation for this object. The default implementation returns
* the same string similar than {@link #toWKT()}, except that no exception is thrown if
* the string contains non-standard keywords. For example the
* WKT
* specification do not defines any keyword for
* {@linkplain org.opengis.referencing.cs.CoordinateSystem coordinate system} objects. If this
* object is an instance of {@link org.geotools.referencing.cs.DefaultCartesianCS}, then the
* WKT will be formatted as "CartesianCS[AXIS["
..."], AXIS["
..."],
*
etc.]"
.
*/
@Override
public String toString() {
return toWKT(Citations.OGC, getIndentation(), false);
}
/**
* Returns a
* Well
* Known Text (WKT) using a default indentation. The default indentation is read from
* {@linkplain Preferences user preferences}.
*
* @return The Well Know Text for this object.
* @throws UnformattableObjectException If this object can't be formatted as WKT.
* A formatting may fails because an object is too complex for the WKT format capability
* (for example an {@linkplain org.geotools.referencing.crs.DefaultEngineeringCRS
* engineering CRS} with different unit for each axis), or because only some specific
* implementations can be formatted as WKT.
*/
public String toWKT() throws UnformattableObjectException {
return toWKT(getIndentation());
}
/**
* Returns a
* Well
* Known Text (WKT) for this object using the specified indentation.
*
* @param indentation The amount of spaces to use in indentation for WKT formatting,
* or {@link #SINGLE_LINE} for formatting the whole WKT on a single line.
* @return The Well Know Text for this object.
* @throws UnformattableObjectException If this object can't be formatted as WKT.
* A formatting may fails because an object is too complex for the WKT format capability
* (for example an {@linkplain org.geotools.referencing.crs.DefaultEngineeringCRS
* engineering CRS} with different unit for each axis), or because only some specific
* implementations can be formatted as WKT.
*/
public String toWKT(final int indentation) throws UnformattableObjectException {
return toWKT(Citations.OGC, indentation);
}
/**
* Returns a
* Well
* Known Text (WKT) for this object using the specified indentation and authority.
*
* @param authority The authority to prefer when choosing WKT entities names.
* @param indentation The amount of spaces to use in indentation for WKT formatting,
* or {@link #SINGLE_LINE} for formatting the whole WKT on a single line.
* @return The Well Know Text for this object.
* @throws UnformattableObjectException If this object can't be formatted as WKT.
* A formatting may fails because an object is too complex for the WKT format capability
* (for example an {@linkplain org.geotools.referencing.crs.DefaultEngineeringCRS
* engineering CRS} with different unit for each axis), or because only some specific
* implementations can be formatted as WKT.
*/
public String toWKT(final Citation authority, final int indentation)
throws UnformattableObjectException
{
return toWKT(authority, indentation, true);
}
/**
* Returns a WKT for this object using the specified indentation and authority.
* If {@code strict} is true, then an exception is thrown if the WKT contains
* invalid keywords.
*/
private String toWKT(final Citation authority, final int indentation, final boolean strict)
throws UnformattableObjectException
{
if (authority == null) {
throw new IllegalArgumentException(Errors.format(
ErrorKeys.NULL_ARGUMENT_$1, "authority"));
}
Formatter formatter = FORMATTER.get();
if (formatter == null ||
formatter.indentation != indentation ||
formatter.getAuthority() != authority)
{
formatter = new Formatter(Symbols.DEFAULT, indentation);
formatter.setAuthority(authority);
FORMATTER.set(formatter);
}
try {
if (this instanceof GeneralParameterValue) {
// Special processing for parameter values, which is formatted
// directly in 'Formatter'. Note that in GeoAPI, this interface
// doesn't share the same parent interface than other interfaces.
formatter.append((GeneralParameterValue) this);
} else {
formatter.append(this);
}
if (strict && formatter.isInvalidWKT()) {
final Class unformattable = formatter.getUnformattableClass();
throw new UnformattableObjectException(formatter.warning, unformattable);
}
return formatter.toString();
} finally {
formatter.clear();
}
}
/**
* Format the inner part of a
* Well
* Known Text (WKT) element. This method is automatically invoked by
* {@link Formatter#append(Formattable)}. Element name and authority code must not be
* formatted here. For example for a {@code GEOGCS} element
* ({@link org.geotools.referencing.crs.DefaultGeographicCRS}), the formatter will invokes
* this method for completing the WKT at the insertion point show below:
*
*
* GEOGCS["WGS 84", AUTHORITY["EPSG","4326"]]
* |
* (insertion point)
*
*
* The default implementation declares that this object produces an invalid WKT.
* Subclasses must override this method for proper WKT formatting and should
* not invoke {@code super.formatWKT(formatter)} if they can
* use a valid WKT syntax.
*
* @param formatter The formatter to use.
* @return The name of the WKT element type (e.g. {@code "GEOGCS"}).
*
* @see #toWKT
* @see #toString
*/
protected String formatWKT(final Formatter formatter) {
Class type = getClass();
formatter.setInvalidWKT(type);
Class[] interfaces = type.getInterfaces();
for (int i=0; iRuntimePermission("preferences").
*/
static void setIndentation(final int indentation) throws SecurityException {
Preferences.userNodeForPackage(Formattable.class).putInt(INDENTATION, indentation);
}
/**
* Cleans up the thread local set in this thread. They can prevent web applications from
* proper shutdown
*/
public static void cleanupThreadLocals() {
FORMATTER.remove();
}
}