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.ndm.odm;
19  
20  import java.io.IOException;
21  import java.util.Map;
22  
23  import org.orekit.files.ccsds.section.AbstractWriter;
24  import org.orekit.files.ccsds.utils.FileFormat;
25  import org.orekit.files.ccsds.utils.generation.Generator;
26  import org.orekit.files.ccsds.utils.generation.XmlGenerator;
27  
28  /** Writer for user defined parameters data.
29   * @author Luc Maisonobe
30   * @since 11.0
31   */
32  public class UserDefinedWriter extends AbstractWriter {
33  
34      /** User defined parameters block. */
35      private final UserDefined userDefined;
36  
37      /** Create a writer.
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       * @param userDefined user defined parameters to write
41       */
42      public UserDefinedWriter(final String xmlTag, final String kvnTag, final UserDefined userDefined) {
43          super(xmlTag, kvnTag);
44          this.userDefined   = userDefined;
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      protected void writeContent(final Generator generator) throws IOException {
50  
51          // user-defined parameters block
52          generator.writeComments(userDefined.getComments());
53  
54          // entries
55          if (generator.getFormat() == FileFormat.XML) {
56              final XmlGenerator xmlGenerator = (XmlGenerator) generator;
57              for (Map.Entry<String, String> entry : userDefined.getParameters().entrySet()) {
58                  xmlGenerator.writeOneAttributeElement(UserDefined.USER_DEFINED_XML_TAG,       entry.getValue(),
59                                                        UserDefined.USER_DEFINED_XML_ATTRIBUTE, entry.getKey());
60              }
61          } else {
62              for (Map.Entry<String, String> entry : userDefined.getParameters().entrySet()) {
63                  generator.writeEntry(UserDefined.USER_DEFINED_PREFIX + entry.getKey(), entry.getValue(), null, false);
64              }
65          }
66  
67      }
68  
69  }