|
JMeter example source code file (RenderAsXML.java)
This example JMeter source code file (RenderAsXML.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The JMeter RenderAsXML.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jmeter.visualizers;
import java.awt.Component;
import java.awt.GridLayout;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.util.XPathUtil;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;
import org.xml.sax.SAXException;
public class RenderAsXML extends SamplerResultTab
implements ResultRenderer {
private static final Logger log = LoggingManager.getLoggerForClass();
private static final byte[] XML_PFX = {'<','?','x','m','l',' '};//"
* TODO implement to find any nodes in the tree using TreePath.
*
*/
private static class DOMTreePanel extends JPanel {
private static final long serialVersionUID = 6871690021183779153L;
private JTree domJTree;
public DOMTreePanel(org.w3c.dom.Document document) {
super(new GridLayout(1, 0));
try {
Node firstElement = getFirstElement(document);
DefaultMutableTreeNode top = new XMLDefaultMutableTreeNode(firstElement);
domJTree = new JTree(top);
domJTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
domJTree.setShowsRootHandles(true);
JScrollPane domJScrollPane = new JScrollPane(domJTree);
domJTree.setAutoscrolls(true);
this.add(domJScrollPane);
ToolTipManager.sharedInstance().registerComponent(domJTree);
domJTree.setCellRenderer(new DomTreeRenderer());
} catch (SAXException e) {
log.warn("Error trying to parse document", e);
}
}
/**
* Skip all DTD nodes, all prolog nodes. They dont support in tree view
* We let user to insert them however in DOMTreeView, we dont display it
*
* @param root
* @return
*/
private Node getFirstElement(Node parent) {
NodeList childNodes = parent.getChildNodes();
Node toReturn = parent; // Must return a valid node, or may generate an NPE
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
toReturn = childNode;
if (childNode.getNodeType() == Node.ELEMENT_NODE){
break;
}
}
return toReturn;
}
/**
* This class is to view as tooltext. This is very useful, when the
* contents has long string and does not fit in the view. it will also
* automatically wrap line for each 100 characters since tool tip
* support html. author <a href="mailto:d.maung@mdl.com">Dave Maung
*/
private static class DomTreeRenderer extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 240210061375790195L;
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean phasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, phasFocus);
DefaultMutableTreeNode valueTreeNode = (DefaultMutableTreeNode) value;
setToolTipText(getHTML(valueTreeNode.toString(), "<br>", 100)); // $NON-NLS-1$
return this;
}
/**
* get the html
*
* @param str
* @param separator
* @param maxChar
* @return
*/
private String getHTML(String str, String separator, int maxChar) {
StringBuilder strBuf = new StringBuilder("<html>"); // $NON-NLS-1$
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (i % maxChar == 0 && i != 0) {
strBuf.append(separator);
}
strBuf.append(encode(chars[i]));
}
strBuf.append("</b> |