KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > composite > parser > MarkupParser


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.cheatsheets.composite.parser;
13
14 import org.w3c.dom.NamedNodeMap JavaDoc;
15 import org.w3c.dom.Node JavaDoc;
16 import org.w3c.dom.NodeList JavaDoc;
17
18 public class MarkupParser {
19
20     public static String JavaDoc parseAndTrimTextMarkup(Node JavaDoc parentNode) {
21         return parseMarkup(parentNode).trim();
22     }
23     
24     private static String JavaDoc parseMarkup(Node JavaDoc parentNode) {
25         NodeList JavaDoc children = parentNode.getChildNodes();
26         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
27         for (int i = 0; i < children.getLength(); i++) {
28             Node JavaDoc childNode = children.item(i);
29             if (childNode.getNodeType() == Node.TEXT_NODE) {
30                 text.append(escapeText(childNode.getNodeValue()));
31             } else if (childNode.getNodeType() == Node.ELEMENT_NODE) {
32                 text.append('<');
33                 text.append(childNode.getNodeName());
34                 // Add the attributes
35
NamedNodeMap JavaDoc attributes = childNode.getAttributes();
36                 if (attributes != null) {
37                     for (int x = 0; x < attributes.getLength(); x++) {
38                         Node JavaDoc attribute = attributes.item(x);
39                         String JavaDoc attributeName = attribute.getNodeName();
40                         if (attributeName == null)
41                             continue;
42                         text.append(' ');
43                         text.append(attributeName);
44                         text.append(" = \""); //$NON-NLS-1$
45
text.append(attribute.getNodeValue());
46                         text.append('"');
47                     }
48                 }
49                 text.append('>');
50                 text.append(parseMarkup(childNode));
51                 text.append("</"); //$NON-NLS-1$
52
text.append(childNode.getNodeName());
53                 text.append('>');
54             }
55         }
56         return text.toString();
57     }
58
59     public static String JavaDoc escapeText(String JavaDoc input) {
60         StringBuffer JavaDoc result = new StringBuffer JavaDoc(input.length() + 10);
61         for (int i = 0; i < input.length(); ++i)
62             appendEscapedChar(result, input.charAt(i));
63         return result.toString();
64     }
65
66     private static void appendEscapedChar(StringBuffer JavaDoc buffer, char c) {
67         String JavaDoc replacement = getReplacement(c);
68         if (replacement != null) {
69             buffer.append(replacement);
70         } else {
71             buffer.append(c);
72         }
73     }
74
75     private static String JavaDoc getReplacement(char c) {
76         // Encode characters which need to be escaped for use in form text
77
// Replace tabs with spaces
78
switch (c) {
79             case '<' :
80                 return "&lt;"; //$NON-NLS-1$
81
case '>' :
82                 return "&gt;"; //$NON-NLS-1$
83
case '&' :
84                 return "&amp;"; //$NON-NLS-1$
85
case '\t' :
86                 return " "; //$NON-NLS-1$
87
}
88         return null;
89     }
90     
91     /*
92      * Add paragraph tags if not already present
93      */

94     public static String JavaDoc createParagraph(String JavaDoc text, String JavaDoc imageTag) {
95         String JavaDoc result = ""; //$NON-NLS-1$
96
String JavaDoc trimmed = text.trim();
97         boolean addParagraphTags = trimmed.length() < 3 || trimmed.charAt(0)!='<' ||
98           (trimmed.charAt(1)!='p' && trimmed.charAt(1) != 'l');
99         if (addParagraphTags) {
100             result += "<p>"; //$NON-NLS-1$
101
}
102
103         if (imageTag != null) {
104             result += "<img HREF=\""; //$NON-NLS-1$
105
result += imageTag;
106             result += "\"/> "; //$NON-NLS-1$
107
}
108
109         result += trimmed;
110
111         if (addParagraphTags) {
112             result += "</p>"; //$NON-NLS-1$
113
}
114         return result;
115     }
116
117 }
118
Popular Tags