/**
* Copyright (c) 2011-2012, James Zhan 詹波 (jfinal@126.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jfinal.core;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* Convert String to other type object.
*/
final class TypeConverter {
private static final int timeStampLen = "2011-01-18 16:18:18".length();
private static final String timeStampPattern = "yyyy-MM-dd HH:mm:ss";
private static final String datePattern = "yyyy-MM-dd";
/**
* test for all types of mysql
*
* 表单提交测试结果:
* 1: 表单中的域,就算不输入任何内容,也会传过来 "", 也即永远不可能为 null.
* 2: 如果输入空格也会提交上来
* 3: 需要考 model中的 string属性,在传过来 "" 时是该转成 null还是不该转换,
* 我想, 因为用户没有输入那么肯定是 null, 而不该是 ""
*
* 注意: 当clazz参数不为String.class, 且参数s为空串blank的情况,
* 此情况下转换结果为 null, 而不应该抛出异常
*/
public static final Object convert(Class<?> clazz, String s) throws ParseException {
// mysql type: varchar, char, enum, set, text, tinytext, mediumtext, longtext
if (clazz == String.class) {
return ("".equals(s) ? null : s); // 用户在表单域中没有输入内容时将提交过来 "", 因为没有输入,所以要转成 null.
}
s = s.trim();
if ("".equals(s)) { // 前面的 String跳过以后,所有的空字符串全都转成 null, 这是合理的
return null;
}
// 以上两种情况无需转换,直接返回, 注意, 本方法不接受null为 s 参数(经测试永远不可能传来null, 因为无输入传来的也是"")
Object result = null;
// mysql type: int, integer, tinyint, smallint, mediumint
if (clazz == Integer.class || clazz == int.class) {
result = Integer.parseInt(s);
}
// mysql type: bigint
else if (clazz == Long.class || clazz == long.class) {
result = Long.parseLong(s);
}
// 经测试java.util.Data类型不会返回, java.sql.Date, java.sql.Time,java.sql.Timestamp 全部直接继承自 java.util.Data, 所以 getDate可以返回这三类数据
else if (clazz == java.util.Date.class) {
if (s.length() >= timeStampLen) {
// Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
// result = new java.util.Date(java.sql.Timestamp.valueOf(s).getTime()); // error under jdk 64bit(maybe)
result = new SimpleDateFormat(timeStampPattern).parse(s);
}
else {
// result = new java.util.Date(java.sql.Date.valueOf(s).getTime()); // error under jdk 64bit
result = new SimpleDateFormat(datePattern).parse(s);
}
}
// mysql type: date, year
else if (clazz == java.sql.Date.class) {
if (s.length() >= timeStampLen) {
// result = new java.sql.Date(java.sql.Timestamp.valueOf(s).getTime()); // error under jdk 64bit(maybe)
result = new java.sql.Date(new SimpleDateFormat(timeStampPattern).parse(s).getTime());
}
else {
// result = new java.sql.Date(java.sql.Date.valueOf(s).getTime()); // error under jdk 64bit
result = new java.sql.Date(new SimpleDateFormat(datePattern).parse(s).getTime());
}
}
// mysql type: time
else if (clazz == java.sql.Time.class) {
result = java.sql.Time.valueOf(s);
}
// mysql type: timestamp, datetime
else if (clazz == java.sql.Timestamp.class) {
result = java.sql.Timestamp.valueOf(s);
}
// mysql type: real, double
else if (clazz == Double.class) {
result = Double.parseDouble(s);
}
// mysql type: float
else if (clazz == Float.class) {
result = Float.parseFloat(s);
}
// mysql type: bit
else if (clazz == Boolean.class) {
result = Boolean.parseBoolean(s);
}
// mysql type: decimal, numeric
else if (clazz == java.math.BigDecimal.class) {
result = new java.math.BigDecimal(s);
}
// mysql type: binary, varbinary, tinyblob, blob, mediumblob, longblob. I have not finished the test.
else if (clazz == byte[].class) {
result = s.getBytes();
}
else {
if (Config.getConstants().getDevMode())
throw new RuntimeException("Please add code in " + TypeConverter.class + ". The type can't be converted: " + clazz.getName());
else
throw new RuntimeException(clazz.getName() + " can not be converted, please use other type of attributes in your model!");
}
return result;
}
}