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.ndm.cdm;
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.section.HeaderKey;
23  import org.orekit.files.ccsds.utils.ContextBinding;
24  import org.orekit.files.ccsds.utils.lexical.ParseToken;
25  import org.orekit.files.ccsds.utils.lexical.TokenType;
26  import org.orekit.files.ccsds.utils.parsing.ProcessingState;
27  
28  /** {@link ProcessingState} for {@link CdmHeader CDM header}.
29   * @author Melina Vanel
30   * @since 11.2
31   */
32  public class CdmHeaderProcessingState implements ProcessingState {
33  
34      /** Context binding for header. */
35      private final ContextBinding context;
36  
37      /** Parser for the complete message. */
38      private final CdmParser parser;
39  
40      /** Simple constructor.
41       * @param parser parser for the complete message
42       */
43      public CdmHeaderProcessingState(final CdmParser 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 iaeH) {
74  
75              try {
76                  return CdmHeaderKey.valueOf(token.getName()).process(token, context, parser.getHeader());
77              } catch (IllegalArgumentException iaeC) {
78                  // token has not been recognized
79                  return false;
80              }
81  
82          }
83  
84      }
85  
86  }
87