KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > server > web > PageParser


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.server.web;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashMap JavaDoc;
9
10 public class PageParser {
11     private WebServer server;
12     private String JavaDoc page;
13     private int pos;
14     private HashMap JavaDoc settings;
15     private int len;
16     private StringBuffer JavaDoc result;
17
18     public static String JavaDoc parse(WebServer server, String JavaDoc page, HashMap JavaDoc settings) {
19         PageParser block = new PageParser(server, page, settings, 0);
20         return block.parse();
21     }
22
23     private PageParser(WebServer server, String JavaDoc page, HashMap JavaDoc settings, int pos) {
24         this.server = server;
25         this.page = page;
26         this.pos = pos;
27         this.len = page.length();
28         this.settings = settings;
29         result = new StringBuffer JavaDoc(len);
30     }
31
32     private void setError(int i) {
33         String JavaDoc s = page.substring(0, i) + "####BUG####" + page.substring(i);
34         s = PageParser.escapeHtml(s);
35         result = new StringBuffer JavaDoc(s);
36     }
37
38     private String JavaDoc parseBlockUntil(String JavaDoc end) throws Exception JavaDoc {
39         PageParser block = new PageParser(server, page, settings, pos);
40         block.parseAll();
41         if (!block.readIf(end)) {
42             throw new Exception JavaDoc();
43         }
44         pos = block.pos;
45         return block.result.toString();
46     }
47
48     public String JavaDoc parse() {
49         try {
50             parseAll();
51             if (pos != len) {
52                 setError(pos);
53             }
54         } catch (Exception JavaDoc e) {
55             // TODO log error
56
setError(pos);
57         }
58         return result.toString();
59     }
60
61     private void parseAll() throws Exception JavaDoc {
62         StringBuffer JavaDoc buff = result;
63         String JavaDoc p = page;
64         int i = pos;
65         for (; i < len; i++) {
66             char c = p.charAt(i);
67             switch (c) {
68             case '<': {
69                 if (p.charAt(i + 1) == '%') {
70                     if (p.charAt(i + 2) == '@') {
71                         i += 3;
72                         pos = i;
73                         read("include");
74                         String JavaDoc file = readParam("file");
75                         read("%>");
76                         String JavaDoc s = server.getTextFile(file);
77                         append(s);
78                         i = pos;
79                     } else {
80                         setError(i);
81                         return;
82                     }
83                     break;
84                 } else if (p.charAt(i + 3) == ':' && p.charAt(i + 1) == '/') {
85                     // end tag
86
pos = i;
87                     return;
88                 } else if (p.charAt(i + 2) == ':') {
89                     pos = i;
90                     if (readIf("<c:forEach")) {
91                         String JavaDoc var = readParam("var");
92                         String JavaDoc items = readParam("items");
93                         read(">");
94                         int start = pos;
95                         ArrayList JavaDoc list = (ArrayList JavaDoc) get(items);
96                         if (list == null) {
97                             result.append("?items?");
98                             list = new ArrayList JavaDoc();
99                         }
100                         if (list.size() == 0) {
101                             parseBlockUntil("</c:forEach>");
102                         }
103                         for (int j = 0; j < list.size(); j++) {
104                             settings.put(var, list.get(j));
105                             pos = start;
106                             String JavaDoc block = parseBlockUntil("</c:forEach>");
107                             result.append(block);
108                         }
109                     } else if (readIf("<c:if")) {
110                         String JavaDoc test = readParam("test");
111                         int eq = test.indexOf("=='");
112                         if (eq < 0) {
113                             setError(i);
114                             return;
115                         }
116                         String JavaDoc val = test.substring(eq + 3, test.length() - 1);
117                         test = test.substring(0, eq);
118                         String JavaDoc value = (String JavaDoc) get(test);
119                         read(">");
120                         String JavaDoc block = parseBlockUntil("</c:if>");
121                         pos--;
122                         if (value.equals(val)) {
123                             result.append(block);
124                         }
125                     } else {
126                         setError(i);
127                         return;
128                     }
129                     i = pos;
130                 } else {
131                     buff.append(c);
132                 }
133                 break;
134             }
135             case '$':
136                 if (p.charAt(i + 1) == '{') {
137                     i += 2;
138                     int j = p.indexOf('}', i);
139                     if (j < 0) {
140                         setError(i);
141                         return;
142                     }
143                     String JavaDoc item = p.substring(i, j).trim();
144                     i = j;
145                     String JavaDoc s = (String JavaDoc) get(item);
146                     append(s);
147                 }
148                 break;
149             default:
150                 buff.append(c);
151             }
152         }
153         pos = i;
154     }
155
156     private Object JavaDoc get(String JavaDoc item) {
157         int dot = item.indexOf('.');
158         if (dot >= 0) {
159             String JavaDoc sub = item.substring(dot + 1);
160             item = item.substring(0, dot);
161             HashMap JavaDoc map = (HashMap JavaDoc) settings.get(item);
162             if (map == null) {
163                 return "?" + item + "?";
164             }
165             return map.get(sub);
166         } else {
167             return settings.get(item);
168         }
169     }
170
171     private void append(String JavaDoc s) {
172         if (s != null) {
173             result.append(PageParser.parse(server, s, settings));
174         }
175     }
176
177     private String JavaDoc readParam(String JavaDoc name) throws Exception JavaDoc {
178         read(name);
179         read("=");
180         read("\"");
181         int start = pos;
182         while (page.charAt(pos) != '"') {
183             pos++;
184         }
185         int end = pos;
186         read("\"");
187         String JavaDoc s = page.substring(start, end);
188         return PageParser.parse(server, s, settings);
189     }
190
191     private void skipSpaces() {
192         while (page.charAt(pos) == ' ') {
193             pos++;
194         }
195     }
196
197     private void read(String JavaDoc s) throws Exception JavaDoc {
198         if (!readIf(s)) {
199             throw new Exception JavaDoc();
200         }
201     }
202
203     private boolean readIf(String JavaDoc s) {
204         skipSpaces();
205         if (page.regionMatches(pos, s, 0, s.length())) {
206             pos += s.length();
207             skipSpaces();
208             return true;
209         }
210         return false;
211     }
212
213     public static String JavaDoc escapeHtmlNoBreak(String JavaDoc s) {
214         return escapeHtml(s, false);
215     }
216
217     public static String JavaDoc escapeHtml(String JavaDoc s) {
218         return escapeHtml(s, true);
219     }
220
221     public static String JavaDoc escapeHtml(String JavaDoc s, boolean convertBreak) {
222         if (s == null) {
223             return null;
224         }
225         if (s.length() == 0) {
226             return "&nbsp;";
227         }
228         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(s.length());
229         boolean leadingSpace = true;
230         for (int i = 0; i < s.length(); i++) {
231             char c = s.charAt(i);
232             if (leadingSpace) {
233                 if (c == ' ') {
234                     buff.append("&nbsp;");
235                     continue;
236                 } else {
237                     leadingSpace = false;
238                 }
239             }
240             switch (c) {
241             case '$':
242                 // so that ${ } in the text is interpreted correctly
243
buff.append("&#36;");
244                 break;
245             case '<':
246                 buff.append("&lt;");
247                 break;
248             case '>':
249                 buff.append("&gt;");
250                 break;
251             case '&':
252                 buff.append("&amp;");
253                 break;
254             case '"':
255                 buff.append("&quot;");
256                 break;
257             case '\'':
258                 buff.append("&#39;");
259                 break;
260             case '\n':
261                 if (convertBreak) {
262                     buff.append("<br>");
263                     leadingSpace = true;
264                 } else {
265                     buff.append(c);
266                 }
267                 break;
268             default:
269                 if (c >= 128) {
270                     buff.append("&#");
271                     buff.append((int) c);
272                     buff.append(';');
273                 } else {
274                     buff.append(c);
275                 }
276             }
277         }
278         return buff.toString();
279     }
280
281     public static String JavaDoc escapeJavaScript(String JavaDoc s) {
282         if (s == null) {
283             return null;
284         }
285         if (s.length() == 0) {
286             return "";
287         }
288         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(s.length());
289         for (int i = 0; i < s.length(); i++) {
290             char c = s.charAt(i);
291             switch (c) {
292             case '"':
293                 buff.append("\\\"");
294                 break;
295             case '\'':
296                 buff.append("\\'");
297                 break;
298             case '\\':
299                 buff.append("\\\\");
300                 break;
301             case '\n':
302                 buff.append("\\n");
303                 break;
304             case '\r':
305                 buff.append("\\r");
306                 break;
307             case '\t':
308                 buff.append("\\t");
309                 break;
310             default:
311                 buff.append(c);
312             }
313         }
314         return buff.toString();
315         // return escapeHtml(buff.toString());
316
}
317 }
318
Popular Tags