1 28 package net.sf.jasperreports.engine.util; 29 30 import java.io.ByteArrayInputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.IOException ; 33 import java.io.NotSerializableException ; 34 import java.io.ObjectInputStream ; 35 import java.io.ObjectOutputStream ; 36 import java.util.HashMap ; 37 import java.util.Map ; 38 39 import org.w3c.tools.codec.Base64Decoder; 40 import org.w3c.tools.codec.Base64Encoder; 41 import org.w3c.tools.codec.Base64FormatException; 42 43 import net.sf.jasperreports.engine.JRRuntimeException; 44 45 46 75 public class JRValueStringUtils 76 { 77 78 protected static interface ValueSerializer 79 { 80 String serialize(Object value); 81 82 Object deserialize(String data); 83 } 84 85 private static final Map serializers; 86 private static final ValueSerializer defaultSerializer; 87 88 static 89 { 90 serializers = getSerializers(); 91 defaultSerializer = new DefaultSerializer(); 92 } 93 94 95 102 public static String serialize(String valueClass, Object value) 103 { 104 String data; 105 if (value == null) 106 { 107 data = null; 108 } 109 else 110 { 111 ValueSerializer serializer = getSerializer(valueClass); 112 data = serializer.serialize(value); 113 } 114 return data; 115 } 116 117 118 125 public static Object deserialize(String valueClass, String data) 126 { 127 Object value; 128 if (data == null) 129 { 130 value = null; 131 } 132 else 133 { 134 ValueSerializer serializer = getSerializer(valueClass); 135 value = serializer.deserialize(data); 136 } 137 return value; 138 } 139 140 protected static ValueSerializer getSerializer(String valueClass) 141 { 142 ValueSerializer serializer = (ValueSerializer) serializers.get(valueClass); 143 if (serializer == null) 144 { 145 serializer = defaultSerializer; 146 } 147 return serializer; 148 } 149 150 151 private static Map getSerializers() 152 { 153 Map map = new HashMap (); 154 map.put(java.lang.String .class.getName(), new StringSerializer()); 155 map.put(java.lang.Character .class.getName(), new CharacterSerializer()); 156 map.put(java.lang.Boolean .class.getName(), new BooleanSerializer()); 157 map.put(java.lang.Byte .class.getName(), new ByteSerializer()); 158 map.put(java.lang.Short .class.getName(), new ShortSerializer()); 159 map.put(java.lang.Integer .class.getName(), new IntegerSerializer()); 160 map.put(java.lang.Long .class.getName(), new LongSerializer()); 161 map.put(java.lang.Float .class.getName(), new FloatSerializer()); 162 map.put(java.lang.Double .class.getName(), new DoubleSerializer()); 163 map.put(java.math.BigInteger .class.getName(), new BigIntegerSerializer()); 164 map.put(java.math.BigDecimal .class.getName(), new BigDecimalSerializer()); 165 map.put(java.util.Date .class.getName(), new DateSerializer()); 166 map.put(java.sql.Timestamp .class.getName(), new TimestampSerializer()); 167 map.put(java.sql.Time .class.getName(), new TimeSerializer()); 168 return map; 169 } 170 171 172 protected static class StringSerializer implements ValueSerializer 173 { 174 public Object deserialize(String data) 175 { 176 return data; 177 } 178 179 public String serialize(Object value) 180 { 181 return (String ) value; 182 } 183 } 184 185 186 protected static class CharacterSerializer implements ValueSerializer 187 { 188 public Object deserialize(String data) 189 { 190 if (data.length() != 1) 191 { 192 throw new JRRuntimeException("Character data \"" + data + "\" should be exactly one character long"); 193 } 194 return new Character (data.charAt(0)); 195 } 196 197 public String serialize(Object value) 198 { 199 return String.valueOf(new char[]{((Character ) value).charValue()}); 200 } 201 } 202 203 204 protected static class BooleanSerializer implements ValueSerializer 205 { 206 public Object deserialize(String data) 207 { 208 if (data.equals("true")) 209 { 210 return Boolean.TRUE; 211 } 212 if (data.equals("false")) 213 { 214 return Boolean.FALSE; 215 } 216 throw new JRRuntimeException("Unkown boolean data \"" + data + "\""); 217 } 218 219 public String serialize(Object value) 220 { 221 return ((Boolean ) value).booleanValue() ? "true" : "false"; 222 } 223 } 224 225 226 protected static class ByteSerializer implements ValueSerializer 227 { 228 public Object deserialize(String data) 229 { 230 try 231 { 232 return Byte.valueOf(data); 233 } 234 catch (NumberFormatException e) 235 { 236 throw new JRRuntimeException("Error parsing Byte data \"" + data + "\"", e); 237 } 238 } 239 240 public String serialize(Object value) 241 { 242 return ((Byte ) value).toString(); 243 } 244 } 245 246 247 protected static class ShortSerializer implements ValueSerializer 248 { 249 public Object deserialize(String data) 250 { 251 try 252 { 253 return Short.valueOf(data); 254 } 255 catch (NumberFormatException e) 256 { 257 throw new JRRuntimeException("Error parsing Short data \"" + data + "\"", e); 258 } 259 } 260 261 public String serialize(Object value) 262 { 263 return ((Short ) value).toString(); 264 } 265 } 266 267 268 protected static class IntegerSerializer implements ValueSerializer 269 { 270 public Object deserialize(String data) 271 { 272 try 273 { 274 return Integer.valueOf(data); 275 } 276 catch (NumberFormatException e) 277 { 278 throw new JRRuntimeException("Error parsing Integer data \"" + data + "\"", e); 279 } 280 } 281 282 public String serialize(Object value) 283 { 284 return ((Integer ) value).toString(); 285 } 286 } 287 288 289 protected static class LongSerializer implements ValueSerializer 290 { 291 public Object deserialize(String data) 292 { 293 try 294 { 295 return Long.valueOf(data); 296 } 297 catch (NumberFormatException e) 298 { 299 throw new JRRuntimeException("Error parsing Long data \"" + data + "\"", e); 300 } 301 } 302 303 public String serialize(Object value) 304 { 305 return ((Long ) value).toString(); 306 } 307 } 308 309 310 protected static class FloatSerializer implements ValueSerializer 311 { 312 public Object deserialize(String data) 313 { 314 try 315 { 316 return Float.valueOf(data); 317 } 318 catch (NumberFormatException e) 319 { 320 throw new JRRuntimeException("Error parsing Float data \"" + data + "\"", e); 321 } 322 } 323 324 public String serialize(Object value) 325 { 326 return ((Float ) value).toString(); 327 } 328 } 329 330 331 protected static class DoubleSerializer implements ValueSerializer 332 { 333 public Object deserialize(String data) 334 { 335 try 336 { 337 return Double.valueOf(data); 338 } 339 catch (NumberFormatException e) 340 { 341 throw new JRRuntimeException("Error parsing Double data \"" + data + "\"", e); 342 } 343 } 344 345 public String serialize(Object value) 346 { 347 return ((Double ) value).toString(); 348 } 349 } 350 351 352 protected static class BigIntegerSerializer implements ValueSerializer 353 { 354 public Object deserialize(String data) 355 { 356 try 357 { 358 return new java.math.BigInteger (data); 359 } 360 catch (NumberFormatException e) 361 { 362 throw new JRRuntimeException("Error parsing BigInteger data \"" + data + "\"", e); 363 } 364 } 365 366 public String serialize(Object value) 367 { 368 return ((java.math.BigInteger ) value).toString(); 369 } 370 } 371 372 373 protected static class BigDecimalSerializer implements ValueSerializer 374 { 375 public Object deserialize(String data) 376 { 377 try 378 { 379 return new java.math.BigDecimal (data); 380 } 381 catch (NumberFormatException e) 382 { 383 throw new JRRuntimeException("Error parsing BigDecimal data \"" + data + "\"", e); 384 } 385 } 386 387 public String serialize(Object value) 388 { 389 return ((java.math.BigDecimal ) value).toString(); 390 } 391 } 392 393 394 protected static class DateSerializer implements ValueSerializer 395 { 396 public Object deserialize(String data) 397 { 398 try 399 { 400 long time = Long.parseLong(data); 401 return new java.util.Date (time); 402 } 403 catch (NumberFormatException e) 404 { 405 throw new JRRuntimeException("Error parsing Date data \"" + data + "\"", e); 406 } 407 } 408 409 public String serialize(Object value) 410 { 411 return Long.toString(((java.util.Date ) value).getTime()); 412 } 413 } 414 415 416 protected static class TimestampSerializer implements ValueSerializer 417 { 418 public Object deserialize(String data) 419 { 420 try 421 { 422 return java.sql.Timestamp.valueOf(data); 423 } 424 catch (IllegalArgumentException e) 425 { 426 throw new JRRuntimeException("Error parsing Timestamp data \"" + data + "\"", e); 427 } 428 } 429 430 public String serialize(Object value) 431 { 432 java.sql.Timestamp timestamp = (java.sql.Timestamp ) value; 433 return timestamp.toString(); 434 } 435 } 436 437 438 protected static class TimeSerializer implements ValueSerializer 439 { 440 public Object deserialize(String data) 441 { 442 try 443 { 444 return java.sql.Time.valueOf(data); 445 } 446 catch (IllegalArgumentException e) 447 { 448 throw new JRRuntimeException("Error parsing Time data \"" + data + "\"", e); 449 } 450 } 451 452 public String serialize(Object value) 453 { 454 java.sql.Time timestamp = (java.sql.Time ) value; 455 return timestamp.toString(); 456 } 457 } 458 459 460 protected static class DefaultSerializer implements ValueSerializer 461 { 462 public Object deserialize(String data) 463 { 464 try 465 { 466 ByteArrayInputStream dataIn = new ByteArrayInputStream (data.getBytes()); 467 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream (); 468 Base64Decoder dec = new Base64Decoder(dataIn, bytesOut); 469 dec.process(); 470 471 ByteArrayInputStream bytesIn = new ByteArrayInputStream (bytesOut.toByteArray()); 472 ObjectInputStream objectIn = new ObjectInputStream (bytesIn); 473 return objectIn.readObject(); 474 } 475 catch (IOException e) 476 { 477 throw new JRRuntimeException(e); 478 } 479 catch (ClassNotFoundException e) 480 { 481 throw new JRRuntimeException(e); 482 } 483 catch (Base64FormatException e) 484 { 485 throw new JRRuntimeException(e); 486 } 487 } 488 489 public String serialize(Object value) 490 { 491 try 492 { 493 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream (); 494 ObjectOutputStream objectOut = new ObjectOutputStream (bytesOut); 495 objectOut.writeObject(value); 496 objectOut.close(); 497 498 ByteArrayInputStream bytesIn = new ByteArrayInputStream (bytesOut.toByteArray()); 499 ByteArrayOutputStream dataOut = new ByteArrayOutputStream (); 500 501 Base64Encoder enc = new Base64Encoder(bytesIn, dataOut); 502 enc.process(); 503 504 return new String (dataOut.toByteArray(), "UTF-8"); 505 } 506 catch (NotSerializableException e) 507 { 508 throw new JRRuntimeException("Value is not serializable", e); 509 } 510 catch (IOException e) 511 { 512 throw new JRRuntimeException(e); 513 } 514 } 515 } 516 } 517 | Popular Tags |