1 24 package org.riotfamily.website.generic.model.hibernate; 25 26 import javax.servlet.http.HttpServletRequest ; 27 28 31 public class StringToPrimitiveResolver extends DefaultParameterResolver { 32 33 private static final int TYPE_INTEGER = 1; 34 35 private static final int TYPE_LONG = 2; 36 37 private static final int TYPE_SHORT = 3; 38 39 private static final int TYPE_DOUBLE = 4; 40 41 private static final int TYPE_FLOAT = 5; 42 43 private static final int TYPE_BOOLEAN = 6; 44 45 private static final int TYPE_CHARACTER = 7; 46 47 private int type; 48 49 50 public void setType(String type) { 51 if (type.equalsIgnoreCase("Integer")) { 52 this.type = TYPE_INTEGER; 53 } 54 else if (type.equalsIgnoreCase("Long")) { 55 this.type = TYPE_LONG; 56 } 57 else if (type.equalsIgnoreCase("Short")) { 58 this.type = TYPE_SHORT; 59 } 60 else if (type.equalsIgnoreCase("Double")) { 61 this.type = TYPE_DOUBLE; 62 } 63 else if (type.equalsIgnoreCase("Float")) { 64 this.type = TYPE_FLOAT; 65 } 66 else if (type.equalsIgnoreCase("Boolean")) { 67 this.type = TYPE_BOOLEAN; 68 } 69 else if (type.equalsIgnoreCase("Character")) { 70 this.type = TYPE_CHARACTER; 71 } 72 else { 73 throw new IllegalArgumentException ("type must be Integer, " + 74 "Long, Short, Double, Float, Boolean or Character"); 75 } 76 } 77 78 79 public Object getValueInternal(HttpServletRequest request) { 80 String s = (String ) super.getValueInternal(request); 81 if (s == null) { 82 return null; 83 } 84 switch (type) { 85 case TYPE_INTEGER: 86 return Integer.valueOf(s); 87 88 case TYPE_LONG: 89 return Long.valueOf(s); 90 91 case TYPE_SHORT: 92 return Short.valueOf(s); 93 94 case TYPE_DOUBLE: 95 return Double.valueOf(s); 96 97 case TYPE_FLOAT: 98 return Float.valueOf(s); 99 100 case TYPE_BOOLEAN: 101 return Boolean.valueOf(s); 102 103 case TYPE_CHARACTER: 104 return new Character (s.charAt(0)); 105 106 default: 107 return s; 108 } 109 } 110 111 } 112 | Popular Tags |