?? types.java
字號:
}
} catch ( ParseException except ) {
throw new IllegalArgumentException( except.toString() );
}
}
public String toString() { return "String->Date"; }
} ),
new TypeConvertorInfo( java.lang.Integer.class, java.util.Date.class, new TypeConvertor() {
public Object convert( Object obj, String param ) {
try {
_paramDateFormat.applyPattern( Types.getFullDatePattern( param ) );
return _paramDateFormat.parse( obj.toString() );
} catch ( ParseException except ) {
throw new IllegalArgumentException( except.toString() );
}
}
public String toString() { return "Integer->Date"; }
} ),
new TypeConvertorInfo( java.math.BigDecimal.class, java.util.Date.class, new TypeConvertor() {
public Object convert( Object obj, String param ) {
try {
_paramDateFormat.applyPattern( Types.getFullDatePattern( param ) );
return _paramDateFormat.parse( obj.toString() );
} catch ( ParseException except ) {
throw new IllegalArgumentException( except.toString() );
}
}
public String toString() { return "BigDecimal->Date"; }
} ),
new TypeConvertorInfo( java.util.Date.class, java.sql.Date.class, new TypeConvertor() {
public Object convert( Object obj, String param ) {
return new java.sql.Date( ( (java.util.Date) obj ).getTime() );
}
public String toString() { return "util.Date->sql.Date"; }
} ),
new TypeConvertorInfo( java.sql.Date.class, java.util.Date.class, new TypeConvertor() {
public Object convert( Object obj, String param ) {
return obj;
}
public String toString() { return "sql.Date->util.Date"; }
} ),
new TypeConvertorInfo( java.util.Date.class, java.sql.Time.class, new TypeConvertor() {
public Object convert( Object obj, String param ) {
return new java.sql.Time( ( (java.util.Date) obj ).getTime() );
}
public String toString() { return "util.Date->sql.Time"; }
} ),
new TypeConvertorInfo( java.sql.Time.class, java.util.Date.class, new TypeConvertor() {
public Object convert( Object obj, String param ) {
return obj;
}
public String toString() { return "sql.Time->util.Date"; }
} ),
new TypeConvertorInfo( java.util.Date.class, java.sql.Timestamp.class, new TypeConvertor() {
public Object convert( Object obj, String param ) {
return new java.sql.Timestamp( ( (java.util.Date) obj ).getTime() );
}
public String toString() { return "util.Date->sql.Timestamp"; }
} ),
new TypeConvertorInfo( java.sql.Timestamp.class, java.util.Date.class, new TypeConvertor() {
public Object convert( Object obj, String param ) {
return obj;
}
public String toString() { return "sql.Timestamp->util.Date"; }
} )
};
/**
* Information about a specific Java type.
*/
static class TypeInfo{
/**
* The short type name (e.g. <tt>integer</tt>).
*/
final String shortName;
/**
* The default value for the type, if known.
*/
final Object defValue;
TypeInfo( String shortName, Object defValue ){
this.shortName = shortName;
this.defValue = defValue;
}
}
/**
* List of all the simple types supported by Castor.
*/
static TypeInfo[] _typeInfos = new TypeInfo[] {
// shortName defValue
new TypeInfo( "string", null ),
new TypeInfo( "integer", new Integer( 0 ) ),
new TypeInfo( "long", new Long( 0 ) ),
new TypeInfo( "boolean", Boolean.FALSE ),
new TypeInfo( "double", new Double( 0 ) ),
new TypeInfo( "float", new Float( 0 ) ),
new TypeInfo( "bigdecimal", new BigDecimal( 0 ) ),
new TypeInfo( "byte", new Byte( (byte) 0 ) ),
new TypeInfo( "date", new java.util.Date() ),
new TypeInfo( "timestamp", null ),
new TypeInfo( "time", null ),
new TypeInfo( "short", new Short( (short) 0 ) ),
new TypeInfo( "char", new Character( (char) 0 ) ),
/*
new TypeInfo( "locale", null,
java.util.Locale.class, true, null ),
new TypeInfo( Stream, "stream", java.io.InputStream.class, null ),
new TypeInfo( Reader, "reader", java.io.Reader.class, null ),
new TypeInfo( XML, "xml", org.w3c.dom.Document.class, org.w3c.dom.Element.class ),
new TypeInfo( Serialized, "ser", java.io.Serializable.class, null )
*/
};
/**
* Information used to locate a type convertor.
*/
static class TypeConvertorInfo{
/**
* The type being converted to.
*/
final Class toType;
/**
* The type being converted from.
*/
final Class fromType;
/**
* The convertor.
*/
final TypeConvertor convertor;
TypeConvertorInfo( Class fromType, Class toType, TypeConvertor convertor ){
this.fromType = fromType;
this.toType = toType;
this.convertor = convertor;
}
}
/**
* Date format used by the date convertor when nonempy parameter
* is specified.
*/
private static SimpleDateFormat _paramDateFormat = new SimpleDateFormat();
/**
* Date format used by the date convertor.
*/
private static DateFormat _dateFormat = new SimpleDateFormat();
/**
* Returns the default value for this Java type (e.g. 0 for integer, empty
* string) or null if no default value is known. The default value only
* applies to primitive types (that is, <tt>Integer.TYPE</tt> but not
* <tt>java.lang.Integer</tt>).
*
* @param shortName name
* @return The default value or null
*/
public static Object getDefault(String shortName){
for ( int i = 0 ; i < _typeInfos.length ; ++i ) {
if ( _typeInfos[ i ].shortName == shortName )
return _typeInfos[ i ].defValue;
}
return null;
}
/**
* Returns a type convertor. A type convertor can be used to convert
* an object from Java type <tt>fromType</tt> to Java type <tt>toType</tt>.
*
* @param fromType The Java type to convert from
* @param toType The Java type to convert to
* @throws MappingException No suitable convertor was found
*/
public static TypeConvertor getConvertor( Class fromType, Class toType )
throws MappingException{
// first seek for exact match
// TODO: the closest possible match
for ( int i = 0 ; i < _typeConvertors.length ; ++i ) {
if ( _typeConvertors[ i ].fromType.equals( fromType ) &&
toType.equals( _typeConvertors[ i ].toType ) )
return _typeConvertors[ i ].convertor;
}
// else seek for any match
for ( int i = 0 ; i < _typeConvertors.length ; ++i ) {
if ( _typeConvertors[ i ].fromType.isAssignableFrom( fromType ) &&
toType.isAssignableFrom( _typeConvertors[ i ].toType ) )
return _typeConvertors[ i ].convertor;
}
throw new MappingException( "mapping.noConvertor", fromType.getName(), toType.getName() );
}
/**
* Transforms short date format pattern into full format pattern
* for SimpleDateFormat (e.g., "YMD" to "yyyyMMdd").
*
* @param pattern The short pattern
* @return The full pattern
*/
public static String getFullDatePattern( String pattern ){
StringBuffer sb;
int len;
if ( pattern == null || pattern.length() == 0 )
return "yyyyMMdd";
sb = new StringBuffer();
len = pattern.length();
for ( int i = 0; i < len; i++ ) {
switch ( pattern.charAt( i ) ) {
case 'y': case 'Y': sb.append( "yyyy" ); break;
case 'M': sb.append( "MM" ); break;
case 'd': case 'D': sb.append( "dd" ); break;
case 'h': case 'H': sb.append( "HH" ); break;
case 'm': sb.append( "mm" ); break;
case 's': sb.append( "ss" ); break;
case 'S': sb.append( "SSS" ); break;
}
}
return sb.toString();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -