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 structsort 021 */ 022package lucee.runtime.functions.struct; 023 024import java.util.Arrays; 025 026import lucee.runtime.PageContext; 027import lucee.runtime.exp.ExpressionException; 028import lucee.runtime.exp.PageException; 029import lucee.runtime.functions.BIF; 030import lucee.runtime.interpreter.VariableInterpreter; 031import lucee.runtime.op.Caster; 032import lucee.runtime.type.Array; 033import lucee.runtime.type.ArrayImpl; 034import lucee.runtime.type.Collection; 035import lucee.runtime.type.Struct; 036import lucee.runtime.type.comparator.ExceptionComparator; 037import lucee.runtime.type.comparator.NumberSortRegisterComparator; 038import lucee.runtime.type.comparator.SortRegister; 039import lucee.runtime.type.comparator.SortRegisterComparator; 040import lucee.runtime.type.util.CollectionUtil; 041 042public final class StructSort extends BIF { 043 private static final long serialVersionUID = -7945612992641626477L; 044 045 public static Array call(PageContext pc , Struct base) throws PageException { 046 return call(pc,base,"text","asc",null); 047 } 048 public static Array call(PageContext pc , Struct base, String sortType) throws PageException { 049 return call(pc,base,sortType,"asc",null); 050 } 051 public static Array call(PageContext pc , Struct base, String sortType, String sortOrder) throws PageException { 052 return call(pc,base,sortType,sortOrder,null); 053 } 054 public static Array call(PageContext pc , Struct base, String sortType, String sortOrder, String pathToSubElement) throws PageException { 055 056 boolean isAsc=true; 057 PageException ee=null; 058 if(sortOrder.equalsIgnoreCase("asc"))isAsc=true; 059 else if(sortOrder.equalsIgnoreCase("desc"))isAsc=false; 060 else throw new ExpressionException("invalid sort order type ["+sortOrder+"], sort order types are [asc and desc]"); 061 062 Collection.Key[] keys = CollectionUtil.keys(base); 063 SortRegister[] arr=new SortRegister[keys.length]; 064 boolean hasSubDef=pathToSubElement!=null; 065 066 for(int i=0;i<keys.length;i++) { 067 Object value=base.get(keys[i],null); 068 069 if(hasSubDef) { 070 value=VariableInterpreter.getVariable(pc,Caster.toCollection(value),pathToSubElement); 071 } 072 arr[i]=new SortRegister(i,value); 073 } 074 075 ExceptionComparator comp=null; 076 // text 077 if(sortType.equalsIgnoreCase("text")) comp=new SortRegisterComparator(pc,isAsc,false,true); 078 079 // text no case 080 else if(sortType.equalsIgnoreCase("textnocase")) comp=new SortRegisterComparator(pc,isAsc,true,true); 081 082 // numeric 083 else if(sortType.equalsIgnoreCase("numeric")) comp=new NumberSortRegisterComparator(isAsc); 084 085 else { 086 throw new ExpressionException("invalid sort type ["+sortType+"], sort types are [text, textNoCase, numeric]"); 087 } 088 089 Arrays.sort(arr,0,arr.length,comp); 090 ee=comp.getPageException(); 091 092 if(ee!=null) { 093 throw ee; 094 } 095 096 Array rtn=new ArrayImpl(); 097 for(int i=0;i<arr.length;i++) { 098 rtn.append(keys[arr[i].getOldPosition()].getString()); 099 } 100 return rtn; 101 102 } 103 @Override 104 public Object invoke(PageContext pc, Object[] args) throws PageException { 105 if(args.length==4) return call(pc,Caster.toStruct(args[0]),Caster.toString(args[1]),Caster.toString(args[2]),Caster.toString(args[3])); 106 if(args.length==3) return call(pc,Caster.toStruct(args[0]),Caster.toString(args[1]),Caster.toString(args[2])); 107 if(args.length==2) return call(pc,Caster.toStruct(args[0]),Caster.toString(args[1])); 108 return call(pc,Caster.toStruct(args[0])); 109 } 110}