1 /* 2 * Copyright 2009-2013 the original author or authors. 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 17 package com.orangesignal.csv.filters; 18 19 import java.io.IOException; 20 import java.util.regex.Pattern; 21 22 /** 23 * 指定された Java プログラム要素のフィールド値が正規表現パターンとマッチするかどうかでフィルタを適用する Java プログラム要素フィルタの実装です。 24 * 25 * @author Koji Sugisawa 26 * @since 1.2.3 27 */ 28 public class BeanRegexExpression extends BeanExpression { 29 30 /** 31 * 正規表現パターンを保持します。 32 */ 33 private Pattern pattern; 34 35 /** 36 * コンストラクタです。 37 * 38 * @param name 項目名 39 * @param pattern 正規表現パターン 40 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 41 */ 42 protected BeanRegexExpression(final String name, final String pattern) { 43 this(name, pattern, 0); 44 } 45 46 /** 47 * コンストラクタです。 48 * 49 * @param name 項目名 50 * @param pattern 正規表現パターン 51 * @param ignoreCase 大文字と小文字を区別するかどうか 52 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 53 */ 54 protected BeanRegexExpression(final String name, final String pattern, final boolean ignoreCase) { 55 this(name, pattern, ignoreCase ? Pattern.CASE_INSENSITIVE : 0); 56 } 57 58 /** 59 * コンストラクタです。 60 * 61 * @param name 項目名 62 * @param pattern 正規表現パターン 63 * @param flags マッチフラグ 64 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 65 */ 66 protected BeanRegexExpression(final String name, final String pattern, final int flags) { 67 super(name); 68 if (pattern == null) { 69 throw new IllegalArgumentException("Pattern must not be null"); 70 } 71 this.pattern = Pattern.compile(pattern, flags); 72 } 73 74 /** 75 * コンストラクタです。 76 * 77 * @param name 項目名 78 * @param pattern 正規表現パターン 79 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 80 */ 81 protected BeanRegexExpression(final String name, final Pattern pattern) { 82 super(name); 83 if (pattern == null) { 84 throw new IllegalArgumentException("Pattern must not be null"); 85 } 86 this.pattern = pattern; 87 } 88 89 @Override 90 public boolean accept(final Object bean) throws IOException { 91 return BeanExpressionUtils.regex(bean, name, pattern); 92 } 93 94 }