001 /** 002 * Implements the CFML Function listsetat 003 */ 004 package railo.runtime.functions.list; 005 006 import railo.runtime.PageContext; 007 import railo.runtime.exp.ExpressionException; 008 import railo.runtime.exp.FunctionException; 009 import railo.runtime.ext.function.Function; 010 import railo.runtime.type.Array; 011 import railo.runtime.type.util.ListUtil; 012 013 public final class ListSetAt implements Function { 014 015 private static final long serialVersionUID = -105782799713547552L; 016 017 public static String call(PageContext pc , String list, double posNumber, String value) throws ExpressionException { 018 return call(pc,list,posNumber,value,",",false); 019 } 020 021 public static String call(PageContext pc , String list, double posNumber, String value, String delimiter) throws ExpressionException { 022 return call(pc,list,posNumber,value,delimiter,false); 023 } 024 public static String call(PageContext pc , String list, double posNumber, String value, String delimiter, boolean includeEmptyFields) throws ExpressionException { 025 026 if(list.length()==0) 027 throw new FunctionException(pc,"listSetAt",1,"list","can't be empty"); 028 029 030 int pos=((int) posNumber); 031 //int[] removedInfo=new int[2]; 032 033 Array arr = ListUtil.listToArray(list,delimiter); 034 int len=arr.size(); 035 036 // invalid index 037 if(pos<1) 038 throw new FunctionException(pc,"listSetAt",2,"position","invalid string list index ["+(pos)+"]"); 039 else if(len<pos) { 040 throw new FunctionException(pc,"listSetAt",2,"position","invalid string list index ["+(pos)+"], indexes go from 1 to "+(len)); 041 } 042 043 StringBuffer sb=new StringBuffer();//RepeatString.call(new StringBuffer(),delimiter,removedInfo[0]); 044 boolean hasStart=false; 045 boolean hasSet=false; 046 String v; 047 int count=0; 048 for(int i=1;i<=len;i++) { 049 v=(String)arr.get(i,""); 050 if(hasStart) { 051 sb.append(delimiter); 052 } 053 else hasStart=true; 054 055 if(includeEmptyFields || v.length()>0)count++; 056 if(!hasSet && pos==count) { 057 sb.append(value); 058 hasSet=true; 059 } 060 else sb.append(arr.get(i,"")); 061 } 062 if(!hasSet){ 063 throw new FunctionException(pc,"listSetAt",2,"position","invalid string list index ["+(pos)+"]"); 064 } 065 066 067 return sb.toString(); 068 } 069 }