KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > engine > util > FunctionParser


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/engine/util/FunctionParser.java,v 1.5.2.1 2004/09/21 21:55:13 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 /*
20  * Created on Jul 25, 2003
21  */

22 package org.apache.jmeter.engine.util;
23
24 import java.io.IOException JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.util.LinkedList JavaDoc;
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 /**
36  * @author ano ano
37  */

38 class FunctionParser
39 {
40     Logger log = LoggingManager.getLoggerForClass();
41     
42     /**
43      * Compile a general string into a list of elements for a CompoundVariable.
44      */

45     LinkedList JavaDoc compileString(String JavaDoc value) throws InvalidVariableException
46     {
47         StringReader JavaDoc reader = new StringReader JavaDoc(value);
48         LinkedList JavaDoc result = new LinkedList JavaDoc();
49         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc 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     /**
109      * Compile a string into a function or SimpleVariable.
110      */

111     Object JavaDoc makeFunction(StringReader JavaDoc reader) throws InvalidVariableException
112     {
113         char[] current = new char[1];
114         char previous = ' ';;
115         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
116         Object JavaDoc 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 JavaDoc 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     /**
177      * Compile a String into a list of parameters, each made into a
178      * CompoundVariable.
179      */

180     LinkedList JavaDoc parseParams(StringReader JavaDoc reader) throws InvalidVariableException
181     {
182         LinkedList JavaDoc result = new LinkedList JavaDoc();
183         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc 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