1 10 package mondrian.olap.fun; 11 12 import mondrian.olap.FunDef; 13 import mondrian.olap.Exp; 14 import mondrian.olap.Literal; 15 import mondrian.olap.Evaluator; 16 import mondrian.calc.Calc; 17 import mondrian.calc.ExpCompiler; 18 import mondrian.calc.StringCalc; 19 import mondrian.calc.impl.AbstractStringCalc; 20 import mondrian.mdx.ResolvedFunCall; 21 import mondrian.util.Format; 22 23 import java.util.Locale ; 24 25 32 class FormatFunDef extends FunDefBase { 33 static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver( 34 "Format", 35 "Format(<Numeric Expression>, <String Expression>)", 36 "Formats a number to string.", 37 new String [] { "fSmS", "fSnS" }, 38 FormatFunDef.class); 39 40 public FormatFunDef(FunDef dummyFunDef) { 41 super(dummyFunDef); 42 } 43 44 public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) { 45 final Exp[] args = call.getArgs(); 46 final Calc calc = compiler.compileScalar(call.getArg(0), true); 47 final Locale locale = compiler.getEvaluator().getConnectionLocale(); 48 if (args[1] instanceof Literal) { 49 String formatString = (String ) ((Literal) args[1]).getValue(); 52 final Format format = new Format(formatString, locale); 53 return new AbstractStringCalc(call, new Calc[] {calc}) { 54 public String evaluateString(Evaluator evaluator) { 55 final Object o = calc.evaluate(evaluator); 56 return format.format(o); 57 } 58 }; 59 } else { 60 final StringCalc stringCalc = 62 compiler.compileString(call.getArg(1)); 63 return new AbstractStringCalc(call, new Calc[] {calc, stringCalc}) { 64 public String evaluateString(Evaluator evaluator) { 65 final Object o = calc.evaluate(evaluator); 66 final String formatString = 67 stringCalc.evaluateString(evaluator); 68 final Format format = 69 new Format(formatString, locale); 70 return format.format(o); 71 } 72 }; 73 74 } 75 }; 76 } 77 78 | Popular Tags |