-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseConfig.java
872 lines (698 loc) · 28.1 KB
/
ParseConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
/*
**
** Copyright (c) 2014 Bill Lanahan
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in all
** copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
**
*/
/**
* ParseConfig - reads the -c config and checks syntax of the various blocks
*
* Note: to the user a RULE block has an ID (-i id) in the code its ruleID
*/
package validate;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import validate.Validate.format;
/**
* ParseConfigFile - given the file associated with the "-c" switch
* this object reads and processes the several block types within the file.
* Most syntax errors are caught here and cause an exit and a message.
*
* @author Bill Lanahan
*
*/
class ParseConfig {
// 0=have not seen, 1=have seen but NOT seen end yet, 2=had seen and now have seen the end
private short haveOptionBlock = 0;
private short haveMacroBlock = 0;
private short haveRuleBlock = 0;
private short haveRegexBlock = 0;
private String fileName = null;
private short lineNumber = 0;
private String line;
private BufferedReader br;
// we do the patterns here so they can be reused without recompiling
private final Pattern blankAndCommentPattern = Pattern.compile( "^\\s*#|^\\s*$" );
private final Pattern optionPattern = Pattern.compile( "^\\s*(\\w+)(?:\\s*=\\s*)(.*)$" );
private Pattern lineNumberPrefixPattern;
private Matcher m = null;
private String[] tokens;
private boolean haveFirstRule = false;
private File inputFile;
Options ruleOption = null;
RegexBlock regexBlock = null;
Rule tmpRule = null;
//*********************************************************************************************************
// we'll instantiate Options even if no option block as other blocks may
// use the default data
static Options opt = new Options();
// build Macro object for built-in and may be more
static Macros macros = new Macros( Validate.macrosFile, opt.getMacroStartString(), opt.getMacroEndString() );
//***********************************************************************************************************
ParseConfig() {
System.out.println( "\nfatal: programming error ParseConfigFile needs a non-default constructor." );
System.exit( 12 );
}
ParseConfig( String configFileName ) {
setFileName( configFileName );
}
// the config file and pass off to other classes as needed
// this method reads the entire rules file and creates/calls other objects as needed for the
// 3 block types option|macro|rule
void parse() {
try {
lineNumberPrefixPattern = Pattern.compile( "^\\s*%(\\d+)%(\\d+)%(.*)$" );
} catch ( PatternSyntaxException e ) {
System.err.println( "\nerror: failed to compile lineNumberPrefixPattern, programming error \n" );
System.exit(55);
}
try {
inputFile = new File( fileName );
br = new BufferedReader( new FileReader( inputFile ) );
while ( true ) {
line = br.readLine();
if ( line == null ) {
checkEOFstate( );
return;
} else {
lineNumber++;
}
// need to swallow lines UNLESS we see a line that starts with "startBlock" (%%)
if ( ! line.matches( "^\\s*" + Validate.blkStart + "\\s*\\w+.*$" ) ) {
// swallow lines NOT within blocks
continue;
} else {
// this MUST follow test for blockStart since they are similar
m = Validate.blkEndPattern.matcher( line );
if ( m.find() ) {
System.out.println( "\nfatal: blkEnd was found on line <" + lineNumber + "> before any block was begun." );
System.exit( 12 );
}
}
// ok now we need to see which block we are in
line = line.trim().replaceFirst("^\\s*" + Validate.blkStart + "\\s*", "");
tokens = line.split( "\\s+" );
// we know 0 is the blkStart string
if ( (tokens.length > 0) && tokens[0].toUpperCase().matches("OPTIONS?")) {
if ( haveOptionBlock != 0 ) {
System.out.println( "\nfatal: a second OPTION block was found on line <" + lineNumber + ">." );
System.exit( 12 );
}
if ( (haveMacroBlock > 0) || (haveRuleBlock > 0) ) {
System.out.println( "\nfatal: the OPTION block on line <" + lineNumber + "> must preceed the MACRO and RULE blocks." );
System.exit( 12 );
}
doParseOPTIONS();
} else if ( (tokens.length == 1) && tokens[0].toUpperCase().matches("MACROS?") ) {
doParseMACROS();
} else if ( (tokens.length == 2) && tokens[0].toUpperCase().matches("RULES?") ) {
doParseRULES( tokens[1] );
} else {
System.err.println( "\nfatal: invalid format on line <" + lineNumber + "> of config file <" + fileName + ">. line=" +line);
System.exit( 12 );
}
}
} catch (final IOException e) {
System.err.println( "\nfatal: can't read config file <" + fileName + ">." );
System.exit( 12 );
} finally {
br = null;
inputFile = null;
}
}
void setFileName ( String fileName ) {
assert fileName != null;
this.fileName = fileName;
}
void doParseOPTIONS() {
haveOptionBlock = 1;
// read until endBlk
try {
while ( true ) {
line = br.readLine();
if ( line == null ) {
checkEOFstate();
break;
} else {
lineNumber++;
}
// skip BLANKS and COMMENTS
m = blankAndCommentPattern.matcher( line );
if ( m.find() ) {
continue;
}
// is it the endBlk - so we check the OPTIONS now that we are done
m = Validate.blkEndPattern.matcher( line );
if ( m.find() ) {
doOptionsCheck();
haveOptionBlock = 2;
return;
}
// here we need check and set each variable
// we'll reduce and tokenize each line and send it to the OPTIONS class for processing
m = optionPattern.matcher( line );
if ( m.find() ) {
opt.optNameValue( m.group( 1 ), m.group( 2 ), lineNumber );
continue;
} else {
if ( opt.getWarnInvalidOption() ) {
System.out.println( "\nwarning: invalid OPTION format on line <" + lineNumber + ">" );
}
continue;
}
}
} catch ( final IOException e ) {
System.err.println( "\nfatal: problem reading config file <" + fileName + "> " + e.getMessage() );
System.exit( 12 );
}
}
/*
* options have all been read - so their format was checked but they
* may have conflicts with other options - this will be checked here
*/
void doOptionsCheck() {
// need to call any time options have been changed
// we have some inconsistent values that could not be tested on the fly
if ( opt.getMacroStartString().equals( opt.getMacroEndString() ) ) {
System.err.println( "\nerror: OPTION <macroStartString> cannot be the same as <macroEndString>." );
System.exit( 12 );
}
if ( opt.getRegexEndBlockRegex().equals( opt.getCommentRegex() ) ) {
System.err.println( "\nerror: OPTION <regexEndBlockRegex> cannot be the same as <regexCommentExpression>." );
System.exit( 12 );
}
if ( Validate.blkEnd.equals( opt.getCommentRegex() ) ) {
System.err.println( "\nfatal: OPTION <commentRegex> cannot be the same as command line option <blkEnd>." );
System.exit( 12 );
}
if ( Validate.blkStart.equals( opt.getCommentRegex() ) ) {
System.err.println( "\nfatal: OPTION <commenrRegex> cannot be the same as command line option <blkStart>." );
System.exit( 12 );
}
if ( Validate.blkEnd.equals( opt.getCommentRegex() ) ) {
System.err.println( "\nfatal: OPTION <commentRegex> cannot be the same as command line option <blkEnd>." );
System.exit( 12 );
}
}
/*
* reads an internal macro block or macro file
*/
void doParseMACROS() {
haveMacroBlock = 1;
// read until endBlk
try {
String tmpline = null;
boolean continuationState = false;
short startLine = 0; //need to save the line that a multiline started on
boolean startedContinuation = false;
while ( true ) {
line = br.readLine();
if ( line == null ) {
checkEOFstate();
break;
} else {
lineNumber++;
}
// skip BLANKS and COMMENTS
m = blankAndCommentPattern.matcher( line );
if ( m.find() ) {
continue;
}
// is it the endBlk - so we check the OPTIONS now that we are done
m = Validate.blkEndPattern.matcher( line );
if ( m.find() ) {
haveMacroBlock = 2;
return;
}
// the Macros class will check line formatting
if ( line.endsWith("\\") && ! line.endsWith("\\\\") ) {
if ( startedContinuation == false ) {
startedContinuation = true;
startLine = lineNumber;
startLine--;
}
// its a continuation; need to adjust line and concatenate
if ( continuationState == false ) {
// swallow the \
continuationState = true;
tmpline = line.substring(0, line.length() - 1 ); // trims the backslash
continue;
} else {
// not the final line, so append and read more
line = line.trim(); // swallows the start of line WS
line = line.substring(0, line.length() - 1); // trims the backslash
tmpline += line;
continue;
}
} else {
if ( continuationState ) {
// the final line now, so trim and use it
line = line.trim();
tmpline += line;
continuationState = false; // reset
line = tmpline;
}
}
if ( tmpline == null ) {
Macros.addSingleMacro( line, lineNumber, opt );
} else {
Macros.addSingleMacro( tmpline, startLine, opt );
tmpline = null;
}
continuationState = false; // reset
}
} catch ( final IOException e ) {
System.err.println( "\nfatal: problem reading config file <" + fileName + "> " + e.getMessage() );
System.exit( 12 );
}
}
/*
* %% RULES has been seen
* this method deals will all lines AFTER the title line
* doParseRULES - since a %% RULES title was encountered, we are now in a RULES block
* we read this block until the blkEnd (default %%) line. This block can set/reset a few OPTIONS; it
* has an ID for association with the command line ID and may have a list of files it is meant to parse.
* @param String ruleID is a \\w string
*/
void doParseRULES( String ruleID ) {
haveRuleBlock = 1;
final Pattern nameValuePattern = Pattern.compile( "^NameValue(?:Pair)?$|^NVP?$", Pattern.CASE_INSENSITIVE );
final Pattern delimitedValuePattern = Pattern.compile( "^Delimited$|^Fields?$|^[DF]$", Pattern.CASE_INSENSITIVE );
final Pattern javaValuePattern = Pattern.compile( "^Java(?:Properties)?$|^JP?$", Pattern.CASE_INSENSITIVE );
final Pattern customValuePattern = Pattern.compile( "^Custom$|^C$", Pattern.CASE_INSENSITIVE );
final Pattern lineValuePattern = Pattern.compile( "^Lines?$|^L$", Pattern.CASE_INSENSITIVE );
final Pattern lineValueCmdLinePattern = Pattern.compile( "^ValueLines?$|^VL$", Pattern.CASE_INSENSITIVE );
final Pattern delimitedValueCmdLinePattern = Pattern.compile( "^ValueDelimited$|^ValueFields$|^V[DF]$", Pattern.CASE_INSENSITIVE );
tmpRule = null;
Matcher m;
if ( haveFirstRule == false ) {
haveFirstRule = true;
// special case if we have -M switch and have now read macros we can display
if ( Validate.showMacros ) {
Macros.showMacros();
System.exit( 0 );
}
Validate.ruleMap = new HashMap<String, Rule>(); // will save all Rule objects for run time
Validate.ruleList = new ArrayList<String>();
}
// check if id is in Map already
if ( ! Validate.ruleMap.containsKey( ruleID ) ) {
// then its not a previously "seen" ruleID so save it IF the format is good
// make sure ruleID is valid and non-existent
if ( ! ruleID.matches( "[a-zA-Z_0-9\\./\\$]+" ) ) {
System.err.println( "\nfatal: invalid format for the ID for the RULE block on line <" + lineNumber + "> of config file <" + fileName + ">.");
System.exit( 12 );
}
// later we will CLONE the OPTIONS object so that each RULE block can have its
// own private copy of OPTIONS
// clone the Options class and then we will over write options for
// local use by this Rule block
try {
ruleOption = (Options) opt.clone();
} catch ( final CloneNotSupportedException ex ) {
System.err.println( "Cloneable should be implemented." );
System.exit( 12 );
}
// this comes late because we had to finish OPTION block and only print it once.
// it DOES mean that warning may come out prior to the tool title
//
if ( Validate.ruleMap.isEmpty() && ruleOption.getShowToolTitle() == true ) {
System.out.println("\n\n\t\t\t\tValidate\n\t\t\t\t========\n\n");
}
// start the real RULE block parsing
// create the RULE object with its own OPTIONS
tmpRule = new Rule( ruleID, ruleOption, fileName );
if ( tmpRule != null ) {
Validate.ruleMap.put( ruleID, tmpRule );
Validate.ruleList.add( ruleID ); // so we can execute in order
} else {
System.out.println( "\nfatal: the ruleID <" + ruleID + "> on line <" + lineNumber + "> could not be placed in ruleMap " );
System.exit( 12 );
}
} else {
System.out.println( "\nfatal: the ruleID <" + ruleID + "> on line <" + lineNumber + "> is a duplicate. " );
System.exit( 12 );
}
// read until endBlk
// so options in the form of name=value found from here on go to the
// new CLONED object, for this RULE block only
try {
while ( true ) {
line = br.readLine();
if ( line == null ) {
// lots of thing to check here
checkEOFstate();
break;
} else {
lineNumber++;
}
// need to swallow lines UNLESS we see a line that starts with "endBlock" (%%)
// test for end or RULE or REGEX anything else that starts with endBLock is an error
m = Validate.blkEndPattern.matcher( line );
if ( m.find() ) {
// see if its a block end or a REGEX block start
line = line.trim().replaceFirst("^" + Validate.blkStart + "\\s*", "");
// all that is left is the TITLE + trailing junk (maybe)
tokens = line.split( "\\s+" );
// we know 0 is the blkStart string
if ( tokens.length == 1 && tokens[0].toUpperCase().matches("REGEX(ES)?")) {
if ( haveRegexBlock != 0 ) {
System.out.println( "\nfatal: a second REGEX block was found on line <" + lineNumber + "> for this RULE block." );
System.exit( 12 );
}
// this RULE block has a REGEX, so we need to parse it
parseRegexBlock( ruleOption, ruleID, lineNumber );
} else if ( line.length() == 0 ) {
// we reached the end
haveRuleBlock = 2;
} else {
System.err.println( "\nfatal: unknown/expected format line on line <" + lineNumber + ">.");
System.exit( 12 );
}
// endBlk we are done here
// if there was a REGEX block, it was already parsed
return;
}
// skip BLANKS or COMMENT lines
m = blankAndCommentPattern.matcher( line );
if ( m.find() ) {
continue;
}
// here we need check and set each variable
// need to check for options, files, type, regex block etc
/* what might we find in RULES?
* 1- options, as name=value
* 2- specific options/instructions for RULES (files, type)
* 3- a REGEX block start
* 4- and END block "\\s*%%
*/
// First we see if we have a RULE specific OPTION
// here we need check and set each variable
// we'll reduce and tokenize each line and send it to the OPTIONS class for processing
m = optionPattern.matcher( line );
if ( m.find() ) {
final String ruleOpt = m.group( 1 );
String ruleValue = m.group( 2 );
//
// FORMAT OPTION
//
if ( ruleOpt.equals( "format" ) ) {
ruleValue = Options.deQuote( ruleValue );
m = nameValuePattern.matcher( ruleValue );
if ( m.find() ) {
tmpRule.setFormat( format.NAME_VALUE );
continue;
}
m = customValuePattern.matcher( ruleValue );
if ( m.find() ) {
tmpRule.setFormat( format.CUSTOM );
continue;
}
m = delimitedValuePattern.matcher( ruleValue );
if ( m.find() ) {
tmpRule.setFormat( format.DELIMITED );
continue;
}
m = javaValuePattern.matcher( ruleValue );
if ( m.find() ) {
tmpRule.setFormat( format.JAVA );
continue;
}
m = lineValuePattern.matcher( ruleValue );
if ( m.find() ) {
tmpRule.setFormat( format.LINE );
continue;
}
m = lineValueCmdLinePattern.matcher( ruleValue );
if ( m.find() ) {
tmpRule.setFormat( format.VALUE_LINE );
continue;
}
m = delimitedValueCmdLinePattern.matcher( ruleValue );
if ( m.find() ) {
tmpRule.setFormat( format.VALUE_DELIMITED );
continue;
}
System.err.println( "\nfatal: unknown value <" + ruleValue + "> for <format> option on line <" + lineNumber + ">." );
System.exit( 12 );
} else if ( ruleOpt.equals( "file" ) ) {
//
// FILE OPTION
//
// 2 approaches for file: file=xxx, or file={\nxxx\nyyy}\n
//
if ( ruleValue.matches( "\\{$" ) ) {
// we assume this is the start of a block
///tmpRule.fileAdd( ruleValue );
while ( true ) {
line = br.readLine();
if ( line == null ) {
// lots of thing to check here
checkEOFstate();
break;
}
// skip comments within the file=definition
m = blankAndCommentPattern.matcher( line );
if ( m.find() ) {
lineNumber++;
continue;
}
// what's left we expect is a file name or the end of the block
if ( line.matches( "^\\s*}\\s*$" )) {
// end of block, swallow the end marker
lineNumber++;
break;
} else {
// its a file we think - note since we already ruled out null, any character is valid in linux
// we ignore the file option in the RULES if we got it on comadn line
if ( ! Validate.fileIsCmdLine) {
tmpRule.fileAdd( line.trim() );
}
lineNumber++;
continue;
}
} // end file reading while
} else {
// its a file we think - note since we already ruled out null, any character is valid in linux
// we ignore the file option in the RULES if we got it on comadn line
if ( ! Validate.fileIsCmdLine) {
// its a single file
// since linux will take any char except null and slash we don't need the old test any more
ruleValue = Options.deQuote( ruleValue );
tmpRule.fileAdd( ruleValue.trim() );
}
continue;
}
continue; // "file" done read more of config file
} else {
// looks like an option format so send it to Options class for evaluation
ruleOption.optNameValue( ruleOpt, ruleValue, lineNumber );
continue;
}
} // end of option checking
if ( line.matches("^\\s*" + Validate.blkStart + "\\s*\\w+.*") ) {
// possibly REGEX lets check
line = line.trim().replaceFirst("^" + Validate.blkStart + "\\s*", "");
tokens = line.split( "\\s+" );
// we know 0 is the blkStart string
if ( (tokens.length == 1) && tokens[0].toUpperCase().matches("REGEX(ES)?") ) {
if ( haveRegexBlock != 0 ) {
System.out.println( "\nfatal: a second REGEX block was found on line <" + lineNumber + ">." );
System.exit( 12 );
} else {
if ( tmpRule.getFormat() == null ) {
System.err.println( "\nerror: RULE block that ends on line <" + lineNumber + "> requires format=<option>.");
System.exit( 12 );
}
// we can now parse the regex line that are NOT comments
lineNumber = parseRegexBlock( ruleOption, ruleID, lineNumber );
return; // reached the end of REGEX and therefore RULE
}
} else {
System.err.println( "\nfatal: invalid block start line <" + line + "> in RULE block, line <" + lineNumber + ">.");
System.exit( 12 );
}
} else {
System.err.println( "\nfatal: invalid line <" + line.trim() + "> in RULE block, line <" + lineNumber + ">.");
System.exit( 12 );
}
} // while end
} catch (final IOException e) {
System.err.println( "\nfatal: problem reading config file: " + e.getMessage() );
System.exit( 12 );
}
} // end reads RULES block
//////////////////////////////////////////////////////////////////////////////////
////// REGEX BLOCK
/////////////////////////////////////////////////////////////////////////////////
/*
* we hit a REGEX title for this RULE block
*/
short parseRegexBlock( Options ruleOption, String ruleID, short lineNumber ) {
boolean haveRegexCommentExpression = false;
Pattern regexCommentPattern = null;
Pattern regexEndBlockPattern = null;
regexBlock = new RegexBlock( tmpRule, tmpRule.ruleFormat, ruleID, ruleOption.getDelimiterRegex(), ruleOption ); // scope only as long as parsing is done
// get the regexCommentExpression and regexEndBlockRegex
Matcher lnpMatcher = null;
try {
if ( ruleOption.getCommentRegex() != null ) {
regexCommentPattern = Pattern.compile( ruleOption.getCommentRegex() );
haveRegexCommentExpression = true;
}
if ( ruleOption.getRegexEndBlockRegex() != null ) {
regexEndBlockPattern = Pattern.compile( ruleOption.getRegexEndBlockRegex() );
} else {
regexEndBlockPattern = Pattern.compile( Validate.blkEnd );
}
// 2 var * 2 combination for line continuation
boolean continuationState = false;
String tmpline = null;
int fromLine = -1;
int toLine = -1;
while ( true ) {
line = br.readLine();
++lineNumber;
if ( line == null ) {
// lots of things to check here
checkEOFstate();
break;
}
// we only want this so we call the correct method in RegexBlock later
if ( tmpRule.getFormat() == format.LINE ) {
fromLine = toLine = 0;
// following just gets line number ranges IF they are given
lnpMatcher = lineNumberPrefixPattern.matcher( line );
if ( lnpMatcher.find() ) {
try {
fromLine = Integer.parseInt( lnpMatcher.group(1) );
toLine = Integer.parseInt( lnpMatcher.group(2) );
} catch ( NumberFormatException e ) {
System.err.println("\nerror, fromLine and toLine must be integers >= zero on line <" + lineNumber + ">");
System.exit(77);
}
line = lnpMatcher.group(3);
// need this because above does NOT catch negatives
if ( fromLine < 0 || toLine < 0 ) {
System.err.println("\nerror, fromLine and toLine must be integers >= zero on line <" + lineNumber + ">");
System.exit(77);
}
if ( toLine != 0 && toLine < fromLine ) {
System.err.println("\nerror, toLine must be greater >= fromLine unless toLine == 0 <" + lineNumber + ">");
System.exit(77);
}
}
}
if ( ruleOption.getRegexLineContinuation() ) {
if ( line.endsWith("\\") && ! line.endsWith("\\\\") ) {
// its a continuation; need to adjust line and concatenate
if ( continuationState == false ) {
// use line as is
// swallow the \
continuationState = true;
tmpline = line.substring(0, line.length() - 1 ); // trims the backslash
continue;
} else {
// got the final line
line = line.trim(); // swallows the start of line WS
line = line.substring(0, line.length() - 1); // trims the backslash
tmpline += line;//
continue; // read more
}
} else {
if ( continuationState ) {
// user wanted this feature, but input doesn't not qualify
line = line.trim();
tmpline += line;
continuationState = false; // reset
line = tmpline;
}
}
continuationState = false; // reset
}
// we are in REGEX it has a special COMMENT string to test for
if ( haveRegexCommentExpression ) {
m = regexCommentPattern.matcher( line );
if ( m.find() ) {
continue;
}
}
// make sure we didn't reach the end of the block
m = regexEndBlockPattern.matcher( line );
if ( m.find() ) {
haveRuleBlock = 2;
regexBlock.finalRegexStaticCheck( tmpRule, lineNumber );
regexBlock = null; // all data now in RULE or OPTIONS
checkEOFstate();
break;
}
// so its a regex line - we need to handle differently based on format type
// so we'll pass the line to REGEX and let it figure it out
if ( fromLine != -1 ) {
regexBlock.addRegexLine( line, lineNumber, ruleOption, fromLine, toLine );
} else {
regexBlock.addRegexLine( line, lineNumber, ruleOption, 0, 0 );
}
}
} catch (final IOException e) {
System.err.println( "\nfatal: problem reading config file <" + fileName + ">: " + e.getMessage() );
System.exit( 12 );
} catch ( final PatternSyntaxException pse ) {
System.err.println( "\nfatal: regex compile issue line <" + lineNumber + "> in config file <" + fileName + "> " + pse.getMessage() );
System.exit( 12 );
}
return lineNumber;
} // end of parseRegexBlock
/*
* checkEOFstate - this method is called when we hit EOF in the config file
* In this case we need to see if any block construct was properly ended vs
* we just fell off the end.
*/
void checkEOFstate( ) {
boolean exitFlag = false;
// we were reading the config file and its the end, need to see if the syntax was all done
if( haveOptionBlock == 1 ) {
exitFlag = true;
System.err.println( "\nfatal: the OPTION block was never ended in config file <" + fileName + ">." );
}
if( haveMacroBlock == 1 ) {
exitFlag = true;
System.err.println( "\nfatal: the MACRO block was never ended in config file <" + fileName + ">." );
}
if( haveRuleBlock == 1 ) {
exitFlag = true;
System.out.println( "\nfatal: the last RULE block was never ended in config file <" + fileName + ">." );
} else if ( haveRuleBlock == 0 ) {
exitFlag = true;
System.err.println( "\nfatal: at least one RULE block is required in config file <" + fileName + ">." );
}
if ( exitFlag ) {
System.exit( 12 );
}
return;
} // end checkEOFstate
}