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 */
017package org.apache.activemq.filter.function;
018
019import org.apache.activemq.filter.FunctionCallExpression;
020import org.apache.activemq.filter.MessageEvaluationContext;
021
022/**
023 * Function which replaces regular expression matches in a source string to a replacement literal.
024 * <p/>
025 * For Example:
026 * REPLACE('1,2/3', '[,/]', ';') returns '1;2;3'
027 */
028
029public class replaceFunction implements FilterFunction {
030    /**
031     * Check whether the given expression is valid for this function.
032     *
033     * @param    expr - the expression consisting of a call to this function.
034     * @return true - if three arguments are passed to the function; false - otherwise.
035     */
036
037    public boolean isValid(FunctionCallExpression expr) {
038        if (expr.getNumArguments() == 3)
039            return true;
040
041        return false;
042    }
043
044
045    /**
046     * Indicate that this function does not return a boolean value.
047     *
048     * @param    expr - the expression consisting of a call to this function.
049     * @return false - this filter function always evaluates to a string.
050     */
051
052    public boolean returnsBoolean(FunctionCallExpression expr) {
053        return false;
054    }
055
056
057    /**
058     * Evaluate the given expression for this function in the given context.  The result of the evaluation is a
059     * string with all matches of the regular expression, from the evaluation of the second argument, replaced by
060     * the string result from the evaluation of the third argument.  Replacement is performed by
061     * String#replaceAll().
062     * <p/>
063     * Note that all three arguments must be Strings.
064     *
065     * @param    expr - the expression consisting of a call to this function.
066     * @return String - the result of the replacement.
067     */
068
069    public Object evaluate(FunctionCallExpression expr, MessageEvaluationContext message_ctx)
070            throws javax.jms.JMSException {
071        String src;
072        String match_regex;
073        String repl_lit;
074        String result;
075
076        src = (String) expr.getArgument(0).evaluate(message_ctx);
077        match_regex = (String) expr.getArgument(1).evaluate(message_ctx);
078        repl_lit = (String) expr.getArgument(2).evaluate(message_ctx);
079
080        result = src.replaceAll(match_regex, repl_lit);
081
082        return result;
083    }
084}