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: /*
020: * Created on May 4, 2003
021: */
022: package org.apache.jmeter.engine.util;
023:
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import org.apache.jmeter.functions.InvalidVariableException;
028: import org.apache.jmeter.testelement.property.JMeterProperty;
029: import org.apache.jmeter.testelement.property.StringProperty;
030: import org.apache.jmeter.util.JMeterUtils;
031: import org.apache.jmeter.util.StringUtilities;
032: import org.apache.jorphan.logging.LoggingManager;
033: import org.apache.log.Logger;
034: import org.apache.oro.text.regex.MalformedPatternException;
035: import org.apache.oro.text.regex.Pattern;
036: import org.apache.oro.text.regex.PatternCompiler;
037: import org.apache.oro.text.regex.PatternMatcher;
038: import org.apache.oro.text.regex.Perl5Compiler;
039: import org.apache.oro.text.regex.StringSubstitution;
040: import org.apache.oro.text.regex.Util;
041:
042: /**
043: * Transforms strings into variable references (in spite of the name, which
044: * suggests the opposite!)
045: *
046: */
047: public class ReplaceFunctionsWithStrings extends AbstractTransformer {
048: private static final Logger log = LoggingManager
049: .getLoggerForClass();
050:
051: // Functions are wrapped in ${ and }
052: private static final String FUNCTION_REF_PREFIX = "${"; //$NON-NLS-1$
053:
054: private static final String FUNCTION_REF_SUFFIX = "}"; //$NON-NLS-1$
055:
056: private boolean regexMatch;// Should we match using regexes?
057:
058: public ReplaceFunctionsWithStrings(CompoundVariable masterFunction,
059: Map variables) {
060: this (masterFunction, variables, false);
061: }
062:
063: public ReplaceFunctionsWithStrings(CompoundVariable masterFunction,
064: Map variables, boolean regexMatch) {
065: super ();
066: setMasterFunction(masterFunction);
067: setVariables(variables);
068: this .regexMatch = regexMatch;
069: }
070:
071: /*
072: * (non-Javadoc)
073: *
074: * @see ValueTransformer#transformValue(JMeterProperty)
075: */
076: public JMeterProperty transformValue(JMeterProperty prop)
077: throws InvalidVariableException {
078: PatternMatcher pm = JMeterUtils.getMatcher();
079: Pattern pattern = null;
080: PatternCompiler compiler = new Perl5Compiler();
081: Iterator iter = getVariables().keySet().iterator();
082: String input = prop.getStringValue();
083: while (iter.hasNext()) {
084: String key = (String) iter.next();
085: String value = (String) getVariables().get(key);
086: if (regexMatch) {
087: try {
088: pattern = compiler.compile(value);
089: input = Util.substitute(pm, pattern,
090: new StringSubstitution(FUNCTION_REF_PREFIX
091: + key + FUNCTION_REF_SUFFIX),
092: input, Util.SUBSTITUTE_ALL);
093: } catch (MalformedPatternException e) {
094: log.warn("Malformed pattern " + value);
095: }
096: } else {
097: input = StringUtilities
098: .substitute(input, value, FUNCTION_REF_PREFIX
099: + key + FUNCTION_REF_SUFFIX);
100: }
101: }
102: StringProperty newProp = new StringProperty(prop.getName(),
103: input);
104: return newProp;
105: }
106: }
|