001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.composite.parser;
011:
012: import org.w3c.dom.NamedNodeMap;
013: import org.w3c.dom.Node;
014: import org.w3c.dom.NodeList;
015:
016: public class MarkupParser {
017:
018: public static String parseAndTrimTextMarkup(Node parentNode) {
019: return parseMarkup(parentNode).trim();
020: }
021:
022: private static String parseMarkup(Node parentNode) {
023: NodeList children = parentNode.getChildNodes();
024: StringBuffer text = new StringBuffer();
025: for (int i = 0; i < children.getLength(); i++) {
026: Node childNode = children.item(i);
027: if (childNode.getNodeType() == Node.TEXT_NODE) {
028: text.append(escapeText(childNode.getNodeValue()));
029: } else if (childNode.getNodeType() == Node.ELEMENT_NODE) {
030: text.append('<');
031: text.append(childNode.getNodeName());
032: // Add the attributes
033: NamedNodeMap attributes = childNode.getAttributes();
034: if (attributes != null) {
035: for (int x = 0; x < attributes.getLength(); x++) {
036: Node attribute = attributes.item(x);
037: String attributeName = attribute.getNodeName();
038: if (attributeName == null)
039: continue;
040: text.append(' ');
041: text.append(attributeName);
042: text.append(" = \""); //$NON-NLS-1$
043: text.append(attribute.getNodeValue());
044: text.append('"');
045: }
046: }
047: text.append('>');
048: text.append(parseMarkup(childNode));
049: text.append("</"); //$NON-NLS-1$
050: text.append(childNode.getNodeName());
051: text.append('>');
052: }
053: }
054: return text.toString();
055: }
056:
057: public static String escapeText(String input) {
058: StringBuffer result = new StringBuffer(input.length() + 10);
059: for (int i = 0; i < input.length(); ++i)
060: appendEscapedChar(result, input.charAt(i));
061: return result.toString();
062: }
063:
064: private static void appendEscapedChar(StringBuffer buffer, char c) {
065: String replacement = getReplacement(c);
066: if (replacement != null) {
067: buffer.append(replacement);
068: } else {
069: buffer.append(c);
070: }
071: }
072:
073: private static String getReplacement(char c) {
074: // Encode characters which need to be escaped for use in form text
075: // Replace tabs with spaces
076: switch (c) {
077: case '<':
078: return "<"; //$NON-NLS-1$
079: case '>':
080: return ">"; //$NON-NLS-1$
081: case '&':
082: return "&"; //$NON-NLS-1$
083: case '\t':
084: return " "; //$NON-NLS-1$
085: }
086: return null;
087: }
088:
089: /*
090: * Add paragraph tags if not already present
091: */
092: public static String createParagraph(String text, String imageTag) {
093: String result = ""; //$NON-NLS-1$
094: String trimmed = text.trim();
095: boolean addParagraphTags = trimmed.length() < 3
096: || trimmed.charAt(0) != '<'
097: || (trimmed.charAt(1) != 'p' && trimmed.charAt(1) != 'l');
098: if (addParagraphTags) {
099: result += "<p>"; //$NON-NLS-1$
100: }
101:
102: if (imageTag != null) {
103: result += "<img href=\""; //$NON-NLS-1$
104: result += imageTag;
105: result += "\"/> "; //$NON-NLS-1$
106: }
107:
108: result += trimmed;
109:
110: if (addParagraphTags) {
111: result += "</p>"; //$NON-NLS-1$
112: }
113: return result;
114: }
115:
116: }
|