001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.visualizers;
020:
021: import javax.swing.tree.DefaultMutableTreeNode;
022:
023: import org.apache.jorphan.logging.LoggingManager;
024: import org.apache.log.Logger;
025: import org.w3c.dom.Attr;
026: import org.w3c.dom.CDATASection;
027: import org.w3c.dom.Comment;
028: import org.w3c.dom.NamedNodeMap;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.NodeList;
031: import org.w3c.dom.Text;
032: import org.xml.sax.SAXException;
033:
034: /**
035: * A extended class of DefaultMutableTreeNode except that it also attached XML
036: * node and convert XML document into DefaultMutableTreeNode.
037: *
038: */
039: public class XMLDefaultMutableTreeNode extends DefaultMutableTreeNode {
040: private static final Logger log = LoggingManager
041: .getLoggerForClass();
042: // private static final int LIMIT_STR_SIZE = 100;
043: // private boolean isRoot;
044: private transient Node xmlNode;
045:
046: public XMLDefaultMutableTreeNode() {
047: log.warn("Constructor only intended for use in testing"); // $NON-NLS-1$
048: }
049:
050: public XMLDefaultMutableTreeNode(Node root) throws SAXException {
051: super (root.getNodeName());
052: initAttributeNode(root, this );
053: initRoot(root);
054:
055: }
056:
057: public XMLDefaultMutableTreeNode(String name, Node xmlNode) {
058: super (name);
059: this .xmlNode = xmlNode;
060:
061: }
062:
063: /**
064: * init root
065: *
066: * @param root
067: * @throws SAXException
068: */
069: private void initRoot(Node xmlRoot) throws SAXException {
070:
071: NodeList childNodes = xmlRoot.getChildNodes();
072: for (int i = 0; i < childNodes.getLength(); i++) {
073: Node childNode = childNodes.item(i);
074: initNode(childNode, this );
075: }
076:
077: }
078:
079: /**
080: * init node
081: *
082: * @param node
083: * @param mTreeNode
084: * @throws SAXException
085: */
086: private void initNode(Node node, XMLDefaultMutableTreeNode mTreeNode)
087: throws SAXException {
088:
089: switch (node.getNodeType()) {
090: case Node.ELEMENT_NODE:
091: initElementNode(node, mTreeNode);
092: break;
093:
094: case Node.TEXT_NODE:
095: initTextNode((Text) node, mTreeNode);
096: break;
097:
098: case Node.CDATA_SECTION_NODE:
099: initCDATASectionNode((CDATASection) node, mTreeNode);
100: break;
101: case Node.COMMENT_NODE:
102: initCommentNode((Comment) node, mTreeNode);
103: break;
104:
105: default:
106: // if other node type, we will just skip it
107: break;
108:
109: }
110:
111: }
112:
113: /**
114: * init element node
115: *
116: * @param node
117: * @param mTreeNode
118: * @throws SAXException
119: */
120: private void initElementNode(Node node,
121: DefaultMutableTreeNode mTreeNode) throws SAXException {
122: String nodeName = node.getNodeName();
123:
124: NodeList childNodes = node.getChildNodes();
125: XMLDefaultMutableTreeNode childTreeNode = new XMLDefaultMutableTreeNode(
126: nodeName, node);
127:
128: mTreeNode.add(childTreeNode);
129: initAttributeNode(node, childTreeNode);
130: for (int i = 0; i < childNodes.getLength(); i++) {
131: Node childNode = childNodes.item(i);
132: initNode(childNode, childTreeNode);
133: }
134:
135: }
136:
137: /**
138: * init attribute node
139: *
140: * @param node
141: * @param mTreeNode
142: * @throws SAXException
143: */
144: private void initAttributeNode(Node node,
145: DefaultMutableTreeNode mTreeNode) throws SAXException {
146: NamedNodeMap nm = node.getAttributes();
147: for (int i = 0; i < nm.getLength(); i++) {
148: Attr nmNode = (Attr) nm.item(i);
149: String value = nmNode.getName() + " = \""
150: + nmNode.getValue() + "\""; // $NON-NLS-1$ $NON-NLS-2$
151: XMLDefaultMutableTreeNode attributeNode = new XMLDefaultMutableTreeNode(
152: value, nmNode);
153: mTreeNode.add(attributeNode);
154:
155: }
156: }
157:
158: /**
159: * init comment Node
160: *
161: * @param node
162: * @param mTreeNode
163: * @throws SAXException
164: */
165: private void initCommentNode(Comment node,
166: DefaultMutableTreeNode mTreeNode) throws SAXException {
167: String data = node.getData();
168: if (data != null && data.length() > 0) {
169: String value = "<!--" + node.getData() + "-->"; // $NON-NLS-1$ $NON-NLS-2$
170: XMLDefaultMutableTreeNode commentNode = new XMLDefaultMutableTreeNode(
171: value, node);
172: mTreeNode.add(commentNode);
173: }
174: }
175:
176: /**
177: * init CDATASection Node
178: *
179: * @param node
180: * @param mTreeNode
181: * @throws SAXException
182: */
183: private void initCDATASectionNode(CDATASection node,
184: DefaultMutableTreeNode mTreeNode) throws SAXException {
185: String data = node.getData();
186: if (data != null && data.length() > 0) {
187: String value = "<!-[CDATA" + node.getData() + "]]>"; // $NON-NLS-1$ $NON-NLS-2$
188: XMLDefaultMutableTreeNode commentNode = new XMLDefaultMutableTreeNode(
189: value, node);
190: mTreeNode.add(commentNode);
191: }
192: }
193:
194: /**
195: * init the TextNode
196: *
197: * @param node
198: * @param mTreeNode
199: * @throws SAXException
200: */
201: private void initTextNode(Text node,
202: DefaultMutableTreeNode mTreeNode) throws SAXException {
203: String text = node.getNodeValue().trim();
204: if (text != null && text.length() > 0) {
205: XMLDefaultMutableTreeNode textNode = new XMLDefaultMutableTreeNode(
206: text, node);
207: mTreeNode.add(textNode);
208: }
209: }
210:
211: /**
212: * get the xml node
213: *
214: * @return the XML node
215: */
216: public Node getXMLNode() {
217: return xmlNode;
218: }
219: }
|