001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.engine.util;
020:
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.jmeter.functions.InvalidVariableException;
029: import org.apache.jmeter.testelement.TestElement;
030: import org.apache.jmeter.testelement.TestPlan;
031: import org.apache.jmeter.testelement.property.JMeterProperty;
032: import org.apache.jmeter.testelement.property.MultiProperty;
033: import org.apache.jmeter.testelement.property.PropertyIterator;
034: import org.apache.jmeter.testelement.property.StringProperty;
035: import org.apache.jorphan.logging.LoggingManager;
036: import org.apache.log.Logger;
037:
038: /**
039: * @author Michael Stover
040: * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
041: * @version $Revision: 493779 $ updated on $Date: 2007-01-07 17:46:38 +0000 (Sun, 07 Jan 2007) $
042: */
043: public class ValueReplacer {
044: private static final Logger log = LoggingManager
045: .getLoggerForClass();
046:
047: private CompoundVariable masterFunction = new CompoundVariable();
048:
049: private Map variables = new HashMap();
050:
051: public ValueReplacer() {
052: }
053:
054: public ValueReplacer(TestPlan tp) {
055: setUserDefinedVariables(tp.getUserDefinedVariables());
056: }
057:
058: boolean containsKey(String k) {
059: return variables.containsKey(k);
060: }
061:
062: public void setUserDefinedVariables(Map variables) {
063: this .variables = variables;
064: }
065:
066: public void replaceValues(TestElement el)
067: throws InvalidVariableException {
068: Collection newProps = replaceValues(el.propertyIterator(),
069: new ReplaceStringWithFunctions(masterFunction,
070: variables));
071: setProperties(el, newProps);
072: }
073:
074: private void setProperties(TestElement el, Collection newProps) {
075: Iterator iter = newProps.iterator();
076: el.clear();
077: while (iter.hasNext()) {
078: el.setProperty((JMeterProperty) iter.next());
079: }
080: }
081:
082: public void reverseReplace(TestElement el)
083: throws InvalidVariableException {
084: Collection newProps = replaceValues(el.propertyIterator(),
085: new ReplaceFunctionsWithStrings(masterFunction,
086: variables));
087: setProperties(el, newProps);
088: }
089:
090: public void reverseReplace(TestElement el, boolean regexMatch)
091: throws InvalidVariableException {
092: Collection newProps = replaceValues(el.propertyIterator(),
093: new ReplaceFunctionsWithStrings(masterFunction,
094: variables, regexMatch));
095: setProperties(el, newProps);
096: }
097:
098: public void undoReverseReplace(TestElement el)
099: throws InvalidVariableException {
100: Collection newProps = replaceValues(el.propertyIterator(),
101: new UndoVariableReplacement(masterFunction, variables));
102: setProperties(el, newProps);
103: }
104:
105: public void addVariable(String name, String value) {
106: variables.put(name, value);
107: }
108:
109: /**
110: * Add all the given variables to this replacer's variables map.
111: *
112: * @param vars
113: * A map of variable name-value pairs (String-to-String).
114: */
115: public void addVariables(Map vars) {
116: variables.putAll(vars);
117: }
118:
119: private Collection replaceValues(PropertyIterator iter,
120: ValueTransformer transform) throws InvalidVariableException {
121: List props = new LinkedList();
122: while (iter.hasNext()) {
123: JMeterProperty val = iter.next();
124: if (log.isDebugEnabled()) {
125: log.debug("About to replace in property of type: "
126: + val.getClass() + ": " + val);
127: }
128: if (val instanceof StringProperty) {
129: // Must not convert TestElement.gui_class etc
130: if (!val.getName().equals(TestElement.GUI_CLASS)
131: && !val.getName()
132: .equals(TestElement.TEST_CLASS)) {
133: val = transform.transformValue(val);
134: if (log.isDebugEnabled()) {
135: log.debug("Replacement result: " + val);
136: }
137: }
138: } else if (val instanceof MultiProperty) {
139: MultiProperty multiVal = (MultiProperty) val;
140: Collection newValues = replaceValues(multiVal
141: .iterator(), transform);
142: multiVal.clear();
143: Iterator propIter = newValues.iterator();
144: while (propIter.hasNext()) {
145: multiVal.addProperty((JMeterProperty) propIter
146: .next());
147: }
148: if (log.isDebugEnabled()) {
149: log.debug("Replacement result: " + multiVal);
150: }
151: } else {
152: if (log.isDebugEnabled()) {
153: log.debug("Won't replace " + val);
154: }
155: }
156: props.add(val);
157: }
158: return props;
159: }
160: }
|