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.constraint;
17  
18  import org.supercsv.cellprocessor.CellProcessorAdaptor;
19  import org.supercsv.cellprocessor.ift.CellProcessor;
20  import org.supercsv.cellprocessor.ift.StringCellProcessor;
21  import org.supercsv.exception.SuperCsvCellProcessorException;
22  import org.supercsv.exception.SuperCsvConstraintViolationException;
23  import org.supercsv.util.CsvContext;
24  
25  /**
26   * This processor checks if the input is <tt>null</tt> or an empty string, and raises an exception in that case. In all
27   * other cases, the next processor in the chain is invoked.
28   * <p>
29   * You should only use this processor, when a column must be non-null, but you do not need to apply any other processor
30   * to the column.
31   * <p>
32   * If you apply other processors to the column, you can safely omit this processor as all other processors should do a
33   * null-check on its input.
34   * 
35   * @since 1.50
36   * @author Dominique De Vito
37   */
38  public class StrNotNullOrEmpty extends CellProcessorAdaptor implements StringCellProcessor {
39  	
40  	/**
41  	 * Constructs a new <tt>StrNotNullOrEmpty</tt> processor, which checks for null/empty Strings.
42  	 */
43  	public StrNotNullOrEmpty() {
44  		super();
45  	}
46  	
47  	/**
48  	 * Constructs a new <tt>StrNotNullOrEmpty</tt> processor, which checks for null/empty Strings, then calls the next
49  	 * processor in the chain.
50  	 * 
51  	 * @param next
52  	 *            the next processor in the chain
53  	 * @throws NullPointerException
54  	 *             if next is null
55  	 */
56  	public StrNotNullOrEmpty(final CellProcessor next) {
57  		super(next);
58  	}
59  	
60  	/**
61  	 * {@inheritDoc}
62  	 * 
63  	 * @throws SuperCsvCellProcessorException
64  	 *             if value is null or isn't a String
65  	 * @throws SuperCsvConstraintViolationException
66  	 *             if value is an empty String
67  	 */
68  	public Object execute(final Object value, final CsvContext context) {
69  		if (value == null){
70  			throw new SuperCsvConstraintViolationException("the String should not be null", context, this);
71  		}
72  		
73  		if( value instanceof String ) {
74  			final String stringValue = (String) value;
75  			if( stringValue.length() == 0 ) {
76  				throw new SuperCsvConstraintViolationException("the String should not be empty", context, this);
77  			}
78  		} else {
79  			throw new SuperCsvCellProcessorException(String.class, value, context, this);
80  		}
81  		
82  		return next.execute(value, context);
83  	}
84  }