Skip to content

Commit fe20b8e

Browse files
committed
Add JtsOp output file option
1 parent 41dacde commit fe20b8e

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

modules/app/src/main/java/org/locationtech/jtstest/cmd/CommandOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class CommandOptions {
3737
public static final String QUIET = "q";
3838
public static final String VALIDATE = "validate";
3939
public static final String WHERE = "where";
40+
public static final String OUTPUT = "o";
4041

4142
public static final String SOURCE_STDIN = "stdin";
4243

modules/app/src/main/java/org/locationtech/jtstest/cmd/CommandOutput.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,79 @@
1111
*/
1212
package org.locationtech.jtstest.cmd;
1313

14+
import java.io.File;
15+
import java.io.FileWriter;
16+
import java.io.IOException;
17+
import java.io.PrintWriter;
18+
1419
public class CommandOutput {
1520

16-
private StringBuilder output = new StringBuilder();
21+
private StringBuilder outputBuffer = new StringBuilder();
1722
private boolean isCapture = false;
23+
private String outputFilename;
24+
private PrintWriter outWriter;
1825

1926
public CommandOutput() {
20-
27+
outWriter = new PrintWriter(System.out, true);
2128
}
2229

2330
public CommandOutput(boolean isCapture) {
2431
this.isCapture = true;
2532
}
2633

34+
public CommandOutput(String outputFile) {
35+
this.outputFilename = outputFile;
36+
File file = new File(outputFile);
37+
FileWriter fw = null;
38+
try {
39+
fw = new FileWriter(file);
40+
} catch (IOException e) {
41+
System.err.println(e.getMessage());
42+
System.exit(1);
43+
}
44+
outWriter = new PrintWriter(fw, true);
45+
}
46+
2747
public void println() {
2848
if (isCapture ) {
29-
output.append("\n");
49+
outputBuffer.append("\n");
3050
}
3151
else {
32-
System.out.println();
52+
outWriter.println();
3353
}
3454
}
3555

36-
public void println(Object o) {
56+
public void logln(Object o) {
3757
if (isCapture ) {
38-
output.append(o);
39-
output.append("\n");
58+
outputBuffer.append(o);
59+
outputBuffer.append("\n");
4060
}
4161
else {
4262
System.out.println(o);
4363
}
4464
}
4565

66+
public void println(Object o) {
67+
if (isCapture ) {
68+
outputBuffer.append(o);
69+
outputBuffer.append("\n");
70+
}
71+
else {
72+
outWriter.println(o);
73+
}
74+
}
75+
4676
public void print(String s) {
4777
if (isCapture ) {
48-
output.append(s);
78+
outputBuffer.append(s);
4979
}
5080
else {
51-
System.out.print(s);
81+
outWriter.print(s);
5282
}
5383
}
5484

5585
public String getOutput() {
56-
return output.toString();
86+
return outputBuffer.toString();
5787
}
5888

5989
}

modules/app/src/main/java/org/locationtech/jtstest/cmd/JTSOpCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private static CommandLine createCmdLine() {
123123
.addOptionSpec(new OptionSpec(CommandOptions.FORMAT, 1))
124124
.addOptionSpec(new OptionSpec(CommandOptions.LIMIT, 1))
125125
.addOptionSpec(new OptionSpec(CommandOptions.OFFSET, 1))
126+
.addOptionSpec(new OptionSpec(CommandOptions.OUTPUT, 1))
126127
.addOptionSpec(new OptionSpec(CommandOptions.REPEAT, 1))
127128
.addOptionSpec(new OptionSpec(CommandOptions.QUIET, 0))
128129
.addOptionSpec(new OptionSpec(CommandOptions.SRID, 1))
@@ -153,6 +154,7 @@ private static CommandLine createCmdLine() {
153154
" [ -q",
154155
" [ -time ]",
155156
" [ -v, -verbose ]",
157+
" [ -o filename ]",
156158
" [ -help ]",
157159
" [ -geomfunc classname ]",
158160
" [ -op ]",
@@ -182,6 +184,7 @@ private static CommandLine createCmdLine() {
182184
" -explode output atomic geometries",
183185
" -f output format to use. Default is txt/wkt",
184186
" -q quiet mode - result is not output",
187+
" -o filename write result output to filename",
185188
"===== Logging options:",
186189
" -time display execution time",
187190
" -v, -verbose display information about execution",
@@ -350,6 +353,10 @@ JTSOpRunner.OpParams parseArgs(String[] args) throws ParseException, ClassNotFou
350353

351354
cmdArgs.isQuiet = commandLine.hasOption(CommandOptions.QUIET);
352355

356+
cmdArgs.outputFile = commandLine.hasOption(CommandOptions.OUTPUT)
357+
? commandLine.getOptionArg(CommandOptions.OUTPUT, 1)
358+
: null;
359+
353360
cmdArgs.repeat = commandLine.hasOption(CommandOptions.REPEAT)
354361
? commandLine.getOptionArgAsInt(CommandOptions.REPEAT, 0)
355362
: 1;

modules/app/src/main/java/org/locationtech/jtstest/cmd/JTSOpRunner.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public class JTSOpRunner {
6666
private boolean captureGeometry = false;
6767
private List<Geometry> resultGeoms = new ArrayList<Geometry>();
6868

69-
private CommandOutput out = new CommandOutput();
70-
private GeometryOutput geomOut = new GeometryOutput(out);
69+
private CommandOutput out;
70+
private GeometryOutput geomOut;
7171
private String symGeom2 = SYM_B;
7272

7373
private IndexedGeometry geomIndexB;
@@ -110,6 +110,8 @@ static class OpParams {
110110
public int filterOp;
111111
public double filterVal = 0;
112112

113+
public String outputFile;
114+
113115
String operation;
114116
public String[] argList;
115117

@@ -159,9 +161,21 @@ public void replaceStdIn(InputStream inStream) {
159161
public String getOutput() {
160162
return out.getOutput();
161163
}
164+
162165
void execute(OpParams param) {
163166
this.param = param;
164167

168+
//-- init output to file or console
169+
if (out == null) {
170+
if (param.outputFile != null) {
171+
out = new CommandOutput(param.outputFile);
172+
}
173+
else {
174+
out = new CommandOutput();
175+
}
176+
geomOut = new GeometryOutput(out);
177+
}
178+
165179
geomFactory = createGeometryFactory(param.srid);
166180
geomA = null;
167181
geomB = null;
@@ -269,7 +283,7 @@ private void executeFunction() {
269283
executeFunctionOverA(fun);
270284

271285
if (isVerbose || isTime) {
272-
out.println("\nOperation " + func.getCategory() + "." + func.getName() + ": " + opCount
286+
out.logln("\nOperation " + func.getCategory() + "." + func.getName() + ": " + opCount
273287
+ " invocations - Total Time: " + Stopwatch.getTimeString( totalTime ));
274288
}
275289
}
@@ -314,7 +328,7 @@ private void executeFunction(Geometry geomA, FunctionInvoker fun, String hdr) {
314328

315329
String opDesc = "[" + (opCount+1) + "] -- " + opSummary(func, arg) + " : ";
316330
if (isVerbose) {
317-
out.println(opDesc + hdr);
331+
out.logln(opDesc + hdr);
318332
}
319333
else {
320334
hdrSave = hdr + "\n" + opDesc;
@@ -388,8 +402,8 @@ private String toStackString(Throwable ex) {
388402

389403
private void logError(String msg) {
390404
// this will be blank if already printed in verbose mode
391-
out.println(hdrSave);
392-
out.println(msg);
405+
out.logln(hdrSave);
406+
out.logln(msg);
393407
}
394408

395409
private void validate(Object result) {
@@ -512,7 +526,7 @@ private void printGeometry(Geometry geom, int srid, String outputFormat) {
512526

513527
private void printlnInfo(String s) {
514528
if (! isVerbose) return;
515-
out.println(s);
529+
out.logln(s);
516530
}
517531

518532
private void printGeometrySummary(String label, List<Geometry> geom, String source) {

0 commit comments

Comments
 (0)