1 /* Copyright 2002-2021 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 52 // enter section 53 final boolean needsClosing; 54 if (generator.getFormat() == FileFormat.XML) { 55 generator.enterSection(xmlTag); 56 needsClosing = true; 57 } else if (generator.getFormat() == FileFormat.KVN && kvnTag != null) { 58 generator.enterSection(kvnTag); 59 needsClosing = true; 60 } else { 61 needsClosing = false; 62 } 63 64 // write content 65 writeContent(generator); 66 67 // exit section 68 if (needsClosing) { 69 generator.exitSection(); 70 } 71 72 } 73 74 /** Write the content of the section, excluding surrounding tags. 75 * @param generator generator to use for producing output 76 * @throws IOException if any buffer writing operations fails 77 */ 78 protected abstract void writeContent(Generator generator) throws IOException; 79 80 /** Convert an array of integer to a comma-separated list. 81 * @param integers integers to write 82 * @return arrays as a string 83 */ 84 protected String intArrayToString(final int[] integers) { 85 final StringBuilder builder = new StringBuilder(); 86 for (int i = 0; i < integers.length; ++i) { 87 if (i > 0) { 88 builder.append(','); 89 } 90 builder.append(integers[i]); 91 } 92 return builder.toString(); 93 } 94 95 }