Skip to content
Navigation Menu
{{ message }}
forked from laomafeima/WebJava
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTemplateCompile.java
More file actions
executable file
·445 lines (400 loc) · 16.4 KB
/
Copy pathTemplateCompile.java
File metadata and controls
executable file
·445 lines (400 loc) · 16.4 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package Web;
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Pattern;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
/**
* 编译模版成Java代码
*
* @author Ma
*/
public class TemplateCompile {
/**
* 模版目录
*/
String TemplatePath = Options.TemplatePath;
protected ArrayList<String> block;
protected ArrayList<String> blockContent;
protected boolean isBlock = false;
protected boolean isExtends = false;//标记是否是有继承关系
private BufferedWriter writer;
public TemplateCompile() {
this.blockContent = new ArrayList<String>();
this.block = new ArrayList<String>();
this.TemplatePath = Options.TemplatePath;
}
/**
* public static void main(String[] args) { TemplateCompile self = new
* TemplateCompile(); File tplDir = new File(self.TemplatePath); //判断目录是否存在
* tplDir.isDirectory();
*
* String[] fileList = tplDir.list();
*
* for (int i = 0; i < fileList.length; i++) { String string = fileList[i];
* if (string.matches(".+\\.java")) {//不处理Java文件 continue; }
* self.start(string); System.out.println(string);//@todo 后期处理子目录的问题 } }
*
*/
public int start(String fileName) {
File tplFile = new File(this.TemplatePath + fileName);
if (tplFile.exists()) {
try {
BufferedReader reader = new BufferedReader(new FileReader(tplFile));
String line = null;
//编译文件Start
File compilerFile = this.getFile(fileName);//获取Java文件
if (compilerFile.exists()) {
//如果文件存在就比较时间
if (compilerFile.lastModified() > tplFile.lastModified()) {
//如果最后修改的时候小于编译后的Java文件的最后日期
//直接返回
//return 0;注释掉,每次都重新编译
}
} else {
//如果文件不存在就创建
compilerFile.createNewFile();
}
this.writer = new BufferedWriter(new FileWriter(compilerFile));
if (!compilerFile.isFile()) {
;//说明创建失败
}
//编译文件END
String fatherTpl = "Web.BaseTemplate";//默认继承
line = reader.readLine();//分析第一行是否继承
Pattern pat = Pattern.compile("\\{\\%\\s*extends\\s*\\s(\\w+)\\s*\\%\\}");
Matcher mat = pat.matcher(line);
if (mat.find()) {
fatherTpl = mat.group(1) + "Template";
line = line.replace(mat.group(), "");
this.isExtends = true;
//遇到继承,就去解析父模版
TemplateCompile templateCompile = new TemplateCompile();
int r = templateCompile.start(mat.group(1) + ".html");
}
//识别继承标识Start
Pattern blockPat = Pattern.compile("\\{\\%\\s*block\\s*(\\w+)\\s*\\%\\}");
//识别继承标识END
//writer.write("package Web.Template" + this.getPackage(fileName) + ";\npublic class "
writer.write("package Template" + this.getPackage(fileName) + ";\npublic class "
+ this.getfileName(fileName)
+ "Template extends " + fatherTpl + "{");
if (!this.isExtends) {
writer.write("\n\tpublic String display(){\n\t\tString str= \n");
}
while (line != null) {
line = line.trim();
if (line.length() < 1) {
line = reader.readLine();//读取下一行
continue;
}
//检查是否是block Start
Matcher blockMat = blockPat.matcher(line);
if (blockMat.find()) {
this.block.add(blockMat.group(1));
this.blockContent.add("");
this.isBlock = true;
line = line.replace(blockMat.group(), "\"\"+this." + blockMat.group(1) + "()");
this.writerFile(line, true);
line = reader.readLine();//读取下一行
continue;
//System.out.println(blockMat.group(1));
}
//检查是否是block End
if (this.isBlock && line.matches("\\{\\%\\s*end\\s*\\%\\}")) {
this.isBlock = false;
line = reader.readLine();//读取下一行
continue;
}
//检查if 语句
if (line.matches(".*\\{\\%\\s*if\\s*\\(.+\\)\\s*\\%\\}.*")) {
line = line.replace("\"", "\\\"");
line = line.replace("{%", "\";");
line = line.replace("%}", "{str=str");
line = "\"" + line + "\n";
if (this.isBlock) {
this.writerBlock(line, true);
} else {
this.writerFile(line, true);
}
line = reader.readLine();//读取下一行
continue;
}
//检查endif 语句
if (line.matches(".*\\{\\%\\s*endif\\s*\\%\\}.*")) {
line = line.replaceAll("\\{\\%\\s*endif\\s*\\%\\}", "\";}str=str+\"");
line = "\"" + line + "\"\n";
if (this.isBlock) {
this.writerBlock(line, true);
} else {
this.writerFile(line, true);
}
line = reader.readLine();//读取下一行
continue;
}
//检查elseif 语句
if (line.matches(".*\\{\\%\\s*elseif\\s*\\(.+\\)\\s*\\%\\}.*")) {
line = line.replace("{%", "\";}");
line = line.replace("%}", "{str=str");
line = line.replace("elseif", "else if");
line = "\"" + line + "\n";
if (this.isBlock) {
this.writerBlock(line, true);
} else {
this.writerFile(line, true);
}
line = reader.readLine();//读取下一行
continue;
}
//检查else 语句
if (line.matches(".*\\{\\%\\s*else\\s*\\%\\}.*")) {
line = line.replaceAll("\\{\\%\\s*else\\s*\\%\\}", "\";}else{str=str");
line = "\"" + line + "\n";
if (this.isBlock) {
this.writerBlock(line, true);
} else {
this.writerFile(line, true);
}
line = reader.readLine();//读取下一行
continue;
}
//检查for 语句
if (line.matches(".*\\{\\%\\s*for\\s*\\(.+\\)\\s*\\%\\}.*")) {
line = line.replace("{%", "\";");
line = line.replace("%}", "{str=str");
line = "\"" + line + "\n";
if (this.isBlock) {
this.writerBlock(line, true);
} else {
this.writerFile(line, true);
}
line = reader.readLine();//读取下一行
continue;
}
//检查endfor 语句
if (line.matches(".*\\{\\%\\s*endfor\\s*\\%\\}.*")) {
line = line.replaceAll("\\{\\%\\s*endfor\\s*\\%\\}", "\";}str=str");
line = "\"" + line + "\n";
if (this.isBlock) {
this.writerBlock(line, true);
} else {
this.writerFile(line, true);
}
line = reader.readLine();//读取下一行
continue;
}
if (this.isBlock) {
this.writerBlock(line);
} else {
this.writerFile(line);
}
writer.newLine();
//writer.newLine();
line = reader.readLine();//读取下一行
}
reader.close();
this.flush();
this.writer.flush();
this.writer.close();
return 0;
} catch (FileNotFoundException ex) {
Logger.getLogger(TemplateCompile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TemplateCompile.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
return 1;
}
return 0;
}
/**
* 创建编译后的文件
*
* @param fileName
* @return
*/
protected File getFile(String fileName) {
fileName = this.getFilePath(fileName) + "/" + this.getfileName(fileName);
String javaTplName = fileName + "Template.java";
File JavaFile = new File(this.TemplatePath + javaTplName);
return JavaFile;
}
/**
* 获取文件名字,不包括扩展名字
*
* @param fileName
* @return
*/
protected String getfileName(String fileName) {
//获取文件名字,不包含扩展名
int index = fileName.lastIndexOf(".html");
fileName = fileName.substring(0, index);
index = fileName.lastIndexOf("/");
fileName = fileName.substring(index + 1);
return fileName;
}
/**
* 获取文件的路径
*
* @param fileName
* @return
*/
protected String getFilePath(String fileName) {
int index = fileName.lastIndexOf(".html");
fileName = fileName.substring(0, index);
index = fileName.lastIndexOf("/");
String path = "";
if (index > 0) {
path = fileName.substring(0, index);
}
return path;
}
protected String getPackage(String fileName) {
String path = this.getFilePath(fileName);
if (path.equals("")) {
path = "";
} else {
path = "." + path.replace("/", ".");
}
return path;
}
/**
* 写入bolck,自动添加引号
*
* @param line
* @return
*/
protected String writerBlock(String line) {
String r = "";
//首先把所有变量修改{{}}
if (line.matches(".*\\{\\{.+\\}\\}.*")) {
Pattern pattern = Pattern.compile(".*\\{\\{(\\w+):(\\w+):?(\\w+)?\\}\\}.*");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
line = line.replace("\"", "\\\"");
if (matcher.group(3) == null) {
line = line.replaceFirst("\\{\\{(.+):(.+)\\}\\}", "\"+this.getArray(\""
+ matcher.group(1) + "\")["
+ matcher.group(2) + "]+\"");
} else {//说明是引用2维数组
line = line.replaceFirst("\\{\\{(.+):(.+):(.+)\\}\\}", "\"+this.getArray2(\""
+ matcher.group(1) + "\")["
+ matcher.group(2) + "][" + matcher.group(3) + "]+\"");
}
} else {
line = line.replace("{{", "\"+this.get(\"");
line = line.replace("}}", "\")+\"");
line = line.replace("\"", "\\\"");
}
r = this.blockContent.set(this.block.size() - 1, this.blockContent.get(this.block.size() - 1) + "+\n" + "\"" + line + "\"");
} else {
r = this.blockContent.set(this.block.size() - 1, this.blockContent.get(this.block.size() - 1) + "+\n" + "\"" + line.replace("\"", "\\\"") + "\"");
}
return r;
}
/**
* 写入 bolck,不写入引号
*
* @param line
* @param eval
* @return
*/
protected String writerBlock(String line, boolean eval) {
//首先把所有变量修改{{}}
Pattern pat = Pattern.compile("\\(.*\\$([_a-zA-Z][_a-zA-Z0-9]*):?(\\w*).*\\)");
Matcher mat = pat.matcher(line);
if (mat.find()) {
String param = mat.group(1);
String valueType = "";
if (mat.group(2).length() > 0) {
valueType = mat.group(2).replaceFirst(mat.group(2).substring(0, 1), mat.group(2).substring(0, 1).toUpperCase());
}
line = line.replaceAll("\\$([_a-zA-Z][_a-zA-Z0-9]*):?(\\w*)", "this.get"
+ valueType
+ "(\"" + param + "\")");
}
String r = this.blockContent.set(this.block.size() - 1, this.blockContent.get(this.block.size() - 1) + "+\n" + line);
return r;
}
/**
* 写入引号
*
* @param line
*/
protected void writerFile(String line) {
line = line.replace("\"", "\\\"");
//首先把所有变量修改{{}}
if (line.matches(".*\\{\\{.+\\}\\}.*")) {
Pattern pattern = Pattern.compile(".*\\{\\{(\\w+):(\\w+)\\}\\}.*");
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
line = line.replaceAll("\\{\\{(.+):(.+)\\}\\}", "\"+this.getArray(\""
+ matcher.group(1) + "\")["
+ matcher.group(2) + "]+\"");
} else {
line = line.replace("{{", "\"+this.get(\"");
line = line.replace("}}", "\")+\"");
}
}
try {
writer.write("\"" + line + "\"+");
} catch (IOException ex) {
Logger.getLogger(TemplateCompile.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* 不写入引号
*
* @param line
* @param eval
*/
protected void writerFile(String line, boolean eval) {
//首先把所有变量修改{{}}
Pattern pat = Pattern.compile("\\(.*\\$([_a-zA-Z][_a-zA-Z0-9]*):?(\\w*).*\\)");
Matcher mat = pat.matcher(line);
if (mat.find()) {
String param = mat.group(1);
String valueType = "";
if (mat.group(2).length() > 0) {
valueType = mat.group(2).replaceFirst(mat.group(2).substring(0, 1), mat.group(2).substring(0, 1).toUpperCase());
}
line = line.replaceAll("\\$([_a-zA-Z][_a-zA-Z0-9]*):?(\\w*)", "this.get"
+ valueType
+ "(\"" + param + "\")");
}
try {
if (!this.isExtends) {
writer.write("" + line + "+");
}
} catch (IOException ex) {
Logger.getLogger(TemplateCompile.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* 处理结尾,把block写在里面
*/
protected void flush() {
try {
if (!this.isExtends) {
this.writer.write("\"\";return str;\n}");
}
for (int i = 0; i < this.block.size(); i++) {
try {
this.writer.write("public String " + this.block.get(i) + " (){ String str = \"\"" + this.blockContent.get(i) + ";return str;}");
} catch (IOException ex) {
Logger.getLogger(TemplateCompile.class.getName()).log(Level.SEVERE, null, ex);
}
}
this.writer.write("\n}");
} catch (IOException ex) {
Logger.getLogger(TemplateCompile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
You can’t perform that action at this time.
