001 /** 002 * Implements the CFML Function replace 003 */ 004 package railo.runtime.functions.string; 005 006 import railo.commons.lang.StringUtil; 007 import railo.runtime.PageContext; 008 import railo.runtime.exp.FunctionException; 009 import railo.runtime.exp.PageException; 010 import railo.runtime.ext.function.Function; 011 import railo.runtime.op.Caster; 012 import railo.runtime.op.Decision; 013 014 public final class Replace implements Function { 015 016 private static final long serialVersionUID = -313884944032266348L; 017 018 public static String call(PageContext pc , String str, String sub1, String sub2) throws FunctionException { 019 return _call(pc, str, sub1, sub2, true); 020 } 021 022 public static String call(PageContext pc , String str, String sub1, String sub2, String scope) throws FunctionException { 023 return _call(pc, str, sub1, sub2, !scope.equalsIgnoreCase("all")); 024 } 025 026 public static String call( PageContext pc, String input, Object find, String repl, String scope ) throws PageException { 027 return _call(pc, input, find, repl, !scope.equalsIgnoreCase("all")); 028 } 029 030 public static String call( PageContext pc, String input, Object find, String repl ) throws PageException { 031 return _call(pc, input, find, repl, true); 032 } 033 034 public static String call( PageContext pc, String input, Object struct ) throws PageException { 035 if(!Decision.isStruct(struct)) 036 throw new FunctionException(pc,"replace",2,"sub1","When passing only two parameters, the second parameter must be a Struct."); 037 return StringUtil.replaceMap( input, Caster.toMap(struct), false ); 038 } 039 040 private static String _call(PageContext pc , String str, String sub1, String sub2, boolean firstOnly) throws FunctionException { 041 if (StringUtil.isEmpty(sub1)) 042 throw new FunctionException(pc,"replace",2,"sub1","The string length must be greater than 0"); 043 return StringUtil.replace(str, sub1, sub2, firstOnly); 044 } 045 046 private static String _call( PageContext pc, String input, Object find, String repl, boolean onlyFirst) throws PageException { 047 if(!Decision.isSimpleValue(find ) ) 048 throw new FunctionException(pc,"replace",2,"sub1","When passing three parameters or more, the second parameter must be a simple value."); 049 return _call(pc, input, Caster.toString(find), repl, onlyFirst); 050 } 051 052 }