RegularXmlTokenBuilder.java

  1. /* Copyright 2002-2022 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.Collections;
  19. import java.util.List;

  20. import org.orekit.utils.units.Unit;
  21. import org.orekit.utils.units.UnitsCache;
  22. import org.xml.sax.Attributes;

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

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

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

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

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

  47.         // elaborate the token type
  48.         final TokenType tokenType = (content == null) ?
  49.                                     (startTag ? TokenType.START : TokenType.STOP) :
  50.                                     TokenType.ENTRY;

  51.         // get units
  52.         final Unit units = cache.getUnits(attributes.getValue(UNITS));

  53.         // final build
  54.         final ParseToken token = new ParseToken(tokenType, qName, content, units, lineNumber, fileName);

  55.         return Collections.singletonList(token);

  56.     }

  57. }