KvnGenerator.java

  1. /* Copyright 2002-2023 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.utils.generation;

  18. import java.io.IOException;
  19. import java.util.List;

  20. import org.hipparchus.util.FastMath;
  21. import org.orekit.files.ccsds.utils.FileFormat;
  22. import org.orekit.utils.AccurateFormatter;
  23. import org.orekit.utils.units.Unit;

  24. /** Generator for Key-Value Notation CCSDS messages.
  25.  * @author Luc Maisonobe
  26.  * @since 11.0
  27.  */
  28. public class KvnGenerator extends AbstractGenerator {

  29.     /** Comment keyword. */
  30.     private static final String COMMENT = "COMMENT";

  31.     /** Start suffix for sections. */
  32.     private static final String START = "_START";

  33.     /** Stop suffix for sections. */
  34.     private static final String STOP = "_STOP";

  35.     /** String format used for all key/value pair lines. **/
  36.     private final String kvFormat;

  37.     /** Column number for aligning units. */
  38.     private final int unitsColumn;

  39.     /** String format used for all comment lines. **/
  40.     private final String commentFormat;

  41.     /** Simple constructor.
  42.      * @param output destination of generated output
  43.      * @param paddingWidth padding width for aligning the '=' sign
  44.      * (not counting the extra blank added before the '=' sign)
  45.      * @param outputName output name for error messages
  46.      * @param maxRelativeOffset maximum offset in seconds to use relative dates
  47.      * (if a date is too far from reference, it will be displayed as calendar elements)
  48.      * @param unitsColumn columns number for aligning units (if negative or zero, units are not output)
  49.      * @see org.orekit.files.ccsds.ndm.tdm.TdmWriter#KVN_PADDING_WIDTH     TdmWriter.KVN_PADDING_WIDTH
  50.      * @see org.orekit.files.ccsds.ndm.adm.aem.AemWriter#KVN_PADDING_WIDTH AemWriter.KVN_PADDING_WIDTH
  51.      * @see org.orekit.files.ccsds.ndm.adm.apm.ApmWriter#KVN_PADDING_WIDTH ApmWriter.KVN_PADDING_WIDTH
  52.      * @see org.orekit.files.ccsds.ndm.odm.opm.OpmWriter#KVN_PADDING_WIDTH OpmWriter.KVN_PADDING_WIDTH
  53.      * @see org.orekit.files.ccsds.ndm.odm.omm.OmmWriter#KVN_PADDING_WIDTH OmmWriter.KVN_PADDING_WIDTH
  54.      * @see org.orekit.files.ccsds.ndm.odm.oem.OemWriter#KVN_PADDING_WIDTH OemWriter.KVN_PADDING_WIDTH
  55.      * @see org.orekit.files.ccsds.ndm.odm.ocm.OcmWriter#KVN_PADDING_WIDTH OcmWriter.KVN_PADDING_WIDTH
  56.      */
  57.     public KvnGenerator(final Appendable output, final int paddingWidth,
  58.                         final String outputName, final double maxRelativeOffset,
  59.                         final int unitsColumn) {
  60.         super(output, outputName, maxRelativeOffset, unitsColumn > 0);
  61.         kvFormat = "%-" + FastMath.max(1, paddingWidth) + "s = %s";
  62.         final StringBuilder builder = new StringBuilder(COMMENT);
  63.         builder.append(' ');
  64.         while (builder.length() < paddingWidth + 3) {
  65.             builder.append(' ');
  66.         }
  67.         builder.append("%s%n");
  68.         this.unitsColumn   = unitsColumn;
  69.         this.commentFormat = builder.toString();
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public FileFormat getFormat() {
  74.         return FileFormat.KVN;
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public void startMessage(final String root, final String messageTypeKey, final double version) throws IOException {
  79.         writeEntry(messageTypeKey, String.format(AccurateFormatter.STANDARDIZED_LOCALE, "%.1f", version), null, true);
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public void endMessage(final String root) {
  84.         // nothing to do
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public void writeComments(final List<String> comments) throws IOException {
  89.         for (final String comment : comments) {
  90.             writeRawData(String.format(AccurateFormatter.STANDARDIZED_LOCALE, commentFormat, comment));
  91.         }
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public void writeEntry(final String key, final String value, final Unit unit, final boolean mandatory) throws IOException {
  96.         if (value == null) {
  97.             complain(key, mandatory);
  98.         } else {
  99.             final String s = String.format(AccurateFormatter.STANDARDIZED_LOCALE, kvFormat, key, value);
  100.             writeRawData(s);
  101.             if (writeUnits(unit)) {
  102.                 for (int column = s.length(); column < unitsColumn; ++column) {
  103.                     writeRawData(' ');
  104.                 }
  105.                 writeRawData('[');
  106.                 writeRawData(siToCcsdsName(unit.getName()));
  107.                 writeRawData(']');
  108.             }
  109.             newLine();
  110.         }
  111.     }

  112.     /** {@inheritDoc} */
  113.     @Override
  114.     public void enterSection(final String name) throws IOException {
  115.         writeRawData(name);
  116.         writeRawData(START);
  117.         newLine();
  118.         super.enterSection(name);
  119.     }

  120.     /** {@inheritDoc} */
  121.     @Override
  122.     public String exitSection() throws IOException {
  123.         final String name = super.exitSection();
  124.         writeRawData(name);
  125.         writeRawData(STOP);
  126.         newLine();
  127.         return name;
  128.     }

  129. }