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  package org.orekit.files.ccsds.utils.lexical;
18  
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.orekit.utils.units.Unit;
25  
26  /** Builder for the root element with CCSDS message version.
27   * <p>
28   * All parsers for CCSDS ADM, ODM and TDM messages need to handle the
29   * root level XML element specially. OPM file for example have a root
30   * element of the form:
31   * </p>
32   * <pre>
33   *   &lt;opm id="CCSDS_OPM_VERS" verion="3.0"&gt;
34   * </pre>
35   * <p>
36   * This {@link XmlTokenBuilder token builder} will generate two
37   * {@link ParseToken parse tokens} from this root element:
38   * </p>
39   * <ol>
40   *   <li>one with name set to "opm", type set to {@link TokenType#START} and no content</li>
41   *   <li>one with name set to "CCSDS_OPM_VERS", type set to {@link TokenType#ENTRY} and content set to "3.0"</li>
42   * </ol>
43   * @author Luc Maisonobe
44   * @since 11.0
45   */
46  public class MessageVersionXmlTokenBuilder implements XmlTokenBuilder {
47  
48      /** Attribute name for id. */
49      private static final String ID = "id";
50  
51      /** Attribute name for version. */
52      private static final String VERSION = "version";
53  
54      /** Empty constructor.
55       * <p>
56       * This constructor is not strictly necessary, but it prevents spurious
57       * javadoc warnings with JDK 18 and later.
58       * </p>
59       * @since 12.0
60       */
61      public MessageVersionXmlTokenBuilder() {
62          // nothing to do
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public List<ParseToken> buildTokens(final boolean startTag, final boolean isLeaf, final String qName,
68                                          final String content, final Map<String, String> attributes,
69                                          final int lineNumber, final String fileName) {
70          if (startTag) {
71              // we replace the start tag with the message version specification
72              final String     id      = attributes.get(ID);
73              final String     version = attributes.get(VERSION);
74              final ParseToken start   = new ParseToken(TokenType.START, qName, null, Unit.NONE, lineNumber, fileName);
75              final ParseToken entry   = new ParseToken(TokenType.ENTRY, id, version, Unit.NONE, lineNumber, fileName);
76              return Arrays.asList(start, entry);
77          } else {
78              final ParseToken stop = new ParseToken(TokenType.STOP, qName, null, Unit.NONE, lineNumber, fileName);
79              return Collections.singletonList(stop);
80          }
81      }
82  
83  }