类 SentenceFactory
java.lang.Object
net.sf.marineapi.nmea.parser.SentenceFactory
Factory for creating sentence parsers.
Custom parsers may be implemented and registered in the factory at runtime by following these steps:
- Define a sentence interface by extending the
Sentenceinterface (e.g.com.acme.XYZSentence). - Implement the interface in a class that extends
SentenceParser, (e.g.com.acme.XYZParser). - Use the protected getters and setters in
SentenceParserto read and write sentence data. - Add a constructor in
XYZParserwithStringparameter, i.e. the sentence to be parsed. Pass this parameter toSentenceParser(String, String)with expected sentence type (e.g."XYZ"). - Add another constructor with
TalkerIdparameter. Pass this parameter toSentenceParser(net.sf.marineapi.nmea.sentence.TalkerId, String, int)with sentence type and the expected number of data fields. - Register
XYZParserinSentenceFactoryby using theregisterParser(String, Class)method. - Use
createParser(String)orcreateParser(net.sf.marineapi.nmea.sentence.TalkerId, String)to obtain an instance of your parser. In addition,SentenceReaderwill now dispatch instances ofXYZSentencewhen "XYZ" sentences are read from the data source.
Notice that there is no need to compile the whole library and the added parser source code may be located in your own codebase. Additionally, it is also possible to override any existing parsers of the library as needed.
- 作者:
- Kimmo Tuukkanen, Gunnar Hillert
-
方法概要
修饰符和类型方法说明createParser(String nmea) Creates a parser for specified NMEA 0183 sentence String.createParser(TalkerId talker, String type) Creates a parser for specified talker and sentence type.createParser(TalkerId talker, SentenceId type) Creates a parser for specified talker and sentence type.static SentenceFactoryReturns the singleton instance ofSentenceFactory.booleanTells if the factory is able to create parser for specified sentence type.Returns a list of currently parseable sentence types.voidregisterParser(String type, Class<? extends SentenceParser> parser) Registers a sentence parser to the factory.voidreset()Resets the factory in it's initial state, i.e. restores and removes all parsers the have been either removed or added.voidunregisterParser(Class<? extends SentenceParser> parser) Unregisters a parser class, regardless of sentence type(s) it is registered for.
-
方法详细资料
-
createParser
Creates a parser for specified NMEA 0183 sentence String. The parser implementation is selected from registered parsers according to sentence type. The returned instance must be cast in to correct sentence interface, for which the type should first be checked by using theSentence.getSentenceId()method.- 参数:
nmea- NMEA 0183 sentence String- 返回:
- Sentence parser instance for specified sentence
- 抛出:
IllegalArgumentException- If there is no parser registered for the given sentence typeIllegalStateException- If parser is found, but it does not implement expected constructors or is otherwise unusable.
-
createParser
Creates a parser for specified talker and sentence type. The returned instance needs to be cast to corresponding sentence interface.- 参数:
talker- Sentence talker idtype- Sentence type- 返回:
- Sentence parser of requested type.
- 抛出:
IllegalArgumentException- If talker id is null or if there is no parser registered for given sentence type.IllegalStateException- If parser instantiation fails.
-
createParser
Creates a parser for specified talker and sentence type. This method is mainly intended to be used when custom parsers have been registered in the factory. The returned instance needs to be cast to corresponding sentence interface.- 参数:
talker- Talker ID to use in parsertype- Type of the parser to create- 返回:
- Sentence parser for requested type
- 抛出:
IllegalArgumentException- If talker id is null or if there is no parser registered for given sentence type.IllegalStateException- If parser is found, but it does not implement expected constructors or is otherwise unusable.
-
hasParser
Tells if the factory is able to create parser for specified sentence type. AllSentenceIdenum values should result returningtrueat all times.- 参数:
type- Sentence type id, e.g. "GLL" or "GGA".- 返回:
- true if type is supported, otherwise false.
-
listParsers
Returns a list of currently parseable sentence types.- 返回:
- List of sentence ids
-
registerParser
Registers a sentence parser to the factory. After registration,createParser(String)method can be used to obtain instances of registered parser.Sentences supported by the library are registered automatically, but they can be overridden simply be registering a new parser implementation for chosen sentence type. That is, each sentence type can have only one parser registered at a time.
- 参数:
type- Sentence type id, e.g. "GGA" or "GLL".parser- Class of parser implementation for giventype.
-
unregisterParser
Unregisters a parser class, regardless of sentence type(s) it is registered for.- 参数:
parser- Parser implementation class fortype.- 另请参阅:
-
getInstance
Returns the singleton instance ofSentenceFactory.- 返回:
- SentenceFactory instance
-
reset
public void reset()Resets the factory in it's initial state, i.e. restores and removes all parsers the have been either removed or added.
-