SimpleTimeStampedTableParser.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.data;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.Pattern;

  26. import org.hipparchus.exception.DummyLocalizable;
  27. import org.orekit.errors.OrekitException;
  28. import org.orekit.errors.OrekitMessages;
  29. import org.orekit.time.TimeStamped;

  30. /**
  31.  * Parser for simple tables containing {@link TimeStamped time stamped} data.
  32.  * @param <T> the type of time stamped data (i.e. parsed table rows)
  33.  * @author Luc Maisonobe
  34.  * @since 6.1
  35.  */
  36. public class SimpleTimeStampedTableParser<T extends TimeStamped> {

  37.     /** Interface for converting a table row into time-stamped data.
  38.      * @param <S> the type of time stamped data (i.e. parsed table rows)
  39.      */
  40.     public interface RowConverter<S extends TimeStamped> {

  41.         /** Convert a row.
  42.          * @param rawFields raw row fields, as read from the file
  43.          * @return converted row
  44.          */
  45.         S convert(double[] rawFields);
  46.     }

  47.     /** Pattern for fields with real type. */
  48.     private static final String  REAL_TYPE_PATTERN =
  49.             "[-+]?(?:(?:\\p{Digit}+(?:\\.\\p{Digit}*)?)|(?:\\.\\p{Digit}+))(?:[eE][-+]?\\p{Digit}+)?";

  50.     /** Number of columns. */
  51.     private final int columns;

  52.     /** Converter for rows. */
  53.     private final RowConverter<T> converter;

  54.     /** Simple constructor.
  55.      * @param columns number of columns
  56.      * @param converter converter for rows
  57.      */
  58.     public SimpleTimeStampedTableParser(final int columns, final RowConverter<T> converter) {
  59.         this.columns   = columns;
  60.         this.converter = converter;
  61.     }

  62.     /** Parse a stream.
  63.      * @param stream stream containing the table
  64.      * @param name name of the resource file (for error messages only)
  65.      * @return parsed table
  66.      */
  67.     public List<T> parse(final InputStream stream, final String name) {

  68.         if (stream == null) {
  69.             throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_FILE, name);
  70.         }

  71.         // regular lines are simply a space separated list of numbers
  72.         final StringBuilder builder = new StringBuilder("^\\p{Space}*");
  73.         for (int i = 0; i < columns; ++i) {
  74.             builder.append("(");
  75.             builder.append(REAL_TYPE_PATTERN);
  76.             builder.append(")");
  77.             builder.append((i < columns - 1) ? "\\p{Space}+" : "\\p{Space}*$");
  78.         }
  79.         final Pattern regularLinePattern = Pattern.compile(builder.toString());

  80.         try {

  81.             // setup the reader
  82.             final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

  83.             final List<T> table = new ArrayList<T>();

  84.             for (String line = reader.readLine(); line != null; line = reader.readLine()) {

  85.                 // replace unicode minus sign ('−') by regular hyphen ('-') for parsing
  86.                 // such unicode characters occur in tables that are copy-pasted from PDF files
  87.                 line = line.replace('\u2212', '-');

  88.                 final Matcher regularMatcher = regularLinePattern.matcher(line);
  89.                 if (regularMatcher.matches()) {
  90.                     // we have found a regular data line

  91.                     final double[] rawFields = new double[columns];
  92.                     for (int i = 0; i < columns; ++i) {
  93.                         rawFields[i] = Double.parseDouble(regularMatcher.group(i + 1));
  94.                     }

  95.                     table.add(converter.convert(rawFields));

  96.                 }

  97.             }

  98.             if (table.isEmpty()) {
  99.                 throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_IERS_DATA_FILE, name);
  100.             }

  101.             return table;

  102.         } catch (IOException ioe) {
  103.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  104.         }

  105.     }

  106. }