KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > util > DateFormatContainer


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.util;
17
18 import java.text.DateFormat JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import net.sf.dozer.util.mapping.fieldmap.ClassMap;
23 import net.sf.dozer.util.mapping.fieldmap.FieldMap;
24
25
26 /**
27  * @author tierney.matt
28  */

29 public class DateFormatContainer {
30   private final ClassMap classMap;
31   private final FieldMap fieldMap;
32   private DateFormat JavaDoc dateFormat;
33   
34   public DateFormatContainer(ClassMap classMap, FieldMap fieldMap) {
35     this.classMap = classMap;
36     this.fieldMap = fieldMap;
37   }
38
39   public DateFormat JavaDoc getDateFormat() {
40     if (dateFormat == null) {
41       dateFormat = determineDateFormat();
42     }
43     return dateFormat;
44   }
45   
46   public void setDateFormat(DateFormat JavaDoc dateFormat) {
47     this.dateFormat = dateFormat;
48   }
49
50   private DateFormat JavaDoc determineDateFormat() {
51     if (classMap == null || fieldMap == null) {
52       return null;
53     }
54     // try to use field mapping date format
55
String JavaDoc dfStr = fieldMap.getSourceField().getDateFormat() == null ? fieldMap.getDestField().getDateFormat()
56         : fieldMap.getSourceField().getDateFormat();
57
58     // if field level date format is not specified, try using class mapping
59
// default date format
60
MappingUtils mappingUtils = new MappingUtils();
61     if (mappingUtils.isBlankOrNull(dfStr)) {
62       dfStr = classMap.getDateFormat();
63     }
64
65     return dfStr == null ? null : new SimpleDateFormat JavaDoc(dfStr, Locale.getDefault());
66   }
67 }
68
Popular Tags