Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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.FileInputStream;
   22   import java.io.FileNotFoundException;
   23   import java.io.IOException;
   24   import java.io.InputStream;
   25   
   26   import javax.xml.parsers.DocumentBuilder;
   27   import javax.xml.parsers.DocumentBuilderFactory;
   28   import javax.xml.parsers.ParserConfigurationException;
   29   import javax.xml.transform.TransformerException;
   30   
   31   import org.apache.jorphan.logging.LoggingManager;
   32   import org.apache.jorphan.util.JOrphanUtils;
   33   import org.apache.log.Logger;
   34   import org.apache.xpath.XPathAPI;
   35   import org.w3c.dom.NodeList;
   36   import org.xml.sax.SAXException;
   37   
   38   //@see org.apache.jmeter.functions.PackageTest for unit tests
   39   
   40   /**
   41    * File data container for XML files Data is accessible via XPath
   42    *
   43    */
   44   public class XPathFileContainer {
   45   
   46       private static final Logger log = LoggingManager.getLoggerForClass();
   47   
   48       private final NodeList nodeList;
   49   
   50       private final String fileName; // name of the file
   51   
   52       private final String xpath;
   53   
   54       /** Keeping track of which row is next to be read. */
   55       private int nextRow;// probably does not need to be synch (always accessed through ThreadLocal?)
   56       int getNextRow(){// give access to Test code
   57           return nextRow;
   58       }
   59   
   60       public XPathFileContainer(String file, String xpath) throws FileNotFoundException, IOException,
   61               ParserConfigurationException, SAXException, TransformerException {
   62           log.debug("XPath(" + file + ") xpath " + xpath + "");
   63           fileName = file;
   64           this.xpath = xpath;
   65           nextRow = 0;
   66           nodeList=load();
   67       }
   68   
   69       private NodeList load() throws IOException, FileNotFoundException, ParserConfigurationException, SAXException,
   70               TransformerException {
   71           InputStream fis = null;
   72           NodeList nl = null;
   73           try {
   74               DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
   75   
   76               fis = new FileInputStream(fileName);
   77               nl = XPathAPI.selectNodeList(builder.parse(fis), xpath);
   78               log.debug("found " + nl.getLength());
   79   
   80           } catch (FileNotFoundException e) {
   81               log.warn(e.toString());
   82               throw e;
   83           } catch (IOException e) {
   84               log.warn(e.toString());
   85               throw e;
   86           } catch (ParserConfigurationException e) {
   87               log.warn(e.toString());
   88               throw e;
   89           } catch (SAXException e) {
   90               log.warn(e.toString());
   91               throw e;
   92           } catch (TransformerException e) {
   93               log.warn(e.toString());
   94               throw e;
   95           } finally {
   96               JOrphanUtils.closeQuietly(fis);
   97           }
   98           return nl;
   99       }
  100   
  101       public String getXPathString(int num) {
  102           return nodeList.item(num).getNodeValue();
  103       }
  104   
  105       /**
  106        * Returns the next row to the caller, and updates it, allowing for wrap
  107        * round
  108        *
  109        * @return the first free (unread) row
  110        *
  111        */
  112       public int nextRow() {
  113           int row = nextRow;
  114           nextRow++;
  115           if (nextRow >= size())// 0-based
  116           {
  117               nextRow = 0;
  118           }
  119           log.debug(new StringBuffer("Row: ").append(row).toString());
  120           return row;
  121       }
  122   
  123       public int size() {
  124           return (nodeList == null) ? -1 : nodeList.getLength();
  125       }
  126   
  127       /**
  128        * @return the file name for this class
  129        */
  130       public String getFileName() {
  131           return fileName;
  132       }
  133   
  134   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]