1 package com.opencsv.bean;
2
3 import com.opencsv.ICSVParser;
4 import com.opencsv.exceptions.CsvDataTypeMismatchException;
5 import org.apache.commons.lang3.StringUtils;
6
7 import java.util.Locale;
8 import java.util.ResourceBundle;
9 import java.util.UUID;
10 import java.util.regex.Pattern;
11
12
13
14
15
16
17
18
19 public class ConverterUUID extends AbstractCsvConverter {
20 private static final String UUID_REGEX_PATTERN = "\\b[0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-\\b[0-9a-fA-F]{12}\\b";
21
22
23
24
25
26 public ConverterUUID(Locale errorLocale) {
27 super(UUID.class, null, null, errorLocale);
28 }
29
30 @Override
31 public Object convertToRead(String value) throws CsvDataTypeMismatchException {
32 if (StringUtils.isBlank(value)) {
33 return null;
34 }
35 String trimmedString = value.trim();
36 if (!Pattern.matches(UUID_REGEX_PATTERN, trimmedString)) {
37 throw new CsvDataTypeMismatchException(value, type, String.format(
38 ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME).getString("invalid.uuid.value"),
39 value, type.getName()));
40 }
41 return UUID.fromString(trimmedString);
42 }
43 }