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.         return builders;

  67.     }

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

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

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

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

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

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

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

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

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

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

  136.     /** Prepare parsing of a constituent.
  137.      * @param parserSupplier supplier for constituent parser
  138.      * @return always return true
  139.      */
  140.     boolean manageConstituent(final Supplier<AbstractMessageParser<? extends NdmConstituent<?, ?>>> parserSupplier) {

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

  143.         // create a parser for the constituent
  144.         constituentParser = parserSupplier.get();
  145.         constituentParser.reset(getFileFormat());

  146.         return true;

  147.     }

  148.     /** Process one token.
  149.      * @param token token to process
  150.      * @return true if token was processed, false otherwise
  151.      */
  152.     private boolean processToken(final ParseToken token) {

  153.         if (getFileFormat() == FileFormat.KVN) {
  154.             // NDM combined instantiation can only be formatted as XML messages
  155.             throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, token.getFileName());
  156.         }

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

  175.     }

  176. }