Skip to content

Commit a1c0e8b

Browse files
committed
added serialization/deserialization capabilities to the cli tool #4 #5
1 parent f04561e commit a1c0e8b

File tree

3 files changed

+92
-7
lines changed

3 files changed

+92
-7
lines changed

cli/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<version>${project.version}</version>
1919
</dependency>
2020

21+
<dependency>
22+
<groupId>com.github.s4ke</groupId>
23+
<artifactId>moar-json</artifactId>
24+
<version>${project.version}</version>
25+
</dependency>
26+
2127
<dependency>
2228
<groupId>commons-cli</groupId>
2329
<artifactId>commons-cli</artifactId>

cli/src/main/java/com/github/s4ke/moar/cli/Main.java

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.github.s4ke.moar.cli;
22

33
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
45
import java.io.File;
56
import java.io.FileInputStream;
7+
import java.io.FileWriter;
68
import java.io.IOException;
79
import java.io.InputStreamReader;
810
import java.util.ArrayList;
911
import java.util.List;
1012

1113
import com.github.s4ke.moar.MoaMatcher;
1214
import com.github.s4ke.moar.MoaPattern;
15+
import com.github.s4ke.moar.json.MoarJSONSerializer;
1316
import org.apache.commons.cli.CommandLine;
1417
import org.apache.commons.cli.CommandLineParser;
1518
import org.apache.commons.cli.DefaultParser;
@@ -29,6 +32,9 @@ public static void main(String[] args) throws ParseException, IOException {
2932
options.addOption( "rf", true, "regexFile" );
3033
options.addOption( "r", true, "regex" );
3134

35+
options.addOption( "mf", true, "moaFile" );
36+
options.addOption( "mo", true, "moaOutputFolder (overwrites if existent)" );
37+
3238
options.addOption( "sf", true, "stringFile" );
3339
options.addOption( "s", true, "string" );
3440

@@ -44,13 +50,15 @@ public static void main(String[] args) throws ParseException, IOException {
4450
formatter.printHelp( "moar-cli", options );
4551
}
4652

53+
List<String> patternNames = new ArrayList<>();
4754
List<MoaPattern> patterns = new ArrayList<>();
4855
List<String> stringsToCheck = new ArrayList<>();
4956

5057
if ( cmd.hasOption( "r" ) ) {
5158
String regexStr = cmd.getOptionValue( "r" );
5259
try {
5360
patterns.add( MoaPattern.compile( regexStr ) );
61+
patternNames.add( regexStr );
5462
}
5563
catch (Exception e) {
5664
System.out.println( e.getMessage() );
@@ -67,6 +75,7 @@ public static void main(String[] args) throws ParseException, IOException {
6775
if ( emptyLineCountAfterRegex >= 1 ) {
6876
if ( regexStr.length() > 0 ) {
6977
patterns.add( MoaPattern.compile( regexStr.toString() ) );
78+
patternNames.add(regexStr.toString());
7079
}
7180
regexStr.setLength( 0 );
7281
emptyLineCountAfterRegex = 0;
@@ -83,6 +92,7 @@ public static void main(String[] args) throws ParseException, IOException {
8392
if ( regexStr.length() > 0 ) {
8493
try {
8594
patterns.add( MoaPattern.compile( regexStr.toString() ) );
95+
patternNames.add(regexStr.toString());
8696
}
8797
catch (Exception e) {
8898
System.out.println( e.getMessage() );
@@ -92,6 +102,28 @@ public static void main(String[] args) throws ParseException, IOException {
92102
}
93103
}
94104

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+
95127
if ( cmd.hasOption( "s" ) ) {
96128
String str = cmd.getOptionValue( "s" );
97129
stringsToCheck.add( str );
@@ -115,28 +147,56 @@ public static void main(String[] args) throws ParseException, IOException {
115147

116148
boolean multiLine = cmd.hasOption( "m" );
117149

118-
if ( stringsToCheck.size() == 0 ) {
119-
System.out.println( "no strings to check" );
120-
return;
121-
}
122150
if ( patterns.size() == 0 ) {
123151
System.out.println( "no patterns to check" );
124152
return;
125153
}
126154

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+
127186
for ( String string : stringsToCheck ) {
187+
int curPattern = 0;
128188
for ( MoaPattern pattern : patterns ) {
129189
MoaMatcher matcher = pattern.matcher( string );
130190
if ( !multiLine ) {
131191
if ( matcher.matches() ) {
132-
System.out.println( "\"" + pattern + "\" matches \"" + string + "\"" );
192+
System.out.println( "\"" + patternNames.get(curPattern) + "\" matches \"" + string + "\"" );
133193
}
134194
else {
135-
System.out.println( "\"" + pattern + "\" does not match \"" + string + "\"" );
195+
System.out.println( "\"" + patternNames.get(curPattern) + "\" does not match \"" + string + "\"" );
136196
}
137197
}
138198
else {
139-
StringBuffer buffer = new StringBuffer( string );
199+
StringBuilder buffer = new StringBuilder( string );
140200
int additionalCharsPerMatch = ("<match>" + "</match>").length();
141201
int matchCount = 0;
142202
while ( matcher.nextMatch() ) {
@@ -153,7 +213,20 @@ public static void main(String[] args) throws ParseException, IOException {
153213
System.out.println( buffer.toString() );
154214
}
155215
}
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+
}
156228
}
229+
return ret.toString();
157230
}
158231

159232
private static List<String> readFileContents(File file) throws IOException {

json/src/main/java/com/github/s4ke/moar/json/MoarJSONSerializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,19 @@ else if ( stateObj.has( "set" ) ) {
142142
if ( matcher.matches() ) {
143143
String action = matcher.getVariableContent( "action" );
144144
String variableName = matcher.getVariableContent( "name" );
145+
if ( !variables.containsKey( variableName ) ) {
146+
throw new IllegalArgumentException( "variable with name " + variableName + " does not exist" );
147+
}
145148
memoryActionSet.add(
146149
new MemoryAction(
147150
ActionType.fromString( action ),
148151
variableName
149152
)
150153
);
151154
}
155+
else {
156+
throw new IllegalArgumentException( memoryActionString + " is no valid memoryAction string" );
157+
}
152158
}
153159
}
154160
}

0 commit comments

Comments
 (0)