Generator.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.utils.generation;

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

  20. import org.orekit.files.ccsds.definitions.TimeConverter;
  21. import org.orekit.files.ccsds.utils.FileFormat;
  22. import org.orekit.time.AbsoluteDate;
  23. import org.orekit.utils.Formatter;
  24. import org.orekit.utils.units.Unit;

  25. /** Generation interface for CCSDS messages.
  26.  * @author Luc Maisonobe
  27.  * @since 11.0
  28.  */
  29. public interface Generator extends AutoCloseable {

  30.     /** Get the name of the output (for error messages).
  31.      * @return name of the output
  32.      */
  33.     String getOutputName();

  34.     /** Get the generated file format.
  35.      * @return generated file format
  36.      */
  37.     FileFormat getFormat();

  38.     /**
  39.      *  Used to format dates and doubles to string.
  40.      * @return formatter
  41.      */
  42.     Formatter getFormatter();

  43.     /** Start CCSDS message.
  44.      * @param messageTypeKey key for message type
  45.      * @param root root element for XML files
  46.      * @param version format version
  47.      * @throws IOException if an I/O error occurs.
  48.      */
  49.     void startMessage(String root, String messageTypeKey, double version) throws IOException;

  50.     /** End CCSDS message.
  51.      * @param root root element for XML files
  52.      * @throws IOException if an I/O error occurs.
  53.      */
  54.     void endMessage(String root) throws IOException;

  55.     /** Write comment lines.
  56.      * @param comments comments to write
  57.      * @throws IOException if an I/O error occurs.
  58.      */
  59.     void writeComments(List<String> comments) throws IOException;

  60.     /** Write a single key/value entry.
  61.      * @param key   the keyword to write
  62.      * @param value the value to write
  63.      * @param unit output unit (may be null)
  64.      * @param mandatory if true, null values triggers exception, otherwise they are silently ignored
  65.      * @throws IOException if an I/O error occurs.
  66.      */
  67.     void writeEntry(String key, String value, Unit unit, boolean mandatory) throws IOException;

  68.     /** Write a single key/value entry.
  69.      * @param key   the keyword to write
  70.      * @param value the value to write
  71.      * @param mandatory if true, null values triggers exception, otherwise they are silently ignored
  72.      * @throws IOException if an I/O error occurs.
  73.      */
  74.     void writeEntry(String key, List<String> value, boolean mandatory) throws IOException;

  75.     /** Write a single key/value entry.
  76.      * @param key   the keyword to write
  77.      * @param value the value to write
  78.      * @param mandatory if true, null values triggers exception, otherwise they are silently ignored
  79.      * @throws IOException if an I/O error occurs.
  80.      */
  81.     void writeEntry(String key, Enum<?> value, boolean mandatory) throws IOException;

  82.     /** Write a single key/value entry.
  83.      * @param key   the keyword to write
  84.      * @param converter converter to use for dates
  85.      * @param date the date to write
  86.      * @param forceCalendar if true, the date is forced to calendar format
  87.      * @param mandatory if true, null values triggers exception, otherwise they are silently ignored
  88.      * @throws IOException if an I/O error occurs.
  89.      */
  90.     void writeEntry(String key, TimeConverter converter, AbsoluteDate date, boolean forceCalendar, boolean mandatory) throws IOException;

  91.     /** Write a single key/value entry.
  92.      * @param key   the keyword to write
  93.      * @param value the value to write
  94.      * @param mandatory if true, null values triggers exception, otherwise they are silently ignored
  95.      * @throws IOException if an I/O error occurs.
  96.      */
  97.     void writeEntry(String key, char value, boolean mandatory) throws IOException;

  98.     /**
  99.      * Write a single key/value entry.
  100.      *
  101.      * <p>Note that the {@code mandatory} flag has no effect and a value is always written
  102.      * because the whole domain of {@code value} is treated as valid. Use {@link
  103.      * #writeEntry(String, Integer, boolean)} for integer values that may not be present.
  104.      *
  105.      * @param key   the keyword to write
  106.      * @param value the value to write
  107.      * @param mandatory if true, null values triggers exception, otherwise they are silently ignored.
  108.      * @throws IOException if an I/O error occurs.
  109.      * @see #writeEntry(String, Integer, boolean)
  110.      */
  111.     void writeEntry(String key, int value, boolean mandatory) throws IOException;

  112.     /** Write a single key/value entry.
  113.      * @param key   the keyword to write
  114.      * @param value the value to write
  115.      * @param mandatory if true, null values triggers exception, otherwise they are silently ignored
  116.      * @throws IOException if an I/O error occurs.
  117.      */
  118.     default void writeEntry(String key, Integer value, boolean mandatory) throws IOException {
  119.         writeEntry(key, value == null ? null : value.toString(), null, mandatory);
  120.     }

  121.     /** Write a single key/value entry.
  122.      * @param key   the keyword to write
  123.      * @param value the value to write (in SI units)
  124.      * @param unit output unit
  125.      * @param mandatory if true, null values triggers exception, otherwise they are silently ignored
  126.      * @throws IOException if an I/O error occurs.
  127.      */
  128.     void writeEntry(String key, double value, Unit unit, boolean mandatory) throws IOException;

  129.     /** Write a single key/value entry.
  130.      * @param key   the keyword to write
  131.      * @param value the value to write (in SI units)
  132.      * @param unit output unit
  133.      * @param mandatory if true, null values triggers exception, otherwise they are silently ignored
  134.      * @throws IOException if an I/O error occurs.
  135.      */
  136.     void writeEntry(String key, Double value, Unit unit, boolean mandatory) throws IOException;

  137.     /** Finish current line.
  138.      * @throws IOException if an I/O error occurs.
  139.      */
  140.     void newLine() throws IOException;

  141.     /** Write raw data.
  142.      * @param data raw data to write
  143.      * @throws IOException if an I/O error occurs.
  144.      */
  145.     void writeRawData(char data) throws IOException;

  146.     /** Write raw data.
  147.      * @param data raw data to write
  148.      * @throws IOException if an I/O error occurs.
  149.      */
  150.     void writeRawData(CharSequence data) throws IOException;

  151.     /** Enter into a new section.
  152.      * @param name section name
  153.      * @throws IOException if an I/O error occurs.
  154.      */
  155.     void enterSection(String name) throws IOException;

  156.     /** Exit last section.
  157.      * @return section name
  158.      * @throws IOException if an I/O error occurs.
  159.      */
  160.     String exitSection() throws IOException;

  161.     /** Close the generator.
  162.      * @throws IOException if an I/O error occurs.
  163.      */
  164.     void close() throws IOException;

  165.     /** Convert a date to string value with high precision.
  166.      * @param converter converter for dates
  167.      * @param date date to write
  168.      * @return date as a string (may be either a relative date or a calendar date)
  169.      */
  170.     String dateToString(TimeConverter converter, AbsoluteDate date);

  171.     /** Convert a date to calendar string value with high precision.
  172.      * @param converter converter for dates
  173.      * @param date date to write
  174.      * @return date as a calendar string
  175.      * @since 12.0
  176.      */
  177.     String dateToCalendarString(TimeConverter converter, AbsoluteDate date);

  178.     /** Convert a date to string value with high precision.
  179.      * @param year year
  180.      * @param month month
  181.      * @param day day
  182.      * @param hour hour
  183.      * @param minute minute
  184.      * @param seconds seconds
  185.      * @return date as a string
  186.      */
  187.     String dateToString(int year, int month, int day, int hour, int minute, double seconds);

  188.     /** Convert a double to string value with high precision.
  189.      * <p>
  190.      * We don't want to loose internal accuracy when writing doubles
  191.      * but we also don't want to have ugly representations like STEP = 1.25000000000000000
  192.      * so we try a few simple formats first and fall back to scientific notation
  193.      * if it doesn't work.
  194.      * </p>
  195.      * @param value value to format
  196.      * @return formatted value, with all original value accuracy preserved, or null
  197.      * if value is null or {@code Double.NaN}
  198.      */
  199.     String doubleToString(double value);

  200.     /** Convert a list of units to a bracketed string.
  201.      * @param units lists to output (may be null or empty)
  202.      * @return bracketed string (null if units list is null or empty)
  203.      */
  204.     String unitsListToString(List<Unit> units);

  205.     /** Convert a SI unit name to a CCSDS name.
  206.      * @param siName si unit name
  207.      * @return CCSDS name for the unit
  208.      */
  209.     String siToCcsdsName(String siName);

  210. }