-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.java
More file actions
185 lines (157 loc) · 6.38 KB
/
Main.java
File metadata and controls
185 lines (157 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.awt.*;
import java.io.*;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONTokener;
import org.json.JSONObject;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
public class Main extends JFrame {
public static final String NUMBER_TAG = "number";
public static final String STRING_TAG = "string";
private String defaultLocaion;
public String packetName;
private static final long serialVersionUID = 1L;
private JButton openFileBtn;
private JTextArea showResultTa;
TextField packetNameTextField;//TextField对象
Label packetNamelabel;//TextField对象
public Main()
{
super("JSON2JAVA");
this.setSize(800, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
packetNameTextField = new TextField("请输入包名,例如:com.insthub.bee.protocol",20);
packetNameTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
packetName = packetNameTextField.getText();
}
});
packetNamelabel = new Label("包名:");
openFileBtn = new JButton("打开文件");
showResultTa = new JTextArea();
showResultTa.setEnabled(false);
this.setLayout(new GridLayout(2,2));
this.add(packetNamelabel);
this.add(packetNameTextField);
this.add(openFileBtn);
this.add(showResultTa);
openFileBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
JFileChooser fc = new JFileChooser();
Duqu();
if (null != defaultLocaion && defaultLocaion.length() > 0)
{
File lastFile = new File(defaultLocaion);
File lastDirectory = new File(lastFile.getAbsolutePath());
fc.setCurrentDirectory(lastDirectory);
}
int retValue = fc.showSaveDialog(Main.this);
if (retValue == 0)
{
packetName = packetNameTextField.getText();
File file = fc.getSelectedFile();
defaultLocaion = file.getAbsolutePath();
showResultTa.append(file.toString());
try
{
JSONObject protocolObject = Main.getProtocolObject(file.getAbsolutePath());
File dirFolder = new File(file.getParent()+"/output");
if (!dirFolder.exists())
{
dirFolder.mkdirs();
}
JSONObject enumObject = protocolObject.optJSONObject("enum");
if (null != enumObject)
{
EnumParse.parseProtocol(enumObject,"enum",file.getParent(),packetName);
}
JSONObject modelObject = protocolObject.optJSONObject("model");
if (null != modelObject)
{
ModelParse.parseProtocol(modelObject,"model",file.getParent(),packetName);
}
JSONObject controllerObject = protocolObject.optJSONObject("controller");
if (null != controllerObject)
{
ControllerParse.parseProtocol(controllerObject,"controller",file.getParent(),packetName);
}
}
catch (IOException exception)
{
exception.printStackTrace();
}
catch (JSONException e2)
{
e2.printStackTrace();
}
savaLocation(defaultLocaion);
}
else
{
//TODO
}
}
});
}
public static JSONObject getProtocolObject(String filePath) throws IOException,JSONException
{
FileInputStream in = new FileInputStream(filePath);
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
in.close();
String res = new String(buffer,"UTF-8");
res = DelCommentsInJava.delComments(res);
JSONObject protocolObject = (JSONObject)Main.parseToJSON(res);
return protocolObject;
}
protected static Object parseToJSON(String protocolBody) throws JSONException {
Object result = null;
//trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If Json is not valid this will return null
protocolBody = protocolBody.trim();
if(protocolBody.startsWith("{") || protocolBody.startsWith("[")) {
result = new JSONTokener(protocolBody).nextValue();
}
return result;
}
public static void main(String[] args)
{
new Main().setVisible(true);
}
public void savaLocation(String lastFileLocation){//save last location
File f=new File(".\\json2java\\location.txt");
try {
FileWriter txt=new FileWriter(f);
txt.write(lastFileLocation);
txt.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
//读取保存的用户名和密码
public void Duqu() {
FileReader fr;
try {
fr = new FileReader(".\\json2java\\location.txt");
BufferedReader br = new BufferedReader(fr);
try {
defaultLocaion = br.readLine();
//System.out.print(line);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}