Package org.apache.sis.io.wkt
Class Element
java.lang.Object
org.apache.sis.io.wkt.Element
An element in a Well Know Text (WKT). An
Element is made of String,
Number and other Element. For example:
Each Element object can contain an arbitrary number of other elements.
The result is a tree, which can be seen with toString() for debugging purpose.
Elements can be pulled by their name and other children (numbers, dates, strings)
can be pulled in a first in, first out order.
Sharing repetitive information
Element instances are mutable because AbstractParser needs to remove elements from
the children list as they are processed. If that parsing does not happen immediately,
then Element content needs to be copied in a different structure (StoredTree)
which is immutable and designed for reducing redundancies.- Since:
- 0.6
- Version:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final LocaleThe locale to be used for formatting an error message if the parsing fails, ornullfor the system default.private final booleantrueif the keyword was not followed by a pair of brackets (e.g.(package private) final booleantrueif this element has been reconstituted fromWKTFormat.fragments.(package private) booleanWhether this element is the root of a tree of WKT elements.final StringKeyword of the WKT element, for example"PrimeMeridian".private byteIndex of the keyword in the array given to thepullElement(int, String...)method.private static final intKind of value expected in the element.(package private) final intIndex of the character where this element starts in the WKT string to parse.private static final intKind of value expected in the element.private static final String[]Hard-coded list of elements in which to parse values as dates instead of numbers. -
Constructor Summary
ConstructorsConstructorDescriptionElement(String keyword, LinkedList<Object> children, int offset, Locale errorLocale) Creates a new node for the given keyword and list of children.Element(AbstractParser parser, String text, ParsePosition position) Constructs a newElementby parsing the given WKT string starting at the given position.Constructs a root element as a modifiable wrapper around the given element. -
Method Summary
Modifier and TypeMethodDescription(package private) final voidCloses this element.private voidformat(StringBuilder buffer, int margin, String lineSeparator) Implementation oftoString()to be invoked recursively.(package private) final Object[]Returns a copy of children list, ornullif thisElementis an enumeration.(package private) final intReturns the index of the keyword in the array given to thepullElement(int, String...)method.(package private) final ParseExceptionillegalCS(org.opengis.referencing.cs.CoordinateSystem cs) Returns aParseExceptionfor an illegal coordinate system.booleanisEmpty()Returnstrueif this element does not contains any remaining child.private ParseExceptionmissingCharacter(int c, int errorIndex, ParsePosition position) Returns an exception saying that a character is missing.(package private) final ParseExceptionmissingComponent(String key) Returns an exception saying that a sub-element is missing.(package private) final ParseExceptionmissingOrUnknownComponent(String expected) Returns aParseExceptionfor a child keyword which is unknown.private intReturns index of the character after the keyword in the WKT string to parse.(package private) final ParseExceptionparseFailed(Exception cause) Returns aParseExceptionwith the specified cause.Returns the next value (not a child element) without removing it.booleanpullBoolean(String key) Removes the nextBooleanfrom the children and returns it.Removes the nextDatefrom the children and returns it.doublepullDouble(String key) Removes the nextNumberfrom the children and returns it.pullElement(int mode, String... keys) Removes the nextElementof the given name from the children and returns it.intpullInteger(String key) Removes the nextNumberfrom the children and returns it as an integer.pullObject(String key) Removes the nextObjectfrom the children and returns it.<T> TpullOptional(Class<T> type) Removes the next object of the given type from the children and returns it, if presents.pullString(String key) Removes the nextStringfrom the children and returns it.pullVoidElement(String key) Removes and returns the nextElementwith no bracket.private static intregionMatches(String text, int index, String word) Increments the givenindexif and only if the word at that position is the given word, ignoring case.toString()Formats thisElementas a tree.private ParseExceptionunparsableString(String text, ParsePosition position) Returns aParseExceptionwith a "Unparsable string" message.
-
Field Details
-
NUMERIC
private static final int NUMERICKind of value expected in the element. Value 0 means "not yet determined".- See Also:
-
TEMPORAL
private static final int TEMPORALKind of value expected in the element. Value 0 means "not yet determined".- See Also:
-
TIME_KEYWORDS
Hard-coded list of elements in which to parse values as dates instead of numbers. We may try to find a more generic approach in a future version. -
offset
final int offsetIndex of the character where this element starts in the WKT string to parse.- See Also:
-
keywordIndex
private byte keywordIndexIndex of the keyword in the array given to thepullElement(int, String...)method. This is a workaround for the lack of multiple return values in Java.- See Also:
-
keyword
Keyword of the WKT element, for example"PrimeMeridian". Nevernull. -
isEnumeration
private final boolean isEnumerationtrueif the keyword was not followed by a pair of brackets (e.g. "north"). Iftrue, thenchildrenshall be an empty list. -
isFragment
final boolean isFragmenttrueif this element has been reconstituted fromWKTFormat.fragments. In such case, alloffsetvalues are identical. -
isRoot
boolean isRootWhether this element is the root of a tree of WKT elements. This field is not used by thisElementclass. It is provided forWKTDictionaryconvenience. -
children
An ordered sequence ofStrings,Numbers and otherElements. Access to this collection should be done using the iterator, not by random access. Parsing will remove elements (in any order) from this list as they are consumed.- See Also:
-
errorLocale
The locale to be used for formatting an error message if the parsing fails, ornullfor the system default. This is not the locale for parting number or date values.Design note: the same reference is duplicated in everyElementinstances. We nevertheless copy it as a convenience for avoiding to make this argument appears in thepullFoo(…)methods.
-
-
Constructor Details
-
Element
Element(Element singleton) Constructs a root element as a modifiable wrapper around the given element. This wrapper is a convenience for branching on different codes depending on the keyword value. For example:- Parameters:
singleton- the only child for this root.- See Also:
-
Element
Element(String keyword, LinkedList<Object> children, int offset, Locale errorLocale) Creates a new node for the given keyword and list of children. This is used byStoredTreefor recreating a tree ofElements from a previously saved snapshot.- Parameters:
keyword- keyword of the WKT element, e.g."PrimeMeridian". Shall not benull.children- children of this element, ornullif this element is an enumeration.offset- index of the character where this element started in the WKT string. If negative, actual offset is~offsetandisFragmentis set totrue.
-
Element
Element(AbstractParser parser, String text, ParsePosition position) throws ParseException Constructs a newElementby parsing the given WKT string starting at the given position.- Parameters:
parser- information about symbols (such as brackets) and formats to use.text- the Well-Known Text (WKT) to parse.position- on input, the position where to start parsing from. On output, the first character after the separator.- Throws:
ParseException- if quotes, brackets or parenthesis are not balanced, or a date/number cannot be parsed, or a referenced WKT fragment (e.g."$FOO") cannot be found.
-
-
Method Details
-
regionMatches
Increments the givenindexif and only if the word at that position is the given word, ignoring case. Otherwise returns the index unchanged. -
parseFailed
Returns aParseExceptionwith the specified cause. A localized string"Error in <will be prepend to the message. The error index will be the starting index of thiskeyword>"Element.- Parameters:
cause- the cause of the failure, ornullif none.- Returns:
- the exception to be thrown.
-
unparsableString
Returns aParseExceptionwith a "Unparsable string" message. The error message is built from the specified string starting at the specified position. TheParsePosition.getErrorIndex()property must be accurate before this method is invoked. TheParsePosition.getIndex()property will be set by this method.- Parameters:
text- the unparsable string.position- the position in the string.- Returns:
- an exception with a formatted error message.
-
missingCharacter
Returns an exception saying that a character is missing.- Parameters:
c- the missing character.errorIndex- the error position.position- the position to update with the error index.
-
missingComponent
Returns an exception saying that a sub-element is missing.- Parameters:
key- the name of the missing sub-element.
-
missingOrUnknownComponent
Returns aParseExceptionfor a child keyword which is unknown.- Parameters:
expected- keyword of a typical element. Used only if this element contains no child element.- Returns:
- the exception to be thrown.
-
illegalCS
Returns aParseExceptionfor an illegal coordinate system.The given
csargument should never be null with Apache SIS implementation ofCSFactory, but could be null with user supplied implementation. But it would be aCSFactorycontract violation, so the user would get aNullPointerExceptionlater. For making easier to trace the cause, we throw here an exception with a similar error message.- Parameters:
cs- the illegal coordinate system.- Returns:
- the exception to be thrown.
-
offsetAfterKeyword
private int offsetAfterKeyword()Returns index of the character after the keyword in the WKT string to parse. -
peekValue
Returns the next value (not a child element) without removing it.- Returns:
- the next value, or
nullif none.
-
pullDate
Removes the nextDatefrom the children and returns it.- Parameters:
key- the parameter name. Used for formatting an error message if no date is found.- Returns:
- the next
Dateamong the children. - Throws:
ParseException- if no more date is available.
-
pullDouble
Removes the nextNumberfrom the children and returns it.- Parameters:
key- the parameter name. Used for formatting an error message if no number is found.- Returns:
- the next
Numberamong the children as adouble. - Throws:
ParseException- if no more number is available.
-
pullInteger
Removes the nextNumberfrom the children and returns it as an integer.- Parameters:
key- the parameter name. Used for formatting an error message if no number is found.- Returns:
- the next
Numberamong the children as anint. - Throws:
ParseException- if no more number is available, or the number is not an integer.
-
pullBoolean
Removes the nextBooleanfrom the children and returns it.- Parameters:
key- the parameter name. Used for formatting an error message if no boolean is found.- Returns:
- the next
Booleanamong the children as aboolean. - Throws:
ParseException- if no more boolean is available.
-
pullString
Removes the nextStringfrom the children and returns it.- Parameters:
key- the parameter name. Used for formatting an error message if no number is found.- Returns:
- the next
Stringamong the children. - Throws:
ParseException- if no more string is available.
-
pullObject
Removes the nextObjectfrom the children and returns it.- Parameters:
key- the parameter name. Used for formatting an error message if no number is found.- Returns:
- the next
Objectamong the children (nevernull). - Throws:
ParseException- if no more object is available.
-
pullElement
Removes the nextElementof the given name from the children and returns it. If the element was mandatory but is missing, then the first entry in the givenkeysarray will be taken as the name of the missing element to report in the exception message.The given
modeargument can be one of the following constants:AbstractParser.MANDATORYthrow an exception if no matching element is found.AbstractParser.OPTIONALreturnnullif no matching element is found.AbstractParser.FIRSTreturnnullif the first element (ignoring all others) does not match.
- Parameters:
mode-AbstractParser.FIRST,AbstractParser.OPTIONALorAbstractParser.MANDATORY.keys- the element names (e.g."PrimeMeridian").- Returns:
- the next
Elementof the given names found among the children, ornullif none. - Throws:
ParseException- ifmodeisMANDATORYand no element of the given names was found.
-
pullVoidElement
Removes and returns the nextElementwith no bracket. The key is used only for only for formatting an error message.- Parameters:
key- the parameter name. Used only for formatting an error message.- Returns:
- the next
Elementamong the children, with no bracket. - Throws:
ParseException- if no more void element is available.
-
pullOptional
Removes the next object of the given type from the children and returns it, if presents.- Parameters:
type- the object type.- Returns:
- the next object among the children, or
nullif none.
-
getChildren
Returns a copy of children list, ornullif thisElementis an enumeration. This method is used only for creating a snapshot of thisElementinStoredTree. The returned array may contain nestedElementinstances. -
isEmpty
public boolean isEmpty()Returnstrueif this element does not contains any remaining child.- Returns:
trueif there are no children remaining.
-
getKeywordIndex
final int getKeywordIndex()Returns the index of the keyword in the array given to thepullElement(int, String...)method. -
close
Closes this element. This method verifies that there are no unprocessed values (dates, numbers, booleans or strings), but ignores inner elements as required by ISO 19162. This method adds the keywords of ignored elements in theignoredElementsmap as below:- Keys: keyword of ignored elements. Note that a key may be null.
- Values: keywords of all elements containing an element identified by the above-cited key. This list is used for helping the users to locate the ignored elements.
- Parameters:
ignoredElements- the collection where to declare ignored elements.- Throws:
ParseException- if the children list still contains some unprocessed values.
-
toString
Formats thisElementas a tree. This method is used for debugging purpose only. -
format
Implementation oftoString()to be invoked recursively.- Parameters:
buffer- where to format.margin- number of spaces to put in the left margin.
-