Record.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.gnss.metric.ntrip;

  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.regex.Pattern;

  21. /** Record in source table.
  22.  * @author Luc Maisonobe
  23.  * @since 11.0
  24.  */
  25. public abstract class Record {

  26.     /** Pattern for delimiting fields. */
  27.     private static final Pattern SEPARATOR = Pattern.compile(";");

  28.     /** Quoting character. */
  29.     private static final String QUOTE = "\"";

  30.     /** Fields from the parsed sourcetable. */
  31.     private final List<String> fields;

  32.     /** Build a record by parsing a source table line.
  33.      * @param line source table line
  34.      */
  35.     protected Record(final String line) {


  36.         // split the line, taking care of possible quoted separators ";"
  37.         final String[] chunks = SEPARATOR.split(line);

  38.         // prepare storage
  39.         fields = new ArrayList<>(chunks.length);

  40.         // distribute all fields, taking care of possible quoted separators ";"
  41.         for (int i = 0; i < chunks.length; ++i) {
  42.             if (i > 0 && chunks[i - 1].endsWith(QUOTE) &&
  43.                 i < chunks.length && chunks[i].startsWith(QUOTE)) {
  44.                 // chunks i-1 and i belong to the same field, with an embedded quoted separator
  45.                 final String before = fields.remove(fields.size() - 1);
  46.                 fields.add(before.substring(0, before.length() - 1) +
  47.                            SEPARATOR +
  48.                            chunks[i].substring(1));
  49.             } else {
  50.                 fields.add(chunks[i]);
  51.             }
  52.         }

  53.     }

  54.     /** Get the type of record.
  55.      * @return type of record
  56.      */
  57.     public abstract RecordType getRecordType();

  58.     /** Get the number of fields.
  59.      * @return number of fields
  60.      */
  61.     protected int getFieldsNumber() {
  62.         return fields.size();
  63.     }

  64.     /** Get one field from the parsed sourcetable.
  65.      * @param index field index
  66.      * @return field value
  67.      */
  68.     protected String getField(final int index) {
  69.         return fields.get(index);
  70.     }

  71.     /** Get miscellaneous information.
  72.      * @return miscellaneous information
  73.      */
  74.     public String getMisc() {
  75.         return getField(getFieldsNumber() - 1);
  76.     }

  77. }