类 SentenceParser

java.lang.Object
net.sf.marineapi.nmea.parser.SentenceParser
所有已实现的接口:
Sentence
直接已知子类:
PositionParser, UBXMessageParser

public class SentenceParser extends Object implements Sentence

Base class for all NMEA 0183 sentence parsers. Contains generic methods such as data field setters and getters, data formatting, validation etc.

NMEA 0183 data is transmitted in form of ASCII Strings that are called sentences. Each sentence starts with a '$', a two letter talker ID, a three letter sentence ID, followed by a number of comma separated data fields, optional checksum and a carriage return/line feed terminator (CR/LF). Sentence may contain up to 82 characters including the CR/LF. If data for certain field is not available, the field value is simply omitted, but the commas that would delimit it are still sent, with no space between them.

Sentence structure:
$<talker-id><sentence-id>,<field #0>,<field #1>,...,<field #n>*<checksum><cr/lf>

For more details, see NMEA Revealed by Eric S. Raymond.

This class can also be used to implement and integrate parsers not provided by in the library. See SentenceFactory for more instructions.

作者:
Kimmo Tuukkanen
  • 字段概要

    从接口继承的字段 net.sf.marineapi.nmea.sentence.Sentence

    ALTERNATIVE_BEGIN_CHAR, BEGIN_CHAR, CHECKSUM_DELIMITER, FIELD_DELIMITER, MAX_LENGTH, TERMINATOR
  • 构造器概要

    构造器
    限定符
    构造器
    说明
    protected
    SentenceParser(char begin, TalkerId talker, String type, int size)
    Creates a new empty sentence with specified begin char, talker id, sentence id and number of fields.
    protected
    SentenceParser(char begin, TalkerId tid, SentenceId sid, int size)
    Creates a new empty sentence with specified begin char, talker id, sentence id and number of fields.
     
    Creates a new instance of SentenceParser.
    protected
    Creates a new instance of SentenceParser.
    protected
    SentenceParser(TalkerId talker, String type, int size)
    Creates a new empty sentence with specified talker and sentence IDs.
  • 方法概要

    修饰符和类型
    方法
    说明
    boolean
     
    final char
    Get the sentence begin character.
    protected final char
    getCharValue(int index)
    Parse a single character from the specified sentence field.
    protected final double
    getDoubleValue(int index)
    Parse double value from the specified sentence field.
    final int
    Returns the current number of data fields in sentence, excluding ID field and checksum.
    protected final int
    getIntValue(int index)
    Parse integer value from the specified sentence field.
    final String
    Get the sentence ID that specifies the sentence type and data it holds.
    protected final String
    getStringValue(int index)
    Get contents of a data field as a String.
    protected final String[]
    getStringValues(int first)
    Returns all field values, starting from the specified index towards the end of sentence.
    final TalkerId
    Gets the talker ID of the sentence.
    int
     
    protected final boolean
    hasValue(int index)
    Tells is if the field specified by the given index contains a value.
    boolean
    Tells if this is an AIS sentence.
    boolean
    Tells if the sentence is of proprietary format.
    boolean
    Tells if the sentence formatting matches NMEA 0183 format.
    final void
    Resets the sentence contents, i.e. removes all existing values from data fields.
    void
    setBeginChar(char ch)
    Set the sentence begin character.
    protected final void
    setCharValue(int index, char value)
    Set a character in specified field.
    protected final void
    setDegreesValue(int index, double deg)
    Set degrees value, e.g. course or heading.
    protected final void
    setDoubleValue(int index, double value)
    Set double value in specified field.
    protected final void
    setDoubleValue(int index, double value, int leading, int decimals)
    Set double value in specified field, with given number of digits before and after the decimal separator ('.').
    protected final void
    setFieldCount(int size)
    Sets the number of sentence data fields.
    protected final void
    setIntValue(int index, int value)
    Set integer value in specified field.
    protected final void
    setIntValue(int index, int value, int leading)
    Set integer value in specified field, with specified minimum number of digits.
    protected final void
    setStringValue(int index, String value)
    Set String value in specified data field.
    protected final void
    setStringValues(int first, String[] newFields)
    Replace multiple fields with given String array, starting at the specified index.
    final void
    Set the talker ID of the sentence.
    final String
    Formats and validates the String representation of sentence.
    final String
    toSentence(int maxLength)
    Formats and validates the sentence like Sentence.toSentence(), but checks also that resulting String does not exceed specified length.
    Returns the String representation of the sentence, without line terminator CR/LR.

    从类继承的方法 java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • 构造器详细资料

    • SentenceParser

      public SentenceParser(String nmea)
      Creates a new instance of SentenceParser. Validates the input String and resolves talker id and sentence type.
      参数:
      nmea - A valid NMEA 0183 sentence
      抛出:
      IllegalArgumentException - If the specified sentence is invalid or if sentence type is not supported.
    • SentenceParser

      protected SentenceParser(char begin, TalkerId tid, SentenceId sid, int size)
      Creates a new empty sentence with specified begin char, talker id, sentence id and number of fields.
      参数:
      begin - Begin char, $ or !
      tid - TalkerId to set
      sid - SentenceId to set
      size - Number of sentence data fields
    • SentenceParser

      protected SentenceParser(char begin, TalkerId talker, String type, int size)
      Creates a new empty sentence with specified begin char, talker id, sentence id and number of fields.
      参数:
      begin - The begin character, e.g. '$' or '!'
      talker - TalkerId to set
      type - Sentence id as String, e.g. "GGA or "GLL".
      size - Number of sentence data fields
    • SentenceParser

      protected SentenceParser(String nmea, String type)
      Creates a new instance of SentenceParser. Parser may be constructed only if parameter nmea contains a valid NMEA 0183 sentence of the specified type.

      For example, GGA sentence parser should specify "GGA" as the type.

      参数:
      nmea - NMEA 0183 sentence String
      type - Expected type of the sentence in nmea parameter
      抛出:
      IllegalArgumentException - If the specified sentence is not a valid or is not of expected type.
    • SentenceParser

      protected SentenceParser(TalkerId talker, String type, int size)
      Creates a new empty sentence with specified talker and sentence IDs.
      参数:
      talker - Talker type Id, e.g. "GP" or "LC".
      type - Sentence type Id, e.g. "GGA or "GLL".
      size - Number of data fields
  • 方法详细资料

    • equals

      public boolean equals(Object obj)
      覆盖:
      equals 在类中 Object
    • getBeginChar

      public final char getBeginChar()
      从接口复制的说明: Sentence
      Get the sentence begin character. Although most of the sentences start with '$', some of them use '!' as begin character.
      指定者:
      getBeginChar 在接口中 Sentence
      返回:
      Sentence begin char, e.g. "$" or "!".
    • getFieldCount

      public final int getFieldCount()
      从接口复制的说明: Sentence
      Returns the current number of data fields in sentence, excluding ID field and checksum.
      指定者:
      getFieldCount 在接口中 Sentence
      返回:
      Data field count
    • getSentenceId

      public final String getSentenceId()
      从接口复制的说明: Sentence
      Get the sentence ID that specifies the sentence type and data it holds. ID is the last three characters in address field. For example, in case of $GPGGA the method returns SentenceId.GGA.
      指定者:
      getSentenceId 在接口中 Sentence
      返回:
      Sentence id String, e.g. "GLL" or "GGA".
      另请参阅:
    • getTalkerId

      public final TalkerId getTalkerId()
      从接口复制的说明: Sentence
      Gets the talker ID of the sentence. Talker ID is the next two characters after $ in sentence address field. For example, in case of $GPGGA, the method returns TalkerId.GP.
      指定者:
      getTalkerId 在接口中 Sentence
      返回:
      Talker id enum.
    • hashCode

      public int hashCode()
      覆盖:
      hashCode 在类中 Object
    • isAISSentence

      public boolean isAISSentence()
      从接口复制的说明: Sentence
      Tells if this is an AIS sentence.
      指定者:
      isAISSentence 在接口中 Sentence
      返回:
      True if AIS sentence, otherwise false.
    • isProprietary

      public boolean isProprietary()
      从接口复制的说明: Sentence
      Tells if the sentence is of proprietary format.
      指定者:
      isProprietary 在接口中 Sentence
      返回:
      True if proprietary, otherwise false.
    • isValid

      public boolean isValid()
      从接口复制的说明: Sentence
      Tells if the sentence formatting matches NMEA 0183 format.
      指定者:
      isValid 在接口中 Sentence
      返回:
      True if validly formatted, otherwise false.
    • reset

      public final void reset()
      从接口复制的说明: Sentence
      Resets the sentence contents, i.e. removes all existing values from data fields. After resetting, address field remains as is and checksum is calculated according to empty data fields.
      指定者:
      reset 在接口中 Sentence
    • setBeginChar

      public void setBeginChar(char ch)
      从接口复制的说明: Sentence
      Set the sentence begin character. Although most of the sentences start with '$', some of them use '!' as begin character.
      指定者:
      setBeginChar 在接口中 Sentence
      参数:
      ch - Sentence begin char to set ('$' or '!')
      另请参阅:
    • setTalkerId

      public final void setTalkerId(TalkerId id)
      从接口复制的说明: Sentence
      Set the talker ID of the sentence. Typically, the ID might be changed if the sentence is to be sent from a computer to an NMEA device.
      指定者:
      setTalkerId 在接口中 Sentence
      参数:
      id - TalkerId to set
    • toSentence

      public final String toSentence()
      从接口复制的说明: Sentence
      Formats and validates the String representation of sentence. Throws an exception if result is not considered a valid sentence. As validation is done by SentenceValidator, notice that resulting sentence length is not checked. To also validate the length, use Sentence.toSentence(int).
      指定者:
      toSentence 在接口中 Sentence
      返回:
      Sentence as String, equal to toString().
      另请参阅:
    • toSentence

      public final String toSentence(int maxLength)
      从接口复制的说明: Sentence
      Formats and validates the sentence like Sentence.toSentence(), but checks also that resulting String does not exceed specified length.
      指定者:
      toSentence 在接口中 Sentence
      参数:
      maxLength - Maximum String length
      返回:
      Sentence as String, equal to toString().
      另请参阅:
    • toString

      public String toString()
      从接口复制的说明: Sentence
      Returns the String representation of the sentence, without line terminator CR/LR. Checksum is calculated and appended at the end of the sentence, but no validation is done. Use Sentence.toSentence() to also validate the result.
      指定者:
      toString 在接口中 Sentence
      覆盖:
      toString 在类中 Object
      返回:
      String representation of sentence
    • getCharValue

      protected final char getCharValue(int index)
      Parse a single character from the specified sentence field.
      参数:
      index - Data field index in sentence
      返回:
      Character contained in the field
      抛出:
      ParseException - If field contains more than one character
    • getDoubleValue

      protected final double getDoubleValue(int index)
      Parse double value from the specified sentence field.
      参数:
      index - Data field index in sentence
      返回:
      Field as parsed by Double.parseDouble(String)
    • getIntValue

      protected final int getIntValue(int index)
      Parse integer value from the specified sentence field.
      参数:
      index - Field index in sentence
      返回:
      Field parsed by Integer.parseInt(String)
    • getStringValue

      protected final String getStringValue(int index)
      Get contents of a data field as a String. Field indexing is zero-based. The address field (e.g. $GPGGA) and checksum at the end are not considered as a data fields and cannot therefore be fetched with this method.

      Field indexing, let i = 1:
      $&lt;id&gt;,&lt;i&gt;,&lt;i+1&gt;,&lt;i+2&gt;,...,&lt;i+n&gt;*&lt;checksum&gt;

      参数:
      index - Field index
      返回:
      Field value as String
      抛出:
      DataNotAvailableException - If the field is empty
    • hasValue

      protected final boolean hasValue(int index)
      Tells is if the field specified by the given index contains a value.
      参数:
      index - Field index
      返回:
      True if field contains value, otherwise false.
    • setCharValue

      protected final void setCharValue(int index, char value)
      Set a character in specified field.
      参数:
      index - Field index
      value - Value to set
    • setDegreesValue

      protected final void setDegreesValue(int index, double deg)
      Set degrees value, e.g. course or heading.
      参数:
      index - Field index where to insert value
      deg - The degrees value to set
      抛出:
      IllegalArgumentException - If degrees value out of range [0..360]
    • setDoubleValue

      protected final void setDoubleValue(int index, double value)
      Set double value in specified field. Value is set "as-is" without any formatting or rounding.
      参数:
      index - Field index
      value - Value to set
      另请参阅:
    • setDoubleValue

      protected final void setDoubleValue(int index, double value, int leading, int decimals)
      Set double value in specified field, with given number of digits before and after the decimal separator ('.'). When necessary, the value is padded with leading zeros and/or rounded to meet the requested number of digits.
      参数:
      index - Field index
      value - Value to set
      leading - Number of digits before decimal separator
      decimals - Maximum number of digits after decimal separator
      另请参阅:
    • setFieldCount

      protected final void setFieldCount(int size)
      Sets the number of sentence data fields. Increases or decreases the fields array, values in fields not affected by the change remain unchanged. Does nothing if specified new size is equal to count returned by getFieldCount().
      参数:
      size - Number of data fields, must be greater than zero.
    • setIntValue

      protected final void setIntValue(int index, int value)
      Set integer value in specified field.
      参数:
      index - Field index
      value - Value to set
    • setIntValue

      protected final void setIntValue(int index, int value, int leading)
      Set integer value in specified field, with specified minimum number of digits. Leading zeros are added to value if when necessary.
      参数:
      index - Field index
      value - Value to set
      leading - Number of digits to use.
    • setStringValue

      protected final void setStringValue(int index, String value)
      Set String value in specified data field.
      参数:
      index - Field index
      value - String to set, null converts to empty String.
    • setStringValues

      protected final void setStringValues(int first, String[] newFields)
      Replace multiple fields with given String array, starting at the specified index. If parameter first is zero, all sentence fields are replaced.

      If the length of newFields does not fit in the sentence field count or it contains less values, fields are removed or added accordingly. As the result, total number of fields may increase or decrease. Thus, if the sentence field count must not change, you may need to add empty Strings to newFields in order to preserve the original number of fields. Also, all existing values after first are lost.

      参数:
      first - Index of first field to set
      newFields - Array of Strings to set
    • getStringValues

      protected final String[] getStringValues(int first)
      Returns all field values, starting from the specified index towards the end of sentence.
      参数:
      first - Index of first field to get.
      返回:
      Array of String values