Skip to content

Instantly share code, notes, and snippets.

@kodiyan
Last active April 11, 2016 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kodiyan/9628486 to your computer and use it in GitHub Desktop.
Save kodiyan/9628486 to your computer and use it in GitHub Desktop.
Gherkin To Json Converter_Core
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import gherkin.formatter.JSONFormatter;
import gherkin.formatter.JSONPrettyFormatter;
import gherkin.parser.Parser;
import gherkin.util.FixJava;
// Gherkin to Json parser core file.
public class GtoJCore {
private String format;
long startTime = System.currentTimeMillis();
public GtoJCore(String outFormat) {
this.format = outFormat;
}
public String getOutFormat() {
return format;
}
public void gherkinTojson(String fPath, String jPath) {
// Feature file and JSON File path define.
String gherkin = null;
try {
gherkin = FixJava.readReader(new InputStreamReader(
new FileInputStream(fPath), "UTF-8"));
} catch (FileNotFoundException e) {
System.out.println("Feature file not found");
// e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
}
StringBuilder json = new StringBuilder();
JSONFormatter formatter;
// Select pretty or ugly.
if (format.equalsIgnoreCase("ugly")) {
formatter = new JSONFormatter(json);// not pretty
} else {
formatter = new JSONPrettyFormatter(json);// pretty
}
// String niceJson = JsonWriter.formatJson(json);
Parser parser = new Parser(formatter);
parser.parse(gherkin, fPath, 0);
formatter.done();
formatter.close();
System.out.println("json output: \n" + json + "'");
// System.out.println("json output: \n" + niceJson + "'");
// Finally flush and close
try {
FileWriter file = new FileWriter(jPath);
file.write(json.toString());
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("\n Total Running Time: " + (endTime - startTime)
+ " milliseconds");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment