1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.Serializable ; 22 import java.util.Collection ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import org.apache.jmeter.engine.util.CompoundVariable; 27 import org.apache.jmeter.samplers.SampleResult; 28 import org.apache.jmeter.samplers.Sampler; 29 import org.apache.jmeter.util.JMeterUtils; 30 import org.apache.jorphan.logging.LoggingManager; 31 import org.apache.log.Logger; 32 33 48 public class XPath extends AbstractFunction implements Serializable 49 { 50 transient private static final Logger log = LoggingManager.getLoggerForClass(); 51 private static final String KEY = "__XPath"; 57 private static final List desc = new LinkedList (); 58 59 60 private Object [] values; 62 static { 63 desc.add(JMeterUtils.getResString("xpath_file_file_name")); 64 desc.add(JMeterUtils.getResString("xpath_expression")); 65 } 66 67 public XPath() 68 { 69 } 70 71 public Object clone() 72 { 73 XPath newReader = new XPath(); 74 return newReader; 75 } 76 77 80 public synchronized String execute( 81 SampleResult previousResult, 82 Sampler currentSampler) 83 throws InvalidVariableException 84 { 85 String myValue = ""; 86 87 String fileName = 88 ((org.apache.jmeter.engine.util.CompoundVariable) values[0]) 89 .execute(); 90 String xpathString = 91 ((org.apache.jmeter.engine.util.CompoundVariable) values[1]) 92 .execute(); 93 94 95 log.debug("execute (" + fileName + " "+xpathString+") "); 96 97 98 try 99 { 100 myValue = XPathWrapper.getXPathString(fileName, xpathString); 101 } 102 catch (NumberFormatException e) 103 { 104 log.warn(Thread.currentThread().getName()+" - can't parse column number: " 105 + " "+ e.toString()); 106 } 107 catch (IndexOutOfBoundsException e) 108 { 109 log.warn(Thread.currentThread().getName()+" - invalid column number: at row " +XPathWrapper.getCurrentRow(fileName) + " " 110 + e.toString()); 111 } 112 113 log.debug("execute value: "+myValue); 114 115 return myValue; 116 } 117 118 121 public List getArgumentDesc() 122 { 123 return desc; 124 } 125 126 129 public String getReferenceKey() 130 { 131 return KEY; 132 } 133 134 137 public void setParameters(Collection parameters) 138 throws InvalidVariableException 139 { 140 log.debug("setParameter - Collection.size=" + parameters.size()); 141 142 values = parameters.toArray(); 143 144 if (log.isDebugEnabled()){ 145 for (int i=0;i <parameters.size();i++){ 146 log.debug("i:"+((CompoundVariable)values[i]).execute()); 147 } 148 } 149 150 if (values.length != 2) 151 { 152 throw new InvalidVariableException("Wrong number of parameters; 2 != "+values.length); 153 } 154 155 160 XPathWrapper.clearAll(); 162 } 163 } | Popular Tags |