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 replace
021 */
022package lucee.runtime.functions.string;
023
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.exp.FunctionException;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.functions.BIF;
029import lucee.runtime.op.Caster;
030import lucee.runtime.op.Decision;
031
032public final class Replace extends BIF {
033        
034        private static final long serialVersionUID = -313884944032266348L;
035
036        public static String call(PageContext pc , String str, String sub1, String sub2) {
037                return _call(pc, str, sub1, sub2, true);
038        }
039        
040        public static String call(PageContext pc , String str, String sub1, String sub2, String scope) {
041                return _call(pc, str, sub1, sub2, !scope.equalsIgnoreCase("all"));
042        }
043
044        public static String call( PageContext pc, String input, Object find, String repl, String scope ) throws PageException {
045                return _call(pc, input, find, repl, !scope.equalsIgnoreCase("all"));
046        }
047
048        public static String call( PageContext pc, String input, Object find, String repl ) throws PageException {
049                return _call(pc, input, find, repl, true);
050        }
051        
052        public static String call( PageContext pc, String input, Object struct ) throws PageException {
053                if(!Decision.isStruct(struct))
054                        throw new FunctionException(pc,"replace",2,"sub1","When passing only two parameters, the second parameter must be a Struct.");
055                return StringUtil.replaceMap( input, Caster.toMap(struct), false );
056        }
057        
058        private static String _call(PageContext pc , String str, String sub1, String sub2, boolean firstOnly) {
059                if (StringUtil.isEmpty(sub1))
060                        return str;
061                return StringUtil.replace(str, sub1, sub2, firstOnly);
062        }
063        
064        private static String _call( PageContext pc, String input, Object find, String repl, boolean onlyFirst) throws PageException {
065                if(!Decision.isSimpleValue(find ) )
066                        throw new FunctionException(pc,"replace",2,"sub1","When passing three parameters or more, the second parameter must be a simple value.");
067                return _call(pc, input, Caster.toString(find), repl, onlyFirst);
068        }
069
070    @Override
071        public Object invoke(PageContext pc, Object[] args) throws PageException {
072        if(args.length==2)
073                        return call(pc, Caster.toString(args[0]), args[1]);
074        if(args.length==3)
075                        return call(pc, Caster.toString(args[0]), args[1],Caster.toString(args[2]));
076        if(args.length==4)
077                        return call(pc, Caster.toString(args[0]), args[1],Caster.toString(args[2]),Caster.toString(args[3]));
078        
079                throw new FunctionException(pc, "Replace", 2, 4, args.length);
080        }
081
082}