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