KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > CheatSheetUtil


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.pde.internal.core.util;
13
14 import org.w3c.dom.Attr JavaDoc;
15 import org.w3c.dom.Element JavaDoc;
16 import org.w3c.dom.NamedNodeMap JavaDoc;
17 import org.w3c.dom.Node JavaDoc;
18 import org.w3c.dom.NodeList JavaDoc;
19 import org.w3c.dom.Text JavaDoc;
20
21 /**
22  * CheatSheetUtil
23  *
24  */

25 public class CheatSheetUtil {
26
27     /**
28      * Recursively finds and accumulates all element's text and element
29      * children into a String in raw XML form
30      * @param element
31      * @return
32      */

33     public static String JavaDoc parseElementText(Element JavaDoc element) {
34         // Puts all Text nodes in the full depth of the sub-tree
35
// underneath this Node
36
// i.e., there are neither adjacent Text nodes nor empty Text nodes.
37
element.normalize();
38         // Process only if there are children
39
if (element.getChildNodes().getLength() > 0) {
40             NodeList JavaDoc children = element.getChildNodes();
41             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
42             // Traverse over each childe
43
for (int i = 0; i < children.getLength(); i++) {
44                 Node JavaDoc node = children.item(i);
45                 if (node.getNodeType() == Node.TEXT_NODE) {
46                     // Accumulate the text children
47
buffer.append(((Text JavaDoc)node).getData());
48                 } else if (node.getNodeType() == Node.ELEMENT_NODE) {
49                     Element JavaDoc subElement = (Element JavaDoc)node;
50                     // Append the open bracket
51
buffer.append('<');
52                     // Append the element name
53
buffer.append(subElement.getNodeName());
54                     // Parse element attributes
55
String JavaDoc elementAttributeText =
56                         parseElementAttributes(subElement);
57                     // Append the attributes (if any)
58
if (elementAttributeText != null) {
59                         buffer.append(elementAttributeText);
60                     }
61                     // Recursively accumulate element children
62
String JavaDoc value = parseElementText(subElement);
63                     // Determine whether the element has any content or not
64
if (value.length() > 0) {
65                         // The element had children
66
// Enclose the accumulated children with start and end tags
67
// Close off start element
68
buffer.append('>');
69                         // Append element content
70
buffer.append(value);
71                         // Append open bracket for end element tag
72
buffer.append('<');
73                         buffer.append('/');
74                         buffer.append(subElement.getNodeName());
75                     } else {
76                         // The element had no children
77
// Generate an abbreviated element tag
78
buffer.append('/');
79                     }
80                     // Append the close bracket
81
buffer.append('>');
82                 }
83             }
84             // Return all accumulated children under the input element as a raw
85
// XML string
86
return buffer.toString();
87         }
88         return ""; //$NON-NLS-1$
89
}
90         
91     /**
92      * Aggregates all attributes from the given element, formats then into
93      * the proper key="value" XML format and returns them as one String
94      * @param element
95      * @return The formatted String or null if the element has no attributes
96      */

97     private static String JavaDoc parseElementAttributes(Element JavaDoc element) {
98         // Verify we have attributes
99
if (element.hasAttributes() == false) {
100             return null;
101         }
102         // Create the buffer
103
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
104         // Get the attributes
105
NamedNodeMap JavaDoc attributeMap = element.getAttributes();
106         // Accumulate all attributes
107
for (int i = 0; i < attributeMap.getLength(); i++) {
108             Node JavaDoc node = attributeMap.item(i);
109             if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
110                 Attr JavaDoc attribute = (Attr JavaDoc)node;
111                 // Append space before attribute
112
buffer.append(' ');
113                 // Append attribute name
114
buffer.append(attribute.getName());
115                 // Append =
116
buffer.append('=');
117                 // Append quote
118
buffer.append('"');
119                 // Append attribute value
120
buffer.append(attribute.getValue());
121                 // Append quote
122
buffer.append('"');
123             }
124         }
125         
126         return buffer.toString();
127     }
128     
129 }
130
Popular Tags