-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServletParser.java
240 lines (215 loc) · 8.75 KB
/
ServletParser.java
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package lib;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class ServletParser {
/*this is a parse class aim to parse jsp file to servlet.java file
*
* https://github.com/XUranus
*
* */
private String className;
private String jspContent;
public ServletParser(File file) {
String content = "";
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String line = "";
while ((line = in.readLine())!=null) {
content = content + line +"\n";
}
}
catch (IOException e) {
System.out.println("Parser Construct Err");
}
String className = file.getName();// 可以加后缀
className = className.replace(".", "_");
System.out.println("this file name is "+className);
String jspContent = content;
this.className = className;
this.jspContent = jspContent;
}
public ServletParser(String className,String jspContent) {
this.className = className;
this.jspContent = jspContent;
}
public String parse() throws Exception {
String str = jspContent;
String importPart = "";
String doGetPartString = "";
String definitionPartString = "";
String servicePartString = "";
int htmlContentBegin = 0;
for(int i=0;i<str.length();i++) {
if (str.charAt(i) == '<'){
if(str.charAt(i+1)=='%') {
if(str.charAt(i+2)=='=') {
//jsp expression tag <%= %>
int end = getStringIndex(i+3,str,"%>");
doGetPartString += htmlContentString(str,htmlContentBegin,i-1);
htmlContentBegin = end+2;
doGetPartString += expresstionsString(str,i+3,end-1);
i = end + 1;
continue;
}
else if(str.charAt(i+2)=='!'){
//jsp declaration tag <%! %>
int end = getStringIndex(i+3,str,"%>");
doGetPartString += htmlContentString(str,htmlContentBegin,i-1);
htmlContentBegin = end+2;
definitionPartString += declarationsString(str,i+3,end-1);
i = end + 1;
continue;
}
else if(str.charAt(i+2)=='@'){
//jsp directive tag <%@ %>
int end = getStringIndex(i+3,str,"%>");
doGetPartString += htmlContentString(str,htmlContentBegin,i-1);
htmlContentBegin = end+2;
jspDirectiveString(str,i+3,end-1);
servicePartString += jspDirectiveString(str,i+3,end-1);
i = end + 1;
continue;
}
else if(str.charAt(i+2)=='-' && str.charAt(i+3)=='-'){
//jsp comment tag <%-- --%>
//do nothing
int end = getStringIndex(i+3,str,"%>");
doGetPartString += htmlContentString(str,htmlContentBegin,i-1);
htmlContentBegin = end+2;
i = end + 1;
continue;
}
else {
//scriptlets tag
int end = getStringIndex(i+2,str,"%>");
doGetPartString += htmlContentString(str,htmlContentBegin,i-1);
htmlContentBegin = end+2;
doGetPartString += scriptletsString(str,i+2,end-1);
i = end + 1;
continue;
}
}
else if(stringMatches(str,i,"<jsp:")) {
//JSP standard action like <jsp:usebean>
int end = getStringIndex(i+5,str,">");
doGetPartString += htmlContentString(str,htmlContentBegin,i-1);
htmlContentBegin = end+1;
System.out.println(scriptletsString(str,i+5,end-1));
i = end;
//temp debug solution !!!
continue;
}
}
}
return new ServletGenerator().generate(className,importPart,definitionPartString,"",servicePartString+"\n"+doGetPartString,servicePartString+"\n"+doGetPartString,"");
}
String scriptletsString(String str,int begin,int end) {
//handle <jsp: ... >
return removeSideBlanks(str.substring(begin,end+1))+"\n";
}
String jspDirectiveString(String str,int begin,int end) {
//handle <%@ ... %>
String content = removeSideBlanks(str.substring(begin,end+1));
String directType = content.split(" ")[0];
String parameter = content.split(" ")[1];
switch (directType) {
case "page":
if(parameter.startsWith("contentType")) {
return "response.setContentType(" + parameter.substring(12,parameter.length()) + ");";
}
break;
case "import":
break;
case "include":
break;
case "taglib":
break;
default:
break;
}
return "[jspDirectiveString Error:" + content + "]";
}
/************************************** codes and logical below is easy... ******************************************************/
String expresstionsString(String str,int begin,int end) {
//handle <%! ... %>
//example : "out.println(content);"
return "out.println("+removeSideLineFeed(removeSideBlanks(str.substring(begin,end+1)))+");"+"\n";
}
String declarationsString(String str,int begin,int end) {
//handle <%! ... %>
//class globe definition of function and variables
return removeSideBlanks(str.substring(begin,end+1))+"\n";
}
String htmlContentString(String str,int begin,int end) {
//handle default ; unrecognized tags
if(begin >= end) return "";
String res = "";
String html = removeSideBlanksAndLineFeeds(str.substring(begin,end+1));
StringTokenizer st = new StringTokenizer(html,"\n");
while (st.hasMoreTokens()) {
String temp = st.nextToken();
if(!temp.trim().isEmpty()) {
res += "out.write(\""+temp+"\");"+"\n";
}
}
return res;
}
String removeSideLineFeed(String str) {
//this function is used to remove the blank in each side of the string
int begin = 0;
while (str.charAt(begin)=='\n') begin++;
int end = str.length();
while (str.charAt(end-1)=='\n') end--;
return str.substring(begin,end);
}
String removeSideBlanks(String str) {
//this function is used to remove the blank in each side of the string
int begin = 0;
while (str.charAt(begin)==' ') begin++;
int end = str.length();
while (str.charAt(end-1)==' ') end--;
return str.substring(begin,end);
}
String removeSideBlanksAndLineFeeds(String str) {
//转义
str = str.replace("\"","\\\"");
str = str.replace(" ","\\t");
return str;
//this function is used to remove the blank in each side of the string
/*int begin = 0;
while (str.charAt(begin)==' '||str.charAt(begin)=='\n') begin++;
int end = str.length();
while (str.charAt(end-1)==' '||str.charAt(end-1)=='\n') end--;
return str.substring(begin,end);*/
}
private boolean stringMatches(String str,int index,String module) {
//return whether a string starts at index has a prefix module
if(module.length()+index>=str.length()) return false;
for(int i=0;i<module.length();i++) {
if(str.charAt(index+i)!=module.charAt(i)) return false;
}
return true;
}
private int getStringIndex(int start,String str,String subStr) {
/*
search str[start,str.length())
return the index that sub_string appear in the string
if found return the index of the first char
if not found return -1
*/
for(int i=start;i<str.length()-subStr.length()+1;i++) {
boolean flag = true;
for(int j=0;j<subStr.length();j++) {
if (subStr.charAt(j) != str.charAt(i + j)) {
flag = false;
break;
}
}
if(flag) return i;
}
return -1;
}
}