KvnGenerator.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.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 unitsColumn columns number for aligning units (if negative or zero, units are not output)
  47.      * @see org.orekit.files.ccsds.ndm.tdm.TdmWriter#KVN_PADDING_WIDTH     TdmWriter.KVN_PADDING_WIDTH
  48.      * @see org.orekit.files.ccsds.ndm.adm.aem.AemWriter#KVN_PADDING_WIDTH AemWriter.KVN_PADDING_WIDTH
  49.      * @see org.orekit.files.ccsds.ndm.adm.apm.ApmWriter#KVN_PADDING_WIDTH ApmWriter.KVN_PADDING_WIDTH
  50.      * @see org.orekit.files.ccsds.ndm.odm.opm.OpmWriter#KVN_PADDING_WIDTH OpmWriter.KVN_PADDING_WIDTH
  51.      * @see org.orekit.files.ccsds.ndm.odm.omm.OmmWriter#KVN_PADDING_WIDTH OmmWriter.KVN_PADDING_WIDTH
  52.      * @see org.orekit.files.ccsds.ndm.odm.oem.OemWriter#KVN_PADDING_WIDTH OemWriter.KVN_PADDING_WIDTH
  53.      * @see org.orekit.files.ccsds.ndm.odm.ocm.OcmWriter#KVN_PADDING_WIDTH OcmWriter.KVN_PADDING_WIDTH
  54.      */
  55.     public KvnGenerator(final Appendable output, final int paddingWidth,
  56.                         final String outputName, final int unitsColumn) {
  57.         super(output, outputName, unitsColumn > 0);
  58.         kvFormat = "%-" + FastMath.max(1, paddingWidth) + "s = %s";
  59.         final StringBuilder builder = new StringBuilder(COMMENT);
  60.         builder.append(' ');
  61.         while (builder.length() < paddingWidth + 3) {
  62.             builder.append(' ');
  63.         }
  64.         builder.append("%s%n");
  65.         this.unitsColumn   = unitsColumn;
  66.         this.commentFormat = builder.toString();
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public FileFormat getFormat() {
  71.         return FileFormat.KVN;
  72.     }

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

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public void endMessage(final String root) {
  81.         // nothing to do
  82.     }

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

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

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

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

  126. }