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 **/
019package lucee.runtime.functions.poi;
020
021import lucee.runtime.PageContext;
022import lucee.runtime.exp.FunctionException;
023import lucee.runtime.exp.PageException;
024import lucee.runtime.op.Caster;
025import lucee.runtime.op.Decision;
026import lucee.runtime.poi.Excel;
027import lucee.runtime.poi.ExcelUtil;
028
029public class SpreadSheetNew {
030        
031        private static final String DEFAULT_NAME = "Sheet1";
032
033        public static Object call(PageContext pc) {
034                return new Excel(DEFAULT_NAME, Excel.FORMAT_HSSF, 0);
035        }
036
037        public static Object call(PageContext pc, Object sheetNameOrXMLFormat) throws PageException {
038                short format=toFormat(sheetNameOrXMLFormat);
039                if(format==Excel.FORMAT_UNDEFINED)
040                        return new Excel(Caster.toString(sheetNameOrXMLFormat), Excel.FORMAT_HSSF, 0);
041                return new Excel(DEFAULT_NAME, format, 0);
042        }
043
044        public static Object call(PageContext pc, Object oSheetName, Object oXmlFormat) throws PageException {
045                // name
046                String sheetName;
047                if(oSheetName==null) sheetName=DEFAULT_NAME;
048                else sheetName=Caster.toString(oSheetName);
049                
050                // format
051                short format=toFormat(oXmlFormat);
052                if(format==Excel.FORMAT_UNDEFINED)
053                        throw new FunctionException(pc, "SpreadSheetNew", 2, "xmlFormat", "invalid value ["+oXmlFormat+"], valid values are [true,false,XSSF,HSSF]");
054                
055                return new Excel(sheetName, format, 0);
056        }
057        
058        
059        
060        private static short toFormat(Object xmlFormat) {
061                if(Decision.isCastableToBoolean(xmlFormat)) {
062                        Boolean b = Caster.toBoolean(xmlFormat,null);
063                        if(b==null) return Excel.FORMAT_UNDEFINED;
064                        return b.booleanValue()?Excel.FORMAT_XSSF:Excel.FORMAT_HSSF;
065                }
066                return ExcelUtil.format(Caster.toString(xmlFormat,""), Excel.FORMAT_UNDEFINED);
067        }
068}