AbstractWriter.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.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.         // enter section
  44.         final boolean needsClosing;
  45.         if (generator.getFormat() == FileFormat.XML) {
  46.             generator.enterSection(xmlTag);
  47.             needsClosing = true;
  48.         } else if (generator.getFormat() == FileFormat.KVN && kvnTag != null) {
  49.             generator.enterSection(kvnTag);
  50.             needsClosing = true;
  51.         } else {
  52.             needsClosing = false;
  53.         }

  54.         // write content
  55.         writeContent(generator);

  56.         // exit section
  57.         if (needsClosing) {
  58.             generator.exitSection();
  59.         }

  60.     }

  61.     /** Write the content of the section, excluding surrounding tags.
  62.      * @param generator generator to use for producing output
  63.      * @throws IOException if any buffer writing operations fails
  64.      */
  65.     protected abstract void writeContent(Generator generator) throws IOException;

  66.     /** Convert an array of integer to a comma-separated list.
  67.      * @param integers integers to write
  68.      * @return arrays as a string
  69.      */
  70.     protected String intArrayToString(final int[] integers) {
  71.         final StringBuilder builder = new StringBuilder();
  72.         for (int i = 0; i < integers.length; ++i) {
  73.             if (i > 0) {
  74.                 builder.append(',');
  75.             }
  76.             builder.append(integers[i]);
  77.         }
  78.         return builder.toString();
  79.     }

  80. }