1
1
package com .github .s4ke .moar .cli ;
2
2
3
3
import java .io .BufferedReader ;
4
+ import java .io .BufferedWriter ;
4
5
import java .io .File ;
5
6
import java .io .FileInputStream ;
7
+ import java .io .FileWriter ;
6
8
import java .io .IOException ;
7
9
import java .io .InputStreamReader ;
8
10
import java .util .ArrayList ;
9
11
import java .util .List ;
10
12
11
13
import com .github .s4ke .moar .MoaMatcher ;
12
14
import com .github .s4ke .moar .MoaPattern ;
15
+ import com .github .s4ke .moar .json .MoarJSONSerializer ;
13
16
import org .apache .commons .cli .CommandLine ;
14
17
import org .apache .commons .cli .CommandLineParser ;
15
18
import org .apache .commons .cli .DefaultParser ;
@@ -29,6 +32,9 @@ public static void main(String[] args) throws ParseException, IOException {
29
32
options .addOption ( "rf" , true , "regexFile" );
30
33
options .addOption ( "r" , true , "regex" );
31
34
35
+ options .addOption ( "mf" , true , "moaFile" );
36
+ options .addOption ( "mo" , true , "moaOutputFolder (overwrites if existent)" );
37
+
32
38
options .addOption ( "sf" , true , "stringFile" );
33
39
options .addOption ( "s" , true , "string" );
34
40
@@ -44,13 +50,15 @@ public static void main(String[] args) throws ParseException, IOException {
44
50
formatter .printHelp ( "moar-cli" , options );
45
51
}
46
52
53
+ List <String > patternNames = new ArrayList <>();
47
54
List <MoaPattern > patterns = new ArrayList <>();
48
55
List <String > stringsToCheck = new ArrayList <>();
49
56
50
57
if ( cmd .hasOption ( "r" ) ) {
51
58
String regexStr = cmd .getOptionValue ( "r" );
52
59
try {
53
60
patterns .add ( MoaPattern .compile ( regexStr ) );
61
+ patternNames .add ( regexStr );
54
62
}
55
63
catch (Exception e ) {
56
64
System .out .println ( e .getMessage () );
@@ -67,6 +75,7 @@ public static void main(String[] args) throws ParseException, IOException {
67
75
if ( emptyLineCountAfterRegex >= 1 ) {
68
76
if ( regexStr .length () > 0 ) {
69
77
patterns .add ( MoaPattern .compile ( regexStr .toString () ) );
78
+ patternNames .add (regexStr .toString ());
70
79
}
71
80
regexStr .setLength ( 0 );
72
81
emptyLineCountAfterRegex = 0 ;
@@ -83,6 +92,7 @@ public static void main(String[] args) throws ParseException, IOException {
83
92
if ( regexStr .length () > 0 ) {
84
93
try {
85
94
patterns .add ( MoaPattern .compile ( regexStr .toString () ) );
95
+ patternNames .add (regexStr .toString ());
86
96
}
87
97
catch (Exception e ) {
88
98
System .out .println ( e .getMessage () );
@@ -92,6 +102,28 @@ public static void main(String[] args) throws ParseException, IOException {
92
102
}
93
103
}
94
104
105
+ if ( cmd .hasOption ( "mf" ) ) {
106
+ String fileName = cmd .getOptionValue ( "mf" );
107
+ File file = new File ( fileName );
108
+ if ( file .isDirectory () ) {
109
+ System .out .println ( fileName + " is a directory, using all *.moar files as patterns" );
110
+ File [] moarFiles = file .listFiles (
111
+ pathname -> pathname .getName ().endsWith ( ".moar" )
112
+ );
113
+ for ( File moar : moarFiles ) {
114
+ String jsonString = readWholeFile ( moar );
115
+ patterns .add ( MoarJSONSerializer .fromJSON ( jsonString ) );
116
+ patternNames .add (moar .getAbsolutePath ());
117
+ }
118
+ }
119
+ else {
120
+ System .out .println ( fileName + " is a single file. using it directly (no check for *.moar suffix)" );
121
+ String jsonString = readWholeFile ( file );
122
+ patterns .add ( MoarJSONSerializer .fromJSON ( jsonString ) );
123
+ patternNames .add (fileName );
124
+ }
125
+ }
126
+
95
127
if ( cmd .hasOption ( "s" ) ) {
96
128
String str = cmd .getOptionValue ( "s" );
97
129
stringsToCheck .add ( str );
@@ -115,28 +147,56 @@ public static void main(String[] args) throws ParseException, IOException {
115
147
116
148
boolean multiLine = cmd .hasOption ( "m" );
117
149
118
- if ( stringsToCheck .size () == 0 ) {
119
- System .out .println ( "no strings to check" );
120
- return ;
121
- }
122
150
if ( patterns .size () == 0 ) {
123
151
System .out .println ( "no patterns to check" );
124
152
return ;
125
153
}
126
154
155
+ if ( cmd .hasOption ( "mo" ) ) {
156
+ String folder = cmd .getOptionValue ( "mo" );
157
+ File folderFile = new File ( folder );
158
+ if ( !folderFile .exists () ) {
159
+ System .out .println ( folder + " does not exist. creating..." );
160
+ if ( !folderFile .mkdirs () ) {
161
+ System .out .println ( "folder " + folder + " could not be created" );
162
+ }
163
+ }
164
+ int cnt = 0 ;
165
+ for ( MoaPattern pattern : patterns ) {
166
+ String patternAsJSON = MoarJSONSerializer .toJSON ( pattern );
167
+ try (BufferedWriter writer = new BufferedWriter (
168
+ new FileWriter (
169
+ new File (
170
+ folderFile ,
171
+ "pattern" + ++cnt + ".moar"
172
+ )
173
+ )
174
+ )) {
175
+ writer .write ( patternAsJSON );
176
+ }
177
+ }
178
+ System .out .println ( "stored " + cnt + " patterns in " + folder );
179
+ }
180
+
181
+ if ( stringsToCheck .size () == 0 ) {
182
+ System .out .println ( "no strings to check" );
183
+ return ;
184
+ }
185
+
127
186
for ( String string : stringsToCheck ) {
187
+ int curPattern = 0 ;
128
188
for ( MoaPattern pattern : patterns ) {
129
189
MoaMatcher matcher = pattern .matcher ( string );
130
190
if ( !multiLine ) {
131
191
if ( matcher .matches () ) {
132
- System .out .println ( "\" " + pattern + "\" matches \" " + string + "\" " );
192
+ System .out .println ( "\" " + patternNames . get ( curPattern ) + "\" matches \" " + string + "\" " );
133
193
}
134
194
else {
135
- System .out .println ( "\" " + pattern + "\" does not match \" " + string + "\" " );
195
+ System .out .println ( "\" " + patternNames . get ( curPattern ) + "\" does not match \" " + string + "\" " );
136
196
}
137
197
}
138
198
else {
139
- StringBuffer buffer = new StringBuffer ( string );
199
+ StringBuilder buffer = new StringBuilder ( string );
140
200
int additionalCharsPerMatch = ("<match>" + "</match>" ).length ();
141
201
int matchCount = 0 ;
142
202
while ( matcher .nextMatch () ) {
@@ -153,7 +213,20 @@ public static void main(String[] args) throws ParseException, IOException {
153
213
System .out .println ( buffer .toString () );
154
214
}
155
215
}
216
+ ++curPattern ;
217
+ }
218
+ }
219
+
220
+ private static String readWholeFile (File file ) throws IOException {
221
+ StringBuilder ret = new StringBuilder ();
222
+ try (FileInputStream fis = new FileInputStream ( file );
223
+ BufferedReader reader = new BufferedReader ( new InputStreamReader ( fis ) )) {
224
+ String str ;
225
+ while ( (str = reader .readLine ()) != null ) {
226
+ ret .append ( str ).append ( "\n " );
227
+ }
156
228
}
229
+ return ret .toString ();
157
230
}
158
231
159
232
private static List <String > readFileContents (File file ) throws IOException {
0 commit comments