001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function replacelist
021 */
022package lucee.runtime.functions.string;
023
024import java.util.Iterator;
025
026import lucee.commons.lang.StringUtil;
027import lucee.runtime.PageContext;
028import lucee.runtime.ext.function.Function;
029import lucee.runtime.op.Caster;
030import lucee.runtime.type.Array;
031import lucee.runtime.type.util.ListUtil;
032
033public final class ReplaceList implements Function {
034        
035        private static final long serialVersionUID = -3935300433837460732L;
036
037        public static String call(PageContext pc , String str, String list1, String list2) {
038                return call(pc, str, list1, list2, ",", ",");
039        }
040        
041        public static String call(PageContext pc , String str, String list1, String list2, String delimiter_list1) {
042                if(delimiter_list1==null) delimiter_list1=",";
043                
044                return call(pc, str, list1, list2, delimiter_list1, delimiter_list1);
045        }
046        
047        public static String call(PageContext pc , String str, String list1, String list2, String delimiter_list1, String delimiter_list2) {
048                if(delimiter_list1==null) delimiter_list1=",";
049                if(delimiter_list2==null) delimiter_list2=",";
050
051                Array arr1=ListUtil.listToArray(list1, delimiter_list1);
052                Array arr2=ListUtil.listToArray(list2, delimiter_list2);
053
054                Iterator<Object> it1 = arr1.valueIterator();
055                Iterator<Object> it2 = arr2.valueIterator();
056                
057        while(it1.hasNext()) {
058            str=StringUtil.replace(str,Caster.toString(it1.next(),null),((it2.hasNext())?Caster.toString(it2.next(),null):""),false);
059        }
060                return str;
061        }
062}