Debian Codegraph @

src/main/org/h2/server/web/PageParser.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.server.web;
7
8 import java.text.ParseException;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import org.h2.util.StringUtils;
15
16 /**
17 * A page parser can parse an HTML page and replace the tags there.
18 * This class is used by the H2 Console.
19 */
20 public class PageParser {
21 private static final int TAB_WIDTH = 4;
22
23 private final String page;
24 private int pos;
25 private final Map<String, Object> settings;
26 private final int len;
27 private StringBuilder result;
28
29 private PageParser(String page, Map<String, Object> settings, int pos) {
30 this.page = page;
31 this.pos = pos;
32 this.len = page.length();
33 this.settings = settings;
34 result = new StringBuilder(len);
35 }
36
37 /**
38 * Replace the tags in the HTML page with the given settings.
39 *
40 * @param page the HTML page
41 * @param settings the settings
42 * @return the converted page
43 */
44 public static String parse(String page, Map<String, Object> settings) {
45 PageParser block = new PageParser(page, settings, 0);
46 return block.replaceTags();
47 }
48
49 private void setError(int i) {
50 String s = page.substring(0, i) + "####BUG####" + page.substring(i);
51 s = PageParser.escapeHtml(s);
52 result = new StringBuilder();
53 result.append(s);
54 }
55
56 private String parseBlockUntil(String end) throws ParseException {
57 PageParser block = new PageParser(page, settings, pos);
58 block.parseAll();
59 if (!block.readIf(end)) {
60 throw new ParseException(page, block.pos);
61 }
62 pos = block.pos;
63 return block.result.toString();
64 }
65
66 private String replaceTags() {
67 try {
68 parseAll();
69 if (pos != len) {
70 setError(pos);
71 }
72 } catch (ParseException e) {
73 setError(pos);
74 }
75 return result.toString();
76 }
77
78 @SuppressWarnings("unchecked")
79 private void parseAll() throws ParseException {
80 StringBuilder buff = result;
81 String p = page;
82 int i = pos;
83 for (; i < len; i++) {
84 char c = p.charAt(i);
85 switch (c) {
86 case '<': {
87 if (p.charAt(i + 3) == ':' && p.charAt(i + 1) == '/') {
88 // end tag
89 pos = i;
90 return;
91 } else if (p.charAt(i + 2) == ':') {
92 pos = i;
93 if (readIf("<c:forEach")) {
94 String var = readParam("var");
95 String items = readParam("items");
96 read(">");
97 int start = pos;
98 List<Object> list = (List<Object>) get(items);
99 if (list == null) {
100 result.append("?items?");
101 list = new ArrayList<>();
102 }
103 if (list.isEmpty()) {
104 parseBlockUntil("</c:forEach>");
105 }
106 for (Object o : list) {
107 settings.put(var, o);
108 pos = start;
109 String block = parseBlockUntil("</c:forEach>");
110 result.append(block);
111 }
112 } else if (readIf("<c:if")) {
113 String test = readParam("test");
114 int eq = test.indexOf("=='");
115 if (eq < 0) {
116 setError(i);
117 return;
118 }
119 String val = test.substring(eq + 3, test.length() - 1);
120 test = test.substring(0, eq);
121 String value = (String) get(test);
122 read(">");
123 String block = parseBlockUntil("</c:if>");
124 pos--;
125 if (value.equals(val)) {
126 result.append(block);
127 }
128 } else {
129 setError(i);
130 return;
131 }
132 i = pos;
133 } else {
134 buff.append(c);
135 }
136 break;
137 }
138 case '$':
139 if (p.length() > i + 1 && p.charAt(i + 1) == '{') {
140 i += 2;
141 int j = p.indexOf('}', i);
142 if (j < 0) {
143 setError(i);
144 return;
145 }
146 String item = StringUtils.trimSubstring(p, i, j);
147 i = j;
148 String s = (String) get(item);
149 replaceTags(s);
150 } else {
151 buff.append(c);
152 }
153 break;
154 default:
155 buff.append(c);
156 break;
157 }
158 }
159 pos = i;
160 }
161
162 @SuppressWarnings("unchecked")
163 private Object get(String item) {
164 int dot = item.indexOf('.');
165 if (dot >= 0) {
166 String sub = item.substring(dot + 1);
167 item = item.substring(0, dot);
168 HashMap<String, Object> map = (HashMap<String, Object>) settings.get(item);
169 if (map == null) {
170 return "?" + item + "?";
171 }
172 return map.get(sub);
173 }
174 return settings.get(item);
175 }
176
177 private void replaceTags(String s) {
178 if (s != null) {
179 result.append(PageParser.parse(s, settings));
180 }
181 }
182
183 private String readParam(String name) throws ParseException {
184 read(name);
185 read("=");
186 read("\"");
187 int start = pos;
188 while (page.charAt(pos) != '"') {
189 pos++;
190 }
191 int end = pos;
192 read("\"");
193 String s = page.substring(start, end);
194 return PageParser.parse(s, settings);
195 }
196
197 private void skipSpaces() {
198 while (page.charAt(pos) == ' ') {
199 pos++;
200 }
201 }
202
203 private void read(String s) throws ParseException {
204 if (!readIf(s)) {
205 throw new ParseException(s, pos);
206 }
207 }
208
209 private boolean readIf(String s) {
210 skipSpaces();
211 if (page.regionMatches(pos, s, 0, s.length())) {
212 pos += s.length();
213 skipSpaces();
214 return true;
215 }
216 return false;
217 }
218
219 /**
220 * Convert data to HTML, but don't convert newlines and multiple spaces.
221 *
222 * @param s the data
223 * @return the escaped html text
224 */
225 static String escapeHtmlData(String s) {
226 return escapeHtml(s, false);
227 }
228
229 /**
230 * Convert data to HTML, including newlines and multiple spaces.
231 *
232 * @param s the data
233 * @return the escaped html text
234 */
235 public static String escapeHtml(String s) {
236 return escapeHtml(s, true);
237 }
238
239 private static String escapeHtml(String s, boolean convertBreakAndSpace) {
240 if (s == null) {
241 return null;
242 }
243 int length = s.length();
244 if (convertBreakAndSpace) {
245 if (length == 0) {
246 return "&nbsp;";
247 }
248 }
249 StringBuilder builder = new StringBuilder(length);
250 boolean convertSpace = true;
251 for (int i = 0; i < length;) {
252 int cp = s.codePointAt(i);
253 if (cp == ' ' || cp == '\t') {
254 // convert tabs into spaces
255 for (int j = 0; j < (cp == ' ' ? 1 : TAB_WIDTH); j++) {
256 if (convertSpace && convertBreakAndSpace) {
257 builder.append("&nbsp;");
258 } else {
259 builder.append(' ');
260 convertSpace = true;
261 }
262 }
263 } else {
264 convertSpace = false;
265 switch (cp) {
266 case '$':
267 // so that ${ } in the text is interpreted correctly
268 builder.append("&#36;");
269 break;
270 case '<':
271 builder.append("&lt;");
272 break;
273 case '>':
274 builder.append("&gt;");
275 break;
276 case '&':
277 builder.append("&amp;");
278 break;
279 case '"':
280 builder.append("&quot;");
281 break;
282 case '\'':
283 builder.append("&#39;");
284 break;
285 case '\n':
286 if (convertBreakAndSpace) {
287 builder.append("<br />");
288 convertSpace = true;
289 } else {
290 builder.append(cp);
291 }
292 break;
293 default:
294 if (cp >= 128) {
295 builder.append("&#").append(cp).append(';');
296 } else {
297 builder.append((char) cp);
298 }
299 }
300 }
301 i += Character.charCount(cp);
302 }
303 return builder.toString();
304 }
305
306 /**
307 * Escape text as a the javascript string.
308 *
309 * @param s the text
310 * @return the javascript string
311 */
312 static String escapeJavaScript(String s) {
313 if (s == null) {
314 return null;
315 }
316 int length = s.length();
317 if (length == 0) {
318 return "";
319 }
320 StringBuilder buff = new StringBuilder(length);
321 for (int i = 0; i < length; i++) {
322 char c = s.charAt(i);
323 switch (c) {
324 case '"':
325 buff.append("\\\"");
326 break;
327 case '\'':
328 buff.append("\\'");
329 break;
330 case '\\':
331 buff.append("\\\\");
332 break;
333 case '\n':
334 buff.append("\\n");
335 break;
336 case '\r':
337 buff.append("\\r");
338 break;
339 case '\t':
340 buff.append("\\t");
341 break;
342 default:
343 buff.append(c);
344 break;
345 }
346 }
347 return buff.toString();
348 }
349 }