KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > converters > StringConverter


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping.converters;
17
18 import org.apache.commons.beanutils.Converter;
19
20 import net.sf.dozer.util.mapping.util.DateFormatContainer;
21
22
23 /**
24  * @author tierney.matt
25  */

26 public class StringConverter implements Converter {
27   private DateFormatContainer dateFormat;
28
29   public StringConverter(DateFormatContainer dateFormat) {
30     this.dateFormat = dateFormat;
31   }
32
33   public Object JavaDoc convert(Class JavaDoc destClass, Object JavaDoc sourceObj) {
34     String JavaDoc result = null;
35
36     Class JavaDoc sourceClass = sourceObj.getClass();
37     if (dateFormat != null && java.util.Date JavaDoc.class.isAssignableFrom(sourceClass)) {
38       result = dateFormat.getDateFormat().format((java.util.Date JavaDoc) sourceObj);
39     } else if (dateFormat != null && java.util.Calendar JavaDoc.class.isAssignableFrom(sourceClass)) {
40       result = dateFormat.getDateFormat().format(((java.util.Calendar JavaDoc)sourceObj).getTime());
41     } else {
42       result = sourceObj.toString();
43     }
44
45     return result;
46   }
47
48   public DateFormatContainer getDateFormat() {
49     return dateFormat;
50   }
51
52   public void setDateFormat(DateFormatContainer dateFormat) {
53     this.dateFormat = dateFormat;
54   }
55 }
56
Popular Tags