RegularXmlTokenBuilder.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.utils.units.Unit;
  23. import org.orekit.utils.units.UnitsCache;

  24. /** Regular builder using XML elements names and content for tokens.
  25.  * <p>
  26.  * Each tag generates exactly one token, either a {@link TokenType#START START},
  27.  * or {@link TokenType#STOP STOP} token without content for non-leaf elements,
  28.  * or a {@link TokenType#ENTRY ENTRY} token with content for leaf elements.
  29.  * </p>
  30.  * @author Luc Maisonobe
  31.  * @since 11.0
  32.  */
  33. public class RegularXmlTokenBuilder implements XmlTokenBuilder {

  34.     /** Attribute name for units. */
  35.     private static final String UNITS = "units";

  36.     /** Cache for parsed units. */
  37.     private final UnitsCache cache;

  38.     /** Simple constructor.
  39.      */
  40.     public RegularXmlTokenBuilder() {
  41.         this.cache = new UnitsCache();
  42.     }

  43.     /** {@inheritDoc} */
  44.     @Override
  45.     public List<ParseToken> buildTokens(final boolean startTag, final boolean isLeaf, final String qName,
  46.                                         final String content, final Map<String, String> attributes,
  47.                                         final int lineNumber, final String fileName) {

  48.         if (startTag) {
  49.             return Collections.singletonList(new ParseToken(TokenType.START, qName, content, Unit.NONE, lineNumber, fileName));
  50.         } else {
  51.             final List<ParseToken> built = new ArrayList<>(2);
  52.             if (isLeaf) {
  53.                 // get units
  54.                 final Unit units = cache.getUnits(attributes.get(UNITS));
  55.                 built.add(new ParseToken(TokenType.ENTRY, qName, content, units, lineNumber, fileName));
  56.             }
  57.             built.add(new ParseToken(TokenType.STOP, qName, null, Unit.NONE, lineNumber, fileName));
  58.             return built;
  59.         }

  60.     }

  61. }