NdmWriter.java

  1. /* Copyright 2002-2024 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.io.IOException;

  19. import org.orekit.errors.OrekitException;
  20. import org.orekit.errors.OrekitInternalError;
  21. import org.orekit.errors.OrekitMessages;
  22. import org.orekit.files.ccsds.ndm.adm.acm.Acm;
  23. import org.orekit.files.ccsds.ndm.adm.aem.Aem;
  24. import org.orekit.files.ccsds.ndm.adm.apm.Apm;
  25. import org.orekit.files.ccsds.ndm.odm.ocm.Ocm;
  26. import org.orekit.files.ccsds.ndm.odm.oem.Oem;
  27. import org.orekit.files.ccsds.ndm.odm.omm.Omm;
  28. import org.orekit.files.ccsds.ndm.odm.opm.Opm;
  29. import org.orekit.files.ccsds.ndm.tdm.Tdm;
  30. import org.orekit.files.ccsds.section.Header;
  31. import org.orekit.files.ccsds.section.Segment;
  32. import org.orekit.files.ccsds.utils.generation.Generator;
  33. import org.orekit.files.ccsds.utils.generation.MessageWriter;

  34. /**
  35.  * Writer for CCSDS Navigation Data Message.
  36.  *
  37.  * @author Luc Maisonobe
  38.  * @since 11.0
  39.  */
  40. public class NdmWriter {

  41.     /** Builder for the constituents writers. */
  42.     private final WriterBuilder builder;

  43.     /** Indicator for started message. */
  44.     private boolean started;

  45.     /** Number of constituents written. */
  46.     private int count;

  47.     /** Simple constructor.
  48.      * <p>
  49.      * Calling this constructor directly is not recommended. Users should rather use
  50.      * {@link org.orekit.files.ccsds.ndm.WriterBuilder#buildNdmWriter()
  51.      * WriterBuilder.buildNdmWriter()}.
  52.      * </p>
  53.      * @param builder builder for the constituents parsers
  54.      */
  55.     public NdmWriter(final WriterBuilder builder) {
  56.         this.builder = builder;
  57.         this.started = false;
  58.         this.count   = 0;
  59.     }

  60.     /** Write one complete message.
  61.      * @param generator generator to use for producing output
  62.      * @param message message to write
  63.      * @throws IOException if the stream cannot write to stream
  64.      */
  65.     public void writeMessage(final Generator generator, final Ndm message)
  66.         throws IOException {

  67.         // write the global comments
  68.         for (final String comment : message.getComments()) {
  69.             writeComment(generator, comment);
  70.         }

  71.         // write the constituents
  72.         for (final NdmConstituent<?, ?> constituent : message.getConstituents()) {
  73.             writeConstituent(generator, constituent);
  74.         }

  75.     }

  76.     /** Start the composite message if needed.
  77.      * @param generator generator to use for producing output
  78.      * @throws IOException if the stream cannot write to stream
  79.      */
  80.     private void startMessageIfNeeded(final Generator generator) throws IOException {
  81.         if (!started) {
  82.             generator.enterSection(NdmStructureKey.ndm.name());
  83.             started = true;
  84.         }
  85.     }

  86.     /** Write a comment line.
  87.      * <p>
  88.      * Comments allows comments only before constituents, so attempting to
  89.      * add comments after the first constituent has been written will
  90.      * produce an exception.
  91.      * </p>
  92.      * @param generator generator to use for producing output
  93.      * @param comment comment line to write
  94.      * @throws IOException if the stream cannot write to stream
  95.      */
  96.     public void writeComment(final Generator generator, final String comment) throws IOException {

  97.         startMessageIfNeeded(generator);

  98.         // check we can still write comments
  99.         if (count > 0) {
  100.             throw new OrekitException(OrekitMessages.ATTEMPT_TO_GENERATE_MALFORMED_FILE, generator.getOutputName());
  101.         }

  102.         generator.writeEntry(NdmStructureKey.COMMENT.name(), comment, null, false);

  103.     }

  104.     /** Write a constituent.
  105.      * @param generator generator to use for producing output
  106.      * @param constituent constituent
  107.      * @param <H> type of the header
  108.      * @param <S> type of the segments
  109.      * @param <F> type of the file
  110.      * @throws IOException if the stream cannot write to stream
  111.      */
  112.     public <H extends Header, S extends Segment<?, ?>, F extends NdmConstituent<H, S>>
  113.         void writeConstituent(final Generator generator, final F constituent) throws IOException {

  114.         // write the root element if needed
  115.         startMessageIfNeeded(generator);

  116.         // write the constituent
  117.         final MessageWriter<H, S, F> writer = buildWriter(constituent);
  118.         writer.writeMessage(generator, constituent);

  119.         // update count
  120.         ++count;

  121.     }

  122.     /** Build writer for a constituent.
  123.      * @param constituent constituent
  124.      * @param <H> type of the header
  125.      * @param <S> type of the segments
  126.      * @param <F> type of the file
  127.      * @return writer suited for the constituent
  128.      * @throws IOException if the stream cannot write to stream
  129.      */
  130.     @SuppressWarnings("unchecked")
  131.     private <H extends Header, S extends Segment<?, ?>, F extends NdmConstituent<H, S>>
  132.         MessageWriter<H, S, F> buildWriter(final F constituent) throws IOException {
  133.         if (constituent instanceof Tdm) {
  134.             return (MessageWriter<H, S, F>) builder.buildTdmWriter();
  135.         } else if (constituent instanceof Opm) {
  136.             return (MessageWriter<H, S, F>) builder.buildOpmWriter();
  137.         } else if (constituent instanceof Omm) {
  138.             return (MessageWriter<H, S, F>) builder.buildOmmWriter();
  139.         } else if (constituent instanceof Oem) {
  140.             return (MessageWriter<H, S, F>) builder.buildOemWriter();
  141.         } else if (constituent instanceof Ocm) {
  142.             return (MessageWriter<H, S, F>) builder.buildOcmWriter();
  143.         } else if (constituent instanceof Apm) {
  144.             return (MessageWriter<H, S, F>) builder.buildApmWriter();
  145.         } else if (constituent instanceof Aem) {
  146.             return (MessageWriter<H, S, F>) builder.buildAemWriter();
  147.         } else if (constituent instanceof Acm) {
  148.             return (MessageWriter<H, S, F>) builder.buildAcmWriter();
  149.         } else {
  150.             // this should never happen
  151.             throw new OrekitInternalError(null);
  152.         }
  153.     }

  154. }