AbstractMessageParser.java

  1. /* Copyright 2002-2022 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.utils.parsing;

  18. import java.io.IOException;
  19. import java.util.HashMap;
  20. import java.util.Map;

  21. import org.hipparchus.exception.LocalizedCoreFormats;
  22. import org.orekit.data.DataSource;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.errors.OrekitInternalError;
  25. import org.orekit.files.ccsds.utils.FileFormat;
  26. import org.orekit.files.ccsds.utils.lexical.LexicalAnalyzerSelector;
  27. import org.orekit.files.ccsds.utils.lexical.MessageParser;
  28. import org.orekit.files.ccsds.utils.lexical.MessageVersionXmlTokenBuilder;
  29. import org.orekit.files.ccsds.utils.lexical.ParseToken;
  30. import org.orekit.files.ccsds.utils.lexical.XmlTokenBuilder;

  31. /** Parser for CCSDS messages.
  32.  * <p>
  33.  * Note than starting with Orekit 11.0, CCSDS message parsers are
  34.  * mutable objects that gather the data being parsed, until the
  35.  * message is complete and the {@link #parseMessage(org.orekit.data.DataSource)
  36.  * parseMessage} method has returned. This implies that parsers
  37.  * should <em>not</em> be used in a multi-thread context. The recommended
  38.  * way to use parsers is to either dedicate one parser for each message
  39.  * and drop it afterwards, or to use a single-thread loop.
  40.  * </p>
  41.  * @param <T> type of the file
  42.  * @author Luc Maisonobe
  43.  * @since 11.0
  44.  */
  45. public abstract class AbstractMessageParser<T> implements MessageParser<T> {

  46.     /** Safety limit for loop over processing states. */
  47.     private static final int MAX_LOOP = 100;

  48.     /** Root element for XML files. */
  49.     private final String root;

  50.     /** Key for format version. */
  51.     private final String formatVersionKey;

  52.     /** Anticipated next processing state. */
  53.     private ProcessingState next;

  54.     /** Current processing state. */
  55.     private ProcessingState current;

  56.     /** Fallback processing state. */
  57.     private ProcessingState fallback;

  58.     /** Format of the file ready to be parsed. */
  59.     private FileFormat format;

  60.     /** Flag for XML end tag. */
  61.     private boolean endTagSeen;

  62.     /** Simple constructor.
  63.      * @param root root element for XML files
  64.      * @param formatVersionKey key for format version
  65.      */
  66.     protected AbstractMessageParser(final String root, final String formatVersionKey) {
  67.         this.root             = root;
  68.         this.formatVersionKey = formatVersionKey;
  69.         this.current          = null;
  70.         setFallback(new ErrorState());
  71.     }

  72.     /** Set fallback processing state.
  73.      * <p>
  74.      * The fallback processing state is used if anticipated state fails
  75.      * to parse the token.
  76.      * </p>
  77.      * @param fallback processing state to use if anticipated state does not work
  78.      */
  79.     public void setFallback(final ProcessingState fallback) {
  80.         this.fallback = fallback;
  81.     }

  82.     /** Reset parser to initial state before parsing.
  83.      * @param fileFormat format of the file ready to be parsed
  84.      * @param initialState initial processing state
  85.      */
  86.     protected void reset(final FileFormat fileFormat, final ProcessingState initialState) {
  87.         format     = fileFormat;
  88.         current    = initialState;
  89.         endTagSeen = false;
  90.         anticipateNext(fallback);
  91.     }

  92.     /** Set the flag for XML end tag.
  93.      * @param endTagSeen if true, the XML end tag has been seen
  94.      */
  95.     public void setEndTagSeen(final boolean endTagSeen) {
  96.         this.endTagSeen = endTagSeen;
  97.     }

  98.     /** Check if XML end tag has been seen.
  99.      * @return true if XML end tag has been seen
  100.      */
  101.     public boolean wasEndTagSeen() {
  102.         return endTagSeen;
  103.     }

  104.     /** Get the current processing state.
  105.      * @return current processing state
  106.      */
  107.     public ProcessingState getCurrent() {
  108.         return current;
  109.     }

  110.     /** Get the file format.
  111.      * @return file format
  112.      */
  113.     protected FileFormat getFileFormat() {
  114.         return format;
  115.     }

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     public T parseMessage(final DataSource source) {
  119.         try {
  120.             return LexicalAnalyzerSelector.select(source).accept(this);
  121.         } catch (IOException ioe) {
  122.             throw new OrekitException(ioe, LocalizedCoreFormats.SIMPLE_MESSAGE,
  123.                                       ioe.getLocalizedMessage());
  124.         }
  125.     }

  126.     /** {@inheritDoc} */
  127.     @Override
  128.     public String getFormatVersionKey() {
  129.         return formatVersionKey;
  130.     }

  131.     /** {@inheritDoc} */
  132.     @Override
  133.     public Map<String, XmlTokenBuilder> getSpecialXmlElementsBuilders() {

  134.         final HashMap<String, XmlTokenBuilder> builders = new HashMap<>();

  135.         if (formatVersionKey != null) {
  136.             // special handling of root tag that contains the format version
  137.             builders.put(root, new MessageVersionXmlTokenBuilder());
  138.         }

  139.         return builders;

  140.     }

  141.     /** Anticipate what next processing state should be.
  142.      * @param anticipated anticipated next processing state
  143.      */
  144.     public void anticipateNext(final ProcessingState anticipated) {
  145.         this.next = anticipated;
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     public void process(final ParseToken token) {

  150.         // loop over the various states until one really processes the token
  151.         for (int i = 0; i < MAX_LOOP; ++i) {
  152.             if (current.processToken(token)) {
  153.                 return;
  154.             }
  155.             current = next;
  156.             next    = fallback;
  157.         }

  158.         // this should never happen
  159.         throw new OrekitInternalError(null);

  160.     }

  161. }