Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: db84f9984df7c445d31cd103d899b65d1e31580d (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.help.ui.internal.util;

/**
 * Utility class for preparing strings for display in a FormText widget by
 * escaping the necessary characters
 */

public class EscapeUtils {

	/**
	 * Replace every occurrence of &, <, >, ', " by an escape character
	 * Replace tabs with spaces
	 * @param value the original string, may not be null
	 * @return the escaped string
	 */
	public static String escapeSpecialChars(String value) {
		return escapeSpecialChars(value, false);
	}

	/**
	 * Replace every occurrence of &, <, >, ', " by an escape character
	 * but allow <b> and </b> through
	 * Replace tabs with spaces
	 * @param value the original string, may not be null
	 * @return the escaped string
	 */
	public static String escapeSpecialCharsLeavinggBold(String value) {
		return escapeSpecialChars(value, true);
	}

	public static String escapeAmpersand(String value) {
		return value.replaceAll("&", "&amp;"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	* Escape any ampersands used in a label
	**/
	public static String escapeForLabel(String message) {
		// Make the most common case - i.e. no ampersand the
		// most efficient
		if (message.indexOf('&') < 0) {
			return message;
		}

		int next = 0;
		StringBuilder result = new StringBuilder();
		int index = message.indexOf('&');
		while (index >= 0) {
			result.append(message.substring(next, index + 1));
			result.append('&');
			next = index + 1;
			index = message.indexOf('&', next);
		}
		result.append(message.substring(next));
		return result.toString();
	}

	private static String escapeSpecialChars(String value, boolean leaveBold) {
		if (value == null) {
			return null;
		}
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < value.length(); i++) {
			char c = value.charAt(i);

			switch (c) {
			case '&':
				buf.append("&amp;"); //$NON-NLS-1$
				break;
			case '<':
				if (leaveBold) {
					int length = value.length();
					if (i +  6 < length) {
						String tag = value.substring(i, i+7);
						if (tag.equalsIgnoreCase("</code>")) { //$NON-NLS-1$
							buf.append("</span>"); //$NON-NLS-1$
							i+= 6;
							continue;
						}
					}
					if (i +  5 < length) {
						String tag = value.substring(i, i+6);
						if (tag.equalsIgnoreCase("<code>")) { //$NON-NLS-1$
							buf.append("<span font=\"code\">"); //$NON-NLS-1$
							i+= 5;
							continue;
						}
					}
					if (i + 3 < length) {
						String tag = value.substring(i, i + 4);
						if (tag.equalsIgnoreCase("</b>")) { //$NON-NLS-1$
							buf.append(tag);
							i += 3;
							continue;
						}
						if (tag.equalsIgnoreCase("<br>")) { //$NON-NLS-1$
							buf.append("<br/>"); //$NON-NLS-1$
							i+= 3;
							continue;
						}
					}
					if (i + 2 < length) {
						String tag = value.substring(i, i + 3);
						if (tag.equalsIgnoreCase("<b>")) { //$NON-NLS-1$
							buf.append(tag);
							i += 2;
							continue;
						}
					}
				}
				buf.append("&lt;"); //$NON-NLS-1$
				break;
			case '>':
				buf.append("&gt;"); //$NON-NLS-1$
				break;
			case '\'':
				buf.append("&apos;"); //$NON-NLS-1$
				break;
			case '\"':
				buf.append("&quot;"); //$NON-NLS-1$
				break;
			case 160:
				buf.append(" "); //$NON-NLS-1$
				break;
			case '\t':
				buf.append(' ');
				break;
			default:
				buf.append(c);
				break;
			}
		}
		return buf.toString();
	}

}

Back to the top