com.github.mygreen.supercsv.cellprocessor.conversion.RegexReplaceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of super-csv-annotation Show documentation
Show all versions of super-csv-annotation Show documentation
CSVのJavaライブラリであるSuperCSVに、アノテーション機能を追加したライブラリです。
package com.github.mygreen.supercsv.cellprocessor.conversion;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.cellprocessor.ift.StringCellProcessor;
import com.github.mygreen.supercsv.annotation.conversion.CsvRegexReplace;
import com.github.mygreen.supercsv.builder.Configuration;
import com.github.mygreen.supercsv.builder.FieldAccessor;
import com.github.mygreen.supercsv.cellprocessor.ConversionProcessorFactory;
import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
import com.github.mygreen.supercsv.exception.SuperCsvInvalidAnnotationException;
import com.github.mygreen.supercsv.localization.MessageBuilder;
import com.github.mygreen.supercsv.util.Utils;
/**
* アノテーション{@link CsvRegexReplace}をハンドリングして、値を置換するCellProcessorの{@link RegexReplace}を追加する。
*
* @since 2.0
* @author T.TSUCHIE
*
*/
public class RegexReplaceFactory implements ConversionProcessorFactory {
@Override
public Optional create(final CsvRegexReplace anno, final Optional next,
final FieldAccessor field, final TextFormatter> formatter, final Configuration config) {
final int flags = Utils.buildRegexFlags(anno.flags());
final Pattern pattern;
try {
pattern = Pattern.compile(anno.regex(), flags);
} catch(PatternSyntaxException e) {
throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.invalidType")
.var("property", field.getNameWithClass())
.varWithAnno("anno", anno.annotationType())
.var("attrName", "regex")
.var("attrValue", anno.regex())
.var("type", "{key.regex}")
.format(true));
}
final String replacement = anno.replacement();
final RegexReplace processor = next.map(n -> new RegexReplace(pattern, replacement, (StringCellProcessor) n))
.orElseGet(() -> new RegexReplace(pattern, replacement));
return Optional.of(processor);
}
}