1 17 package org.eclipse.emf.codegen.ecore.genmodel.impl; 18 19 20 import java.math.BigDecimal ; 21 import java.math.BigInteger ; 22 import java.util.Date ; 23 24 import org.eclipse.emf.codegen.ecore.genmodel.GenModel; 25 26 27 31 public class Literals 32 { 33 private Literals() {} 35 36 46 public static String toLiteral(Object o) 47 { 48 return toLiteral(o, null); 49 } 50 51 62 public static String toLiteral(Object o, GenModel genModel) 63 { 64 if (o instanceof Boolean ) 65 { 66 return toBooleanLiteral(((Boolean )o).booleanValue(), genModel); 67 } 68 if (o instanceof Byte ) 69 { 70 return toByteLiteral(((Byte )o).byteValue(), genModel); 71 } 72 if (o instanceof Short ) 73 { 74 return toShortLiteral(((Short )o).shortValue(), genModel); 75 } 76 if (o instanceof Integer ) 77 { 78 return toIntLiteral(((Integer )o).intValue(), genModel); 79 } 80 if (o instanceof Long ) 81 { 82 return toLongLiteral(((Long )o).longValue(), genModel); 83 } 84 if (o instanceof Float ) 85 { 86 return toFloatLiteral(((Float )o).floatValue(), genModel); 87 } 88 if (o instanceof Double ) 89 { 90 return toDoubleLiteral(((Double )o).doubleValue(), genModel); 91 } 92 if (o instanceof Character ) 93 { 94 return toCharLiteral(((Character )o).charValue(), genModel); 95 } 96 if (o instanceof String ) 97 { 98 return toStringLiteral((String )o, genModel); 99 } 100 if (o instanceof BigDecimal ) 101 { 102 return toBigDecimalLiteral((BigDecimal )o, genModel); 103 } 104 if (o instanceof BigInteger ) 105 { 106 return toBigIntegerLiteral((BigInteger )o, genModel); 107 } 108 if (o instanceof Date ) 109 { 110 return toDateLiteral((Date )o, genModel); 111 } 112 if (o instanceof Class ) 113 { 114 return toClassLiteral((Class )o, genModel); 115 } 116 return null; 117 } 118 119 122 public static String toBooleanLiteral(boolean b, GenModel genModel) 123 { 124 return b ? "true" : "false"; 125 } 126 127 131 public static String toByteLiteral(byte b, GenModel genModel) 132 { 133 return Byte.toString(b); 134 } 135 136 140 public static String toShortLiteral(short s, GenModel genModel) 141 { 142 return Short.toString(s); 143 } 144 145 149 public static String toIntLiteral(int i, GenModel genModel) 150 { 151 return Integer.toString(i); 152 } 153 154 158 public static String toLongLiteral(long l, GenModel genModel) 159 { 160 return Long.toString(l) + "L"; 161 } 162 163 170 public static String toFloatLiteral(float f, GenModel genModel) 171 { 172 if (Float.isNaN(f)) return importName("java.lang.Float", genModel) + ".NaN"; 173 if (Float.isInfinite(f)) return f > 0 ? 174 importName("java.lang.Float", genModel) + ".POSITIVE_INFINITY" : 175 importName("java.lang.Float", genModel) + ".NEGATIVE_INFINITY"; 176 return Float.toString(f) + "F"; 177 } 178 179 186 public static String toDoubleLiteral(double d, GenModel genModel) 187 { 188 if (Double.isNaN(d)) return importName("java.lang.Double", genModel) + ".NaN"; 189 if (Double.isInfinite(d)) return d > 0 ? 190 importName("java.lang.Double", genModel) + ".POSITIVE_INFINITY" : 191 importName("java.lang.Double", genModel) + ".NEGATIVE_INFINITY"; 192 return Double.toString(d); 193 } 194 195 private static String importName(String name, GenModel genModel) 196 { 197 return genModel != null ? genModel.getImportedName(name) : name; 198 } 199 200 209 public static String toCharLiteral(char c, GenModel genModel) 210 { 211 StringBuffer result = new StringBuffer (8); 212 result.append('\''); 213 result.append(escapeChar(c)); 214 result.append('\''); 215 return result.toString(); 216 } 217 218 223 public static String toStringLiteral(String s, GenModel genModel) 224 { 225 if (s == null) return "null"; 226 int len = s.length(); 227 StringBuffer result = new StringBuffer (len + 16); 228 result.append('\"'); 229 for (int i = 0; i < len; i++) 230 { 231 result.append(escapeChar(s.charAt(i))); 232 } 233 result.append('\"'); 234 return result.toString(); 235 } 236 237 private static String escapeChar(char c) 238 { 239 if (c == '\b') return "\\b"; 240 if (c == '\t') return "\\t"; 241 if (c == '\n') return "\\n"; 242 if (c == '\f') return "\\f"; 243 if (c == '\r') return "\\r"; 244 if (c == '\"') return "\\\""; 245 if (c == '\'') return "\\\'"; 246 if (c == '\\') return "\\\\"; 247 if (c >= 32 && c < 127) return String.valueOf(c); 248 249 String num = Integer.toHexString(c); 251 switch(num.length()) { 252 case 1: return "\\u000" + num; 253 case 2: return "\\u00" + num; 254 case 3: return "\\u0" + num; 255 } 256 return "\\u" + num; 257 } 258 259 262 public static String toBigDecimalLiteral(BigDecimal bigDecimal, GenModel genModel) 263 { 264 if (bigDecimal == null) return "null"; 265 return "new " + importName("java.math.BigDecimal", genModel) + "(\"" + bigDecimal.toString() + "\")"; 266 } 267 268 271 public static String toBigIntegerLiteral(BigInteger bigInteger, GenModel genModel) 272 { 273 if (bigInteger == null) return "null"; 274 return "new " + importName("java.math.BigInteger", genModel) + "(\"" + bigInteger.toString() + "\")"; 275 } 276 277 280 public static String toDateLiteral(Date date, GenModel genModel) 281 { 282 String timeLiteral = toLongLiteral(date.getTime(), genModel); 283 return "new " + importName("java.util.Date", genModel) + "(" + timeLiteral + ")"; 284 } 285 286 289 public static String toClassLiteral(Class c, GenModel genModel) 290 { 291 if (c == null) return "null"; 292 String name = c.getName(); 293 294 int arrayDepth = 0; 296 for (; name.charAt(arrayDepth) == '['; arrayDepth++); 297 298 if (arrayDepth > 0) 299 { 300 if (name.charAt(arrayDepth) == 'B') name = "byte"; 301 else if (name.charAt(arrayDepth) == 'C') name = "char"; 302 else if (name.charAt(arrayDepth) == 'D') name = "double"; 303 else if (name.charAt(arrayDepth) == 'F') name = "float"; 304 else if (name.charAt(arrayDepth) == 'I') name = "int"; 305 else if (name.charAt(arrayDepth) == 'J') name = "long"; 306 else if (name.charAt(arrayDepth) == 'S') name = "short"; 307 else if (name.charAt(arrayDepth) == 'Z') name = "boolean"; 308 else if (name.charAt(arrayDepth) == 'L') name = importName(name.substring(arrayDepth + 1, name.length() - 1), genModel); 309 else throw new IllegalArgumentException ("Invalid class name: " + name); 310 } 311 else if (!c.isPrimitive()) 312 { 313 name = importName(name, genModel); 314 } 315 316 StringBuffer result = new StringBuffer (name.length() + 2 * arrayDepth + 8); 317 result.append(name); 318 for (int i = 0; i < arrayDepth; i++) 319 { 320 result.append('['); 321 result.append(']'); 322 } 323 result.append(".class"); 324 return result.toString(); 325 } 326 } 327 | Popular Tags |