1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.files.sinex;
18
19 import org.hipparchus.exception.LocalizedCoreFormats;
20 import org.orekit.data.DataSource;
21 import org.orekit.errors.OrekitException;
22 import org.orekit.errors.OrekitMessages;
23 import org.orekit.time.TimeScales;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.Reader;
28 import java.util.Collections;
29
30
31
32
33
34
35
36 public abstract class AbstractSinexParser<T extends AbstractSinex, P extends ParseInfo<T>> {
37
38
39 private final TimeScales timeScales;
40
41
42
43
44 protected AbstractSinexParser(final TimeScales timeScales) {
45 this.timeScales = timeScales;
46 }
47
48
49
50
51
52 public T parse(final DataSource... sources) {
53
54
55 final P parseInfo = buildParseInfo();
56
57 for (final DataSource source : sources) {
58
59
60 parseInfo.newSource(source.getName());
61 Iterable<LineParser<P>> candidateParsers = Collections.singleton(firstLineParser());
62
63 try (Reader reader = source.getOpener().openReaderOnce(); BufferedReader br = new BufferedReader(reader)) {
64 nextLine:
65 for (parseInfo.setLine(br.readLine()); parseInfo.getLine() != null; parseInfo.setLine(br.readLine())) {
66 parseInfo.incrementLineNumber();
67
68
69 if (parseInfo.getLine().charAt(0) == '*') {
70 continue;
71 }
72
73
74 for (final LineParser<P> candidate : candidateParsers) {
75 try {
76 if (candidate.parseIfRecognized(parseInfo)) {
77
78 candidateParsers = candidate.allowedNextParsers(parseInfo);
79 continue nextLine;
80 }
81 } catch (StringIndexOutOfBoundsException | NumberFormatException e) {
82 throw new OrekitException(e, OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
83 parseInfo.getLineNumber(), parseInfo.getName(),
84 parseInfo.getLine());
85 }
86 }
87
88
89 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
90 parseInfo.getLineNumber(), parseInfo.getName(),
91 parseInfo.getLine());
92
93 }
94
95 } catch (IOException ioe) {
96 throw new OrekitException(ioe, LocalizedCoreFormats.SIMPLE_MESSAGE, ioe.getLocalizedMessage());
97 }
98
99 }
100
101 return parseInfo.build();
102
103 }
104
105
106
107
108 protected abstract LineParser<P> firstLineParser();
109
110
111
112
113 protected abstract P buildParseInfo();
114
115
116
117
118 public TimeScales getTimeScales() {
119 return timeScales;
120 }
121
122 }