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.report.engine;
020:
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import org.apache.jmeter.functions.InvalidVariableException;
027: import org.apache.jmeter.testelement.ReportPlan;
028: import org.apache.jmeter.testelement.TestElement;
029: import org.apache.jmeter.testelement.property.JMeterProperty;
030: import org.apache.jorphan.logging.LoggingManager;
031: import org.apache.log.Logger;
032:
033: /**
034: * @author Michael Stover
035: * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
036: * @version $Revision: 493793 $ updated on $Date: 2007-01-07 18:19:27 +0000 (Sun, 07 Jan 2007) $
037: */
038: public class ValueReplacer {
039: transient private static Logger log = LoggingManager
040: .getLoggerForClass();
041:
042: Map variables = new HashMap();
043:
044: public ValueReplacer() {
045: }
046:
047: public ValueReplacer(ReportPlan tp) {
048: setUserDefinedVariables(tp.getUserDefinedVariables());
049: }
050:
051: public void setUserDefinedVariables(Map variables) {
052: this .variables = variables;
053: }
054:
055: public void replaceValues(TestElement el)
056: throws InvalidVariableException {
057: /**
058: Collection newProps = replaceValues(el.propertyIterator(), new ReplaceStringWithFunctions(masterFunction,
059: variables));
060: setProperties(el, newProps);
061: **/
062: }
063:
064: private void setProperties(TestElement el, Collection newProps) {
065: Iterator iter = newProps.iterator();
066: el.clear();
067: while (iter.hasNext()) {
068: el.setProperty((JMeterProperty) iter.next());
069: }
070: }
071:
072: public void reverseReplace(TestElement el)
073: throws InvalidVariableException {
074: /**
075: Collection newProps = replaceValues(el.propertyIterator(), new ReplaceFunctionsWithStrings(masterFunction,
076: variables));
077: setProperties(el, newProps);
078: **/
079: }
080:
081: public void reverseReplace(TestElement el, boolean regexMatch)
082: throws InvalidVariableException {
083: /**
084: Collection newProps = replaceValues(el.propertyIterator(), new ReplaceFunctionsWithStrings(masterFunction,
085: variables, regexMatch));
086: setProperties(el, newProps);
087: **/
088: }
089:
090: public void undoReverseReplace(TestElement el)
091: throws InvalidVariableException {
092: /**
093: Collection newProps = replaceValues(el.propertyIterator(), new UndoVariableReplacement(masterFunction,
094: variables));
095: setProperties(el, newProps);
096: **/
097: }
098:
099: public void addVariable(String name, String value) {
100: variables.put(name, value);
101: }
102:
103: /**
104: * Add all the given variables to this replacer's variables map.
105: *
106: * @param vars
107: * A map of variable name-value pairs (String-to-String).
108: */
109: public void addVariables(Map vars) {
110: variables.putAll(vars);
111: }
112:
113: /**
114: private Collection replaceValues(PropertyIterator iter, ValueTransformer transform) throws InvalidVariableException {
115: List props = new LinkedList();
116: while (iter.hasNext()) {
117: JMeterProperty val = iter.next();
118: if (log.isDebugEnabled()) {
119: log.debug("About to replace in property of type: " + val.getClass() + ": " + val);
120: }
121: if (val instanceof StringProperty) {
122: // Must not convert TestElement.gui_class etc
123: if (!val.getName().equals(TestElement.GUI_CLASS) &&
124: !val.getName().equals(TestElement.TEST_CLASS)) {
125: val = transform.transformValue(val);
126: if (log.isDebugEnabled()) {
127: log.debug("Replacement result: " + val);
128: }
129: }
130: } else if (val instanceof MultiProperty) {
131: MultiProperty multiVal = (MultiProperty) val;
132: Collection newValues = replaceValues(multiVal.iterator(), transform);
133: multiVal.clear();
134: Iterator propIter = newValues.iterator();
135: while (propIter.hasNext()) {
136: multiVal.addProperty((JMeterProperty) propIter.next());
137: }
138: if (log.isDebugEnabled()) {
139: log.debug("Replacement result: " + multiVal);
140: }
141: } else {
142: if (log.isDebugEnabled()) {
143: log.debug("Won't replace " + val);
144: }
145: }
146: props.add(val);
147: }
148: return props;
149: }
150: **/
151: }
|