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;
17  
18  import org.supercsv.cellprocessor.ift.BoolCellProcessor;
19  import org.supercsv.cellprocessor.ift.DateCellProcessor;
20  import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
21  import org.supercsv.cellprocessor.ift.LongCellProcessor;
22  import org.supercsv.cellprocessor.ift.StringCellProcessor;
23  import org.supercsv.exception.SuperCsvCellProcessorException;
24  import org.supercsv.util.CsvContext;
25  
26  /**
27   * Ensure that Strings or String-representations of objects are trimmed (contain no surrounding whitespace).
28   * <p>
29   * Prior to 2.0.0, this processor truncated Strings - this functionality can now be found in the {@link Truncate}
30   * processor.
31   * 
32   * @author James Bassett
33   */
34  public class Trim extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor, DoubleCellProcessor,
35  	LongCellProcessor, StringCellProcessor {
36  	
37  	/**
38  	 * Constructs a new <tt>Trim</tt> processor, which trims a String to ensure it has no surrounding whitespace.
39  	 */
40  	public Trim() {
41  		super();
42  	}
43  	
44  	/**
45  	 * Constructs a new <tt>Trim</tt> processor, which trims a String to ensure it has no surrounding whitespace then
46  	 * calls the next processor in the chain.
47  	 * 
48  	 * @param next
49  	 *            the next processor in the chain
50  	 * @throws NullPointerException
51  	 *             if next is null
52  	 */
53  	public Trim(final StringCellProcessor next) {
54  		super(next);
55  	}
56  	
57  	/**
58  	 * {@inheritDoc}
59  	 * 
60  	 * @throws SuperCsvCellProcessorException
61  	 *             if value is null
62  	 */
63  	public Object execute(final Object value, final CsvContext context) {
64  		validateInputNotNull(value, context);
65  		
66  		final String result = value.toString().trim();
67  		return next.execute(result, context);
68  	}
69  }