1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.ediknight.edifact.setup;
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.UnsupportedEncodingException;
27 import java.util.Iterator;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 import net.sf.ediknight.common.edi.directory.DirectoryImpl;
32
33
34 /***
35 * This class reads an input stream line by line.
36 *
37 * @author Holger Joest
38 */
39 public abstract class LineScanner {
40
41 /*** The current directory. */
42 private DirectoryImpl directory;
43
44 /*** An array of patterns to match. */
45 private Pattern[] patterns;
46
47 /*** The analyzer context. */
48 private AnalyzerContext context;
49
50 /*** */
51 private boolean verbose;
52
53
54 /***
55 * Creates a new line scanner.
56 */
57 protected LineScanner() {
58 }
59
60
61 /***
62 * Returns the regular expression patterns.
63 *
64 * @return the patterns
65 */
66 public final Pattern[] getPatterns() {
67 return patterns;
68 }
69
70
71 /***
72 * Sets the regular expression patterns.
73 * @param patterns the patterns
74 */
75 public final void setPatterns(Pattern[] patterns) {
76 this.patterns = patterns;
77 }
78
79
80 /***
81 * @return the analyzer context
82 */
83 public final AnalyzerContext getContext() {
84 return context;
85 }
86
87
88 /***
89 * @param context the analyzer context to set
90 */
91 public final void setContext(AnalyzerContext context) {
92 this.context = context;
93 }
94
95
96 /***
97 * @param name the name
98 * @return a string iterator
99 */
100 public Iterator<String> getIterator(String name) {
101 return null;
102 }
103
104
105 /***
106 * @return true if verbose
107 */
108 public final boolean isVerbose() {
109 return verbose;
110 }
111
112
113 /***
114 * @param verbose true if verbose
115 */
116 public final void setVerbose(boolean verbose) {
117 this.verbose = verbose;
118 }
119
120
121 /***
122 * @return the directory
123 */
124 public final DirectoryImpl getDirectory() {
125 return directory;
126 }
127
128
129 /***
130 * @param directory the directory to set
131 */
132 public final void setDirectory(DirectoryImpl directory) {
133 this.directory = directory;
134 }
135
136
137 /***
138 * Read all lines of an input stream.
139 *
140 * @param istream an input stream
141 * @throws UnsupportedEncodingException if the character
142 * encoding is not supported
143 * @throws IOException if an I/O error occurs
144 */
145 public final void scan(InputStream istream)
146 throws UnsupportedEncodingException, IOException {
147 BufferedReader reader =
148 new BufferedReader(
149 new InputStreamReader(istream, "ISO-8859-1"));
150 try {
151 String line = null;
152 while ((line = reader.readLine()) != null) {
153 nextLine(line);
154 }
155 } finally {
156 reader.close();
157 }
158 }
159
160
161 /***
162 * @param n the number of the matched expression
163 * @param groups the matched expression groups
164 */
165 protected abstract void take(int n, String[] groups);
166
167
168 /***
169 * Process the next input line.
170 *
171 * @param line the next line
172 */
173 private void nextLine(String line) {
174 if (isVerbose()) {
175 System.out.println(line);
176 }
177 for (int n = 0; n < patterns.length; ++n) {
178 Matcher matcher = patterns[n].matcher(line);
179 if (matcher.find()) {
180 String[] groups = new String[matcher.groupCount()];
181 for (int k = 0; k < groups.length; ++k) {
182 groups[k] = matcher.group(k + 1).trim();
183 }
184 if (isVerbose()) {
185 System.out.print(n + ": ");
186 for (int k = 0; k < groups.length; ++k) {
187 System.out.print("[" + groups[k] + "]");
188 }
189 System.out.println();
190 }
191 take(n, groups);
192 break;
193 }
194 }
195 }
196
197 }
198