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.                     // ignore all comment lines
  58.                     if (parseInfo.getLine().charAt(0) == '*') {
  59.                         continue;
  60.                     }

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

  75.                     // no candidate could parse this line
  76.                     throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  77.                                               parseInfo.getLineNumber(), parseInfo.getName(),
  78.                                               parseInfo.getLine());

  79.                 }

  80.             } catch (IOException ioe) {
  81.                 throw new OrekitException(ioe, LocalizedCoreFormats.SIMPLE_MESSAGE, ioe.getLocalizedMessage());
  82.             }

  83.         }

  84.         return parseInfo.build();

  85.     }

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

  90.     /** Build the container for parsing info.
  91.      * @return container for parsing info
  92.      */
  93.     protected abstract P buildParseInfo();

  94.     /** Get the time scales.
  95.      * @return time scales
  96.      */
  97.     public TimeScales getTimeScales() {
  98.         return timeScales;
  99.     }

  100. }