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.BufferedReader;
   22   import java.io.FileNotFoundException;
   23   import java.io.FileReader;
   24   import java.io.IOException;
   25   import java.util.ArrayList;
   26   import java.util.StringTokenizer;
   27   
   28   import org.apache.jmeter.util.JMeterUtils;
   29   import org.apache.jorphan.logging.LoggingManager;
   30   import org.apache.log.Logger;
   31   
   32   /**
   33    * File data container for CSV (and similar delimited) files Data is accessible
   34    * via row and column number
   35    *
   36    */
   37   public class FileRowColContainer {
   38   
   39       private static final Logger log = LoggingManager.getLoggerForClass();
   40   
   41       private final ArrayList fileData; // Lines in the file, split into columns
   42   
   43       private final String fileName; // name of the file
   44   
   45       public static final String DELIMITER
   46           = JMeterUtils.getPropDefault("csvread.delimiter",  // $NON-NLS-1$
   47                   ","); // $NON-NLS-1$
   48   
   49       /** Keeping track of which row is next to be read. */
   50       private int nextRow;
   51   
   52       /** Delimiter for this file */
   53       private final String delimiter;
   54   
   55       public FileRowColContainer(String file, String delim) throws IOException, FileNotFoundException {
   56           log.debug("FRCC(" + file + "," + delim + ")");
   57           fileName = file;
   58           delimiter = delim;
   59           nextRow = 0;
   60           fileData = new ArrayList();
   61           load();
   62       }
   63   
   64       public FileRowColContainer(String file) throws IOException, FileNotFoundException {
   65           log.debug("FRCC(" + file + ")[" + DELIMITER + "]");
   66           fileName = file;
   67           delimiter = DELIMITER;
   68           nextRow = 0;
   69           fileData = new ArrayList();
   70           load();
   71       }
   72   
   73       private void load() throws IOException, FileNotFoundException {
   74   
   75           BufferedReader myBread = null;
   76           try {
   77               FileReader fis = new FileReader(fileName);
   78               myBread = new BufferedReader(fis);
   79               String line = myBread.readLine();
   80               /*
   81                * N.B. Stop reading the file if we get a blank line: This allows
   82                * for trailing comments in the file
   83                */
   84               while (line != null && line.length() > 0) {
   85                   fileData.add(splitLine(line, delimiter));
   86                   line = myBread.readLine();
   87               }
   88           } catch (FileNotFoundException e) {
   89               fileData.clear();
   90               log.warn(e.toString());
   91               throw e;
   92           } catch (IOException e) {
   93               fileData.clear();
   94               log.warn(e.toString());
   95               throw e;
   96           } finally {
   97               if (myBread != null) {
   98                   myBread.close();
   99               }
  100           }
  101       }
  102   
  103       /**
  104        * Get the string for the column from the current row
  105        *
  106        * @param row
  107        *            row number (from 0)
  108        * @param col
  109        *            column number (from 0)
  110        * @return the string (empty if out of bounds)
  111        * @throws IndexOutOfBoundsException
  112        *             if the column number is out of bounds
  113        */
  114       public String getColumn(int row, int col) throws IndexOutOfBoundsException {
  115           String colData;
  116           colData = (String) ((ArrayList) fileData.get(row)).get(col);
  117           log.debug(fileName + "(" + row + "," + col + "): " + colData);
  118           return colData;
  119       }
  120   
  121       /**
  122        * Returns the next row to the caller, and updates it, allowing for wrap
  123        * round
  124        *
  125        * @return the first free (unread) row
  126        *
  127        */
  128       public int nextRow() {
  129           int row = nextRow;
  130           nextRow++;
  131           if (nextRow >= fileData.size())// 0-based
  132           {
  133               nextRow = 0;
  134           }
  135           log.debug("Row: " + row);
  136           return row;
  137       }
  138   
  139       /**
  140        * Splits the line according to the specified delimiter
  141        *
  142        * @return an ArrayList of Strings containing one element for each value in
  143        *         the line
  144        */
  145       private static ArrayList splitLine(String theLine, String delim) {
  146           ArrayList result = new ArrayList();
  147           StringTokenizer tokener = new StringTokenizer(theLine, delim, true);
  148           /*
  149            * the beginning of the line is a "delimiter" so that ,a,b,c returns ""
  150            * "a" "b" "c"
  151            */
  152           boolean lastWasDelim = true;
  153           while (tokener.hasMoreTokens()) {
  154               String token = tokener.nextToken();
  155               if (token.equals(delim)) {
  156                   if (lastWasDelim) {
  157                       // two delimiters in a row; add an empty String
  158                       result.add("");
  159                   }
  160                   lastWasDelim = true;
  161               } else {
  162                   lastWasDelim = false;
  163                   result.add(token);
  164               }
  165           }
  166           if (lastWasDelim) // Catch the trailing delimiter
  167           {
  168               result.add(""); // $NON-NLS-1$
  169           }
  170           return result;
  171       }
  172   
  173       /**
  174        * @return the file name for this class
  175        */
  176       public String getFileName() {
  177           return fileName;
  178       }
  179   
  180       /**
  181        * @return Returns the delimiter.
  182        */
  183       final String getDelimiter() {
  184           return delimiter;
  185       }
  186   
  187       // Added to support external testing
  188       public int getSize(){
  189           return fileData.size();
  190       }
  191   }

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