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  
19  import java.util.List;
20  
21  import org.orekit.errors.OrekitException;
22  import org.orekit.errors.OrekitMessages;
23  import org.orekit.gnss.metric.messages.ParsedMessage;
24  import org.orekit.time.TimeScales;
25  
26  /** Parser for IGS encoded messages.
27   * @author Luc Maisonobe
28   * @since 11.0
29   */
30  public abstract class MessagesParser {
31  
32      /** Set of needed messages. */
33      private final List<Integer> messages;
34  
35      /** Known time scales. */
36      private final TimeScales timeScales;
37  
38      /**
39       * Constructor.
40       * @param messages list of needed messages
41       * @param timeScales known time scales
42       * @since 13.0
43       */
44      protected MessagesParser(final List<Integer> messages,
45                               final TimeScales timeScales) {
46          this.messages   = messages;
47          this.timeScales = timeScales;
48      }
49  
50      /** Parse one message.
51       * @param message encoded message to parse
52       * @param ignoreUnknownMessageTypes if true, unknown messages types are silently ignored
53       * @return parsed message, or null if parse not possible and {@code ignoreUnknownMessageTypes} is true
54       */
55      public ParsedMessage parse(final EncodedMessage message, final boolean ignoreUnknownMessageTypes) {
56  
57          try {
58  
59              // get the message number as a String
60              final String messageNumberString = parseMessageNumber(message);
61              final int    messageNumberInt    = Integer.parseInt(messageNumberString);
62  
63              // get the message parser for the extracted message number
64              final MessageType messageType = getMessageType(messageNumberString);
65  
66              // if set to 0, notification will be triggered regardless of message type
67              if (messages.contains(0)) {
68                  return messageType.parse(message, messageNumberInt, timeScales);
69              }
70  
71              // parse one message
72              return messages.contains(messageNumberInt) ? messageType.parse(message, messageNumberInt, timeScales) : null;
73  
74          } catch (OrekitException oe) {
75              if (ignoreUnknownMessageTypes &&
76                              oe.getSpecifier() == OrekitMessages.UNKNOWN_ENCODED_MESSAGE_NUMBER) {
77                  // message is unknown but we ignore it
78                  return null;
79              } else {
80                  // throw an exception
81                  throw oe;
82              }
83          }
84  
85      }
86  
87      /** Parse the message number.
88       * @param message encoded message to parse
89       * @return the message number
90       */
91      protected abstract String parseMessageNumber(EncodedMessage message);
92  
93      /** Get the message type corresponding to the message number.
94       * @param messageNumber String reprensentation of the message number
95       * @return the message type
96       */
97      protected abstract MessageType getMessageType(String messageNumber);
98  
99  }