001    package railo.runtime.functions.international;
002    
003    import java.text.NumberFormat;
004    import java.text.ParsePosition;
005    import java.util.Locale;
006    import java.util.WeakHashMap;
007    
008    import railo.runtime.PageContext;
009    import railo.runtime.exp.ExpressionException;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.ext.function.Function;
012    import railo.runtime.i18n.LocaleFactory;
013    
014    /**
015     * Implements the Cold Fusion Function lsparsecurrency
016     */
017    public final class LSParseNumber implements Function {
018            
019            private static final long serialVersionUID = 2219030609677513651L;
020            
021            private static WeakHashMap<Locale,NumberFormat> whm=new WeakHashMap<Locale,NumberFormat>();
022    
023            public static double call(PageContext pc , String string) throws PageException {
024                    return toDoubleValue(pc.getLocale(),string);
025            }
026            
027            public static double call(PageContext pc , String string,String strLocale) throws PageException {
028                    return toDoubleValue(strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale),string);
029            }
030            
031            
032            public synchronized static double toDoubleValue(Locale locale,String str) throws PageException {
033                    Object o=whm.get(locale);
034                    NumberFormat nf=null;
035                    if(o==null) {
036                            nf=NumberFormat.getInstance(locale);
037                            whm.put(locale,nf);
038                    }
039                    else {
040                            nf=(NumberFormat) o;
041                    }
042                    str=optimze(str.toCharArray());
043                    
044                    ParsePosition pp = new ParsePosition(0);
045            Number result = nf.parse(str, pp);
046                    
047            if (pp.getIndex() < str.length()) {
048                throw new ExpressionException("can't parse String [" + str + "] against locale ["+LocaleFactory.toString(locale)+"] to a number");
049            }
050            if(result==null)
051                    throw new ExpressionException("can't parse String [" + str + "] against locale ["+LocaleFactory.toString(locale)+"] to a number");
052            return result.doubleValue();
053                    
054            }
055            
056            
057            private static String optimze(char[] carr) {
058                    StringBuffer sb=new StringBuffer();
059                    char c;
060                    for(int i=0;i<carr.length;i++){
061                            c=carr[i];
062                            if(!Character.isWhitespace(c) && c!='+')sb.append(carr[i]);
063                    }
064                    
065                    return sb.toString();
066            }
067    
068            
069            
070    }