RotationXmlTokenBuilder.java

  1. /* Copyright 2002-2023 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.ndm.adm;

  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.utils.lexical.ParseToken;
  23. import org.orekit.files.ccsds.utils.lexical.TokenType;
  24. import org.orekit.files.ccsds.utils.lexical.XmlTokenBuilder;
  25. import org.orekit.utils.units.Unit;
  26. import org.orekit.utils.units.UnitsCache;

  27. /** Builder for rotation angles and rates.
  28.  * <p>
  29.  * Instances of this class are immutable.
  30.  * </p>
  31.  * @author Luc Maisonobe
  32.  * @since 11.0
  33.  */
  34. public class RotationXmlTokenBuilder implements XmlTokenBuilder {

  35.     /** Attribute defining rotation angle. */
  36.     private static final String ANGLE = "angle";

  37.     /** Attribute defining rotation rate. */
  38.     private static final String RATE = "rate";

  39.     /** Attribute name for units. */
  40.     private static final String UNITS = "units";

  41.     /** Cache for parsed units. */
  42.     private final UnitsCache cache;

  43.     /** Simple constructor.
  44.      */
  45.     public RotationXmlTokenBuilder() {
  46.         this.cache = new UnitsCache();
  47.     }

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

  53.         // get the token name from the first attribute found
  54.         String name = attributes.get(ANGLE);
  55.         if (name == null) {
  56.             name = attributes.get(RATE);
  57.         }

  58.         if (startTag) {
  59.             return Collections.singletonList(new ParseToken(TokenType.START, name, content, Unit.NONE, lineNumber, fileName));
  60.         } else {
  61.             final List<ParseToken> built = new ArrayList<>(2);
  62.             if (isLeaf) {
  63.                 // get units
  64.                 final Unit units = cache.getUnits(attributes.get(UNITS));

  65.                 built.add(new ParseToken(TokenType.ENTRY, name, content, units, lineNumber, fileName));
  66.             }
  67.             built.add(new ParseToken(TokenType.STOP, name, null, Unit.NONE, lineNumber, fileName));
  68.             return built;
  69.         }

  70.     }

  71. }