View Javadoc

1   /*
2    * Copyright 2004-2005, 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.transform.converters;
17  
18  import java.text.DateFormat;
19  import java.util.Date;
20  import java.util.Locale;
21  
22  import net.sf.composite.util.ObjectUtils;
23  import net.sf.morph.Defaults;
24  import net.sf.morph.transform.Converter;
25  import net.sf.morph.transform.DecoratedConverter;
26  import net.sf.morph.transform.transformers.BaseTransformer;
27  
28  public class TextToTimeConverter extends BaseTransformer implements DecoratedConverter {
29  
30  	private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
31  
32  	private DateFormat dateFormat;
33  	private Converter timeConverter;
34  	private Converter textConverter;
35  
36  	/**
37  	 * {@inheritDoc}
38  	 */
39  	protected Object convertImpl(Class destinationClass, Object source,
40  			Locale locale) throws Exception {
41  
42  		String text = (String) getTextConverter().convert(String.class, source, locale);
43  
44  		if (ObjectUtils.isEmpty(text)) {
45  			return null;
46  		}
47  		Date date = getDateFormat().parse(text);
48  		return getTimeConverter().convert(destinationClass, date, locale);
49  	}
50  
51  	/**
52  	 * {@inheritDoc}
53  	 */
54  	protected Class[] getSourceClassesImpl() throws Exception {
55  		return getTextConverter().getDestinationClasses();
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	protected Class[] getDestinationClassesImpl() throws Exception {
62  		return getTimeConverter().getSourceClasses();
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	protected boolean isWrappingRuntimeExceptions() {
69  	    return true;
70      }
71  
72  	/**
73  	 * Get the DateFormat used by this TextToTimeConverter.
74  	 * @return DateFormat
75  	 */
76  	public DateFormat getDateFormat() {
77  		if (dateFormat == null) {
78  			setDateFormat(DEFAULT_DATE_FORMAT);
79  		}
80  		return dateFormat;
81  	}
82  
83  	/**
84  	 * Set the DateFormat to be used by this TextToTimeConverter.
85  	 * @param dateFormat
86  	 */
87  	public void setDateFormat(DateFormat dateFormat) {
88  		this.dateFormat = dateFormat;
89  	}
90  
91  	/**
92  	 * Get the text converter used by this TextToTimeConverter.
93  	 * @return Converter
94  	 */
95  	public Converter getTextConverter() {
96  		if (textConverter == null) {
97  			setTextConverter(Defaults.createTextConverter());
98  		}
99  		return textConverter;
100 	}
101 
102 	/**
103 	 * Set the text converter used by this TextToTimeConverter.
104 	 * @param textConverter
105 	 */
106 	public void setTextConverter(Converter textConverter) {
107 		this.textConverter = textConverter;
108 	}
109 
110 	/**
111 	 * Get the time converter used by this TextToTimeConverter.
112 	 * @return Converter
113 	 */
114 	public Converter getTimeConverter() {
115 		if (timeConverter == null) {
116 			setTimeConverter(Defaults.createTimeConverter());
117 		}
118 		return timeConverter;
119 	}
120 
121 	/**
122 	 * Set the time converter used by this TextToTimeConverter.
123 	 * @param timeConverter
124 	 */
125 	public void setTimeConverter(Converter timeConverter) {
126 		this.timeConverter = timeConverter;
127 	}
128 
129 }