NdmWriter.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.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.aem.Aem;
  23. import org.orekit.files.ccsds.ndm.adm.apm.Apm;
  24. import org.orekit.files.ccsds.ndm.odm.ocm.Ocm;
  25. import org.orekit.files.ccsds.ndm.odm.oem.Oem;
  26. import org.orekit.files.ccsds.ndm.odm.omm.Omm;
  27. import org.orekit.files.ccsds.ndm.odm.opm.Opm;
  28. import org.orekit.files.ccsds.ndm.tdm.Tdm;
  29. import org.orekit.files.ccsds.section.Header;
  30. import org.orekit.files.ccsds.section.Segment;
  31. import org.orekit.files.ccsds.utils.generation.Generator;
  32. import org.orekit.files.ccsds.utils.generation.MessageWriter;

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

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

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

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

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

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

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

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

  74.     }

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

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

  96.         startMessageIfNeeded(generator);

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

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

  102.     }

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

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

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

  118.         // update count
  119.         ++count;

  120.     }

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

  151. }