UserDefinedXmlTokenBuilder.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.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.orekit.files.ccsds.ndm.odm.UserDefined;
  23. import org.orekit.utils.units.Unit;

  24. /** Builder for user-defined parameters.
  25.  * <p>
  26.  * User-defined elements are of the form:
  27.  * </p>
  28.  * <pre>
  29.  *   &lt;USER_DEFINED parameter="SOME_PARAMETER_NAME"&gt;value&lt;/USER_DEFINED&gt;
  30.  * </pre>
  31.  * <p>
  32.  * This {@link XmlTokenBuilder token builder} will generate a single
  33.  * {@link ParseToken parse token} from this root element with name set to
  34.  * "SOME_PARAMETER_NAME", type set to {@link TokenType#ENTRY} and content
  35.  * set to {@code value}.
  36.  * </p>
  37.  * @author Luc Maisonobe
  38.  * @since 11.0
  39.  */
  40. public class UserDefinedXmlTokenBuilder implements XmlTokenBuilder {

  41.     /** Empty constructor.
  42.      * <p>
  43.      * This constructor is not strictly necessary, but it prevents spurious
  44.      * javadoc warnings with JDK 18 and later.
  45.      * </p>
  46.      * @since 12.0
  47.      */
  48.     public UserDefinedXmlTokenBuilder() {
  49.         // nothing to do
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public List<ParseToken> buildTokens(final boolean startTag, final boolean isLeaf, final String qName,
  54.                                         final String content, final Map<String, String> attributes,
  55.                                         final int lineNumber, final String fileName) {

  56.         // elaborate name
  57.         final String name = UserDefined.USER_DEFINED_PREFIX +
  58.                             attributes.get(UserDefined.USER_DEFINED_XML_ATTRIBUTE);

  59.         if (startTag) {
  60.             return Collections.singletonList(new ParseToken(TokenType.START, name, content, Unit.NONE, lineNumber, fileName));
  61.         } else {
  62.             final List<ParseToken> built = new ArrayList<>(2);
  63.             if (isLeaf) {
  64.                 built.add(new ParseToken(TokenType.ENTRY, name, content, Unit.NONE, lineNumber, fileName));
  65.             }
  66.             built.add(new ParseToken(TokenType.STOP, name, null, Unit.NONE, lineNumber, fileName));
  67.             return built;
  68.         }

  69.     }

  70. }