001    /**
002     * Implements the CFML Function replacenocase
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 ReplaceNoCase implements Function {
015    
016            private static final long serialVersionUID = -8001488161940178031L;
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            private static String _call(PageContext pc , String str, String sub1, String sub2, boolean onlyFirst) throws FunctionException {
035                    if (StringUtil.isEmpty(sub1))
036                            throw new FunctionException(pc,"ReplaceNoCase",2,"sub1","The string length must be greater than 0");
037                    return StringUtil.replace(str, sub1, sub2, onlyFirst, true);
038            }
039            
040            private static String _call( PageContext pc, String input, Object find, String repl , boolean onlyFirst) throws PageException {
041                    if(!Decision.isSimpleValue(find ) )
042                            throw new FunctionException(pc,"ReplaceNoCase",2,"sub1","When passing three parameters or more, the second parameter must be a String.");
043                    return _call(pc, input, Caster.toString(find), repl, onlyFirst);
044            }
045            
046            public static String call( PageContext pc, String input, Object struct ) throws PageException {
047                    if(!Decision.isStruct(struct))
048                            throw new FunctionException(pc,"ReplaceNoCase",2,"sub1","When passing only two parameters, the second parameter must be a Struct.");
049                    return StringUtil.replaceMap( input, Caster.toStruct(struct), true );
050            }
051            
052    }