MessageVersionXmlTokenBuilder.java

  1. /* Copyright 2002-2024 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. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.orekit.utils.units.Unit;

  23. /** Builder for the root element with CCSDS message version.
  24.  * <p>
  25.  * All parsers for CCSDS ADM, ODM and TDM messages need to handle the
  26.  * root level XML element specially. OPM file for example have a root
  27.  * element of the form:
  28.  * </p>
  29.  * <pre>
  30.  *   &lt;opm id="CCSDS_OPM_VERS" verion="3.0"&gt;
  31.  * </pre>
  32.  * <p>
  33.  * This {@link XmlTokenBuilder token builder} will generate two
  34.  * {@link ParseToken parse tokens} from this root element:
  35.  * </p>
  36.  * <ol>
  37.  *   <li>one with name set to "opm", type set to {@link TokenType#START} and no content</li>
  38.  *   <li>one with name set to "CCSDS_OPM_VERS", type set to {@link TokenType#ENTRY} and content set to "3.0"</li>
  39.  * </ol>
  40.  * @author Luc Maisonobe
  41.  * @since 11.0
  42.  */
  43. public class MessageVersionXmlTokenBuilder implements XmlTokenBuilder {

  44.     /** Attribute name for id. */
  45.     private static final String ID = "id";

  46.     /** Attribute name for version. */
  47.     private static final String VERSION = "version";

  48.     /** Empty constructor.
  49.      * <p>
  50.      * This constructor is not strictly necessary, but it prevents spurious
  51.      * javadoc warnings with JDK 18 and later.
  52.      * </p>
  53.      * @since 12.0
  54.      */
  55.     public MessageVersionXmlTokenBuilder() {
  56.         // nothing to do
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public List<ParseToken> buildTokens(final boolean startTag, final boolean isLeaf, final String qName,
  61.                                         final String content, final Map<String, String> attributes,
  62.                                         final int lineNumber, final String fileName) {
  63.         if (startTag) {
  64.             // we replace the start tag with the message version specification
  65.             final String     id      = attributes.get(ID);
  66.             final String     version = attributes.get(VERSION);
  67.             final ParseToken start   = new ParseToken(TokenType.START, qName, null, Unit.NONE, lineNumber, fileName);
  68.             final ParseToken entry   = new ParseToken(TokenType.ENTRY, id, version, Unit.NONE, lineNumber, fileName);
  69.             return Arrays.asList(start, entry);
  70.         } else {
  71.             final ParseToken stop = new ParseToken(TokenType.STOP, qName, null, Unit.NONE, lineNumber, fileName);
  72.             return Collections.singletonList(stop);
  73.         }
  74.     }

  75. }