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.gnss.metric.ntrip;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.regex.Pattern;
22  
23  /** Record in source table.
24   * @author Luc Maisonobe
25   * @since 11.0
26   */
27  public abstract class Record {
28  
29      /** Pattern for delimiting fields. */
30      private static final Pattern SEPARATOR = Pattern.compile(";");
31  
32      /** Quoting character. */
33      private static final String QUOTE = "\"";
34  
35      /** Fields from the parsed sourcetable. */
36      private final List<String> fields;
37  
38      /** Build a record by parsing a source table line.
39       * @param line source table line
40       */
41      protected Record(final String line) {
42  
43  
44          // split the line, taking care of possible quoted separators ";"
45          final String[] chunks = SEPARATOR.split(line);
46  
47          // prepare storage
48          fields = new ArrayList<>(chunks.length);
49  
50          // distribute all fields, taking care of possible quoted separators ";"
51          for (int i = 0; i < chunks.length; ++i) {
52              if (i > 0 && chunks[i - 1].endsWith(QUOTE) &&
53                  i < chunks.length && chunks[i].startsWith(QUOTE)) {
54                  // chunks i-1 and i belong to the same field, with an embedded quoted separator
55                  final String before = fields.remove(fields.size() - 1);
56                  fields.add(before.substring(0, before.length() - 1) +
57                             SEPARATOR +
58                             chunks[i].substring(1));
59              } else {
60                  fields.add(chunks[i]);
61              }
62          }
63  
64      }
65  
66      /** Get the type of record.
67       * @return type of record
68       */
69      public abstract RecordType getRecordType();
70  
71      /** Get the number of fields.
72       * @return number of fields
73       */
74      protected int getFieldsNumber() {
75          return fields.size();
76      }
77  
78      /** Get one field from the parsed sourcetable.
79       * @param index field index
80       * @return field value
81       */
82      protected String getField(final int index) {
83          return fields.get(index);
84      }
85  
86      /** Get miscellaneous information.
87       * @return miscellaneous information
88       */
89      public String getMisc() {
90          return getField(getFieldsNumber() - 1);
91      }
92  
93  }