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
|
/*******************************************************************************
* Copyright (c) 2001, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* yyyymmdd bug Email and other contact information
* -------- -------- -----------------------------------------------------------
* 20060222 127443 jesper@selskabet.org - Jesper S Moller
* 20080324 186456 makandre@ca.ibm.com - Andrew Mak
*******************************************************************************/
package org.eclipse.wst.ws.internal.explorer.platform.util;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.http.HttpServletResponse;
public final class HTMLUtils
{
public static final String UTF8_ENCODING = "UTF-8";
public static final String LINE_BREAK = "<br>";
public static final String LINE_SEPARATOR = System.getProperties().getProperty("line.separator");
/**
* Get the HTML tag for an image.
* @return String The HTML tag for this image.
* @param HttpServletResponse To encode the src attribute.
* @param String src attribute defining the path to the image file. This must include the context path.
* @param String Optional alt text to be displayed with the image. Set this to null to disable the attribute.
* @param String Optional width of the rendered image. Set this to null to disable the attribute.
* @param String Optional height of the rendered imag. Set this to null to disable the attribute.
* @param Hashtable Optional key-value pairs of additional strings to be added as-is to the tag. Set this to null to disable.
* @return String The image tag.
*/
public static final String getHTMLImageTag(HttpServletResponse response,String src,String alt,String width,String height,Hashtable additionalAttributes)
{
StringBuffer tag = new StringBuffer("<img src=\"");
tag.append(response.encodeURL(src)).append('\"');
if (alt == null)
alt = "";
tag.append(" alt=\"").append(alt).append('\"');
tag.append(" title=\"").append(alt).append('\"');
if (width != null)
tag.append(" width=").append(width);
if (height != null)
tag.append(" height=").append(height);
tag.append(" border=0");
if (additionalAttributes != null)
{
Enumeration keys = additionalAttributes.keys();
while (keys.hasMoreElements())
{
String key = (String)keys.nextElement();
tag.append(' ').append(key).append("=\"").append((String)additionalAttributes.get(key)).append('\"');
}
}
tag.append('>');
return tag.toString();
}
/**
* Get the HTML tag for this link. The complete tag consists of the open tag <a> followed by a subelement and, finally, by an end tag </a>.
* @return String The HTML tag of the link.
* @param HttpServletResponse To encode the href attribute.
* @param String The href attribute of the link. This must include the context path.
* @param String target Optional target attribute for the link. Set this to null to disable the attribute.
* @param String Optional name for the link. Set this to null to disable the attribute.
* @param String The label which acts as the subelement. i.e. <a>label</a>.
* @param Hashtable Optional key-value pairs of additional string attributes to be added as-is to the open tag. Set this to null to disable.
*/
public static final String getHTMLLinkTag(HttpServletResponse response,String href,String target,String name,String label,Hashtable additionalAttributes)
{
StringBuffer tag = new StringBuffer("<a href=\"");
tag.append(response.encodeURL(href)).append('\"');
if (target != null)
tag.append(" target=\"").append(target).append('\"');
if (name != null)
tag.append(" name=\"").append(name).append('\"');
if (additionalAttributes != null)
{
Enumeration keys = additionalAttributes.keys();
while (keys.hasMoreElements())
{
String key = (String)keys.nextElement();
tag.append(' ').append(key).append("=\"").append((String)additionalAttributes.get(key)).append('\"');
}
}
tag.append('>');
tag.append(label);
tag.append("</a>");
return tag.toString();
}
/**
* Get the HTML tag for a red asterist.
* @return String The HTML tag for the red asterisk.
*/
public static final String redAsterisk()
{
return "<font color=\"#ff0000\">*</font>";
}
/**
* Get the Javascript mangled version of a given input String.
* @return String The Javascript mangled String.
*/
public static final String JSMangle(String input)
{
if (input == null)
return "";
StringBuffer mangledOutput = new StringBuffer();
for (int i=0;i<input.length();i++)
{
char c = input.charAt(i);
switch (c)
{
case '\n':
mangledOutput.append("\\n");
break;
case '\r':
mangledOutput.append("\\r");
break;
case '\\':
case '\"':
case '\'':
mangledOutput.append('\\');
default:
mangledOutput.append(c);
}
}
return mangledOutput.toString();
}
private static final String LESS_THAN = "<";
private static final String LESS_THAN_HTML_ENTITY = "<";
private static final String GREATER_THAN = ">";
private static final String GREATER_THAN_HTML_ENTITY = ">";
private static final String SPACE = " ";
private static final String SPACE_HTML_ENTITY = " ";
private static final String AMPERSAND = "&";
private static final String AMPERSAND_HTML_ENTITY = "&";
private static final String QUOTATION = "\"";
private static final String QUOTATION_HTML_ENTITY = """;
/**
* Replace special characters with HTML entities representing these characters.
* Note that this will also convert normal spaces into non-breaking spaces, which may be good for
* presentation but not for editing.
*
* @return String The converted String. Note: Order is important so that corrected entity references are not re-mangled.
*/
public static final String charactersToHTMLEntities(String s) {
s = stringReplace(s, AMPERSAND, AMPERSAND_HTML_ENTITY);
s = stringReplace(s, LESS_THAN, LESS_THAN_HTML_ENTITY);
s = stringReplace(s, GREATER_THAN, GREATER_THAN_HTML_ENTITY);
s = stringReplace(s, SPACE, SPACE_HTML_ENTITY);
s = stringReplace(s, QUOTATION, QUOTATION_HTML_ENTITY);
return s;
}
/**
* Replace special characters with HTML entities representing these characters
* @return String The converted String. Note: Order is important so that corrected entity references are not re-mangled.
*/
public static final String charactersToHTMLEntitiesStrict(String s) {
s = stringReplace(s, AMPERSAND, AMPERSAND_HTML_ENTITY);
s = stringReplace(s, LESS_THAN, LESS_THAN_HTML_ENTITY);
s = stringReplace(s, GREATER_THAN, GREATER_THAN_HTML_ENTITY);
s = stringReplace(s, QUOTATION, QUOTATION_HTML_ENTITY);
return s;
}
/**
* Replace HTML character entities with their associated characters.
* @return String The converted String.
*/
public static final String htmlEntitiesToCharacters(String s) {
s = stringReplace(s, LESS_THAN_HTML_ENTITY, LESS_THAN);
s = stringReplace(s, GREATER_THAN_HTML_ENTITY, GREATER_THAN);
s = stringReplace(s, SPACE_HTML_ENTITY, SPACE);
s = stringReplace(s, AMPERSAND_HTML_ENTITY, AMPERSAND);
s = stringReplace(s, QUOTATION_HTML_ENTITY, QUOTATION);
return s;
}
private static final String stringReplace(String s, String oldString, String newString) {
String sCopy = s;
int fromIndex = 0;
int oldStringIndex = sCopy.indexOf(oldString, fromIndex);
StringBuffer sb = new StringBuffer();
while (oldStringIndex != -1) {
sb.setLength(0);
sb.append(sCopy.substring(0, oldStringIndex));
sb.append(newString);
sb.append(sCopy.substring(oldStringIndex + oldString.length(), sCopy.length()));
sCopy = sb.toString();
fromIndex = oldStringIndex + newString.length();
oldStringIndex = sCopy.indexOf(oldString, fromIndex);
}
return sCopy;
}
}
|