AbstractSinexParser.java

  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.sinex;

  18. import org.hipparchus.exception.LocalizedCoreFormats;
  19. import org.orekit.data.DataSource;
  20. import org.orekit.errors.OrekitException;
  21. import org.orekit.errors.OrekitMessages;
  22. import org.orekit.time.TimeScales;

  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.Reader;
  26. import java.util.Collections;

  27. /** Base parser for Solution INdependent EXchange (SINEX) files.
  28.  * @param <T> type of the SINEX file
  29.  * @param <P> type of the SINEX files parse info
  30.  * @author Luc Maisonobe
  31.  * @since 13.0
  32.  */
  33. public abstract class AbstractSinexParser<T extends AbstractSinex, P extends ParseInfo<T>> {

  34.     /** Time scales. */
  35.     private final TimeScales timeScales;

  36.     /** Simple constructor.
  37.      * @param timeScales time scales
  38.      */
  39.     protected AbstractSinexParser(final TimeScales timeScales) {
  40.         this.timeScales = timeScales;
  41.     }

  42.     /** Parse one or more SINEX files.
  43.      * @param sources sources providing the data to parse
  44.      * @return parsed file combining all sources
  45.      */
  46.     public T parse(final DataSource... sources) {

  47.         // placeholders for parsed data
  48.         final P parseInfo = buildParseInfo();

  49.         for (final DataSource source : sources) {

  50.             // start parsing a new file
  51.             parseInfo.newSource(source.getName());
  52.             Iterable<LineParser<P>> candidateParsers = Collections.singleton(firstLineParser());

  53.             try (Reader reader = source.getOpener().openReaderOnce(); BufferedReader br = new BufferedReader(reader)) {
  54.                 nextLine:
  55.                 for (parseInfo.setLine(br.readLine()); parseInfo.getLine() != null; parseInfo.setLine(br.readLine())) {
  56.                     parseInfo.incrementLineNumber();

  57.                     if (!parseInfo.getLine().isEmpty()) {

  58.                         // ignore all comment lines
  59.                         if (parseInfo.getLine().charAt(0) == '*') {
  60.                             continue;
  61.                         }

  62.                         // find a parser for the current line among the available candidates
  63.                         for (final LineParser<P> candidate : candidateParsers) {
  64.                             try {
  65.                                 if (candidate.parseIfRecognized(parseInfo)) {
  66.                                     // candidate successfully parsed the line
  67.                                     candidateParsers = candidate.allowedNextParsers(parseInfo);
  68.                                     continue nextLine;
  69.                                 }
  70.                             }
  71.                             catch (StringIndexOutOfBoundsException | NumberFormatException e) {
  72.                                 throw new OrekitException(e, OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  73.                                                           parseInfo.getLineNumber(), parseInfo.getName(),
  74.                                                           parseInfo.getLine());
  75.                             }
  76.                         }

  77.                         // no candidate could parse this line
  78.                         throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  79.                                                   parseInfo.getLineNumber(), parseInfo.getName(),
  80.                                                   parseInfo.getLine());
  81.                     }

  82.                 }

  83.             } catch (IOException ioe) {
  84.                 throw new OrekitException(ioe, LocalizedCoreFormats.SIMPLE_MESSAGE, ioe.getLocalizedMessage());
  85.             }

  86.         }

  87.         return parseInfo.build();

  88.     }

  89.     /** Get parser for the first line.
  90.      * @return parser for the firsty line of the file
  91.      */
  92.     protected abstract LineParser<P> firstLineParser();

  93.     /** Build the container for parsing info.
  94.      * @return container for parsing info
  95.      */
  96.     protected abstract P buildParseInfo();

  97.     /** Get the time scales.
  98.      * @return time scales
  99.      */
  100.     public TimeScales getTimeScales() {
  101.         return timeScales;
  102.     }

  103. }