MessagesParser.java

  1. /* Copyright 2002-2025 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. import org.orekit.time.TimeScales;

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

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

  30.     /** Known time scales. */
  31.     private final TimeScales timeScales;

  32.     /**
  33.      * Constructor.
  34.      * @param messages list of needed messages
  35.      * @param timeScales known time scales
  36.      * @since 13.0
  37.      */
  38.     protected MessagesParser(final List<Integer> messages,
  39.                              final TimeScales timeScales) {
  40.         this.messages   = messages;
  41.         this.timeScales = timeScales;
  42.     }

  43.     /** Parse one message.
  44.      * @param message encoded message to parse
  45.      * @param ignoreUnknownMessageTypes if true, unknown messages types are silently ignored
  46.      * @return parsed message, or null if parse not possible and {@code ignoreUnknownMessageTypes} is true
  47.      */
  48.     public ParsedMessage parse(final EncodedMessage message, final boolean ignoreUnknownMessageTypes) {

  49.         try {

  50.             // get the message number as a String
  51.             final String messageNumberString = parseMessageNumber(message);
  52.             final int    messageNumberInt    = Integer.parseInt(messageNumberString);

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

  55.             // if set to 0, notification will be triggered regardless of message type
  56.             if (messages.contains(0)) {
  57.                 return messageType.parse(message, messageNumberInt, timeScales);
  58.             }

  59.             // parse one message
  60.             return messages.contains(messageNumberInt) ? messageType.parse(message, messageNumberInt, timeScales) : null;

  61.         } catch (OrekitException oe) {
  62.             if (ignoreUnknownMessageTypes &&
  63.                             oe.getSpecifier() == OrekitMessages.UNKNOWN_ENCODED_MESSAGE_NUMBER) {
  64.                 // message is unknown but we ignore it
  65.                 return null;
  66.             } else {
  67.                 // throw an exception
  68.                 throw oe;
  69.             }
  70.         }

  71.     }

  72.     /** Parse the message number.
  73.      * @param message encoded message to parse
  74.      * @return the message number
  75.      */
  76.     protected abstract String parseMessageNumber(EncodedMessage message);

  77.     /** Get the message type corresponding to the message number.
  78.      * @param messageNumber String reprensentation of the message number
  79.      * @return the message type
  80.      */
  81.     protected abstract MessageType getMessageType(String messageNumber);

  82. }