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.files.ccsds.section;
18  
19  import org.orekit.errors.OrekitException;
20  import org.orekit.errors.OrekitMessages;
21  import org.orekit.files.ccsds.definitions.TimeSystem;
22  import org.orekit.files.ccsds.utils.ContextBinding;
23  import org.orekit.files.ccsds.utils.lexical.ParseToken;
24  import org.orekit.files.ccsds.utils.lexical.TokenType;
25  import org.orekit.files.ccsds.utils.parsing.AbstractConstituentParser;
26  import org.orekit.files.ccsds.utils.parsing.ProcessingState;
27  
28  /** {@link ProcessingState} for {@link Header NDM header}.
29   * @author Luc Maisonobe
30   * @since 11.0
31   */
32  public class HeaderProcessingState implements ProcessingState {
33  
34      /** Context binding for header. */
35      private final ContextBinding context;
36  
37      /** Parser for the complete message. */
38      private final AbstractConstituentParser<?, ?, ?> parser;
39  
40      /** Simple constructor.
41       * @param parser parser for the complete message
42       */
43      public HeaderProcessingState(final AbstractConstituentParser<?, ?, ?> parser) {
44          this.context = new ContextBinding(
45              parser::getConventions, parser::isSimpleEOP,
46              parser::getDataContext, parser::getParsedUnitsBehavior, () -> null,
47              () -> TimeSystem.UTC, () -> 0.0, () -> 1.0);
48          this.parser  = parser;
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public boolean processToken(final ParseToken token) {
54  
55          parser.inHeader();
56  
57          if (Double.isNaN(parser.getHeader().getFormatVersion())) {
58              // the first thing we expect is the format version
59              // (however, in XML files it was already set before entering header)
60              if (parser.getFormatVersionKey() != null &&
61                  parser.getFormatVersionKey().equals(token.getName()) &&
62                  token.getType() == TokenType.ENTRY) {
63                  parser.getHeader().setFormatVersion(token.getContentAsDouble());
64                  return true;
65              } else {
66                  throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, token.getFileName());
67              }
68          }
69  
70          try {
71              return token.getName() != null &&
72                     HeaderKey.valueOf(token.getName()).process(token, context, parser.getHeader());
73          } catch (IllegalArgumentException iae) {
74              // token has not been recognized
75              return false;
76          }
77  
78      }
79  
80  }