Class AbstractParser
- All Implemented Interfaces:
Parser
- Direct Known Subclasses:
MathTransformParser
Formatter
.
Like the latter, a parser is constructed with a given set of symbols.
Parsers also need a set of factories to be used for instantiating the parsed objects.
In current version, parsers are not intended to be subclassed outside this package.
Parsers are not synchronized. It is recommended to create separate parser instances for each thread. If many threads access the same parser instance concurrently, it must be synchronized externally.
- Since:
- 0.6
- Version:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate DateFormat
The object to use for parsing dates, created when first needed.(package private) final Locale
The locale for formatting error messages if parsing fails, ornull
for system default.private final String
The symbol for scientific notation, ornull
if none.(package private) static final int
A mode for theElement.pullElement(int, String...)
method meaning that only the first element should be checked.(package private) final Map<String,
StoredTree> Reference to theWKTFormat.fragments
map, or an empty map if none.Keyword of unknown elements.(package private) static final int
A mode for theElement.pullElement(int, String...)
method meaning that an exception shall be thrown if no element has a name matching one of the requested names.private final NumberFormat
The object to use for parsing numbers.(package private) static final int
A mode for theElement.pullElement(int, String...)
method meaning that the requested element is optional but not necessarily first.(package private) final Symbols
The symbols to use for parsing WKT.private UnitFormat
The object to use for parsing unit symbols, created when first needed.private Warnings
The warning (other thanignoredElements
) that occurred during the parsing. -
Constructor Summary
ConstructorsConstructorDescriptionAbstractParser
(Symbols symbols, Map<String, StoredTree> fragments, NumberFormat numberFormat, DateFormat dateFormat, UnitFormat unitFormat, Locale errorLocale) Constructs a parser using the specified set of symbols. -
Method Summary
Modifier and TypeMethodDescription(package private) abstract Object
buildFromTree
(Element element) Parses the next element in the specified Well Know Text (WKT) tree.final Object
createFromWKT
(String wkt) Creates the object from a WKT string and logs the warnings if any.(package private) Object
createFromWKT
(String text, ParsePosition position) Parses a Well-Know Text from specified position as a geodetic object.(package private) static int
endOfFragmentName
(String text, int position) Returns the index after the end of the fragment name starting at the given index.(package private) final Warnings
getAndClearWarnings
(Object result) Returns the warnings, ornull
if none.(package private) String
Returns the name of the method invoked fromgetPublicFacade()
.(package private) abstract String
Returns the name of the class providing the publicly-accessiblecreateFromWKT(String)
method.(package private) final void
Logs the given record for a warning that occurred during parsing.(package private) final Date
parseDate
(String text, ParsePosition position) Parses the date at the given position.(package private) final Number
parseNumber
(String text, ParsePosition position) Parses the number at the given position.(package private) final javax.measure.Unit<?>
Parses the given unit name or symbol.(package private) final Element
textToTree
(String wkt, ParsePosition position) Parses the Well Know Text from specified position as a tree ofElement
s.(package private) final void
Reports a non-fatal warning that occurred while parsing a WKT.(package private) final void
warning
(Element parent, Element element, org.opengis.util.InternationalString message, Exception ex) Reports a non-fatal warning that occurred while parsing a WKT.
-
Field Details
-
FIRST
static final int FIRSTA mode for theElement.pullElement(int, String...)
method meaning that only the first element should be checked. If the name of the first element does not match one of the specified names, thenpullElement(…)
returnsnull
.- See Also:
-
OPTIONAL
static final int OPTIONALA mode for theElement.pullElement(int, String...)
method meaning that the requested element is optional but not necessarily first. If no element has a name matching one of the requested names, thenpullElement(…)
returnsnull
.- See Also:
-
MANDATORY
static final int MANDATORYA mode for theElement.pullElement(int, String...)
method meaning that an exception shall be thrown if no element has a name matching one of the requested names.- See Also:
-
errorLocale
The locale for formatting error messages if parsing fails, ornull
for system default. This is not the locale for parsing number or date values. The locale for numbers and dates is contained insymbols
. -
symbols
The symbols to use for parsing WKT. -
exponentSymbol
The symbol for scientific notation, ornull
if none. This is usually"E"
(note the upper case), but could also be something like"×10^"
. -
numberFormat
The object to use for parsing numbers. -
dateFormat
The object to use for parsing dates, created when first needed. -
unitFormat
The object to use for parsing unit symbols, created when first needed. -
fragments
Reference to theWKTFormat.fragments
map, or an empty map if none. Shall be used in read-only mode; never write through this reference. -
ignoredElements
Keyword of unknown elements. The ISO 19162 specification requires that we ignore unknown elements, but we will nevertheless report them as warnings. The meaning of this map is:- 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.
Warnings
will copy its content only when first needed.- See Also:
-
warnings
The warning (other thanignoredElements
) that occurred during the parsing. Created when first needed and reset tonull
when a new parsing start. Warnings are reported whengetAndClearWarnings(Object)
is invoked.
-
-
Constructor Details
-
AbstractParser
AbstractParser(Symbols symbols, Map<String, StoredTree> fragments, NumberFormat numberFormat, DateFormat dateFormat, UnitFormat unitFormat, Locale errorLocale) Constructs a parser using the specified set of symbols.- Parameters:
symbols
- the set of symbols to use.fragments
- reference to theWKTFormat.fragments
map, or an empty map if none.numberFormat
- the number format provided byWKTFormat
, ornull
for a default format.dateFormat
- the date format provided byWKTFormat
, ornull
for a default format.unitFormat
- the unit format provided byWKTFormat
, ornull
for a default format.errorLocale
- the locale for error messages (not for parsing), ornull
for the system default.
-
-
Method Details
-
getPublicFacade
Returns the name of the class providing the publicly-accessiblecreateFromWKT(String)
method. This information is used for logging purposes only. Values can be:"org.apache.sis.io.wkt.WKTFormat"
"org.apache.sis.referencing.factory.GeodeticObjectFactory"
"org.apache.sis.referencing.operation.transform.DefaultMathTransformFactory"
-
getFacadeMethod
String getFacadeMethod()Returns the name of the method invoked fromgetPublicFacade()
. This information is used for logging purposes only. Another possible value is"parse"
. -
log
Logs the given record for a warning that occurred during parsing. This is used when we cannot use thewarning methods
, or when the information is not worth to report as a warning. -
createFromWKT
Creates the object from a WKT string and logs the warnings if any. This method is for implementation ofcreateFromWKT(String)
method in SIS factories only. Callers should ensure thatwkt
is non-null and non-empty (this method does not verify).- Specified by:
createFromWKT
in interfaceParser
- Parameters:
wkt
- object encoded in Well-Known Text format (version 1 or 2).- Returns:
- the result of parsing the given text.
- Throws:
org.opengis.util.FactoryException
- if the object creation failed.- See Also:
-
createFromWKT
Parses a Well-Know Text from specified position as a geodetic object. Caller should invokegetAndClearWarnings(Object)
in afinally
block after this method and should decide what to do with remaining character at the end of the string.If this method is invoked from
WKTFormat
, thenWKTFormat.clear()
should be invoked before this method for making sure that noWarnings
instance is referencingignoredElements
.- Parameters:
text
- the Well-Known Text (WKT) to parse.position
- index of the first character to parse (on input) or after last parsed character (on output).- Returns:
- the parsed object.
- Throws:
ParseException
- if the string cannot be parsed.
-
endOfFragmentName
Returns the index after the end of the fragment name starting at the given index. Current implementation assumes that the fragment name is a Unicode identifier, except for the first character which is not required to be an identifier start. -
textToTree
Parses the Well Know Text from specified position as a tree ofElement
s. This tree can be given tobuildFromTree(Element)
for producing a geodetic object.- Parameters:
wkt
- the Well-Known Text to be parsed.position
- before parsing, provides index of the first character to parse in thewkt
string. After parsing completion, provides index after the last character parsed.- Returns:
- the parsed object as a tree of
Element
s. - Throws:
ParseException
- if the string cannot be parsed.- See Also:
-
buildFromTree
Parses the next element in the specified Well Know Text (WKT) tree. Subclasses will typically get the name of the first element and delegate to a specialized method such asparseAxis(…)
,parseEllipsoid(…)
,parseTimeDatum(…)
, etc.Callers should clear
ignoredElements
before to invoke this method. Cleaningwarnings
can be done for safety but should not be necessary.- Parameters:
element
- the element to be parsed.- Returns:
- the parsed object.
- Throws:
ParseException
- if the element cannot be parsed.
-
parseNumber
Parses the number at the given position. This is a helper method forElement
only. -
parseDate
Parses the date at the given position. This is a helper method forElement
only.The WKT 2 format expects dates formatted according the ISO 9075-2 standard.
-
parseUnit
Parses the given unit name or symbol. Contrarily to otherparseFoo()
methods, this method has noParsePosition
and expects the given string to be the full unit symbol.- Throws:
javax.measure.format.ParserException
-
warning
final void warning(Element parent, Element element, org.opengis.util.InternationalString message, Exception ex) Reports a non-fatal warning that occurred while parsing a WKT.- Parameters:
parent
- the parent element, ornull
if unknown.element
- the element that we cannot parse, ornull
if unknown.message
- the message. Can benull
only ifex
is non-null.ex
- the non-fatal exception that occurred while parsing the element, ornull
.
-
warning
final void warning(Element parent, String element, org.opengis.util.InternationalString message, Exception ex) Reports a non-fatal warning that occurred while parsing a WKT.- Parameters:
parent
- the parent element, ornull
if unknown.element
- the name of the element that we cannot parse, ornull
if unknown.message
- the message. Can benull
only ifex
is non-null.ex
- the non-fatal exception that occurred while parsing the element, ornull
.
-
getAndClearWarnings
Returns the warnings, ornull
if none. This method clears the warnings after the call.The returned object is valid only until a new parsing starts. If a longer lifetime is desired, then the caller must invokes
Warnings.publish()
.- Parameters:
result
- the object that resulted from the parsing operation, ornull
.- Returns:
- the warnings, or
null
if none.
-