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 splits a string into a list of strings given a regular expression for the separator.
024 */
025
026public class splitFunction implements FilterFunction {
027    /**
028     * Check whether the given expression is valid for this function.
029     *
030     * @param    expr - the expression consisting of a call to this function.
031     * @return true - if two or three arguments are passed to the function; false - otherwise.
032     */
033
034    public boolean isValid(FunctionCallExpression expr) {
035        if ((expr.getNumArguments() >= 2) && (expr.getNumArguments() <= 3))
036            return true;
037
038        return false;
039    }
040
041
042    /**
043     * Indicate that this function does not return a boolean value.
044     *
045     * @param    expr - the expression consisting of a call to this function.
046     * @return false - indicating this filter function never evaluates to a boolean result.
047     */
048
049    public boolean returnsBoolean(FunctionCallExpression expr) {
050        return false;
051    }
052
053
054    /**
055     * Evaluate the given expression for this function in the given context.  A list of zero or more strings
056     * results from the evaluation.  The result of the evaluation of the first argument is split with the regular
057     * expression which results from the evaluation of the second argument.  If a third argument is given, it
058     * is an integer which limits the split.  String#split() performs the split.
059     * <p/>
060     * The first two arguments must be Strings.  If a third is given, it must be an Integer.
061     *
062     * @param    expr - the expression consisting of a call to this function.
063     * @return List - a list of Strings resulting from the split.
064     */
065
066    public Object evaluate(FunctionCallExpression expr, MessageEvaluationContext message_ctx)
067            throws javax.jms.JMSException {
068        String src;
069        String split_pat;
070        String[] result;
071
072        src = (String) expr.getArgument(0).evaluate(message_ctx);
073        split_pat = (String) expr.getArgument(1).evaluate(message_ctx);
074
075        if (expr.getNumArguments() > 2) {
076            Integer limit;
077
078            limit = (Integer) expr.getArgument(2).evaluate(message_ctx);
079
080            result = src.split(split_pat, limit.intValue());
081        } else {
082            result = src.split(split_pat);
083        }
084
085        return java.util.Arrays.asList(result);
086    }
087}