1 17 18 package org.apache.jmeter.functions; 19 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.transform.TransformerException ; 28 29 import org.apache.jorphan.logging.LoggingManager; 30 import org.apache.log.Logger; 31 import org.xml.sax.SAXException ; 32 33 44 public class XPathWrapper 45 { 46 47 transient private static Logger log = LoggingManager.getLoggerForClass(); 48 49 private XPathFileContainer container; 50 private int currentRow; 51 private static final int NO_LINE = -1; 52 53 private static String defaultFile = ""; private String xpathString; 55 56 private static Map fileContainers = new HashMap (); 58 61 private XPathWrapper(XPathFileContainer fdc) 62 { 63 super(); 64 container = fdc; 65 currentRow = -1; 66 } 67 68 69 private static ThreadLocal filePacks = new ThreadLocal (){ 70 protected Object initialValue(){ 71 return new HashMap (); 72 } 73 }; 74 75 private static String checkDefault(String file) 76 { 77 if (file.length() == 0) 78 { 79 if (fileContainers.size() == 1 && defaultFile.length() > 0) 80 { 81 log.warn("Using default: "+defaultFile); 82 file = defaultFile; 83 } 84 else 85 { 86 log.error("Cannot determine default file name"); 87 } 88 } 89 return file; 90 } 91 94 public static synchronized void open(String file,String xpathString, String alias) 95 { 96 log.info("Opening "+file+ " as " + alias); 97 file = checkDefault(file); 98 if (alias.length() == 0) 99 { 100 log.error("Alias cannot be empty"); 101 return; 102 } 103 Map m = (Map ) filePacks.get(); 104 if (m.get(alias) == null) 105 { 106 XPathFileContainer frcc; 107 try 108 { 109 frcc = getFile(file, xpathString, alias); 110 log.info("Stored "+file+" as "+alias); 111 m.put(alias,new XPathWrapper(frcc)); 112 } 113 catch (FileNotFoundException e) 114 { 115 log.warn(e.getLocalizedMessage()); 116 } 117 catch (IOException e) 118 { 119 log.warn(e.getLocalizedMessage()); 120 } catch (ParserConfigurationException e) { 121 log.warn(e.getLocalizedMessage()); 122 } catch (SAXException e) { 123 log.warn(e.getLocalizedMessage()); 124 } catch (TransformerException e) { 125 log.warn(e.getLocalizedMessage()); 126 } 127 } 128 } 129 130 private static XPathFileContainer getFile(String file, String xpathString, String alias) 131 throws FileNotFoundException , IOException , ParserConfigurationException , SAXException , TransformerException 132 { 133 XPathFileContainer frcc; 134 if ((frcc = (XPathFileContainer) fileContainers.get(alias)) == null) 135 { 136 frcc = new XPathFileContainer(file, xpathString); 137 fileContainers.put(alias,frcc); 138 log.info("Saved "+file+" as "+alias); 139 if (defaultFile.length() == 0){ 140 defaultFile = file; } 142 } 143 return frcc; 144 } 145 146 151 public static void endRow(String file) 152 { 153 file=checkDefault(file); 154 Map my = (Map ) filePacks.get(); 155 XPathWrapper fw = (XPathWrapper) (my).get(file); 156 if (fw == null) 157 { 158 log.warn("endRow(): no entry for "+file); 159 } 160 else 161 { 162 fw.endRow(); 163 } 164 } 165 166 private void endRow() 167 { 168 if (currentRow == NO_LINE) 169 { 170 log.warn("endRow() called twice in succession"); 171 } 172 currentRow = NO_LINE; 173 } 174 175 public static String getXPathString(String file,String xpathString) 176 { 177 Map my = (Map ) filePacks.get(); 178 XPathWrapper fw = (XPathWrapper) (my).get(file); 179 if (fw == null) { 181 if (file.startsWith("*")) { 182 log.warn("Cannot perform initial open using alias "+file); 183 } 184 else 185 { 186 file=checkDefault(file); 187 log.info("Attaching "+file); 188 open(file,xpathString,file); 189 fw = (XPathWrapper) my.get(file); 190 } 191 if (fw == null) { 193 log.error("XPathWrapper is null!"); 194 return ""; 195 } 196 } 197 return fw.getXPathString(); 198 } 199 200 private String getXPathString() 201 { 202 if (currentRow == NO_LINE) { 203 currentRow = container.nextRow(); 204 } 205 log.debug("getting match number "+currentRow); 206 207 return container.getXPathString(currentRow); 208 } 209 210 216 public static int getCurrentRow(String file) 217 { 218 219 Map my = (Map ) filePacks.get(); 220 XPathWrapper fw = (XPathWrapper) (my).get(file); 221 if (fw == null) { 223 return -1; 224 } 225 else 226 { 227 return fw.currentRow; 228 } 229 } 230 231 234 public static void clearAll() 235 { 236 log.debug("clearAll()"); 237 Map my = (Map ) filePacks.get(); 238 for (Iterator i=my.entrySet().iterator();i.hasNext();) 239 { 240 Object fw = i.next(); 241 log.info("Removing "+fw.toString()); 242 i.remove(); 243 } 244 fileContainers.clear(); 245 defaultFile = ""; 246 } 247 } 248 | Popular Tags |