source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSHTML.java@ 24395

Last change on this file since 24395 was 16869, checked in by kjdon, 16 years ago

added license message

  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1/*
2 * GSHTML.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21/** GSHTML - provides some convenience methods for dealing
22 * with html
23 */
24public class GSHTML {
25
26 /** make a string html safe */
27 public static String htmlSafe(String input) {
28
29 StringBuffer filtered = new StringBuffer(input.length());
30 char c;
31 for (int i=0; i<input.length(); i++) {
32 c = input.charAt(i);
33 if (c == '<') {
34 filtered.append("&lt;");
35 } else if (c == '>') {
36 filtered.append("&gt;");
37 } else if (c == '"') {
38 filtered.append("&quot;");
39 } else if (c == '&') {
40 filtered.append("&amp;");
41 } else {
42 filtered.append(c);
43 }
44 }
45 return(filtered.toString());
46 }
47 /** undo the html safe action */
48 public static String htmlUnsafe(String input) {
49 StringBuffer filtered = new StringBuffer(input.length());
50 char c;
51 for (int i=0; i<input.length(); i++) {
52 c = input.charAt(i);
53 if (c =='&') {
54 int j=input.indexOf(';', i);
55 String entity = input.substring(i, j);
56 i=j;
57 if (entity.equals("&amp")) {
58 filtered.append('&');
59 } else if (entity.equals("&lt")){
60 filtered.append('<');
61 } else if (entity.equals("&gt")){
62 filtered.append('>');
63 } else if (entity.equals("&quot")) {
64 filtered.append('"');
65 } // else just ignore it.
66 } else {
67 filtered.append(c);
68 }
69
70 }
71 return(filtered.toString());
72 }
73
74 /** produce a default error page */
75 public static String errorPage(String error) {
76 String page = "<html><head><Title>GSDL3 Error!</Title></head>\n"+
77 "<body><h1>Greenstone Error!</h1>"+
78 "<p/>"+error+"</body></html>";
79 return page;
80 }
81
82}
Note: See TracBrowser for help on using the repository browser.