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