KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > generic > model > hibernate > StringToPrimitiveResolver


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.website.generic.model.hibernate;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 /**
29  * ParameterResolver that converts a String value to primitive wrapper object.
30  */

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 JavaDoc 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 JavaDoc("type must be Integer, " +
74                     "Long, Short, Double, Float, Boolean or Character");
75         }
76     }
77
78
79     public Object JavaDoc getValueInternal(HttpServletRequest JavaDoc request) {
80         String JavaDoc s = (String JavaDoc) 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 JavaDoc(s.charAt(0));
105             
106         default:
107             return s;
108         }
109     }
110     
111 }
112
Popular Tags