KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18 package org.apache.jmeter.functions;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import javax.xml.transform.TransformerException JavaDoc;
28
29 import org.apache.jmeter.junit.JMeterTestCase;
30 import org.apache.jorphan.logging.LoggingManager;
31 import org.apache.log.Logger;
32 import org.apache.xpath.XPathAPI;
33 import org.w3c.dom.NodeList JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36 /**
37  * File data container for XML files
38  * Data is accessible via XPath
39  *
40  */

41 public class XPathFileContainer
42 {
43     
44     transient private static Logger log = LoggingManager.getLoggerForClass();
45     
46
47     private NodeList JavaDoc nodeList;
48     
49     private String JavaDoc fileName; // name of the file
50
private String JavaDoc xpath;
51      
52     /** Keeping track of which row is next to be read. */
53     private int nextRow;
54
55     private XPathFileContainer()// Not intended to be called directly
56
{
57     }
58
59     public XPathFileContainer(String JavaDoc file,String JavaDoc xpath)
60     throws FileNotFoundException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc, TransformerException JavaDoc
61     {
62         log.debug("XPath("+file+") xpath "+xpath+"");
63         fileName = file;
64         this.xpath = xpath;
65         nextRow = 0;
66         load();
67     }
68
69     private void load()
70     throws IOException JavaDoc,FileNotFoundException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc, TransformerException JavaDoc
71     {
72         InputStream JavaDoc fis=null;
73         try
74         {
75             DocumentBuilder JavaDoc
76                     builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
77
78             fis = new FileInputStream JavaDoc(fileName);
79             nodeList = XPathAPI.selectNodeList(builder.parse(fis), xpath);
80             log.debug("found "+nodeList.getLength());
81         
82         }
83         catch (FileNotFoundException JavaDoc e)
84         {
85             nodeList = null;
86             log.warn(e.toString());
87             throw e;
88         }
89         catch (IOException JavaDoc e)
90         {
91             nodeList = null;
92             log.warn(e.toString());
93             throw e;
94         } catch (ParserConfigurationException JavaDoc e) {
95             nodeList = null;
96             log.warn(e.toString());
97             throw e;
98         } catch (SAXException JavaDoc e) {
99             nodeList = null;
100             log.warn(e.toString());
101             throw e;
102         } catch (TransformerException JavaDoc e) {
103             nodeList = null;
104             log.warn(e.toString());
105             throw e;
106         }
107         finally
108         {
109             if (fis != null) fis.close();
110         }
111     }
112
113     public String JavaDoc getXPathString(int num) {
114         return nodeList.item(num).getNodeValue();
115     }
116     
117     /**
118      * Returns the next row to the caller, and updates it,
119      * allowing for wrap round
120      *
121      * @return the first free (unread) row
122      *
123      */

124     public int nextRow()
125     {
126         int row = nextRow;
127         nextRow++;
128         if (nextRow >= size())// 0-based
129
{
130             nextRow = 0;
131         }
132         log.debug (new StringBuffer JavaDoc("Row: ").append(row).toString());
133         return row;
134     }
135     
136     public int size() {
137         return (nodeList == null ) ? -1 : nodeList.getLength();
138     }
139
140   public static class Test extends JMeterTestCase
141     {
142
143         static{
144             //LoggingManager.setPriority("DEBUG","jmeter");
145
//LoggingManager.setTarget(new java.io.PrintWriter(System.out));
146
}
147
148
149         public Test(String JavaDoc a)
150         {
151             super(a);
152         }
153         
154         public void testNull() throws Exception JavaDoc
155         {
156             try
157             {
158                 new XPathFileContainer("nosuch.xml","/");
159                 fail("Should not find the file");
160             }
161             catch (FileNotFoundException JavaDoc e)
162             {
163             }
164         }
165         
166         public void testrowNum() throws Exception JavaDoc
167         {
168             XPathFileContainer f = new XPathFileContainer("../build.xml", "/project/target/@name");
169             assertNotNull(f);
170         // assertEquals("Expected 4 lines",4,f.size());
171

172             int myRow=f.nextRow();
173             assertEquals(0,myRow);
174             assertEquals(1,f.nextRow);
175
176             myRow = f.nextRow();
177             assertEquals(1,myRow);
178             assertEquals(2,f.nextRow);
179             
180             myRow = f.nextRow();
181             assertEquals(2,myRow);
182             assertEquals(3,f.nextRow);
183             
184             
185             //myRow = f.nextRow();
186
//assertEquals(3,myRow);
187
//assertEquals(0,f.nextRow);
188

189             //myRow = f.nextRow();
190
//assertEquals(0,myRow);
191
//assertEquals(1,f.nextRow);
192

193         }
194         
195         public void testColumns() throws Exception JavaDoc
196         {
197             XPathFileContainer f = new XPathFileContainer("../build.xml","/project/target/@name");
198             assertNotNull(f);
199             assertTrue("Not empty",f.size() > 0);
200             int last = 0;
201             for (int i=0; i< f.size(); i++) {
202                 last = f.nextRow();
203                 log.debug("found ["+i+"]"+f.getXPathString(last));
204             }
205             assertEquals(last+1, f.size());
206
207         }
208         public void testDefault() throws Exception JavaDoc
209         {
210             XPathFileContainer f = new XPathFileContainer("../build.xml","/project/@default");
211             assertNotNull(f);
212             assertTrue("Not empty",f.size() > 0);
213             assertEquals("all", f.getXPathString(0));
214
215         }
216
217     }
218     /**
219      * @return the file name for this class
220      */

221     public String JavaDoc getFileName()
222     {
223         return fileName;
224     }
225
226 }
Popular Tags