1 package org.metricshub.jawk.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.io.InputStream;
26 import java.io.PrintStream;
27 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Locale;
32 import java.util.Map;
33 import java.util.Objects;
34
35
36
37
38
39
40
41
42
43 public class AwkSettings {
44
45
46
47
48 public static final AwkSettings DEFAULT_SETTINGS = new ImmutableAwkSettings();
49
50
51
52
53
54 private InputStream input = System.in;
55
56
57
58
59
60
61
62 private Map<String, Object> variables = new HashMap<String, Object>();
63
64
65
66
67
68
69 private List<String> nameValueOrFileNames = new ArrayList<String>();
70
71
72
73
74
75 private String fieldSeparator = null;
76
77
78
79
80
81 private boolean useSortedArrayKeys = false;
82
83
84
85
86
87
88 private boolean catchIllegalFormatExceptions = true;
89
90
91
92
93
94
95 private PrintStream outputStream = System.out;
96
97
98
99
100
101 private Locale locale = Locale.US;
102
103
104
105
106 private String defaultRS = System.getProperty("line.separator", "\n");
107
108
109
110
111 private String defaultORS = System.getProperty("line.separator", "\n");
112
113
114
115
116
117
118
119
120 public String toDescriptionString() {
121 StringBuilder desc = new StringBuilder();
122
123 final char newLine = '\n';
124
125 desc.append("variables = ").append(getVariables()).append(newLine);
126 desc.append("nameValueOrFileNames = ").append(getNameValueOrFileNames()).append(newLine);
127 desc.append("fieldSeparator = ").append(getFieldSeparator()).append(newLine);
128 desc.append("useSortedArrayKeys = ").append(isUseSortedArrayKeys()).append(newLine);
129 desc.append("catchIllegalFormatExceptions = ").append(isCatchIllegalFormatExceptions()).append(newLine);
130
131 return desc.toString();
132 }
133
134
135
136
137
138
139
140
141
142 public String toExtensionDescription() {
143 StringBuilder extensions = new StringBuilder();
144
145 if (isUseSortedArrayKeys()) {
146 extensions.append(", associative array keys are sorted");
147 }
148 if (isCatchIllegalFormatExceptions()) {
149 extensions.append(", IllegalFormatExceptions NOT trapped");
150 }
151 if (extensions.length() > 0) {
152 return "{extensions: " + extensions.substring(2) + "}";
153 } else {
154 return "{no compiled extensions utilized}";
155 }
156 }
157
158 @SuppressWarnings("unused")
159 private void addInitialVariable(String keyValue) {
160 int equalsIdx = keyValue.indexOf('=');
161 assert equalsIdx >= 0;
162 String name = keyValue.substring(0, equalsIdx);
163 String valueString = keyValue.substring(equalsIdx + 1);
164 Object value;
165
166 try {
167 value = Integer.parseInt(valueString);
168 } catch (NumberFormatException nfe) {
169 try {
170 value = Double.parseDouble(valueString);
171 } catch (NumberFormatException nfe2) {
172 value = valueString;
173 }
174 }
175
176 putVariable(name, value);
177 }
178
179
180
181
182
183
184
185 public InputStream getInput() {
186 return input;
187 }
188
189
190
191
192
193
194
195 public void setInput(InputStream input) {
196 this.input = Objects.requireNonNull(input, "input");
197 }
198
199
200
201
202
203
204
205
206
207 public Map<String, Object> getVariables() {
208 return new HashMap<String, Object>(variables);
209 }
210
211
212
213
214
215
216
217
218
219 public void setVariables(Map<String, Object> variables) {
220 this.variables = new HashMap<String, Object>(variables);
221 }
222
223
224
225
226
227
228
229 public void putVariable(String name, Object value) {
230 variables.put(name, value);
231 }
232
233
234
235
236
237
238
239
240 public List<String> getNameValueOrFileNames() {
241 return new ArrayList<String>(nameValueOrFileNames);
242 }
243
244
245
246
247
248
249 public void addNameValueOrFileName(String entry) {
250 nameValueOrFileNames.add(entry);
251 }
252
253
254
255
256
257
258
259 public String getFieldSeparator() {
260 return fieldSeparator;
261 }
262
263
264
265
266
267
268
269 public void setFieldSeparator(String fieldSeparator) {
270 this.fieldSeparator = fieldSeparator;
271 }
272
273
274
275
276
277
278
279 public boolean isUseSortedArrayKeys() {
280 return useSortedArrayKeys;
281 }
282
283
284
285
286
287
288
289 public void setUseSortedArrayKeys(boolean useSortedArrayKeys) {
290 this.useSortedArrayKeys = useSortedArrayKeys;
291 }
292
293
294
295
296
297
298
299
300 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "OutputStream reference is intentionally shared so callers can control output.")
301 public PrintStream getOutputStream() {
302 return outputStream;
303 }
304
305
306
307
308
309
310 public void setOutputStream(PrintStream pOutputStream) {
311 outputStream = Objects.requireNonNull(pOutputStream, "outputStream");
312 }
313
314
315
316
317
318
319
320
321 public boolean isCatchIllegalFormatExceptions() {
322 return catchIllegalFormatExceptions;
323 }
324
325
326
327
328
329
330
331
332 public void setCatchIllegalFormatExceptions(boolean catchIllegalFormatExceptions) {
333 this.catchIllegalFormatExceptions = catchIllegalFormatExceptions;
334 }
335
336
337
338
339
340
341
342
343 public Locale getLocale() {
344 return locale;
345 }
346
347
348
349
350
351
352 public void setLocale(Locale pLocale) {
353 locale = pLocale;
354 }
355
356
357
358
359
360
361
362
363 public String getDefaultRS() {
364 return defaultRS;
365 }
366
367
368
369
370
371
372 public void setDefaultRS(String rs) {
373 defaultRS = Objects.requireNonNull(rs, "defaultRS");
374 }
375
376
377
378
379
380
381
382
383 public String getDefaultORS() {
384 return defaultORS;
385 }
386
387
388
389
390
391
392 public void setDefaultORS(String ors) {
393 defaultORS = Objects.requireNonNull(ors, "defaultORS");
394 }
395
396 private static final class ImmutableAwkSettings extends AwkSettings {
397
398 private ImmutableAwkSettings() {
399 super();
400 }
401
402 @Override
403 public void setInput(InputStream input) {
404 throw unsupported();
405 }
406
407 @Override
408 public void setVariables(Map<String, Object> variables) {
409 throw unsupported();
410 }
411
412 @Override
413 public void putVariable(String name, Object value) {
414 throw unsupported();
415 }
416
417 @Override
418 public void addNameValueOrFileName(String entry) {
419 throw unsupported();
420 }
421
422 @Override
423 public void setFieldSeparator(String fieldSeparator) {
424 throw unsupported();
425 }
426
427 @Override
428 public void setUseSortedArrayKeys(boolean useSortedArrayKeys) {
429 throw unsupported();
430 }
431
432 @Override
433 public void setOutputStream(PrintStream pOutputStream) {
434 throw unsupported();
435 }
436
437 @Override
438 public void setCatchIllegalFormatExceptions(boolean catchIllegalFormatExceptions) {
439 throw unsupported();
440 }
441
442 @Override
443 public void setLocale(Locale pLocale) {
444 throw unsupported();
445 }
446
447 @Override
448 public void setDefaultRS(String rs) {
449 throw unsupported();
450 }
451
452 @Override
453 public void setDefaultORS(String ors) {
454 throw unsupported();
455 }
456
457 private UnsupportedOperationException unsupported() {
458 return new UnsupportedOperationException("DEFAULT_SETTINGS is immutable");
459 }
460 }
461 }