Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: b33f2df579c271a4169e8a95b1eaf2712b54d2d2 (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
/*******************************************************************************
 * Copyright (c) 2004 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
 *******************************************************************************/
package org.eclipse.jst.jsp.core.internal.java;



import java.util.Properties;
import com.ibm.icu.util.StringTokenizer;

import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;

/**
 * class to handle chunks of text/regions
 * with escaped character data
 * @author pavery
 */
public class EscapedTextUtil {

	public static Properties fXMLtoJavaLookup = null;

	/**
	 * @return unescaped full text of that region, "" if there is no text
	 */
	public static String getUnescapedText(IStructuredDocumentRegion parent, ITextRegion r) {
		String test = (parent != r) ? parent.getFullText(r) : parent.getFullText();
		return getUnescapedText(test);
	}

	public static String getUnescapedText(String test) {
		initLookup();
		StringBuffer buffer = new StringBuffer();
		if (test != null) {
			StringTokenizer st = new StringTokenizer(test, "&;", true); //$NON-NLS-1$
			String tok1, tok2, tok3, transString;
			while (st.hasMoreTokens()) {
				tok1 = tok2 = tok3 = transString = ""; //$NON-NLS-1$
				tok1 = st.nextToken();
				if (tok1.equals("&") && st.hasMoreTokens()) //$NON-NLS-1$
				{
					tok2 = st.nextToken();
					if (st.hasMoreTokens()) {
						tok3 = st.nextToken();
					}
				}
				if (!(transString = fXMLtoJavaLookup.getProperty(tok1 + tok2 + tok3, "")).equals("")) //$NON-NLS-2$ //$NON-NLS-1$
				{
					buffer.append(transString);
				}
				else {
					buffer.append(tok1 + tok2 + tok3);
				}
			}
			return buffer.toString();
		}
		return ""; //$NON-NLS-1$
	}

	/**
	 * initialize lookup tables
	 */
	private static void initLookup() {
		fXMLtoJavaLookup = new Properties();
		fXMLtoJavaLookup.setProperty("'", "'"); //$NON-NLS-2$ //$NON-NLS-1$
		fXMLtoJavaLookup.setProperty(""", "\""); //$NON-NLS-2$ //$NON-NLS-1$
		fXMLtoJavaLookup.setProperty("&", "&"); //$NON-NLS-2$ //$NON-NLS-1$
		fXMLtoJavaLookup.setProperty("&lt;", "<"); //$NON-NLS-2$ //$NON-NLS-1$
		fXMLtoJavaLookup.setProperty("&gt;", ">"); //$NON-NLS-2$ //$NON-NLS-1$
		fXMLtoJavaLookup.setProperty("&nbsp;", " "); //$NON-NLS-2$ //$NON-NLS-1$
	}

	/**
	 * Get the String representation of an entity reference.
	 */
	public static String translateEntityReference(String entity) {
		return fXMLtoJavaLookup.getProperty(entity, entity);
	}
}

Back to the top