KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > XPath


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/Attic/XPath.java,v 1.1.2.2 2005/01/09 18:16:52 sebb Exp $
2
/*
3  * Copyright 2005 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 package org.apache.jmeter.functions;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
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 /**
34  * The function represented by this class allows data to be read from XML
35  * files. Syntax is similar to the CVSRead function. The function allows
36  * the test to line-thru the nodes in the XML file - one node per each test.
37  * E.g. inserting the following in the test scripts :
38  *
39  * ${_XPath(c:/BOF/abcd.xml,/xpath/)} // match the (first) node of 'c:/BOF/abcd.xml' , return the 1st column ( represented by the '0'),
40  * ${_XPath(c:/BOF/abcd.xml,/xpath/)} // read (first) match of '/xpath/' expressions
41  * ${_XPath(c:/BOF/abcd.xml,/xpath/)} // Go to next match of '/xpath/' expressions
42  *
43  * NOTE: A single instance of each different file is opened and used for all threads.
44  *
45  *
46  *
47  */

48 public class XPath extends AbstractFunction implements Serializable JavaDoc
49 {
50     transient private static final Logger log = LoggingManager.getLoggerForClass();
51  // static {
52
// LoggingManager.setPriority("DEBUG","jmeter");
53
// LoggingManager.setTarget(new java.io.PrintWriter(System.out));
54
// }
55
private static final String JavaDoc KEY = "__XPath"; // Function name
56

57     private static final List JavaDoc desc = new LinkedList JavaDoc();
58
59     
60     private Object JavaDoc[] values; // Parameter list
61

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 JavaDoc clone()
72     {
73         XPath newReader = new XPath();
74         return newReader;
75     }
76
77     /**
78      * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
79      */

80     public synchronized String JavaDoc execute(
81         SampleResult previousResult,
82         Sampler currentSampler)
83         throws InvalidVariableException
84     {
85         String JavaDoc myValue = "";
86
87         String JavaDoc fileName =
88                 ((org.apache.jmeter.engine.util.CompoundVariable) values[0])
89                     .execute();
90         String JavaDoc 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 JavaDoc e)
103         {
104             log.warn(Thread.currentThread().getName()+" - can't parse column number: "
105                       + " "+ e.toString());
106         }
107         catch (IndexOutOfBoundsException JavaDoc 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     /**
119      * @see org.apache.jmeter.functions.Function#getArgumentDesc()
120      */

121     public List JavaDoc getArgumentDesc()
122     {
123         return desc;
124     }
125
126     /**
127      * @see org.apache.jmeter.functions.Function#getReferenceKey()
128      */

129     public String JavaDoc getReferenceKey()
130     {
131         return KEY;
132     }
133     
134     /**
135      * @see org.apache.jmeter.functions.Function#setParameters(Collection)
136      */

137     public void setParameters(Collection JavaDoc 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         /*
156          * Need to reset the containers for repeated runs; about the only way for
157          * functions to detect that a run is starting seems to be the setParameters()
158          * call.
159         */

160         XPathWrapper.clearAll();//TODO only clear the relevant entry - if possible...
161

162     }
163 }
Popular Tags