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 java.util.ArrayList; 19 import java.util.Arrays; 20 import java.util.List; 21 22 import org.supercsv.cellprocessor.CellProcessorAdaptor; 23 import org.supercsv.cellprocessor.ift.CellProcessor; 24 import org.supercsv.cellprocessor.ift.StringCellProcessor; 25 import org.supercsv.exception.SuperCsvCellProcessorException; 26 import org.supercsv.exception.SuperCsvConstraintViolationException; 27 import org.supercsv.util.CsvContext; 28 29 /** 30 * Converts the input to a String and ensures that it doesn't contain any of the supplied substrings. For example, this 31 * constraint might be handy when reading/writing filenames and wanting to ensure no filename contains ":", "/", etc. 32 * 33 * @since 1.10 34 * @author Kasper B. Graversen 35 * @author James Bassett 36 */ 37 public class ForbidSubStr extends CellProcessorAdaptor implements StringCellProcessor { 38 39 private final List<String> forbiddenSubStrings = new ArrayList<String>(); 40 41 /** 42 * Constructs a new <tt>ForbidSubStr</tt> processor which ensures the input doesn't contain any of the supplied 43 * substrings. 44 * 45 * @param forbiddenSubStrings 46 * the List of forbidden substrings 47 * @throws NullPointerException 48 * if forbiddenSubStrings or one of its elements is null 49 * @throws IllegalArgumentException 50 * if forbiddenSubStrings is empty 51 */ 52 public ForbidSubStr(final List<String> forbiddenSubStrings) { 53 super(); 54 checkPreconditions(forbiddenSubStrings); 55 checkAndAddForbiddenStrings(forbiddenSubStrings); 56 } 57 58 /** 59 * Constructs a new <tt>ForbidSubStr</tt> processor which ensures the input doesn't contain any of the supplied 60 * substrings. 61 * 62 * @param forbiddenSubStrings 63 * the forbidden substrings 64 * @throws NullPointerException 65 * if forbiddenSubStrings or one of its elements is null 66 * @throws IllegalArgumentException 67 * if forbiddenSubStrings is empty 68 */ 69 public ForbidSubStr(final String... forbiddenSubStrings) { 70 super(); 71 checkPreconditions(forbiddenSubStrings); 72 checkAndAddForbiddenStrings(forbiddenSubStrings); 73 } 74 75 /** 76 * Constructs a new <tt>ForbidSubStr</tt> processor which ensures the input doesn't contain any of the supplied 77 * substrings, then calls the next processor in the chain. 78 * 79 * @param forbiddenSubStrings 80 * the List of forbidden substrings 81 * @param next 82 * the next processor in the chain 83 * @throws NullPointerException 84 * if forbiddenSubStrings, one of its elements or next is null 85 * @throws IllegalArgumentException 86 * if forbiddenSubStrings is empty 87 */ 88 public ForbidSubStr(final List<String> forbiddenSubStrings, final CellProcessor next) { 89 super(next); 90 checkPreconditions(forbiddenSubStrings); 91 checkAndAddForbiddenStrings(forbiddenSubStrings); 92 } 93 94 /** 95 * Constructs a new <tt>ForbidSubStr</tt> processor which ensures the input doesn't contain the supplied substring, 96 * then calls the next processor in the chain. 97 * 98 * @param forbiddenSubString 99 * the forbidden substring 100 * @param next 101 * the next processor in the chain 102 * @throws NullPointerException 103 * if forbiddenSubString or next is null 104 */ 105 public ForbidSubStr(final String forbiddenSubString, final CellProcessor next) { 106 this(new String[] { forbiddenSubString }, next); 107 } 108 109 /** 110 * Constructs a new <tt>ForbidSubStr</tt> processor which ensures the input doesn't contain any of the supplied 111 * substrings, then calls the next processor in the chain. 112 * 113 * @param forbiddenSubStrings 114 * the forbidden substrings 115 * @param next 116 * the next processor in the chain 117 * @throws NullPointerException 118 * if forbiddenSubStrings, one of its elements or next is null 119 * @throws IllegalArgumentException 120 * if forbiddenSubStrings is empty 121 */ 122 public ForbidSubStr(final String[] forbiddenSubStrings, final CellProcessor next) { 123 super(next); 124 checkPreconditions(forbiddenSubStrings); 125 checkAndAddForbiddenStrings(forbiddenSubStrings); 126 } 127 128 /** 129 * Checks the preconditions for creating a new ForbidSubStr processor with a List of forbidden substrings. 130 * 131 * @param forbiddenSubStrings 132 * the forbidden substrings 133 * @throws NullPointerException 134 * if forbiddenSubStrings is null 135 * @throws IllegalArgumentException 136 * if forbiddenSubStrings is empty 137 */ 138 private static void checkPreconditions(final List<String> forbiddenSubStrings) { 139 if( forbiddenSubStrings == null ) { 140 throw new NullPointerException("forbiddenSubStrings list should not be null"); 141 } else if( forbiddenSubStrings.isEmpty() ) { 142 throw new IllegalArgumentException("forbiddenSubStrings list should not be empty"); 143 } 144 } 145 146 /** 147 * Checks the preconditions for creating a new ForbidSubStr processor with an array of forbidden substrings. 148 * 149 * @param forbiddenSubStrings 150 * the forbidden substrings 151 * @throws NullPointerException 152 * if forbiddenSubStrings is null 153 * @throws IllegalArgumentException 154 * if forbiddenSubStrings is empty 155 */ 156 private static void checkPreconditions(final String... forbiddenSubStrings) { 157 if( forbiddenSubStrings == null ) { 158 throw new NullPointerException("forbiddenSubStrings array should not be null"); 159 } else if( forbiddenSubStrings.length == 0 ) { 160 throw new IllegalArgumentException("forbiddenSubStrings array should not be empty"); 161 } 162 } 163 164 /** 165 * Adds each forbidden substring, checking that it's not null. 166 * 167 * @param forbiddenSubStrings 168 * the forbidden substrings 169 * @throws NullPointerException 170 * if a forbidden substring is null 171 */ 172 private void checkAndAddForbiddenStrings(final String... forbiddenSubStrings) { 173 checkAndAddForbiddenStrings(Arrays.asList(forbiddenSubStrings)); 174 } 175 176 /** 177 * Adds each forbidden substring, checking that it's not null. 178 * 179 * @param forbiddenSubStrings 180 * the forbidden substrings 181 * @throws NullPointerException 182 * if a forbidden substring is null 183 */ 184 private void checkAndAddForbiddenStrings(final List<String> forbiddenSubStrings) { 185 for( String forbidden : forbiddenSubStrings ) { 186 if( forbidden == null ) { 187 throw new NullPointerException("forbidden substring should not be null"); 188 } 189 this.forbiddenSubStrings.add(forbidden); 190 } 191 } 192 193 /** 194 * {@inheritDoc} 195 * 196 * @throws SuperCsvCellProcessorException 197 * if value is null 198 * @throws SuperCsvConstraintViolationException 199 * if value is in the forbidden list 200 */ 201 public Object execute(final Object value, final CsvContext context) { 202 validateInputNotNull(value, context); 203 204 final String stringValue = value.toString(); 205 206 for( String forbidden : forbiddenSubStrings ) { 207 if( stringValue.contains(forbidden) ) { 208 throw new SuperCsvConstraintViolationException(String.format( 209 "'%s' contains the forbidden substring '%s'", value, forbidden), context, this); 210 } 211 } 212 213 return next.execute(value, context); 214 } 215 }