1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import org.objectstyle.cayenne.exp.Expression; 59 import org.objectstyle.cayenne.exp.ExpressionException; 60 import org.objectstyle.cayenne.exp.parser.ParseException; 61 import org.objectstyle.cayenne.util.Util; 62 import org.scopemvc.util.convertor.StringConvertor; 63 64 70 public class ExpressionConvertor implements StringConvertor { 71 72 public String valueAsString(Object value) throws IllegalArgumentException { 73 if (value == null) { 74 return null; 75 } 76 77 if (!(value instanceof Expression)) { 78 throw new IllegalArgumentException ( 79 "Unsupported value class: " + value.getClass().getName()); 80 } 81 82 return value.toString(); 83 } 84 85 public Object stringAsValue(String string) throws IllegalArgumentException { 86 if (string == null || string.trim().length() == 0) { 87 return null; 88 } 89 90 try { 91 return Expression.fromString(string); 92 } 93 catch (ExpressionException eex) { 94 Throwable cause = Util.unwindException(eex); 96 String message = 97 (cause instanceof ParseException) 98 ? cause.getMessage() 99 : "Invalid expression: " + string; 100 101 throw new IllegalArgumentException (message); 102 } 103 } 104 105 public boolean supportsStringAsValue() { 106 return true; 107 } 108 } 109 | Popular Tags |