1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | /** GSHTML - provides some convenience methods for dealing
|
---|
4 | * with html
|
---|
5 | */
|
---|
6 | public class GSHTML {
|
---|
7 |
|
---|
8 | /** make a string html safe */
|
---|
9 | public static String htmlSafe(String input) {
|
---|
10 |
|
---|
11 | StringBuffer filtered = new StringBuffer(input.length());
|
---|
12 | char c;
|
---|
13 | for (int i=0; i<input.length(); i++) {
|
---|
14 | c = input.charAt(i);
|
---|
15 | if (c == '<') {
|
---|
16 | filtered.append("<");
|
---|
17 | } else if (c == '>') {
|
---|
18 | filtered.append(">");
|
---|
19 | } else if (c == '"') {
|
---|
20 | filtered.append(""");
|
---|
21 | } else if (c == '&') {
|
---|
22 | filtered.append("&");
|
---|
23 | } else {
|
---|
24 | filtered.append(c);
|
---|
25 | }
|
---|
26 | }
|
---|
27 | return(filtered.toString());
|
---|
28 | }
|
---|
29 | /** undo the html safe action */
|
---|
30 | public static String htmlUnsafe(String input) {
|
---|
31 | StringBuffer filtered = new StringBuffer(input.length());
|
---|
32 | char c;
|
---|
33 | for (int i=0; i<input.length(); i++) {
|
---|
34 | c = input.charAt(i);
|
---|
35 | if (c =='&') {
|
---|
36 | int j=input.indexOf(';', i);
|
---|
37 | String entity = input.substring(i, j);
|
---|
38 | i=j;
|
---|
39 | if (entity.equals("&")) {
|
---|
40 | filtered.append('&');
|
---|
41 | } else if (entity.equals("<")){
|
---|
42 | filtered.append('<');
|
---|
43 | } else if (entity.equals(">")){
|
---|
44 | filtered.append('>');
|
---|
45 | } else if (entity.equals(""")) {
|
---|
46 | filtered.append('"');
|
---|
47 | } // else just ignore it.
|
---|
48 | } else {
|
---|
49 | filtered.append(c);
|
---|
50 | }
|
---|
51 |
|
---|
52 | }
|
---|
53 | return(filtered.toString());
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** produce a default error page */
|
---|
57 | public static String errorPage(String error) {
|
---|
58 | String page = "<html><head><Title>GSDL3 Error!</Title></head>\n"+
|
---|
59 | "<body><h1>Greenstone Error!</h1>"+
|
---|
60 | "<p/>"+error+"</body></html>";
|
---|
61 | return page;
|
---|
62 | }
|
---|
63 |
|
---|
64 | }
|
---|