|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectFormat
WKTFormat
public class WKTFormat
Parser and formatter for Well Known Text (WKT) objects. This format handles a
pair of Parser and Formatter, to be used by parse and format
methods respectively.
WKTFormat objects allow the following configuration:
Formatter.getName(IdentifiedObject) for more information)
String expansion
Because the strings to be parsed by this class are long and tend to contain repetitive
substrings, WKTFormat provides a mechanism for performing string substitutions
before the parsing take place. Long strings can be assigned short names by calls to the
definitions().put(key,value) method.
After definitions have been added, any call to a parsing method will replace all occurrences
of a short name by the associated long string.
The short names must comply with the rules of Java identifiers. It is recommended, but not
required, to prefix the names by some symbol like "$" in order to avoid ambiguity.
Note however that this class doesn't replace occurrences between quoted text, so string
expansion still relatively safe even when used with non-prefixed identifiers.
In the example below, the $WGS84 substring which appear in the argument given to the
parseObject method will be expanded into the full GEOGCS["WGS84", ...] string
before the parsing proceed.
definitions().put("$WGS84", "GEOGCS[\"WGS84\", DATUM[...etc...]]);...etc...
Object crs = parseObject("PROJCS[\"Mercator_1SP\", $WGS84, PROJECTION[]]");
Synchronization
WKTFormats are not synchronized. It is recommended to create separate format instances
for each thread. If multiple threads access a format concurrently, it must be synchronized
externally.
| referencing/geotk-referencing (download) | View source code for this class |
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class Format |
|---|
Format.Field |
| Constructor Summary | |
|---|---|
WKTFormat()
Constructs a format using the default factories. |
|
WKTFormat(Hints hints)
Constructs a format using the given factories for creating parsed objects. |
|
| Method Summary | ||
|---|---|---|
Map<String,String> |
definitions()
Returns a map of short identifiers to substitute by WKT string before parsing. |
|
StringBuffer |
format(Object object,
StringBuffer toAppendTo,
FieldPosition pos)
Formats the specified object as a Well Know Text. |
|
Citation |
getAuthority()
Returns the preferred authority for formatting WKT entities. |
|
static Class<?> |
getClassOf(String element)
Returns the class of the specified WKT element. |
|
Colors |
getColors()
Returns the set of colors to use for syntax coloring, or null if none. |
|
int |
getIndentation()
Returns the current indentation to be used for formatting objects. |
|
static String |
getNameOf(Class<?> type)
Returns the WKT name of the specified object type. |
|
Symbols |
getSymbols()
Returns the symbols used for parsing and formatting WKT. |
|
String |
getWarning()
If a warning occurred during the last WKT formatting, returns the warning. |
|
|
parse(String text,
int offset,
Class<T> type)
Parses the specified text and ensures that the resulting object is of the specified type. |
|
Object |
parseObject(String text)
Parses the specified Well Know Text (WKT). |
|
Object |
parseObject(String text,
ParsePosition position)
Parses the specified Well Know Text starting at the specified position. |
|
void |
printDefinitions(Writer out)
Prints to the specified stream a table of all definitions. |
|
void |
reformat(Reader in,
Writer out,
PrintWriter err)
Reads WKT strings from an input stream and reformats them to the specified output stream. |
|
void |
setAuthority(Citation authority)
Sets the preferred authority for formatting WKT entities. |
|
void |
setColors(Colors colors)
Sets the colors to use for syntax coloring on ANSI X3.64 (aka ECMA-48 and ISO/IEC 6429) compatible terminal. |
|
void |
setIndentation(int indentation)
Sets a new indentation to be used for formatting objects. |
|
void |
setSymbols(Symbols symbols)
Sets the symbols used for parsing and formatting WKT. |
|
| Methods inherited from class Format |
|---|
clone, format, formatToCharacterIterator |
| Methods inherited from class Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public WKTFormat()
public WKTFormat(Hints hints)
hints - The hints to be used for fetching the factories, or
null for the system-wide default hints.| Method Detail |
|---|
public Symbols getSymbols()
public void setSymbols(Symbols symbols)
symbols - The new set of symbols to use for parsing and formatting WKT.public Colors getColors()
null if none.
If non-null, the set of colors are escape sequences for ANSI X3.64 (aka ECMA-48
and ISO/IEC 6429) compatible terminal. By default there is no syntax coloring.
null if none.public void setColors(Colors colors)
Newly created WKTFormat have no syntax coloring. If the DEFAULT
set of colors is given to this method, then the format method tries to highlight
most of the elements that are relevant to CRS.equalsIgnoreMetadata(java.lang.Object, java.lang.Object).
colors - The set of colors for syntax coloring, or null if none.public Citation getAuthority()
format
method will use the name specified by this authority, if available. See the
Formatter javadoc for more information.
Formatter.getName(IdentifiedObject)public void setAuthority(Citation authority)
format
method will use the name specified by this authority, if available. See the
Formatter javadoc for more information.
authority - The new authority.Formatter.getName(IdentifiedObject)public int getIndentation()
public void setIndentation(int indentation)
indentation - The new indentation to use.public Map<String,String> definitions()
Entries added in the definitions map will have immediate effect in this WKTFormat
object. They must obey the following constraints:
Any attempt to put an illegal key or value in the definitions map will result in
an IllegalArgumentException being thrown.
public void printDefinitions(Writer out)
throws IOException
out - writer The output stream where to write the table.
IOException - if an error occurred while writing to the output stream.
public <T> T parse(String text,
int offset,
Class<T> type)
throws ParseException
T - The expected type of the object to be parsed.text - The WKT to parse, or an identifier given to the
definitions map.offset - The index of the first character to parse in the given text. This
information is explicitly given instead than expecting the caller to compute
text.substring(offset) in order to provide more accurate error offset
in case of ParseException.type - The expected type of the object to be parsed (usually a
CoordinateReferenceSystem.class or
MathTransform.class).
ParseException - if the string can't be parsed.
public Object parseObject(String text)
throws ParseException
parse(text, 0, Object.class).
parseObject in class Formattext - The text to be parsed.
ParseException - if the string can't be parsed.
public Object parseObject(String text,
ParsePosition position)
parseObject(wkt.substring(position.getIndex())).
Note: The other way around (parseObject(String)invokingparseObject(String,ParsePosition)as in the defaultFormatimplementation) is not pratical in the context ofWKTFormat. Among other problems, it doesn't provide any accurate error message.
parseObject in class Formattext - The text to parse.position - The index of the first character to parse.
null in case of failure.
public StringBuffer format(Object object,
StringBuffer toAppendTo,
FieldPosition pos)
format in class Formatobject - The object to format.toAppendTo - Where the text is to be appended.pos - An identification of a field in the formatted text.getWarning()
public void reformat(Reader in,
Writer out,
PrintWriter err)
throws IOException
This method is useful for changing the indentation, rewriting the WKT using parameter names specified by a different authority, for adding syntax coloring, expanding the WKT strings according the definitions, etc.
in - The input stream.out - The output stream.err - The error stream.
IOException - if an error occurred while reading from the input stream
or writing to the output stream.public String getWarning()
null. The warning is cleared
every time a new object is formatted.
null if none.public static Class<?> getClassOf(String element)
ProjectedCRS.class for element "PROJCS".
This method is the converse of getNameOf(java.lang.Class>).
element - The WKT element name.
null if unknown.public static String getNameOf(Class<?> type)
PROJCS" for type ProjectedCRS.class.
This method is the converse of getClassOf(java.lang.String).
type - The GeoAPI class of the specified element.
null if unknown.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||