1 13 14 package mondrian.olap; 15 import mondrian.olap.type.Type; 16 import mondrian.mdx.MdxVisitor; 17 18 import java.io.PrintWriter ; 19 import java.util.List ; 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 23 26 public class Id 27 extends ExpBase 28 implements Cloneable { 29 30 private final List <Segment> segments; 31 32 37 public Id(Segment segment) { 38 segments = Collections.singletonList(segment); 39 } 40 41 private Id(List <Segment> segments) { 42 this.segments = segments; 43 } 44 45 public Id clone() { 46 return this; 48 } 49 50 public int getCategory() { 51 return Category.Unknown; 52 } 53 54 public Type getType() { 55 throw new UnsupportedOperationException (); 57 } 58 59 public String toString() { 60 return Util.quoteMdxIdentifier(toStringArray()); 61 } 62 63 public String [] toStringArray() { 64 String [] names = new String [segments.size()]; 65 int k = 0; 66 for (Segment segment : segments) { 67 names[k++] = segment.name; 68 } 69 return names; 70 } 71 72 public String getElement(int i) { 73 return segments.get(i).name; 74 } 75 76 83 public Id append(Segment segment) { 84 List <Segment> newSegments = new ArrayList <Segment>(segments); 85 newSegments.add(segment); 86 return new Id(newSegments); 87 } 88 89 public Exp accept(Validator validator) { 90 if (segments.size() == 1) { 91 final Segment s = segments.get(0); 92 if (s.quoting == Quoting.UNQUOTED && 93 validator.getFunTable().isReserved(s.name)) { 94 return Literal.createSymbol(s.name.toUpperCase()); 95 } 96 } 97 final String [] names = toStringArray(); 98 final Exp element = Util.lookup(validator.getQuery(), names, true); 99 if (element == null) { 100 return null; 101 } 102 return element.accept(validator); 103 } 104 105 public Object accept(MdxVisitor visitor) { 106 return visitor.visit(this); 107 } 108 109 public void unparse(PrintWriter pw) { 110 int k = 0; 111 for (Segment s : segments) { 112 if (k++ > 0) { 113 pw.print("."); 114 } 115 switch (s.quoting) { 116 case UNQUOTED: 117 pw.print(s.name); 118 break; 119 case KEY: 120 pw.print("&[" + Util.mdxEncodeString(s.name) + "]"); 121 break; 122 case QUOTED: 123 pw.print("[" + Util.mdxEncodeString(s.name) + "]"); 124 break; 125 } 126 } 127 } 128 129 141 public static class Segment { 142 public final String name; 143 public final Quoting quoting; 144 145 public Segment(String name, Quoting quoting) { 146 this.name = name; 147 this.quoting = quoting; 148 } 149 } 150 151 public enum Quoting { 152 153 156 UNQUOTED, 157 158 161 QUOTED, 162 163 167 KEY 168 } 169 } 170 171 | Popular Tags |