PageParser.java
10.8 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
package org.legrog.util;
// Code copied from org.h2.server.web.PageParser
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
//package org.h2.server.web;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.h2.util.New;
/**
* A page parser can parse an HTML page and replace the tags there.
* This class is used by the H2 Console.
*/
public class PageParser {
private static final int TAB_WIDTH = 4;
private final String page;
private int pos;
private final Map<String, Object> settings;
private final int len;
private StringBuilder result;
private PageParser(String page, Map<String, Object> settings, int pos) {
this.page = page;
this.pos = pos;
this.len = page.length();
this.settings = settings;
result = new StringBuilder(len);
}
/**
* Replace the tags in the HTML page with the given settings.
*
* @param page the HTML page
* @param settings the settings
* @return the converted page
*/
public static String parse(String page, Map<String, Object> settings) {
PageParser block = new PageParser(page, settings, 0);
return block.replaceTags();
}
private void setError(int i) {
String s = page.substring(0, i) + "####BUG####" + page.substring(i);
s = PageParser.escapeHtml(s);
result = new StringBuilder();
result.append(s);
}
private String parseBlockUntil(String end) throws ParseException {
PageParser block = new PageParser(page, settings, pos);
block.parseAll();
if (!block.readIf(end)) {
throw new ParseException(page, block.pos);
}
pos = block.pos;
return block.result.toString();
}
private String replaceTags() {
try {
parseAll();
if (pos != len) {
setError(pos);
}
} catch (ParseException e) {
setError(pos);
}
return result.toString();
}
@SuppressWarnings("unchecked")
private void parseAll() throws ParseException {
StringBuilder buff = result;
String p = page;
int i = pos;
for (; i < len; i++) {
char c = p.charAt(i);
switch (c) {
case '<': {
if (p.charAt(i + 3) == ':' && p.charAt(i + 1) == '/') {
// end tag
pos = i;
return;
} else if (p.charAt(i + 2) == ':') {
pos = i;
if (readIf("<c:forEach")) {
String var = readParam("var");
String items = readParam("items");
read(">");
int start = pos;
List<Object> list = (List<Object>) get(items);
if (list == null) {
result.append("?items?");
list = New.arrayList();
}
if (list.size() == 0) {
parseBlockUntil("</c:forEach>");
}
for (Object o : list) {
settings.put(var, o);
pos = start;
String block = parseBlockUntil("</c:forEach>");
result.append(block);
}
} else if (readIf("<c:if")) {
String test = readParam("test");
int eq = test.indexOf("=='");
if (eq < 0) {
setError(i);
return;
}
String val = test.substring(eq + 3, test.length() - 1);
test = test.substring(0, eq);
String value = (String) get(test);
read(">");
String block = parseBlockUntil("</c:if>");
pos--;
if (value.equals(val)) {
result.append(block);
}
} else {
setError(i);
return;
}
i = pos;
} else {
buff.append(c);
}
break;
}
case '$':
if (p.length() > i + 1 && p.charAt(i + 1) == '{') {
i += 2;
int j = p.indexOf('}', i);
if (j < 0) {
setError(i);
return;
}
String item = p.substring(i, j).trim();
i = j;
String s = (String) get(item);
replaceTags(s);
} else {
buff.append(c);
}
break;
default:
buff.append(c);
break;
}
}
pos = i;
}
@SuppressWarnings("unchecked")
private Object get(String item) {
int dot = item.indexOf('.');
if (dot >= 0) {
String sub = item.substring(dot + 1);
item = item.substring(0, dot);
HashMap<String, Object> map = (HashMap<String, Object>) settings.get(item);
if (map == null) {
return "?" + item + "?";
}
return map.get(sub);
}
return settings.get(item);
}
private void replaceTags(String s) {
if (s != null) {
result.append(PageParser.parse(s, settings));
}
}
private String readParam(String name) throws ParseException {
read(name);
read("=");
read("\"");
int start = pos;
while (page.charAt(pos) != '"') {
pos++;
}
int end = pos;
read("\"");
String s = page.substring(start, end);
return PageParser.parse(s, settings);
}
private void skipSpaces() {
while (page.charAt(pos) == ' ') {
pos++;
}
}
private void read(String s) throws ParseException {
if (!readIf(s)) {
throw new ParseException(s, pos);
}
}
private boolean readIf(String s) {
skipSpaces();
if (page.regionMatches(pos, s, 0, s.length())) {
pos += s.length();
skipSpaces();
return true;
}
return false;
}
/**
* Convert data to HTML, but don't convert newlines and multiple spaces.
*
* @param s the data
* @return the escaped html text
*/
static String escapeHtmlData(String s) {
return escapeHtml(s, false);
}
/**
* Convert data to HTML, including newlines and multiple spaces.
*
* @param s the data
* @return the escaped html text
*/
public static String escapeHtml(String s) {
return escapeHtml(s, true);
}
private static String escapeHtml(String s, boolean convertBreakAndSpace) {
if (s == null) {
return null;
}
if (convertBreakAndSpace) {
if (s.length() == 0) {
return " ";
}
}
StringBuilder buff = new StringBuilder(s.length());
boolean convertSpace = true;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == ' ' || c == '\t') {
// convert tabs into spaces
for (int j = 0; j < (c == ' ' ? 1 : TAB_WIDTH); j++) {
if (convertSpace && convertBreakAndSpace) {
buff.append(" ");
} else {
buff.append(' ');
convertSpace = true;
}
}
continue;
}
convertSpace = false;
switch (c) {
case '$':
// so that ${ } in the text is interpreted correctly
buff.append("$");
break;
case '<':
buff.append("<");
break;
case '>':
buff.append(">");
break;
case '&':
buff.append("&");
break;
case '"':
buff.append(""");
break;
case '\'':
buff.append("'");
break;
case '\n':
if (convertBreakAndSpace) {
buff.append("<br />");
convertSpace = true;
} else {
buff.append(c);
}
break;
default:
if (c >= 128) {
buff.append("&#").append((int) c).append(';');
} else {
buff.append(c);
}
break;
}
}
return buff.toString();
}
/**
* Escape text as a the javascript string.
*
* @param s the text
* @return the javascript string
*/
static String escapeJavaScript(String s) {
if (s == null) {
return null;
}
if (s.length() == 0) {
return "";
}
StringBuilder buff = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '"':
buff.append("\\\"");
break;
case '\'':
buff.append("\\'");
break;
case '\\':
buff.append("\\\\");
break;
case '\n':
buff.append("\\n");
break;
case '\r':
buff.append("\\r");
break;
case '\t':
buff.append("\\t");
break;
default:
buff.append(c);
break;
}
}
return buff.toString();
}
}