net.sf.marineapi.nmea.parser
Class SentenceFactory

java.lang.Object
  extended by net.sf.marineapi.nmea.parser.SentenceFactory

public final class SentenceFactory
extends Object

Factory for creating sentence parsers. Custom parsers may be registered using the registerParser(String, Class) method.

How-to create and register custom parsers:

  1. First, define the sentence interface (e.g. XYZSentence).
  2. Implement the interface in a class that extends SentenceParser, (e.g. XYZParser).
  3. Use the protected get/set methods in SentenceParser to read and write sentence fields.
  4. Make sure your parser has a constructor with single String parameter, i.e. the sentence to be parsed. Pass this parameter to SentenceParser.SentenceParser(String, String) together with assumed sentence id (e.g. "XYZ").
  5. Register your parser class in SentenceFactory using the registerParser(String, Class) method.
  6. Finally, feed the createParser(String) with XYZ sentence String and you should get an instance of XYZSentence interface.

Version:
$Revision: 100 $
Author:
Kimmo Tuukkanen

Method Summary
 Sentence createParser(String nmea)
          Creates a parser for specified NMEA sentence String.
static SentenceFactory getInstance()
          Returns the singleton instance of SentenceFactory.
 boolean hasParser(String type)
          Tells if the factory is able to create parser for specified sentence type.
 void registerParser(String type, Class<? extends SentenceParser> parser)
          Register a sentence parser to factory.
 void unregisterParser(Class<? extends SentenceParser> parser)
          Unregisters a parser class, regardless of sentence type(s) it is registered for.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createParser

public Sentence createParser(String nmea)
Creates a parser for specified NMEA sentence String. The parser implementation is selected by the sentence id

Parameters:
nmea - NMEA 0183 sentence String
Returns:
Sentence parser instance for specified sentence
Throws:
IllegalArgumentException - If there is no parser registered for the given sentence type
IllegalStateException - If parser is found, but it does not implement expected constructor with single String parameter.
RuntimeException - If unable to find or access the parser.

hasParser

public boolean hasParser(String type)
Tells if the factory is able to create parser for specified sentence type. All SentenceId enum values should result returning true at all times.

Parameters:
type - Sentence type id, e.g. "GLL" or "GGA".
Returns:
true if type is supported, otherwise false.

registerParser

public void registerParser(String type,
                           Class<? extends SentenceParser> parser)
Register a sentence parser to factory. After registration, createParser(String) method can be used to obtain parsers for registered type. All sentences supported by the library are registered by default, and this method is provided mainly for registering custom parsers that can be created by extending the SentenceParser class.

Parameters:
type - Sentence type id, e.g. "GGA" or "GLL".
parser - Parser implementation class for type.

unregisterParser

public void unregisterParser(Class<? extends SentenceParser> parser)
Unregisters a parser class, regardless of sentence type(s) it is registered for.

Parameters:
parser - Parser implementation class for type.
See Also:
registerParser(String, Class)

getInstance

public static SentenceFactory getInstance()
Returns the singleton instance of SentenceFactory.

Returns:
SentenceFactory instance


Copyright (C) 2010-2011 Java Marine API author(s). All Rights Reserved.