View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
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 org.supercsv.cellprocessor.joda;
17  
18  import org.joda.time.DateTimeZone;
19  import org.supercsv.cellprocessor.CellProcessorAdaptor;
20  import org.supercsv.cellprocessor.ift.CellProcessor;
21  import org.supercsv.exception.SuperCsvCellProcessorException;
22  import org.supercsv.util.CsvContext;
23  
24  /**
25   * Converts a String (the ID of the timezone, e.g. 'Australia/Brisbane') to a
26   * Joda DateTimeZone.
27   * 
28   * @since 2.3.0
29   * @author James Bassett
30   */
31  public class ParseDateTimeZone extends CellProcessorAdaptor {
32  
33  	/**
34  	 * Constructs a new <tt>ParseDateTimeZone</tt> processor, which parses a
35  	 * String as a Joda DateTimeZone.
36  	 */
37  	public ParseDateTimeZone() {
38  	}
39  
40  	/**
41  	 * Constructs a new <tt>ParseDateTimeZone</tt> processor, which parses a
42  	 * String as a Joda DateTimeZone, then calls the next processor in the
43  	 * chain.
44  	 * 
45  	 * @param next
46  	 *            the next processor in the chain
47  	 */
48  	public ParseDateTimeZone(final CellProcessor next) {
49  		super(next);
50  	}
51  
52  	/**
53  	 * {@inheritDoc}
54  	 * 
55  	 * @throws SuperCsvCellProcessorException
56  	 *             if value is null or is not a String
57  	 */
58  	public Object execute(final Object value, final CsvContext context) {
59  		validateInputNotNull(value, context);
60  		if (!(value instanceof String)) {
61  			throw new SuperCsvCellProcessorException(String.class, value,
62  					context, this);
63  		}
64  		final DateTimeZone result;
65  		try {
66  			result = DateTimeZone.forID((String) value);
67  		} catch (IllegalArgumentException e) {
68  			throw new SuperCsvCellProcessorException(
69  					"Failed to parse value as a DateTimeZone", context, this, e);
70  		}
71  		return next.execute(result, context);
72  
73  	}
74  
75  }