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.ndm.adm;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.orekit.files.ccsds.utils.lexical.ParseToken;
25  import org.orekit.files.ccsds.utils.lexical.TokenType;
26  import org.orekit.files.ccsds.utils.lexical.XmlTokenBuilder;
27  import org.orekit.utils.units.Unit;
28  import org.orekit.utils.units.UnitsCache;
29  
30  /** Builder for rotation angles and rates.
31   * <p>
32   * Instances of this class are immutable.
33   * </p>
34   * @author Luc Maisonobe
35   * @since 11.0
36   */
37  public class RotationXmlTokenBuilder implements XmlTokenBuilder {
38  
39      /** Attribute defining rotation angle. */
40      private static final String ANGLE = "angle";
41  
42      /** Attribute defining rotation rate. */
43      private static final String RATE = "rate";
44  
45      /** Attribute name for units. */
46      private static final String UNITS = "units";
47  
48      /** Cache for parsed units. */
49      private final UnitsCache cache;
50  
51      /** Simple constructor.
52       */
53      public RotationXmlTokenBuilder() {
54          this.cache = new UnitsCache();
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public List<ParseToken> buildTokens(final boolean startTag, final boolean isLeaf, final String qName,
60                                          final String content, final Map<String, String> attributes,
61                                          final int lineNumber, final String fileName) {
62  
63          // get the token name from the first attribute found
64          String name = attributes.get(ANGLE);
65          if (name == null) {
66              name = attributes.get(RATE);
67          }
68  
69          if (startTag) {
70              return Collections.singletonList(new ParseToken(TokenType.START, name, content, Unit.NONE, lineNumber, fileName));
71          } else {
72              final List<ParseToken> built = new ArrayList<>(2);
73              if (isLeaf) {
74                  // get units
75                  final Unit units = cache.getUnits(attributes.get(UNITS));
76  
77                  built.add(new ParseToken(TokenType.ENTRY, name, content, units, lineNumber, fileName));
78              }
79              built.add(new ParseToken(TokenType.STOP, name, null, Unit.NONE, lineNumber, fileName));
80              return built;
81          }
82  
83      }
84  
85  }