1 13 package com.tonbeller.jpivot.mondrian; 14 15 import java.text.NumberFormat ; 16 import java.text.ParseException ; 17 18 import mondrian.olap.Query; 19 import mondrian.olap.OlapElement; 20 import mondrian.olap.Util; 21 22 import org.apache.log4j.Logger; 23 24 import com.tonbeller.jpivot.core.ExtensionSupport; 25 import com.tonbeller.jpivot.olap.model.Dimension; 26 import com.tonbeller.jpivot.olap.model.DoubleExpr; 27 import com.tonbeller.jpivot.olap.model.Expression; 28 import com.tonbeller.jpivot.olap.model.Hierarchy; 29 import com.tonbeller.jpivot.olap.model.IntegerExpr; 30 import com.tonbeller.jpivot.olap.model.Level; 31 import com.tonbeller.jpivot.olap.model.Member; 32 import com.tonbeller.jpivot.olap.model.impl.DoubleExprImpl; 33 import com.tonbeller.jpivot.olap.model.impl.IntegerExprImpl; 34 import com.tonbeller.jpivot.olap.model.impl.StringExprImpl; 35 import com.tonbeller.jpivot.olap.navi.ExpressionParser; 36 37 40 public class MondrianExpressionParser extends ExtensionSupport implements ExpressionParser { 41 42 static NumberFormat nf = NumberFormat.getInstance(); 43 static Logger logger = Logger.getLogger(MondrianSetParameter.class); 44 45 47 public MondrianExpressionParser() { 48 super.setId(ExpressionParser.ID); 49 } 50 51 54 public String unparse(Expression expr) { 55 if (expr instanceof DoubleExpr) { 56 double d = ((DoubleExpr) expr).getValue(); 57 return nf.format(d); 58 } else if (expr instanceof IntegerExpr) { 59 int ii = ((IntegerExpr) expr).getValue(); 60 return String.valueOf(ii); 61 } else if (expr instanceof MondrianMember) { 62 MondrianMember m = (MondrianMember) expr; 63 mondrian.olap.Member monMember = m.getMonMember(); 64 return monMember.getUniqueName(); 65 } 66 else if (expr instanceof MondrianLevel) { 67 return ((MondrianLevel)expr).getUniqueName(); 68 } 69 else if (expr instanceof MondrianHierarchy) { 70 return ((MondrianHierarchy)expr).getUniqueName(); 71 } 72 else if (expr instanceof MondrianDimension) { 73 return ((MondrianDimension)expr).getUniqueName(); 74 } 75 return null; 76 } 77 78 87 public Expression parse(String expr) throws InvalidSyntaxException { 88 89 String trimmed = expr.trim(); 91 int len = trimmed.length(); 92 if (trimmed.charAt(0) == '"' && trimmed.charAt(len - 1) == '"') 93 return new StringExprImpl(trimmed.substring(1, trimmed.length() - 1)); 94 if (trimmed.charAt(0) == '\'' && trimmed.charAt(len - 1) == '\'') 95 return new StringExprImpl(trimmed.substring(1, trimmed.length() - 1)); 96 97 Number number = null; 99 try { 100 number = nf.parse(trimmed); 101 } catch (ParseException pex) { 102 } 104 if (number != null) { 105 if (number instanceof Double ) { 106 return new DoubleExprImpl(number.doubleValue()); 107 } else { 108 return new IntegerExprImpl(number.intValue()); 109 } 110 } 111 112 MondrianModel model = (MondrianModel) getModel(); 113 Query query = ((MondrianQueryAdapter)model.getQueryAdapter()).getMonQuery(); 114 115 OlapElement element; 117 try { 118 element = Util.lookup(query, Util.explode(trimmed)); 119 } catch (Exception e) { 120 logger.info(e); 121 throw new InvalidSyntaxException(trimmed); 122 } 123 if (element instanceof mondrian.olap.Member) { 124 final mondrian.olap.Member monMember = (mondrian.olap.Member) element; 125 Member member = model.lookupMemberByUName(monMember.getUniqueName()); 126 return member; 127 } else if (element instanceof mondrian.olap.Level) { 128 mondrian.olap.Level monLevel = (mondrian.olap.Level) element; 129 MondrianLevel level = model.lookupLevel(monLevel.getUniqueName()); 130 return level; 131 } else if (element instanceof mondrian.olap.Hierarchy) { 132 mondrian.olap.Hierarchy monHier = (mondrian.olap.Hierarchy) element; 133 MondrianHierarchy hier = model.lookupHierarchy(monHier.getUniqueName()); 134 return hier; 135 } else if (element instanceof mondrian.olap.Dimension) { 136 mondrian.olap.Dimension monDim = (mondrian.olap.Dimension) element; 137 MondrianDimension dim = model.lookupDimension(monDim.getUniqueName()); 138 return dim; 139 } 140 141 throw new InvalidSyntaxException("could not resolve expression " + trimmed); 142 } 143 144 public Member lookupMember(String uniqueName) throws InvalidSyntaxException { 145 MondrianModel model = (MondrianModel) getModel(); 146 return model.lookupMemberByUName(uniqueName); 147 } 148 149 public Level lookupLevel(String uniqueName) throws InvalidSyntaxException { 150 MondrianModel model = (MondrianModel) getModel(); 151 return model.lookupLevel(uniqueName); 152 } 153 154 public Hierarchy lookupHierarchy(String uniqueName) throws InvalidSyntaxException { 155 MondrianModel model = (MondrianModel) getModel(); 156 return model.lookupHierarchy(uniqueName); 157 } 158 159 public Dimension lookupDimension(String uniqueName) throws InvalidSyntaxException { 160 MondrianModel model = (MondrianModel) getModel(); 161 return model.lookupDimension(uniqueName); 162 } 163 164 } | Popular Tags |