1 package jimm.datavision; 2 import jimm.datavision.field.Field; 3 import jimm.util.XMLWriter; 4 import jimm.util.StringUtils; 5 import jimm.util.Replacer; 6 import jimm.datavision.source.Column; 7 import jimm.util.I18N; 8 import java.util.*; 9 10 33 public abstract class Expression 34 extends Observable 35 implements Identity, Nameable, Writeable, Draggable, Observer 36 { 37 38 protected Long id; 39 protected Report report; 40 protected String name; 41 protected String expr; 42 protected String exceptAfter; 43 protected ArrayList observedContents; 44 45 53 public static String expressionToDisplay(Report report, String str) { 54 if (str == null || str.length() == 0 || str.indexOf("{") == -1) 55 return str; 56 57 StringBuffer buf = new StringBuffer (); 58 int len = str.length(); 59 for (int i = 0; i < len; ++i) { 60 char c = str.charAt(i); 61 if (c == '{' && (i + 1) < len) { 62 int nameStart, nameEnd; 63 switch (str.charAt(i + 1)) { 64 case '@': nameStart = i + 2; 66 nameEnd = str.indexOf("}", nameStart); 67 if (nameEnd != -1) { 68 String idAsString = str.substring(nameStart, nameEnd); 69 buf.append("{@"); 70 buf.append(report.findFormula(idAsString).getName()); 71 buf.append("}"); 72 i = nameEnd; 73 } 74 break; 75 case '?': nameStart = i + 2; 77 nameEnd = str.indexOf("}", nameStart); 78 if (nameEnd != -1) { 79 String idAsString = str.substring(nameStart, nameEnd); 80 buf.append("{?"); 81 buf.append(report.findParameter(idAsString).getName()); 82 buf.append("}"); 83 i = nameEnd; 84 } 85 break; 86 case '!': nameStart = i + 2; 88 nameEnd = str.indexOf("}", nameStart); 89 if (nameEnd != -1) { 90 String idAsString = str.substring(nameStart, nameEnd); 91 buf.append("{!"); 92 buf.append(report.findUserColumn(idAsString).getName()); 93 buf.append("}"); 94 i = nameEnd; 95 } 96 break; 97 default: 98 buf.append(c); 99 break; 100 } 101 } 102 else { 103 buf.append(c); 104 } 105 } 106 return buf.toString(); 107 } 108 109 118 public static String displayToExpression(Report report, String str) { 119 if (str == null || str.length() == 0 || str.indexOf("{") == -1) 120 return str; 121 122 StringBuffer buf = new StringBuffer (); 123 int len = str.length(); 124 for (int i = 0; i < len; ++i) { 125 char c = str.charAt(i); 126 if (c == '{' && (i + 1) < len) { 127 int nameStart, nameEnd; 128 switch (str.charAt(i + 1)) { 129 case '@': nameStart = i + 2; 131 nameEnd = str.indexOf("}", nameStart); 132 if (nameEnd != -1) { 133 String formulaName = str.substring(nameStart, nameEnd); 134 buf.append("{@"); 135 Formula formula = report.findFormulaByName(formulaName); 136 if (formula == null) { 137 str = I18N.get("Utils.in") 138 + " \"" + str + "\": " 139 + I18N.get("Utils.no_such_formula") 140 + ' ' + formulaName; 141 throw new IllegalArgumentException (str); 142 } 143 buf.append(formula.getId()); 144 buf.append("}"); 145 i = nameEnd; 146 } 147 break; 148 case '?': nameStart = i + 2; 150 nameEnd = str.indexOf("}", nameStart); 151 if (nameEnd != -1) { 152 String paramName = str.substring(nameStart, nameEnd); 153 buf.append("{?"); 154 Parameter param = report.findParameterByName(paramName); 155 if (param == null) { 156 str = I18N.get("Utils.in") 157 + " \"" + str + "\": " 158 + I18N.get("Utils.no_such_param") 159 + ' ' + paramName; 160 throw new IllegalArgumentException (str); 161 } 162 buf.append(param.getId()); 163 buf.append("}"); 164 i = nameEnd; 165 } 166 break; 167 case '!': nameStart = i + 2; 169 nameEnd = str.indexOf("}", nameStart); 170 if (nameEnd != -1) { 171 String ucName = str.substring(nameStart, nameEnd); 172 buf.append("{!"); 173 UserColumn uc = report.findUserColumnByName(ucName); 174 if (uc == null) { 175 str = I18N.get("Utils.in") 176 + " \"" + str + "\": " 177 + I18N.get("Utils.no_such_usercol") 178 + ' ' + ucName; 179 throw new IllegalArgumentException (str); 180 } 181 buf.append(uc.getId()); 182 buf.append("}"); 183 i = nameEnd; 184 } 185 break; 186 default: 187 buf.append(c); 188 break; 189 } 190 } 191 else { 192 buf.append(c); 193 } 194 } 195 196 return buf.toString(); 197 } 198 199 214 protected Expression(Long id, Report report, String name, String expression, 215 String exceptAfter) 216 { 217 if (id == null) throw new IllegalArgumentException ("Subclasses of Expression must" 219 + " not pass in a null id"); 220 221 this.report = report; 222 this.id = id; 223 this.name = name; 224 expr = expression; 225 this.exceptAfter = exceptAfter; 226 227 observedContents = null; 232 } 233 234 protected void finalize() throws Throwable { 235 stopObservingContents(); 236 super.finalize(); 237 } 238 239 public void update(Observable o, Object arg) { 240 setChanged(); 241 notifyObservers(arg); 242 } 243 244 public Object getId() { return id; } 245 246 251 public String getName() { return name; } 252 253 258 public void setName(String newName) { 259 if (name != newName && (name == null || !name.equals(newName))) { 260 name = newName; 261 setChanged(); 262 notifyObservers(); 263 } 264 } 265 266 271 public String getExpression() { 272 if (observedContents == null) 277 startObservingContents(); 278 279 return expr; 280 } 281 282 287 public void setExpression(String newExpression) { 288 if (expr != newExpression 289 && (expr == null || !expr.equals(newExpression))) 290 { 291 stopObservingContents(); 292 expr = newExpression; 293 294 298 setChanged(); 299 notifyObservers(); 300 } 301 } 302 303 307 protected void startObservingContents() { 308 observedContents = new ArrayList(); 310 if (expr == null || expr.length() == 0) 311 return; 312 313 316 StringUtils.replaceDelimited(exceptAfter, "{@", "}", new Replacer() { 318 public Object replace(String str) { 319 Formula f = report.findFormula(str); 320 observedContents.add(f); 321 f.addObserver(Expression.this); 322 return ""; }}, 324 expr); 325 326 StringUtils.replaceDelimited(exceptAfter, "{?", "}", new Replacer() { 328 public Object replace(String str) { 329 Parameter p = report.findParameter(str); 330 observedContents.add(p); 331 p.addObserver(Expression.this); 332 return ""; }}, 334 expr); 335 336 StringUtils.replaceDelimited(exceptAfter, "{!", "}", new Replacer() { 338 public Object replace(String str) { 339 UserColumn uc = report.findUserColumn(str); 340 observedContents.add(uc); 341 uc.addObserver(Expression.this); 342 return ""; }}, 344 expr); 345 } 346 347 350 protected void stopObservingContents() { 351 if (observedContents != null) { 352 for (Iterator iter = observedContents.iterator(); iter.hasNext(); ) 353 ((Observable)iter.next()).deleteObserver(this); 354 observedContents = null; 355 } 356 } 357 358 367 public String getEditableExpression() { 368 return expressionToDisplay(report, getExpression()); 369 } 370 371 382 public void setEditableExpression(String newExpression) { 383 setExpression(displayToExpression(report, newExpression)); 384 } 385 386 public abstract String dragString(); 387 public abstract String designLabel(); 388 public abstract String formulaString(); 389 390 398 public boolean refersTo(Field f) { 399 String str = getExpression(); 400 if (str != null && str.length() > 0) 401 return str.indexOf(f.formulaString()) != -1; 402 else 403 return false; 404 } 405 406 414 public boolean refersTo(Expression expression) { 415 String str = getExpression(); 416 if (str != null && str.length() > 0) 417 return str.indexOf(expression.formulaString()) != -1; 418 else 419 return false; 420 } 421 422 430 public boolean refersTo(Parameter p) { 431 String str = getExpression(); 432 if (str != null && str.length() > 0) 433 return str.indexOf(p.formulaString()) != -1; 434 else 435 return false; 436 } 437 438 446 public Collection columnsUsed() { 447 final ArrayList list = new ArrayList(); 448 449 StringUtils.replaceDelimited(exceptAfter, "{", "}", new Replacer() { 452 public Object replace(String str) { 453 switch (str.charAt(0)) { 454 case '!': UserColumn uc = report.findUserColumn(str.substring(1)); 456 if (uc != null) list.addAll(uc.columnsUsed()); 458 break; 459 case '%': case '@': case '?': break; default: 464 Column col = report.findColumn(str); 465 if (col != null) list.add(col); 467 } 468 return ""; }}, 470 getExpression()); 471 472 return list; 473 } 474 475 483 public Collection userColumnsUsed() { 484 final ArrayList list = new ArrayList(); 485 486 StringUtils.replaceDelimited(exceptAfter, "{!", "}", new Replacer() { 489 public Object replace(String str) { 490 UserColumn uc = report.findUserColumn(str); 491 if (uc != null) list.add(uc); 493 return ""; }}, 495 getExpression()); 496 497 return list; 498 } 499 500 505 public abstract void writeXML(XMLWriter out); 506 507 protected void writeXML(XMLWriter out, String elementName) { 508 out.startElement(elementName); 509 out.attr("id", id); 510 out.attr("name", name); 511 writeAdditionalAttributes(out); 512 out.cdata(getExpression()); 513 out.endElement(); 514 } 515 516 521 protected void writeAdditionalAttributes(XMLWriter out) {} 522 523 } 524 | Popular Tags |