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.example;
17  
18  import org.supercsv.cellprocessor.CellProcessorAdaptor;
19  import org.supercsv.cellprocessor.ift.CellProcessor;
20  import org.supercsv.exception.SuperCsvCellProcessorException;
21  import org.supercsv.util.CsvContext;
22  
23  /**
24   * An example of a custom cell processor.
25   */
26  public class ParseDay extends CellProcessorAdaptor {
27  	
28  	public ParseDay() {
29  		super();
30  	}
31  	
32  	public ParseDay(CellProcessor next) {
33  		// this constructor allows other processors to be chained after ParseDay
34  		super(next);
35  	}
36  	
37  	public Object execute(Object value, CsvContext context) {
38  		
39  		validateInputNotNull(value, context); // throws an Exception if the input is null
40  		
41  		for( Day day : Day.values() ) {
42  			if( day.name().equalsIgnoreCase(value.toString()) ) {
43  				// passes the Day enum to the next processor in the chain
44  				return next.execute(day, context);
45  			}
46  		}
47  		
48  		throw new SuperCsvCellProcessorException(String.format("Could not parse '%s' as a day", value), context, this);
49  	}
50  }