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

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

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

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

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

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

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

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

  43.     /** Simple constructor.
  44.      * <p>
  45.      * Calling this constructor directly is not recommended. Users should rather use
  46.      * {@link org.orekit.files.ccsds.ndm.ParserBuilder#buildNdmParser()
  47.      * parserBuilder.buildNdmParser()}.
  48.      * </p>
  49.      * @param builder builder for the constituents parsers
  50.      * @param filters filters to apply to parse tokens
  51.      * @since 12.0
  52.      */
  53.     public NdmParser(final ParserBuilder builder,
  54.                      final Function<ParseToken, List<ParseToken>>[] filters) {
  55.         super(NdmStructureKey.ndm.name(), null, filters);
  56.         this.builder = builder;
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public Map<String, XmlTokenBuilder> getSpecialXmlElementsBuilders() {
  61.         final Map<String, XmlTokenBuilder> builders = super.getSpecialXmlElementsBuilders();

  62.         // special handling of root elements for all constituents
  63.         builders.putAll(builder.buildTdmParser().getSpecialXmlElementsBuilders());
  64.         builders.putAll(builder.buildOpmParser().getSpecialXmlElementsBuilders());
  65.         builders.putAll(builder.buildOmmParser().getSpecialXmlElementsBuilders());
  66.         builders.putAll(builder.buildOemParser().getSpecialXmlElementsBuilders());
  67.         builders.putAll(builder.buildOcmParser().getSpecialXmlElementsBuilders());
  68.         builders.putAll(builder.buildApmParser().getSpecialXmlElementsBuilders());
  69.         builders.putAll(builder.buildAemParser().getSpecialXmlElementsBuilders());
  70.         builders.putAll(builder.buildAcmParser().getSpecialXmlElementsBuilders());
  71.         builders.putAll(builder.buildCdmParser().getSpecialXmlElementsBuilders());

  72.         return builders;

  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public void reset(final FileFormat fileFormat) {
  77.         reset(fileFormat, this::processToken);
  78.         constituentParser = null;
  79.         comments          = new CommentsContainer();
  80.         constituents      = new ArrayList<>();
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public Ndm build() {
  85.         // build the file from parsed comments and constituents
  86.         return new Ndm(comments.getComments(), constituents);
  87.     }

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

  100.     /** Prepare parsing of a TDM constituent.
  101.      * @return always return true
  102.      */
  103.     boolean manageTdmConstituent() {
  104.         return manageConstituent(builder::buildTdmParser);
  105.     }

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

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

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

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

  130.     /** Prepare parsing of an APM constituent.
  131.      * @return always return true
  132.      */
  133.     boolean manageApmConstituent() {
  134.         return manageConstituent(builder::buildApmParser);
  135.     }

  136.     /** Prepare parsing of a AEM constituent.
  137.      * @return always return true
  138.      */
  139.     boolean manageAemConstituent() {
  140.         return manageConstituent(builder::buildAemParser);
  141.     }

  142.     /** Prepare parsing of a ACM constituent.
  143.      * @return always return true
  144.      * @since 12.0
  145.      */
  146.     boolean manageAcmConstituent() {
  147.         return manageConstituent(builder::buildAcmParser);
  148.     }

  149.     /** Prepare parsing of a CDM constituent.
  150.      * @return always return true
  151.      */
  152.     boolean manageCdmConstituent() {
  153.         return manageConstituent(builder::buildCdmParser);
  154.     }

  155.     /** Prepare parsing of a constituent.
  156.      * @param parserSupplier supplier for constituent parser
  157.      * @return always return true
  158.      */
  159.     boolean manageConstituent(final Supplier<AbstractMessageParser<? extends NdmConstituent<?, ?>>> parserSupplier) {

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

  162.         // create a parser for the constituent
  163.         constituentParser = parserSupplier.get();
  164.         constituentParser.reset(getFileFormat());

  165.         return true;

  166.     }

  167.     /** Process one token.
  168.      * @param token token to process
  169.      * @return true if token was processed, false otherwise
  170.      */
  171.     private boolean processToken(final ParseToken token) {

  172.         if (getFileFormat() == FileFormat.KVN) {
  173.             // NDM combined instantiation can only be formatted as XML messages
  174.             throw new OrekitException(OrekitMessages.UNSUPPORTED_FILE_FORMAT, token.getFileName());
  175.         }

  176.         if (constituentParser == null) {
  177.             // we are in the global NDM structure
  178.             try {
  179.                 return NdmStructureKey.valueOf(token.getName()).process(token, this);
  180.             } catch (IllegalArgumentException iae) {
  181.                 // token has not been recognized
  182.                 return false;
  183.             }
  184.         } else {
  185.             // we are inside one constituent
  186.             constituentParser.process(token);
  187.             if (constituentParser.wasEndTagSeen()) {
  188.                 // we have seen the end tag, we must go back global structure parsing
  189.                 constituents.add(constituentParser.build());
  190.                 constituentParser = null;
  191.             }
  192.             return true;
  193.         }

  194.     }

  195. }