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 java.util.regex.Pattern;
19 import java.util.regex.PatternSyntaxException;
20
21 import org.supercsv.cellprocessor.ift.BoolCellProcessor;
22 import org.supercsv.cellprocessor.ift.DateCellProcessor;
23 import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
24 import org.supercsv.cellprocessor.ift.LongCellProcessor;
25 import org.supercsv.cellprocessor.ift.StringCellProcessor;
26 import org.supercsv.exception.SuperCsvCellProcessorException;
27 import org.supercsv.util.CsvContext;
28
29 /**
30 * Replaces each substring of the input string that matches the given regular expression with the given replacement. The
31 * regular expression pattern is compiled once then reused for efficiency.
32 *
33 * @author Kasper B. Graversen
34 * @author Dominique De Vito
35 * @author James Bassett
36 */
37 public class StrReplace extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor,
38 DoubleCellProcessor, LongCellProcessor, StringCellProcessor {
39
40 private final Pattern regexPattern;
41 private final String replacement;
42
43 /**
44 * Constructs a new <tt>StrReplace</tt> processor, which replaces each substring of the input that matches the regex
45 * with the supplied replacement.
46 *
47 * @param regex
48 * the regular expression to match
49 * @param replacement
50 * the string to be substituted for each match
51 * @throws IllegalArgumentException
52 * if regex is empty
53 * @throws NullPointerException
54 * if regex or replacement is null
55 * @throws PatternSyntaxException
56 * if regex is not a valid regular expression
57 */
58 public StrReplace(final String regex, final String replacement) {
59 super();
60 checkPreconditions(regex, replacement);
61 this.regexPattern = Pattern.compile(regex);
62 this.replacement = replacement;
63 }
64
65 /**
66 * Constructs a new <tt>StrReplace</tt> processor, which replaces each substring of the input that matches the regex
67 * with the supplied replacement, then calls the next processor in the chain.
68 *
69 * @param regex
70 * the regular expression to match
71 * @param replacement
72 * the string to be substituted for each match
73 * @param next
74 * the next processor in the chain
75 * @throws IllegalArgumentException
76 * if regex is empty
77 * @throws NullPointerException
78 * if regex or replacement is null
79 * @throws PatternSyntaxException
80 * if regex is not a valid regular expression
81 */
82 public StrReplace(final String regex, final String replacement, final StringCellProcessor next) {
83 super(next);
84 checkPreconditions(regex, replacement);
85 this.regexPattern = Pattern.compile(regex);
86 this.replacement = replacement;
87 }
88
89 /**
90 * Checks the preconditions for creating a new StrRegExReplace processor.
91 *
92 * @param regex
93 * the supplied regular expression
94 * @param replacement
95 * the supplied replacement text
96 * @throws IllegalArgumentException
97 * if regex is empty
98 * @throws NullPointerException
99 * if regex or replacement is null
100 */
101 private static void checkPreconditions(final String regex, final String replacement) {
102 if( regex == null ) {
103 throw new NullPointerException("regex should not be null");
104 } else if( regex.length() == 0 ) {
105 throw new IllegalArgumentException("regex should not be empty");
106 }
107
108 if( replacement == null ) {
109 throw new NullPointerException("replacement should not be null");
110 }
111 }
112
113 /**
114 * {@inheritDoc}
115 *
116 * @throws SuperCsvCellProcessorException
117 * if value is null
118 */
119 public Object execute(final Object value, final CsvContext context) {
120 validateInputNotNull(value, context);
121 String result = regexPattern.matcher(value.toString()).replaceAll(replacement);
122 return next.execute(result, context);
123 }
124
125 }