HeaderProcessingState.java

  1. /* Copyright 2002-2023 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. import org.orekit.errors.OrekitException;
  19. import org.orekit.errors.OrekitMessages;
  20. import org.orekit.files.ccsds.definitions.TimeSystem;
  21. import org.orekit.files.ccsds.utils.ContextBinding;
  22. import org.orekit.files.ccsds.utils.lexical.ParseToken;
  23. import org.orekit.files.ccsds.utils.lexical.TokenType;
  24. import org.orekit.files.ccsds.utils.parsing.AbstractConstituentParser;
  25. import org.orekit.files.ccsds.utils.parsing.ProcessingState;

  26. /** {@link ProcessingState} for {@link Header NDM header}.
  27.  * @author Luc Maisonobe
  28.  * @since 11.0
  29.  */
  30. public class HeaderProcessingState implements ProcessingState {

  31.     /** Context binding for header. */
  32.     private final ContextBinding context;

  33.     /** Parser for the complete message. */
  34.     private final AbstractConstituentParser<?, ?, ?> parser;

  35.     /** Simple constructor.
  36.      * @param parser parser for the complete message
  37.      */
  38.     public HeaderProcessingState(final AbstractConstituentParser<?, ?, ?> parser) {
  39.         this.context = new ContextBinding(
  40.             parser::getConventions, parser::isSimpleEOP,
  41.             parser::getDataContext, parser::getParsedUnitsBehavior, () -> null,
  42.             () -> TimeSystem.UTC, () -> 0.0, () -> 1.0);
  43.         this.parser  = parser;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public boolean processToken(final ParseToken token) {

  48.         parser.inHeader();

  49.         if (Double.isNaN(parser.getHeader().getFormatVersion())) {
  50.             // the first thing we expect is the format version
  51.             // (however, in XML files it was already set before entering header)
  52.             if (parser.getFormatVersionKey() != null &&
  53.                 parser.getFormatVersionKey().equals(token.getName()) &&
  54.                 token.getType() == TokenType.ENTRY) {
  55.                 parser.getHeader().setFormatVersion(token.getContentAsDouble());
  56.                 return true;
  57.             } else {
  58.                 throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, token.getFileName());
  59.             }
  60.         }

  61.         try {
  62.             return token.getName() != null &&
  63.                    HeaderKey.valueOf(token.getName()).process(token, context, parser.getHeader());
  64.         } catch (IllegalArgumentException iae) {
  65.             // token has not been recognized
  66.             return false;
  67.         }

  68.     }

  69. }