NdmParser.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.ndm;

  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.function.Supplier;

  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.files.ccsds.section.CommentsContainer;
  25. import org.orekit.files.ccsds.utils.FileFormat;
  26. import org.orekit.files.ccsds.utils.lexical.ParseToken;
  27. import org.orekit.files.ccsds.utils.lexical.XmlTokenBuilder;
  28. import org.orekit.files.ccsds.utils.parsing.AbstractMessageParser;

  29. /** A parser for the CCSDS NDM (Navigation Data Message).
  30.  * @author Luc Maisonobe
  31.  * @since 11.0
  32.  */
  33. public class NdmParser extends AbstractMessageParser<Ndm> {

  34.     /** Builder for the constituents parsers. */
  35.     private final ParserBuilder builder;

  36.     /** Current constituent parser. */
  37.     private AbstractMessageParser<? extends NdmConstituent<?, ?>> constituentParser;

  38.     /** Container for comments. */
  39.     private CommentsContainer comments;

  40.     /** Container for constituents. */
  41.     private List<NdmConstituent<?, ?>> constituents;

  42.     /** Simple constructor.
  43.      * <p>
  44.      * Calling this constructor directly is not recommended. Users should rather use
  45.      * {@link org.orekit.files.ccsds.ndm.ParserBuilder#buildNdmParser()
  46.      * parserBuilder.buildNdmParser()}.
  47.      * </p>
  48.      * @param builder builder for the constituents parsers
  49.      */
  50.     public NdmParser(final ParserBuilder builder) {
  51.         super(NdmStructureKey.ndm.name(), null);
  52.         this.builder = builder;
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public Map<String, XmlTokenBuilder> getSpecialXmlElementsBuilders() {
  57.         final Map<String, XmlTokenBuilder> builders = super.getSpecialXmlElementsBuilders();

  58.         // special handling of root elements for all constituents
  59.         builders.putAll(builder.buildTdmParser().getSpecialXmlElementsBuilders());
  60.         builders.putAll(builder.buildOpmParser().getSpecialXmlElementsBuilders());
  61.         builders.putAll(builder.buildOmmParser().getSpecialXmlElementsBuilders());
  62.         builders.putAll(builder.buildOemParser().getSpecialXmlElementsBuilders());
  63.         builders.putAll(builder.buildOcmParser().getSpecialXmlElementsBuilders());
  64.         builders.putAll(builder.buildApmParser().getSpecialXmlElementsBuilders());
  65.         builders.putAll(builder.buildAemParser().getSpecialXmlElementsBuilders());
  66.         builders.putAll(builder.buildCdmParser().getSpecialXmlElementsBuilders());

  67.         return builders;

  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public void reset(final FileFormat fileFormat) {
  72.         reset(fileFormat, this::processToken);
  73.         constituentParser = null;
  74.         comments          = new CommentsContainer();
  75.         constituents      = new ArrayList<>();
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     public Ndm build() {
  80.         // build the file from parsed comments and constituents
  81.         return new Ndm(comments.getComments(), constituents);
  82.     }

  83.     /**
  84.      * Add comment.
  85.      * <p>
  86.      * Comments are accepted only at start. Once
  87.      * other content is stored in the same section, comments are refused.
  88.      * </p>
  89.      * @param comment comment line
  90.      * @return true if comment was accepted
  91.      */
  92.     public boolean addComment(final String comment) {
  93.         return comments.addComment(comment);
  94.     }

  95.     /** Prepare parsing of a TDM constituent.
  96.      * @return always return true
  97.      */
  98.     boolean manageTdmConstituent() {
  99.         return manageConstituent(builder::buildTdmParser);
  100.     }

  101.     /** Prepare parsing of an OPM constituent.
  102.      * @return always return true
  103.      */
  104.     boolean manageOpmConstituent() {
  105.         return manageConstituent(builder::buildOpmParser);
  106.     }

  107.     /** Prepare parsing of an OMM constituent.
  108.      * @return always return true
  109.      */
  110.     boolean manageOmmConstituent() {
  111.         return manageConstituent(builder::buildOmmParser);
  112.     }

  113.     /** Prepare parsing of an OEM constituent.
  114.      * @return always return true
  115.      */
  116.     boolean manageOemConstituent() {
  117.         return manageConstituent(builder::buildOemParser);
  118.     }

  119.     /** Prepare parsing of an OCM constituent.
  120.      * @return always return true
  121.      */
  122.     boolean manageOcmConstituent() {
  123.         return manageConstituent(builder::buildOcmParser);
  124.     }

  125.     /** Prepare parsing of an APM constituent.
  126.      * @return always return true
  127.      */
  128.     boolean manageApmConstituent() {
  129.         return manageConstituent(builder::buildApmParser);
  130.     }

  131.     /** Prepare parsing of a AEM constituent.
  132.      * @return always return true
  133.      */
  134.     boolean manageAemConstituent() {
  135.         return manageConstituent(builder::buildAemParser);
  136.     }

  137.     /** Prepare parsing of a CDM constituent.
  138.      * @return always return true
  139.      */
  140.     boolean manageCdmConstituent() {
  141.         return manageConstituent(builder::buildCdmParser);
  142.     }

  143.     /** Prepare parsing of a constituent.
  144.      * @param parserSupplier supplier for constituent parser
  145.      * @return always return true
  146.      */
  147.     boolean manageConstituent(final Supplier<AbstractMessageParser<? extends NdmConstituent<?, ?>>> parserSupplier) {

  148.         // as we have started parsing constituents, we cannot accept any further comments
  149.         comments.refuseFurtherComments();

  150.         // create a parser for the constituent
  151.         constituentParser = parserSupplier.get();
  152.         constituentParser.reset(getFileFormat());

  153.         return true;

  154.     }

  155.     /** Process one token.
  156.      * @param token token to process
  157.      * @return true if token was processed, false otherwise
  158.      */
  159.     private boolean processToken(final ParseToken token) {

  160.         if (getFileFormat() == FileFormat.KVN) {
  161.             // NDM combined instantiation can only be formatted as XML messages
  162.             throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, token.getFileName());
  163.         }

  164.         if (constituentParser == null) {
  165.             // we are in the global NDM structure
  166.             try {
  167.                 return NdmStructureKey.valueOf(token.getName()).process(token, this);
  168.             } catch (IllegalArgumentException iae) {
  169.                 // token has not been recognized
  170.                 return false;
  171.             }
  172.         } else {
  173.             // we are inside one constituent
  174.             constituentParser.process(token);
  175.             if (constituentParser.wasEndTagSeen()) {
  176.                 // we have seen the end tag, we must go back global structure parsing
  177.                 constituents.add(constituentParser.build());
  178.                 constituentParser = null;
  179.             }
  180.             return true;
  181.         }

  182.     }

  183. }