MessagesParser.java

  1. /* Copyright 2002-2023 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.gnss.metric.parser;

  18. import java.util.List;

  19. import org.orekit.errors.OrekitException;
  20. import org.orekit.errors.OrekitMessages;
  21. import org.orekit.gnss.metric.messages.ParsedMessage;

  22. /** Parser for IGS encoded messages.
  23.  * @author Luc Maisonobe
  24.  * @since 11.0
  25.  */
  26. public abstract class MessagesParser {

  27.     /** Set of needed messages. */
  28.     private final List<Integer> messages;

  29.     /**
  30.      * Constructor.
  31.      * @param messages list of needed messages
  32.      */
  33.     public MessagesParser(final List<Integer> messages) {
  34.         this.messages = messages;
  35.     }

  36.     /** Parse one message.
  37.      * @param message encoded message to parse
  38.      * @param ignoreUnknownMessageTypes if true, unknown messages types are silently ignored
  39.      * @return parsed message, or null if parse not possible and {@code ignoreUnknownMessageTypes} is true
  40.      */
  41.     public ParsedMessage parse(final EncodedMessage message, final boolean ignoreUnknownMessageTypes) {

  42.         try {

  43.             // get the message number as a String
  44.             final String messageNumberString = parseMessageNumber(message);
  45.             final int    messageNumberInt    = Integer.parseInt(messageNumberString);

  46.             // get the message parser for the extracted message number
  47.             final MessageType messageType = getMessageType(messageNumberString);

  48.             // if set to 0, notification will be triggered regardless of message type
  49.             if (messages.contains(0)) {
  50.                 return messageType.parse(message, messageNumberInt);
  51.             }

  52.             // parse one message
  53.             return messages.contains(messageNumberInt) ? messageType.parse(message, messageNumberInt) : null;

  54.         } catch (OrekitException oe) {
  55.             if (ignoreUnknownMessageTypes &&
  56.                             oe.getSpecifier() == OrekitMessages.UNKNOWN_ENCODED_MESSAGE_NUMBER) {
  57.                 // message is unknown but we ignore it
  58.                 return null;
  59.             } else {
  60.                 // throw an exception
  61.                 throw oe;
  62.             }
  63.         }

  64.     }

  65.     /** Parse the message number.
  66.      * @param message encoded message to parse
  67.      * @return the message number
  68.      */
  69.     protected abstract String parseMessageNumber(EncodedMessage message);

  70.     /** Get the message type corresponding to the message number.
  71.      * @param messageNumber String reprensentation of the message number
  72.      * @return the message type
  73.      */
  74.     protected abstract MessageType getMessageType(String messageNumber);

  75. }