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