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.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.orekit.utils.units.Unit;
25 import org.orekit.utils.units.UnitsCache;
26
27 /** Regular builder using XML elements names and content for tokens.
28 * <p>
29 * Each tag generates exactly one token, either a {@link TokenType#START START},
30 * or {@link TokenType#STOP STOP} token without content for non-leaf elements,
31 * or a {@link TokenType#ENTRY ENTRY} token with content for leaf elements.
32 * </p>
33 * @author Luc Maisonobe
34 * @since 11.0
35 */
36 public class RegularXmlTokenBuilder implements XmlTokenBuilder {
37
38 /** Attribute name for units. */
39 private static final String UNITS = "units";
40
41 /** Cache for parsed units. */
42 private final UnitsCache cache;
43
44 /** Simple constructor.
45 */
46 public RegularXmlTokenBuilder() {
47 this.cache = new UnitsCache();
48 }
49
50 /** {@inheritDoc} */
51 @Override
52 public List<ParseToken> buildTokens(final boolean startTag, final boolean isLeaf, final String qName,
53 final String content, final Map<String, String> attributes,
54 final int lineNumber, final String fileName) {
55
56 if (startTag) {
57 return Collections.singletonList(new ParseToken(TokenType.START, qName, content, Unit.NONE, lineNumber, fileName));
58 } else {
59 final List<ParseToken> built = new ArrayList<>(2);
60 if (isLeaf) {
61 // get units
62 final Unit units = cache.getUnits(attributes.get(UNITS));
63 built.add(new ParseToken(TokenType.ENTRY, qName, content, units, lineNumber, fileName));
64 }
65 built.add(new ParseToken(TokenType.STOP, qName, null, Unit.NONE, lineNumber, fileName));
66 return built;
67 }
68
69 }
70
71 }