|
7 | 7 | import java.io.BufferedReader;
|
8 | 8 | import java.io.FileOutputStream;
|
9 | 9 | import java.io.FileReader;
|
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.Base64; |
10 | 12 |
|
11 | 13 | public class SqlToJsonParser {
|
| 14 | +// /** |
| 15 | +// * main method |
| 16 | +// * used for executable jar |
| 17 | +// * |
| 18 | +// * @param args [0] : specified input file name |
| 19 | +// * [1] : specified output file name |
| 20 | +// */ |
| 21 | +// public static void main(String[] args) { |
| 22 | +// String inputFileName = Config.INPUT_FILE_NAME; |
| 23 | +// String outputFileName = Config.OUTPUT_FILE_NAME; |
| 24 | +// if (args != null && args.length == 2) { |
| 25 | +// inputFileName = args[0]; |
| 26 | +// outputFileName = args[1]; |
| 27 | +// } |
| 28 | +// |
| 29 | +// SqlToJsonParser sqlToJsonParser = new SqlToJsonParser(); |
| 30 | +// Parser parser = new Parser(); |
| 31 | +// try { |
| 32 | +// String sql = sqlToJsonParser.readSqlFromFile(inputFileName); |
| 33 | +// if (sql == null) |
| 34 | +// throw new Exception("input file does not exist"); |
| 35 | +// |
| 36 | +// JSONObject json = parser.parse(sql); |
| 37 | +// if (json == null) |
| 38 | +// throw new Exception("sql syntax error"); |
| 39 | +// |
| 40 | +// String jsonString = json.toString(4); |
| 41 | +// System.out.println("input sql\n\n" + sql); |
| 42 | +// System.out.println("output json\n\n" + jsonString); |
| 43 | +// |
| 44 | +// sqlToJsonParser.saveFile(jsonString, outputFileName); |
| 45 | +// System.out.println("parse success"); |
| 46 | +// } catch (Exception e) { |
| 47 | +// sqlToJsonParser.saveFile(Config.SQL_SYNTAX_ERROR, outputFileName); |
| 48 | +// System.out.println(e.getMessage()); |
| 49 | +// } |
| 50 | +// } |
| 51 | + |
12 | 52 | /**
|
13 | | - * main method |
14 | | - * used for executable jar |
| 53 | + * used for java code |
15 | 54 | *
|
16 | | - * @param args [0] : specified input file name |
17 | | - * [1] : specified output file name |
| 55 | + * @param sql raw sql query encoded with base 64 |
| 56 | + * @return parsed json string |
| 57 | + * return null if exception was caught |
18 | 58 | */
|
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 | | - SqlToJsonParser sqlToJsonParser = new SqlToJsonParser(); |
28 | | - Parser parser = new Parser(); |
29 | | - try { |
30 | | - String sql = sqlToJsonParser.readSqlFromFile(inputFileName); |
31 | | - if (sql == null) |
32 | | - throw new Exception("input file does not exist"); |
33 | | - |
34 | | - JSONObject json = parser.parse(sql); |
35 | | - if (json == null) |
36 | | - throw new Exception("sql syntax error"); |
37 | | - |
38 | | - String jsonString = json.toString(4); |
39 | | - System.out.println("input sql\n\n" + sql); |
40 | | - System.out.println("output json\n\n" + jsonString); |
41 | | - |
42 | | - sqlToJsonParser.saveFile(jsonString, outputFileName); |
43 | | - System.out.println("parse success"); |
44 | | - } catch (Exception e) { |
45 | | - sqlToJsonParser.saveFile(Config.SQL_SYNTAX_ERROR, outputFileName); |
46 | | - System.out.println(e.getMessage()); |
47 | | - } |
| 59 | + public String parse(String sql) { |
| 60 | + return parse(sql, true); |
48 | 61 | }
|
49 | 62 |
|
50 | 63 | /**
|
51 | 64 | * used for java code
|
52 | 65 | *
|
53 | 66 | * @param sql raw sql query
|
| 67 | + * @param isEncoded64 true if encoded with base 64 |
54 | 68 | * @return parsed json string
|
55 | 69 | * return null if exception was caught
|
56 | 70 | */
|
57 | | - public String parse(String sql) { |
| 71 | + public String parse(String sql, boolean isEncoded64) { |
| 72 | + if(isEncoded64) |
| 73 | + sql = decode64(sql); |
| 74 | + |
58 | 75 | try {
|
59 | 76 | return new Parser().parse(sql).toString(4);
|
60 | 77 | } catch (Exception e) {
|
61 | 78 | return null;
|
62 | 79 | }
|
63 | 80 | }
|
64 | 81 |
|
| 82 | + private String encode64(String raw) { |
| 83 | + byte[] bytes = raw.getBytes(StandardCharsets.UTF_8); |
| 84 | + Base64.Encoder encoder = Base64.getEncoder(); |
| 85 | + byte[] encodedBytes = encoder.encode(bytes); |
| 86 | + return encoder.encodeToString(encodedBytes); |
| 87 | + } |
| 88 | + |
| 89 | + private String decode64(String encoded64) { |
| 90 | + byte[] encodedBytes = encoded64.getBytes(); |
| 91 | + Base64.Decoder decoder = Base64.getDecoder(); |
| 92 | + byte[] decodedBytes = decoder.decode(encodedBytes); |
| 93 | + return new String(decodedBytes, StandardCharsets.UTF_8); |
| 94 | + } |
| 95 | + |
65 | 96 | /**
|
66 | 97 | * read sql from com.inzapp.SqlToJsonParser.config.Config.INPUT_FILE_NAME
|
67 | 98 | * used for executable jar
|
|
0 commit comments