-
-
Save mageddo/a1b2369dbf1b6f875a8bdd3f91c39df5 to your computer and use it in GitHub Desktop.
Gherkin To Json Converter_Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
// Set as pretty / ugly format for for JSON output. By default it is pretty | |
static GtoJCore testG = new GtoJCore("pretty"); | |
public static void main(String[] args) { | |
testG.gherkinTojson(featurePath, jasonPath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment