Advertisement
mnaufaldillah

XPathCFileContainer JMeter

Oct 23rd, 2021
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  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. package org.apache.jmeter.functions;
  19.  
  20. import java.io.BufferedInputStream;
  21. import java.io.FileInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24.  
  25. import javax.xml.XMLConstants;
  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.jmeter.util.XPathUtil;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import org.w3c.dom.NodeList;
  35. import org.xml.sax.SAXException;
  36.  
  37. //@see org.apache.jmeter.functions.PackageTest for unit tests
  38.  
  39. /**
  40.  * File data container for XML files Data is accessible via XPath
  41.  *
  42.  */
  43. public class XPathFileContainer {
  44.  
  45.     private static final Logger log = LoggerFactory.getLogger(XPathFileContainer.class);
  46.  
  47.     private final NodeList nodeList;
  48.  
  49.     private final String fileName; // name of the file
  50.  
  51.     /** Keeping track of which row is next to be read. */
  52.     private int nextRow;// probably does not need to be synch (always accessed through ThreadLocal?)
  53.     int getNextRow(){// give access to Test code
  54.         return nextRow;
  55.     }
  56.  
  57.     public XPathFileContainer(String file, String xpath) throws FileNotFoundException, IOException,
  58.             ParserConfigurationException, SAXException, TransformerException {
  59.         log.debug("XPath({}) xpath {}", file, xpath);
  60.         fileName = file;
  61.         nextRow = 0;
  62.         nodeList=load(xpath);
  63.     }
  64.  
  65.     private NodeList load(String xpath) throws IOException, FileNotFoundException, ParserConfigurationException, SAXException,
  66.             TransformerException {
  67.         NodeList nl = null;
  68.         try ( FileInputStream fis = new FileInputStream(fileName);
  69.                 BufferedInputStream bis = new BufferedInputStream(fis) ){
  70.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  71.             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
  72.             DocumentBuilder builder = factory.newDocumentBuilder();
  73.             nl = XPathUtil.selectNodeList(builder.parse(bis), xpath);
  74.             if(log.isDebugEnabled()) {
  75.                 log.debug("found {}", nl.getLength());
  76.             }
  77.         } catch (TransformerException | SAXException
  78.                 | ParserConfigurationException | IOException e) {
  79.             log.warn(e.toString());
  80.             throw e;
  81.         }
  82.         return nl;
  83.     }
  84.  
  85.     public String getXPathString(int num) {
  86.         return XPathUtil.getValueForNode(nodeList.item(num));
  87.     }
  88.  
  89.     /**
  90.      * Returns the next row to the caller, and updates it, allowing for wrap
  91.      * round
  92.      *
  93.      * @return the first free (unread) row
  94.      *
  95.      */
  96.     public int nextRow() {
  97.         int row = nextRow;
  98.         nextRow++;
  99.         if (nextRow >= size())// 0-based
  100.         {
  101.             nextRow = 0;
  102.         }
  103.         log.debug("Row: {}", row);
  104.         return row;
  105.     }
  106.  
  107.     public int size() {
  108.         return (nodeList == null) ? -1 : nodeList.getLength();
  109.     }
  110.  
  111.     /**
  112.      * @return the file name for this class
  113.      */
  114.     public String getFileName() {
  115.         return fileName;
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement