Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 99acee9e0a841c4c0a05764dadddd3ce395aaadb (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
/*******************************************************************************
 * Copyright (c) 2006, 2015 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.ui.internal.cheatsheets.composite.parser;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class MarkupParser {

	public static String parseAndTrimTextMarkup(Node parentNode) {
		return parseMarkup(parentNode).trim();
	}

	private static String parseMarkup(Node parentNode) {
	    NodeList children = parentNode.getChildNodes();
		StringBuffer text = new StringBuffer();
		for (int i = 0; i < children.getLength(); i++) {
			Node childNode = children.item(i);
			if (childNode.getNodeType() == Node.TEXT_NODE) {
				text.append(escapeText(childNode.getNodeValue()));
			} else if (childNode.getNodeType() == Node.ELEMENT_NODE) {
				text.append('<');
				text.append(childNode.getNodeName());
				// Add the attributes
				NamedNodeMap attributes = childNode.getAttributes();
				if (attributes != null) {
					for (int x = 0; x < attributes.getLength(); x++) {
						Node attribute = attributes.item(x);
						String attributeName = attribute.getNodeName();
						if (attributeName == null)
							continue;
						text.append(' ');
						text.append(attributeName);
						text.append(" = \""); //$NON-NLS-1$
						text.append(attribute.getNodeValue());
						text.append('"');
					}
				}
				text.append('>');
				text.append(parseMarkup(childNode));
				text.append("</"); //$NON-NLS-1$
				text.append(childNode.getNodeName());
				text.append('>');
			}
		}
		return text.toString();
	}

	public static String escapeText(String input) {
		StringBuffer result = new StringBuffer(input.length() + 10);
		for (int i = 0; i < input.length(); ++i)
			appendEscapedChar(result, input.charAt(i));
		return result.toString();
	}

	private static void appendEscapedChar(StringBuffer buffer, char c) {
		String replacement = getReplacement(c);
		if (replacement != null) {
			buffer.append(replacement);
		} else {
			buffer.append(c);
		}
	}

	private static String getReplacement(char c) {
		// Encode characters which need to be escaped for use in form text
		// Replace tabs with spaces
		switch (c) {
			case '<' :
				return "&lt;"; //$NON-NLS-1$
			case '>' :
				return "&gt;"; //$NON-NLS-1$
			case '&' :
				return "&amp;"; //$NON-NLS-1$
			case '\t' :
				return " "; //$NON-NLS-1$
		}
		return null;
	}

	/*
	 * Add paragraph tags if not already present
	 */
	public static String createParagraph(String text, String imageTag) {
		String result = ""; //$NON-NLS-1$
		String trimmed = text.trim();
		boolean addParagraphTags = trimmed.length() < 3 || trimmed.charAt(0)!='<' ||
		  (trimmed.charAt(1)!='p' && trimmed.charAt(1) != 'l');
		if (addParagraphTags) {
			result +=  "<p>"; //$NON-NLS-1$
		}

		if (imageTag != null) {
			result += "<img href=\""; //$NON-NLS-1$
			result += imageTag;
			result += "\"/> "; //$NON-NLS-1$
		}

		result += trimmed;

		if (addParagraphTags) {
			result += "</p>"; //$NON-NLS-1$
		}
		return result;
	}

}

Back to the top