1 18 19 22 package org.apache.jmeter.engine.util; 23 24 import java.io.IOException ; 25 import java.io.StringReader ; 26 import java.util.LinkedList ; 27 28 import org.apache.jmeter.engine.StandardJMeterEngine; 29 import org.apache.jmeter.functions.Function; 30 import org.apache.jmeter.functions.InvalidVariableException; 31 import org.apache.jmeter.testelement.TestListener; 32 import org.apache.jorphan.logging.LoggingManager; 33 import org.apache.log.Logger; 34 35 38 class FunctionParser 39 { 40 Logger log = LoggingManager.getLoggerForClass(); 41 42 45 LinkedList compileString(String value) throws InvalidVariableException 46 { 47 StringReader reader = new StringReader (value); 48 LinkedList result = new LinkedList (); 49 StringBuffer buffer = new StringBuffer (); 50 char previous = ' '; 51 char[] current = new char[1]; 52 try 53 { 54 while(reader.read(current) == 1) 55 { 56 if(current[0] == '\\') 57 { 58 previous = current[0]; 59 if(reader.read(current) == 0) 60 { 61 break; 62 } 63 if (current[0] != '$' 64 && current[0] != ',' 65 && current[0] != '\\') 66 { 67 buffer.append(previous); 68 } 69 previous = ' '; 70 buffer.append(current[0]); 71 continue; 72 } 73 else if(current[0] == '{' && previous == '$') 74 { 75 buffer.deleteCharAt(buffer.length()-1); 76 if(buffer.length() > 0) 77 { 78 result.add(buffer.toString()); 79 buffer.setLength(0); 80 } 81 result.add(makeFunction(reader)); 82 previous = ' '; 83 } 84 else 85 { 86 buffer.append(current[0]); 87 previous = current[0]; 88 } 89 } 90 if(buffer.length() > 0) 91 { 92 result.add(buffer.toString()); 93 } 94 } 95 catch (IOException e) 96 { 97 log.error("Error parsing function: " + value,e); 98 result.clear(); 99 result.add(value); 100 } 101 if(result.size() == 0) 102 { 103 result.add(""); 104 } 105 return result; 106 } 107 108 111 Object makeFunction(StringReader reader) throws InvalidVariableException 112 { 113 char[] current = new char[1]; 114 char previous = ' ';; 115 StringBuffer buffer = new StringBuffer (); 116 Object function; 117 try 118 { 119 while(reader.read(current) == 1) 120 { 121 if(current[0] == '\\') 122 { 123 if(reader.read(current) == 0) 124 { 125 break; 126 } 127 previous = ' '; 128 buffer.append(current[0]); 129 continue; 130 } 131 else if(current[0] == '(' && previous != ' ') 132 { 133 function = 134 CompoundVariable.getNamedFunction(buffer.toString()); 135 buffer.setLength(0); 136 if(function instanceof Function) 137 { 138 ((Function)function).setParameters(parseParams(reader)); 139 if(reader.read(current) == 0 || current[0] != '}') 140 { 141 throw new InvalidVariableException(); 142 } 143 if (function instanceof TestListener){ 144 StandardJMeterEngine.register((TestListener)function); 145 } 146 return function; 147 } 148 else 149 { 150 continue; 151 } 152 } 153 else if(current[0] == '}') 154 { 155 function = 156 CompoundVariable.getNamedFunction(buffer.toString()); 157 buffer.setLength(0); 158 return function; 159 } 160 else 161 { 162 buffer.append(current[0]); 163 previous = current[0]; 164 } 165 } 166 } 167 catch (IOException e) 168 { 169 log.error("Error parsing function: " + buffer.toString(),e); 170 return null; 171 } 172 log.warn("Probably an invalid function string: " + buffer.toString()); 173 return buffer.toString(); 174 } 175 176 180 LinkedList parseParams(StringReader reader) throws InvalidVariableException 181 { 182 LinkedList result = new LinkedList (); 183 StringBuffer buffer = new StringBuffer (); 184 char[] current = new char[1]; 185 char previous = ' '; 186 int functionRecursion = 0; 187 int parenRecursion = 0; 188 try 189 { 190 while(reader.read(current) == 1) 191 { 192 if(current[0] == '\\') 193 { 194 buffer.append(current[0]); 195 if(reader.read(current) == 0) 196 { 197 break; 198 } 199 previous = ' '; 200 buffer.append(current[0]); 201 continue; 202 } 203 else if(current[0] == ',' && functionRecursion == 0) 204 { 205 CompoundVariable param = new CompoundVariable(); 206 param.setParameters(buffer.toString()); 207 buffer.setLength(0); 208 result.add(param); 209 } 210 else if ( 211 current[0] == ')' 212 && functionRecursion == 0 213 && parenRecursion == 0) 214 { 215 CompoundVariable param = new CompoundVariable(); 216 param.setParameters(buffer.toString()); 217 buffer.setLength(0); 218 result.add(param); 219 return result; 220 } 221 else if(current[0] == '{' && previous == '$') 222 { 223 buffer.append(current[0]); 224 previous = current[0]; 225 functionRecursion++; 226 } 227 else if(current[0] == '}' && functionRecursion > 0) 228 { 229 buffer.append(current[0]); 230 previous = current[0]; 231 functionRecursion--; 232 } 233 else if ( 234 current[0] == ')' 235 && functionRecursion == 0 236 && parenRecursion > 0) 237 { 238 buffer.append(current[0]); 239 previous = current[0]; 240 parenRecursion--; 241 } 242 else if(current[0] == '(' && functionRecursion == 0) 243 { 244 buffer.append(current[0]); 245 previous = current[0]; 246 parenRecursion++; 247 } 248 else 249 { 250 buffer.append(current[0]); 251 previous = current[0]; 252 } 253 } 254 } 255 catch (IOException e) 256 { 257 log.error("Error parsing function: " + buffer.toString(),e); 258 } 259 log.warn("Probably an invalid function string: " + buffer.toString()); 260 CompoundVariable var = new CompoundVariable(); 261 var.setParameters(buffer.toString()); 262 result.add(var); 263 return result; 264 } 265 } 266 | Popular Tags |