1 18 19 package org.apache.jmeter.save; 20 21 import java.util.LinkedList ; 22 import java.util.NoSuchElementException ; 23 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.DefaultConfiguration; 26 import org.apache.jmeter.testelement.TestElement; 27 import org.apache.jmeter.testelement.TestElementTraverser; 28 import org.apache.jmeter.testelement.property.CollectionProperty; 29 import org.apache.jmeter.testelement.property.JMeterProperty; 30 import org.apache.jmeter.testelement.property.MapProperty; 31 import org.apache.jmeter.testelement.property.TestElementProperty; 32 33 36 public class TestElementSaver 37 implements TestElementTraverser, SaveServiceConstants 38 { 39 String name; 40 LinkedList stack = new LinkedList (); 41 42 DefaultConfiguration rootConfig = null; 43 44 public TestElementSaver(String name) 45 { 46 this.name = name; 47 } 48 49 public Configuration getConfiguration() 50 { 51 return rootConfig; 52 } 53 54 57 public void startTestElement(TestElement el) 58 { 59 DefaultConfiguration config = 60 new DefaultConfiguration("testelement", "testelement"); 61 config.setAttribute("class", el.getClass().getName()); 62 if (rootConfig == null) 63 { 64 rootConfig = config; 65 if (name != null && name.length() > 0) 66 { 67 rootConfig.setAttribute("name", name); 68 } 69 } 70 else 71 { 72 setConfigName(config); 73 } 74 stack.add(config); 75 } 76 77 public void setConfigName(DefaultConfiguration config) 78 { 79 if (!(stack.getLast() instanceof Configuration)) 80 { 81 Object key = stack.removeLast(); 82 config.setAttribute("name", key.toString()); 83 } 84 } 85 86 89 public void endTestElement(TestElement el) 90 { 91 } 92 93 96 public void simplePropertyValue(JMeterProperty value) 97 { 98 try 99 { 100 Object parent = stack.getLast(); 101 if (!(parent instanceof Configuration)) 102 { 103 DefaultConfiguration config = 104 new DefaultConfiguration("property", "property"); 105 config.setValue(value != null ? value.toString() : ""); 106 config.setAttribute("name", parent.toString()); 107 config.setAttribute(XML_SPACE, PRESERVE); 108 stack.removeLast(); 109 stack.add(config); 110 } 111 112 if (parent instanceof DefaultConfiguration 113 && value instanceof Configuration) 114 { 115 ((DefaultConfiguration) parent).addChild((Configuration) value); 116 } 117 else if ( 118 parent instanceof DefaultConfiguration 119 && !(value instanceof Configuration)) 120 { 121 DefaultConfiguration config = 122 new DefaultConfiguration("string", "string"); 123 config.setValue(value.toString()); 124 config.setAttribute(XML_SPACE, PRESERVE); 125 ((DefaultConfiguration) parent).addChild(config); 126 } 127 } 128 catch (NoSuchElementException e) 129 {} 130 } 131 132 135 public void startMap(MapProperty map) 136 { 137 DefaultConfiguration config = new DefaultConfiguration("map", "map"); 138 config.setAttribute("class", map.getObjectValue().getClass().getName()); 139 config.setAttribute("name",map.getName()); 140 config.setAttribute("propType",map.getClass().getName()); 141 stack.add(config); 142 } 143 144 151 152 155 public void startCollection(CollectionProperty col) 156 { 157 DefaultConfiguration config = 158 new DefaultConfiguration("collection", "collection"); 159 config.setAttribute("class", col.getObjectValue().getClass().getName()); 160 config.setAttribute("name",col.getName()); 161 config.setAttribute("propType",col.getClass().getName()); 162 stack.add(config); 163 } 164 165 172 173 176 public void endProperty(JMeterProperty key) 177 { 178 finishConfig(); 179 } 180 181 private void finishConfig() 182 { 183 if (stack.size() > 1) 184 { 185 Configuration config = (Configuration) stack.removeLast(); 186 ((DefaultConfiguration) stack.getLast()).addChild(config); 187 } 188 } 189 190 193 public void startProperty(JMeterProperty key) 194 { 195 if (key instanceof CollectionProperty) 196 { 197 startCollection((CollectionProperty) key); 198 } 199 else if (key instanceof MapProperty) 200 { 201 startMap((MapProperty) key); 202 } 203 else if (key instanceof TestElementProperty) 204 { 205 stack.addLast(key.getName()); 206 } 207 else 208 { 209 DefaultConfiguration config = 210 new DefaultConfiguration("property", "property"); 211 config.setValue(key.getStringValue()); 212 config.setAttribute("name", key.getName()); 213 config.setAttribute("propType", key.getClass().getName()); 214 config.setAttribute(XML_SPACE, PRESERVE); 215 stack.addLast(config); 216 } 217 218 } 219 220 } 221 | Popular Tags |