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 18 package org.orekit.files.ccsds.section; 19 20 import java.io.IOException; 21 22 import org.orekit.files.ccsds.utils.FileFormat; 23 import org.orekit.files.ccsds.utils.generation.Generator; 24 25 /** Top level class for writing CCSDS message sections. 26 * @author Luc Maisonobe 27 * @since 11.0 28 */ 29 public abstract class AbstractWriter { 30 31 /** Name of the XML tag surrounding the section. */ 32 private String xmlTag; 33 34 /** Name of the KVN tag surrounding the section (may be null). */ 35 private String kvnTag; 36 37 /** Simple constructor. 38 * @param xmlTag name of the XML tag surrounding the section 39 * @param kvnTag name of the KVN tag surrounding the section (may be null) 40 */ 41 protected AbstractWriter(final String xmlTag, final String kvnTag) { 42 this.xmlTag = xmlTag; 43 this.kvnTag = kvnTag; 44 } 45 46 /** Write the section, including surrounding tags. 47 * @param generator generator to use for producing output 48 * @throws IOException if any buffer writing operations fails 49 */ 50 public void write(final Generator generator) throws IOException { 51 enterSection(generator); 52 writeContent(generator); 53 exitSection(generator); 54 } 55 56 /** Enter the section. 57 * @param generator generator to use for producing output 58 * @throws IOException if an I/O error occurs. 59 * @since 12.0 60 */ 61 public void enterSection(final Generator generator) throws IOException { 62 if (generator.getFormat() == FileFormat.XML) { 63 generator.enterSection(xmlTag); 64 } else if (generator.getFormat() == FileFormat.KVN && kvnTag != null) { 65 generator.enterSection(kvnTag); 66 } 67 } 68 69 /** Exit the section. 70 * @param generator generator to use for producing output 71 * @throws IOException if an I/O error occurs. 72 * @since 12.0 73 */ 74 public void exitSection(final Generator generator) throws IOException { 75 if (generator.getFormat() == FileFormat.XML || 76 generator.getFormat() == FileFormat.KVN && kvnTag != null) { 77 generator.exitSection(); 78 } 79 } 80 81 /** Write the content of the section, excluding surrounding tags. 82 * @param generator generator to use for producing output 83 * @throws IOException if any buffer writing operations fails 84 */ 85 protected abstract void writeContent(Generator generator) throws IOException; 86 87 /** Convert an array of integer to a comma-separated list. 88 * @param integers integers to write 89 * @return arrays as a string 90 */ 91 protected String intArrayToString(final int[] integers) { 92 final StringBuilder builder = new StringBuilder(); 93 for (int i = 0; i < integers.length; ++i) { 94 if (i > 0) { 95 builder.append(','); 96 } 97 builder.append(integers[i]); 98 } 99 return builder.toString(); 100 } 101 102 }