XmlStructureProcessingState.java

  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. import org.orekit.errors.OrekitException;
  19. import org.orekit.errors.OrekitMessages;
  20. import org.orekit.files.ccsds.utils.FileFormat;
  21. import org.orekit.files.ccsds.utils.lexical.ParseToken;
  22. import org.orekit.files.ccsds.utils.lexical.TokenType;
  23. import org.orekit.files.ccsds.utils.parsing.AbstractConstituentParser;
  24. import org.orekit.files.ccsds.utils.parsing.ProcessingState;

  25. /** {@link ProcessingState} for structure of {@link FileFormat#XML} CCSDS Messages.
  26.  * @author Luc Maisonobe
  27.  * @since 11.0
  28.  */
  29. public class XmlStructureProcessingState implements ProcessingState {

  30.     /** Name of the root element. */
  31.     private final String root;

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

  34.     /** Simple constructor.
  35.      * @param root name of the root element
  36.      * @param parser parser for the complete message
  37.      */
  38.     public XmlStructureProcessingState(final String root, final AbstractConstituentParser<?, ?, ?> parser) {
  39.         this.root   = root;
  40.         this.parser = parser;
  41.     }

  42.     /** {@inheritDoc} */
  43.     @Override
  44.     public boolean processToken(final ParseToken token) {

  45.         if (root.equals(token.getName())) {
  46.             parser.setEndTagSeen(token.getType() == TokenType.STOP);
  47.             return true;
  48.         }

  49.         if (Double.isNaN(parser.getHeader().getFormatVersion())) {
  50.             // the first thing we expect (after the ignored start tag for root element) is the format version
  51.             if (parser.getFormatVersionKey() != null &&
  52.                 parser.getFormatVersionKey().equals(token.getName()) && token.getType() == TokenType.ENTRY) {
  53.                 parser.getHeader().setFormatVersion(token.getContentAsDouble());
  54.                 return true;
  55.             } else {
  56.                 throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, token.getFileName());
  57.             }
  58.         }

  59.         try {
  60.             return XmlStructureKey.valueOf(token.getName()).process(token, parser);
  61.         } catch (IllegalArgumentException iae) {
  62.             // ignored, we delegate handling this token to fallback state
  63.             return false;
  64.         }
  65.     }

  66. }