001    package railo.runtime.functions.poi;
002    
003    import railo.runtime.PageContext;
004    import railo.runtime.exp.FunctionException;
005    import railo.runtime.exp.PageException;
006    import railo.runtime.op.Caster;
007    import railo.runtime.op.Decision;
008    import railo.runtime.poi.Excel;
009    import railo.runtime.poi.ExcelUtil;
010    
011    public class SpreadSheetNew {
012            
013            private static final String DEFAULT_NAME = "Sheet1";
014    
015            public static Object call(PageContext pc) {
016                    return new Excel(DEFAULT_NAME, Excel.FORMAT_HSSF, 0);
017            }
018    
019            public static Object call(PageContext pc, Object sheetNameOrXMLFormat) throws PageException {
020                    short format=toFormat(sheetNameOrXMLFormat);
021                    if(format==Excel.FORMAT_UNDEFINED)
022                            return new Excel(Caster.toString(sheetNameOrXMLFormat), Excel.FORMAT_HSSF, 0);
023                    return new Excel(DEFAULT_NAME, format, 0);
024            }
025    
026            public static Object call(PageContext pc, Object oSheetName, Object oXmlFormat) throws PageException {
027                    // name
028                    String sheetName;
029                    if(oSheetName==null) sheetName=DEFAULT_NAME;
030                    else sheetName=Caster.toString(oSheetName);
031                    
032                    // format
033                    short format=toFormat(oXmlFormat);
034                    if(format==Excel.FORMAT_UNDEFINED)
035                            throw new FunctionException(pc, "SpreadSheetNew", 2, "xmlFormat", "invalid value ["+oXmlFormat+"], valid values are [true,false,XSSF,HSSF]");
036                    
037                    return new Excel(sheetName, format, 0);
038            }
039            
040            
041            
042            private static short toFormat(Object xmlFormat) {
043                    if(Decision.isCastableToBoolean(xmlFormat)) {
044                            Boolean b = Caster.toBoolean(xmlFormat,null);
045                            if(b==null) return Excel.FORMAT_UNDEFINED;
046                            return b.booleanValue()?Excel.FORMAT_XSSF:Excel.FORMAT_HSSF;
047                    }
048                    return ExcelUtil.format(Caster.toString(xmlFormat,""), Excel.FORMAT_UNDEFINED);
049            }
050    }