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.report.engine; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 24 import org.apache.jmeter.functions.InvalidVariableException; 25 import org.apache.jmeter.testelement.ReportPlan; 26 import org.apache.jmeter.testelement.TestElement; 27 28 public class ValueReplacer { 29 // private static final Logger log = LoggingManager.getLoggerForClass(); 30 31 Map variables = new HashMap(); 32 33 public ValueReplacer() { 34 } 35 36 public ValueReplacer(ReportPlan tp) { 37 setUserDefinedVariables(tp.getUserDefinedVariables()); 38 } 39 40 public void setUserDefinedVariables(Map variables) { 41 this.variables = variables; 42 } 43 44 /** 45 * @throws InvalidVariableException not thrown currently 46 */ 47 public void replaceValues(TestElement el) throws InvalidVariableException { 48 /** 49 Collection newProps = replaceValues(el.propertyIterator(), new ReplaceStringWithFunctions(masterFunction, 50 variables)); 51 setProperties(el, newProps); 52 **/ 53 } 54 55 // private void setProperties(TestElement el, Collection newProps) { 56 // Iterator iter = newProps.iterator(); 57 // el.clear(); 58 // while (iter.hasNext()) { 59 // el.setProperty((JMeterProperty) iter.next()); 60 // } 61 // } 62 63 /** 64 * @throws InvalidVariableException not thrown currently 65 */ 66 public void reverseReplace(TestElement el) throws InvalidVariableException { 67 /** 68 Collection newProps = replaceValues(el.propertyIterator(), new ReplaceFunctionsWithStrings(masterFunction, 69 variables)); 70 setProperties(el, newProps); 71 **/ 72 } 73 74 /** 75 * @throws InvalidVariableException not thrown currently 76 */ 77 public void reverseReplace(TestElement el, boolean regexMatch) throws InvalidVariableException { 78 /** 79 Collection newProps = replaceValues(el.propertyIterator(), new ReplaceFunctionsWithStrings(masterFunction, 80 variables, regexMatch)); 81 setProperties(el, newProps); 82 **/ 83 } 84 85 /** 86 * @throws InvalidVariableException not thrown currently 87 */ 88 public void undoReverseReplace(TestElement el) throws InvalidVariableException { 89 /** 90 Collection newProps = replaceValues(el.propertyIterator(), new UndoVariableReplacement(masterFunction, 91 variables)); 92 setProperties(el, newProps); 93 **/ 94 } 95 96 public void addVariable(String name, String value) { 97 variables.put(name, value); 98 } 99 100 /** 101 * Add all the given variables to this replacer's variables map. 102 * 103 * @param vars 104 * A map of variable name-value pairs (String-to-String). 105 */ 106 public void addVariables(Map vars) { 107 variables.putAll(vars); 108 } 109 110 /** 111 private Collection replaceValues(PropertyIterator iter, ValueTransformer transform) throws InvalidVariableException { 112 List props = new LinkedList(); 113 while (iter.hasNext()) { 114 JMeterProperty val = iter.next(); 115 if (log.isDebugEnabled()) { 116 log.debug("About to replace in property of type: " + val.getClass() + ": " + val); 117 } 118 if (val instanceof StringProperty) { 119 // Must not convert TestElement.gui_class etc 120 if (!val.getName().equals(TestElement.GUI_CLASS) && 121 !val.getName().equals(TestElement.TEST_CLASS)) { 122 val = transform.transformValue(val); 123 if (log.isDebugEnabled()) { 124 log.debug("Replacement result: " + val); 125 } 126 } 127 } else if (val instanceof MultiProperty) { 128 MultiProperty multiVal = (MultiProperty) val; 129 Collection newValues = replaceValues(multiVal.iterator(), transform); 130 multiVal.clear(); 131 Iterator propIter = newValues.iterator(); 132 while (propIter.hasNext()) { 133 multiVal.addProperty((JMeterProperty) propIter.next()); 134 } 135 if (log.isDebugEnabled()) { 136 log.debug("Replacement result: " + multiVal); 137 } 138 } else { 139 if (log.isDebugEnabled()) { 140 log.debug("Won't replace " + val); 141 } 142 } 143 props.add(val); 144 } 145 return props; 146 } 147 **/ 148 }