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 arraytolist 021 */ 022package lucee.runtime.functions.list; 023 024import lucee.runtime.PageContext; 025import lucee.runtime.exp.PageException; 026import lucee.runtime.functions.BIF; 027import lucee.runtime.op.Caster; 028import lucee.runtime.type.Array; 029 030public final class ArrayToList extends BIF { 031 032 private static final long serialVersionUID = -4909685848106371747L; 033 034 public static String call(PageContext pc , Array array) throws PageException { 035 return call(pc,array,','); 036 } 037 public static String call(PageContext pc , Array array, String delimiter) throws PageException { 038 if(delimiter.length()==1) return call(pc,array,delimiter.charAt(0)); 039 int len=array.size(); 040 if(len==0) return ""; 041 if(len==1)return Caster.toString(array.get(1,"")); 042 043 Object o=array.get(1,null); 044 StringBuilder sb=new StringBuilder(o==null?"":Caster.toString(o)); 045 for(int i=2;i<=len;i++) { 046 sb.append(delimiter); 047 o=array.get(i,null); 048 sb.append(o==null?"":Caster.toString(o)); 049 } 050 return sb.toString(); 051 } 052 public static String call(PageContext pc , Array array, char delimiter) throws PageException { 053 int len=array.size(); 054 if(len==0) return ""; 055 if(len==1)return Caster.toString(array.getE(1)); 056 057 Object o=array.get(1,null); 058 StringBuilder sb=new StringBuilder(o==null?"":Caster.toString(o)); 059 for(int i=2;i<=len;i++) { 060 sb.append(delimiter); 061 o=array.get(i,null); 062 sb.append(o==null?"":Caster.toString(o)); 063 } 064 return sb.toString(); 065 } 066 067 @Override 068 public Object invoke(PageContext pc, Object[] args) throws PageException { 069 if(args.length==1) return call(pc,Caster.toArray(args[0])); 070 return call(pc,Caster.toArray(args[0]),Caster.toString(args[1])); 071 } 072}