001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.functions;
020:
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.io.InputStream;
025:
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.ParserConfigurationException;
029: import javax.xml.transform.TransformerException;
030:
031: import org.apache.jorphan.logging.LoggingManager;
032: import org.apache.jorphan.util.JOrphanUtils;
033: import org.apache.log.Logger;
034: import org.apache.xpath.XPathAPI;
035: import org.w3c.dom.NodeList;
036: import org.xml.sax.SAXException;
037:
038: //@see org.apache.jmeter.functions.PackageTest for unit tests
039:
040: /**
041: * File data container for XML files Data is accessible via XPath
042: *
043: */
044: public class XPathFileContainer {
045:
046: private static Logger log = LoggingManager.getLoggerForClass();
047:
048: private NodeList nodeList;
049:
050: private String fileName; // name of the file
051:
052: private String xpath;
053:
054: /** Keeping track of which row is next to be read. */
055: private int nextRow;
056:
057: int getNextRow() {// give access to Test code
058: return nextRow;
059: }
060:
061: private XPathFileContainer()// Not intended to be called directly
062: {
063: }
064:
065: public XPathFileContainer(String file, String xpath)
066: throws FileNotFoundException, IOException,
067: ParserConfigurationException, SAXException,
068: TransformerException {
069: log.debug("XPath(" + file + ") xpath " + xpath + "");
070: fileName = file;
071: this .xpath = xpath;
072: nextRow = 0;
073: load();
074: }
075:
076: private void load() throws IOException, FileNotFoundException,
077: ParserConfigurationException, SAXException,
078: TransformerException {
079: InputStream fis = null;
080: try {
081: DocumentBuilder builder = DocumentBuilderFactory
082: .newInstance().newDocumentBuilder();
083:
084: fis = new FileInputStream(fileName);
085: nodeList = XPathAPI.selectNodeList(builder.parse(fis),
086: xpath);
087: log.debug("found " + nodeList.getLength());
088:
089: } catch (FileNotFoundException e) {
090: nodeList = null;
091: log.warn(e.toString());
092: throw e;
093: } catch (IOException e) {
094: nodeList = null;
095: log.warn(e.toString());
096: throw e;
097: } catch (ParserConfigurationException e) {
098: nodeList = null;
099: log.warn(e.toString());
100: throw e;
101: } catch (SAXException e) {
102: nodeList = null;
103: log.warn(e.toString());
104: throw e;
105: } catch (TransformerException e) {
106: nodeList = null;
107: log.warn(e.toString());
108: throw e;
109: } finally {
110: JOrphanUtils.closeQuietly(fis);
111: }
112: }
113:
114: public String getXPathString(int num) {
115: return nodeList.item(num).getNodeValue();
116: }
117:
118: /**
119: * Returns the next row to the caller, and updates it, allowing for wrap
120: * round
121: *
122: * @return the first free (unread) row
123: *
124: */
125: public int nextRow() {
126: int row = nextRow;
127: nextRow++;
128: if (nextRow >= size())// 0-based
129: {
130: nextRow = 0;
131: }
132: log.debug(new StringBuffer("Row: ").append(row).toString());
133: return row;
134: }
135:
136: public int size() {
137: return (nodeList == null) ? -1 : nodeList.getLength();
138: }
139:
140: /**
141: * @return the file name for this class
142: */
143: public String getFileName() {
144: return fileName;
145: }
146:
147: }
|