1 13 package com.tonbeller.jpivot.olap.model; 14 15 import java.util.HashSet ; 16 import java.util.Set ; 17 18 23 public class ExpressionPrinter extends VisitorSupportStrict { 24 StringBuffer sb = new StringBuffer (); 25 26 static final Set infix = new HashSet (); 27 static final Set prefix = new HashSet (); 28 static { 29 String [] s = new String [] { "*", "/", "+", "-", "%", "<", ">", "<=", ">=", "<>", "=", "and", "or", "xor" }; 30 for (int i = 0; i < s.length; i++) 31 infix.add(s[i]); 32 prefix.add("not"); 33 prefix.add("-"); 34 }; 35 36 public String print(Visitable e) { 37 if (e == null) 38 return "(null)"; 39 sb = new StringBuffer (); 40 e.accept(this); 41 return sb.toString(); 42 } 43 44 public String toString() { 45 return sb.toString(); 46 } 47 48 void append(String s) { 49 sb.append(s); 50 } 51 52 public void visitBooleanExpr(BooleanExpr v) { 53 sb.append(v.getValue()); 54 } 55 public void visitDimension(Dimension v) { 56 sb.append("[" + v.getLabel() + "]"); 57 } 58 public void visitDoubleExpr(DoubleExpr v) { 59 sb.append(v.getValue()); 60 } 61 public void visitFunCallExpr(FunCallExpr v) { 62 ExpressionPrinter ep = new ExpressionPrinter(); 63 Expression[] args = v.getArgs(); 64 65 String name = v.getName(); 67 if (infix.contains(name) && args.length == 2) { 68 String left = ep.print(args[0]); 69 String right = ep.print(args[1]); 70 sb.append(left).append(name).append(right); 71 return; 72 } 73 74 for (int i = 0; i < args.length; i++) { 75 if (i > 0) 76 ep.append(", "); 77 args[i].accept(ep); 78 } 79 if ("()".equals(name)) 80 sb.append("(").append(ep).append(")"); 81 else if ("{}".equals(name)) 82 sb.append("{").append(ep).append("}"); 83 else if (prefix.contains(name)) 84 sb.append(name).append(ep); else if (name.startsWith(".")) { 86 if (args.length > 1) { 87 ep = new ExpressionPrinter(); 89 args[0].accept(ep); 90 sb.append(ep).append(name).append("("); 91 ep = new ExpressionPrinter(); 92 for (int i = 1; i < args.length; i++) { 93 if (i > 1) 94 ep.append(", "); 95 args[i].accept(ep); 96 } 97 sb.append(ep).append(")"); 98 } 99 else 100 sb.append(ep).append(name); 101 } 102 else 103 sb.append(name).append("(").append(ep).append(")"); 104 } 105 public void visitHierarchy(Hierarchy v) { 106 sb.append("[" + v.getLabel() + "]"); 107 } 108 public void visitIntegerExpr(IntegerExpr v) { 109 sb.append(v.getValue()); 110 } 111 public void visitLevel(Level v) { 112 sb.append("[" + v.getLabel() + "]"); 113 } 114 public void visitMember(Member v) { 115 sb.append("[" + v.getLabel() + "]"); 116 } 117 public void visitParameterExpr(ParameterExpr v) { 118 sb.append("Parameter(\"RandomID\""); 121 switch (v.getType()) { 122 case ParameterExpr.MEMBER : 123 sb.append(", MEMBER"); 124 break; 125 case ParameterExpr.NUMBER : 126 sb.append(", NUMBER"); 127 break; 128 case ParameterExpr.STRING : 129 sb.append(", STRING"); 130 break; 131 default : 132 throw new IllegalArgumentException (); 133 } 134 sb.append(", \"").append(v.getLabel()).append("\""); 135 sb.append(", "); 136 v.getValue().accept(this); 137 sb.append(")"); 138 } 139 public void visitStringExpr(StringExpr v) { 140 sb.append("\"" + v.getValue() + "\""); 141 } 142 143 public void visitPropertyExpr(PropertyExpr v) { 144 sb.append(v.getName()).append("="); 145 v.getValueExpr().accept(this); 146 sb.append(";"); 147 148 } 149 150 } 151 | Popular Tags |