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/** DIFF 23
020 * Implements the CFML Function lsnumberformat
021 */
022package lucee.runtime.functions.international;
023
024import java.util.Locale;
025
026import lucee.runtime.PageContext;
027import lucee.runtime.exp.FunctionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.ext.function.Function;
030import lucee.runtime.functions.displayFormatting.NumberFormat;
031import lucee.runtime.i18n.LocaleFactory;
032import lucee.runtime.util.InvalidMaskException;
033
034public final class LSNumberFormat implements Function {
035        
036        private static final long serialVersionUID = -7981883050285346336L;
037
038        public static String call(PageContext pc , Object object) throws PageException {
039                return call(pc, object, null, pc.getLocale());
040        }
041        
042        public static String call(PageContext pc , Object object, String mask) throws PageException {
043            return call(pc, object, mask, pc.getLocale());
044        }
045        
046        public static String call(PageContext pc , Object object, String mask, String locale) throws PageException {
047                return call(pc, object, mask, 
048                                locale==null?pc.getLocale():LocaleFactory.getLocale(locale));
049        }
050        
051        private static String call(PageContext pc , Object object, String mask, Locale locale) throws PageException {
052                try {
053            if(mask==null) 
054                return new lucee.runtime.util.NumberFormat().format(locale,NumberFormat.toNumber(pc,object));
055                        return new lucee.runtime.util.NumberFormat().format(locale,NumberFormat.toNumber(pc,object),mask);
056        } 
057        catch (InvalidMaskException e) {
058            throw new FunctionException(pc,"lsnumberFormat",1,"number",e.getMessage());
059        }
060        }
061}