11package com .inzapp .jsonToSqlParser ;
22
3+ import com .inzapp .jsonToSqlParser .config .Config ;
34import com .inzapp .jsonToSqlParser .core .Parser ;
45import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
56import org .json .JSONObject ;
67
7- import java .nio . charset . StandardCharsets ;
8- import java .security . MessageDigest ;
9- import java .security . NoSuchAlgorithmException ;
8+ import java .io . BufferedReader ;
9+ import java .io . FileOutputStream ;
10+ import java .io . FileReader ;
1011
1112public class JsonToSqlParser extends Parser {
12- // /**
13- // * entry point in execution jar file
14- // *
15- // * @param args [0] : input file name, default is "input.json"
16- // * [1] : output file name, default is "output.txt"
17- // */
18- // public static void main(String[] args) {
19- // String inputFileName = Config.INPUT_FILE_NAME;
20- // String outputFileName = Config.OUTPUT_FILE_NAME;
21- // if (args != null && args.length == 2) {
22- // inputFileName = args[0];
23- // outputFileName = args[1];
24- // }
25- //
26- // JsonToSqlParser jsonToSqlParser = new JsonToSqlParser();
27- // JSONObject json = jsonToSqlParser.readJsonFromFile(inputFileName);
28- // if (json == null) {
29- // System.out.println("failed to load json");
30- // return;
31- // }
32- //
33- // String sql = jsonToSqlParser .parse(json);
34- // if (sql == null) {
35- // System.out.println("parse failure");
36- // return;
37- // }
38- //
39- // try {
40- // System.out.println("input json\n");
41- // System.out.println(json.toString(4));
42- // System.out.println();
43- //
44- // System.out.println("output sql\n");
45- // System.out.println(sql);
46- // System.out.println();
47- // } catch (Exception e) {
48- // e.printStackTrace();
49- // }
50- //
51- // jsonToSqlParser.saveFile(sql, outputFileName);
52- // System.out.println("parse success");
53- // }
13+ /**
14+ * entry point in execution jar file
15+ *
16+ * @param args [0] : input file name, default is "input.json"
17+ * [1] : output file name, default is "output.txt"
18+ */
19+ public static void main (String [] args ) {
20+ String inputFileName = Config .INPUT_FILE_NAME ;
21+ String outputFileName = Config .OUTPUT_FILE_NAME ;
22+ if (args != null && args .length == 2 ) {
23+ inputFileName = args [0 ];
24+ outputFileName = args [1 ];
25+ }
26+
27+ JsonToSqlParser jsonToSqlParser = new JsonToSqlParser ();
28+ JSONObject json = jsonToSqlParser .readJsonFromFile (inputFileName );
29+ if (json == null ) {
30+ System .out .println ("failed to load json" );
31+ return ;
32+ }
33+
34+ String sql = new JsonToSqlParser () .parse (json . toString () );
35+ if (sql == null ) {
36+ System .out .println ("parse failure" );
37+ return ;
38+ }
39+
40+ try {
41+ System .out .println ("input json\n " );
42+ System .out .println (json .toString (4 ));
43+ System .out .println ();
44+
45+ System .out .println ("output sql\n " );
46+ System .out .println (sql );
47+ System .out .println ();
48+ } catch (Exception e ) {
49+ e .printStackTrace ();
50+ }
51+
52+ jsonToSqlParser .saveFile (sql , outputFileName );
53+ System .out .println ("parse success" );
54+ }
5455
5556 /**
5657 * head method as java library
@@ -60,51 +61,53 @@ public class JsonToSqlParser extends Parser {
6061 */
6162 public String parse (String jsonString ) {
6263 try {
63- String sql = new Parser ().parse (new JSONObject (jsonString ));
64+ JSONObject json = new JSONObject (jsonString );
65+ String sql = new Parser ().parse (json );
6466 CCJSqlParserUtil .parse (sql ); // query execution test
6567 return sql ;
6668 } catch (Exception e ) {
69+ e .printStackTrace ();
70+ return null ;
71+ }
72+ }
73+
74+ /**
75+ * read json string from file and convert is to json object
76+ *
77+ * @param fileName input file name, default is "input.json"
78+ * @return converted json object
79+ */
80+ private JSONObject readJsonFromFile (String fileName ) {
81+ try {
82+ BufferedReader br = new BufferedReader (new FileReader (fileName ));
83+ StringBuilder sb = new StringBuilder ();
84+ while (true ) {
85+ String line = br .readLine ();
86+ if (line == null )
87+ break ;
88+
89+ sb .append (line ).append ('\n' );
90+ }
91+ String jsonString = sb .toString ();
92+ return new JSONObject (jsonString );
93+ } catch (Exception e ) {
94+ e .printStackTrace ();
6795 return null ;
6896 }
6997 }
7098
71- // /**
72- // * read json string from file and convert is to json object
73- // *
74- // * @param fileName input file name, default is "input.json"
75- // * @return converted json object
76- // */
77- // private JSONObject readJsonFromFile(String fileName) {
78- // try {
79- // BufferedReader br = new BufferedReader(new FileReader(fileName));
80- // StringBuilder sb = new StringBuilder();
81- // while (true) {
82- // String line = br.readLine();
83- // if (line == null)
84- // break;
85- //
86- // sb.append(line).append('\n');
87- // }
88- // String jsonString = sb.toString();
89- // return new JSONObject(jsonString);
90- // } catch (Exception e) {
91- // e.printStackTrace();
92- // return null;
93- // }
94- // }
95- //
96- // /**
97- // * save converted sql string to file
98- // *
99- // * @param sql converted sql from parser
100- // * @param fileName output file name, default is "output.txt"
101- // */
102- // private void saveFile(String sql, String fileName) {
103- // try {
104- // FileOutputStream fos = new FileOutputStream(fileName);
105- // fos.write(sql.getBytes());
106- // } catch (Exception e) {
107- // e.printStackTrace();
108- // }
109- // }
99+ /**
100+ * save converted sql string to file
101+ *
102+ * @param sql converted sql from parser
103+ * @param fileName output file name, default is "output.txt"
104+ */
105+ private void saveFile (String sql , String fileName ) {
106+ try {
107+ FileOutputStream fos = new FileOutputStream (fileName );
108+ fos .write (sql .getBytes ());
109+ } catch (Exception e ) {
110+ e .printStackTrace ();
111+ }
112+ }
110113}
0 commit comments