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.pde.internal.core.util;
011:
012: import org.w3c.dom.Attr;
013: import org.w3c.dom.Element;
014: import org.w3c.dom.NamedNodeMap;
015: import org.w3c.dom.Node;
016: import org.w3c.dom.NodeList;
017: import org.w3c.dom.Text;
018:
019: /**
020: * CheatSheetUtil
021: *
022: */
023: public class CheatSheetUtil {
024:
025: /**
026: * Recursively finds and accumulates all element's text and element
027: * children into a String in raw XML form
028: * @param element
029: * @return
030: */
031: public static String parseElementText(Element element) {
032: // Puts all Text nodes in the full depth of the sub-tree
033: // underneath this Node
034: // i.e., there are neither adjacent Text nodes nor empty Text nodes.
035: element.normalize();
036: // Process only if there are children
037: if (element.getChildNodes().getLength() > 0) {
038: NodeList children = element.getChildNodes();
039: StringBuffer buffer = new StringBuffer();
040: // Traverse over each childe
041: for (int i = 0; i < children.getLength(); i++) {
042: Node node = children.item(i);
043: if (node.getNodeType() == Node.TEXT_NODE) {
044: // Accumulate the text children
045: buffer.append(((Text) node).getData());
046: } else if (node.getNodeType() == Node.ELEMENT_NODE) {
047: Element subElement = (Element) node;
048: // Append the open bracket
049: buffer.append('<');
050: // Append the element name
051: buffer.append(subElement.getNodeName());
052: // Parse element attributes
053: String elementAttributeText = parseElementAttributes(subElement);
054: // Append the attributes (if any)
055: if (elementAttributeText != null) {
056: buffer.append(elementAttributeText);
057: }
058: // Recursively accumulate element children
059: String value = parseElementText(subElement);
060: // Determine whether the element has any content or not
061: if (value.length() > 0) {
062: // The element had children
063: // Enclose the accumulated children with start and end tags
064: // Close off start element
065: buffer.append('>');
066: // Append element content
067: buffer.append(value);
068: // Append open bracket for end element tag
069: buffer.append('<');
070: buffer.append('/');
071: buffer.append(subElement.getNodeName());
072: } else {
073: // The element had no children
074: // Generate an abbreviated element tag
075: buffer.append('/');
076: }
077: // Append the close bracket
078: buffer.append('>');
079: }
080: }
081: // Return all accumulated children under the input element as a raw
082: // XML string
083: return buffer.toString();
084: }
085: return ""; //$NON-NLS-1$
086: }
087:
088: /**
089: * Aggregates all attributes from the given element, formats then into
090: * the proper key="value" XML format and returns them as one String
091: * @param element
092: * @return The formatted String or null if the element has no attributes
093: */
094: private static String parseElementAttributes(Element element) {
095: // Verify we have attributes
096: if (element.hasAttributes() == false) {
097: return null;
098: }
099: // Create the buffer
100: StringBuffer buffer = new StringBuffer();
101: // Get the attributes
102: NamedNodeMap attributeMap = element.getAttributes();
103: // Accumulate all attributes
104: for (int i = 0; i < attributeMap.getLength(); i++) {
105: Node node = attributeMap.item(i);
106: if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
107: Attr attribute = (Attr) node;
108: // Append space before attribute
109: buffer.append(' ');
110: // Append attribute name
111: buffer.append(attribute.getName());
112: // Append =
113: buffer.append('=');
114: // Append quote
115: buffer.append('"');
116: // Append attribute value
117: buffer.append(attribute.getValue());
118: // Append quote
119: buffer.append('"');
120: }
121: }
122:
123: return buffer.toString();
124: }
125:
126: }
|