AbstractWriter.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.section;

  18. import java.io.IOException;

  19. import org.orekit.files.ccsds.utils.FileFormat;
  20. import org.orekit.files.ccsds.utils.generation.Generator;

  21. /** Top level class for writing CCSDS message sections.
  22.  * @author Luc Maisonobe
  23.  * @since 11.0
  24.  */
  25. public abstract class AbstractWriter {

  26.     /** Name of the XML tag surrounding the section. */
  27.     private String xmlTag;

  28.     /** Name of the KVN tag surrounding the section (may be null). */
  29.     private String kvnTag;

  30.     /** Simple constructor.
  31.      * @param xmlTag name of the XML tag surrounding the section
  32.      * @param kvnTag name of the KVN tag surrounding the section (may be null)
  33.      */
  34.     protected AbstractWriter(final String xmlTag, final String kvnTag) {
  35.         this.xmlTag = xmlTag;
  36.         this.kvnTag = kvnTag;
  37.     }

  38.     /** Write the section, including surrounding tags.
  39.      * @param generator generator to use for producing output
  40.      * @throws IOException if any buffer writing operations fails
  41.      */
  42.     public void write(final Generator generator) throws IOException {
  43.         enterSection(generator);
  44.         writeContent(generator);
  45.         exitSection(generator);
  46.     }

  47.     /** Enter the section.
  48.      * @param generator generator to use for producing output
  49.      * @throws IOException if an I/O error occurs.
  50.      * @since 12.0
  51.      */
  52.     public void enterSection(final Generator generator) throws IOException {
  53.         if (generator.getFormat() == FileFormat.XML) {
  54.             generator.enterSection(xmlTag);
  55.         } else if (generator.getFormat() == FileFormat.KVN && kvnTag != null) {
  56.             generator.enterSection(kvnTag);
  57.         }
  58.     }

  59.     /** Exit the section.
  60.      * @param generator generator to use for producing output
  61.      * @throws IOException if an I/O error occurs.
  62.      * @since 12.0
  63.      */
  64.     public void exitSection(final Generator generator) throws IOException {
  65.         if (generator.getFormat() == FileFormat.XML ||
  66.             generator.getFormat() == FileFormat.KVN && kvnTag != null) {
  67.             generator.exitSection();
  68.         }
  69.     }

  70.     /** Write the content of the section, excluding surrounding tags.
  71.      * @param generator generator to use for producing output
  72.      * @throws IOException if any buffer writing operations fails
  73.      */
  74.     protected abstract void writeContent(Generator generator) throws IOException;

  75.     /** Convert an array of integer to a comma-separated list.
  76.      * @param integers integers to write
  77.      * @return arrays as a string
  78.      */
  79.     protected String intArrayToString(final int[] integers) {
  80.         final StringBuilder builder = new StringBuilder();
  81.         for (int i = 0; i < integers.length; ++i) {
  82.             if (i > 0) {
  83.                 builder.append(',');
  84.             }
  85.             builder.append(integers[i]);
  86.         }
  87.         return builder.toString();
  88.     }

  89. }