1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package cz.hobrasoft.pdfmu;
18
19 import cz.hobrasoft.pdfmu.error.ErrorType;
20 import cz.hobrasoft.pdfmu.operation.OperationException;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27
28
29
30
31 public class ExceptionMessagePattern {
32
33 private final ErrorType errorType;
34 private final String regex;
35 private final List<String> groupNames;
36
37 public ExceptionMessagePattern(ErrorType errorType, String regex, List<String> groupNames) {
38 this.errorType = errorType;
39 this.regex = regex;
40 this.groupNames = groupNames;
41 }
42
43
44
45
46
47
48
49
50 public OperationException getOperationException(Exception e) {
51 String message = e.getMessage();
52 Pattern p = Pattern.compile(regex);
53 Matcher m = p.matcher(message);
54 if (m.matches()) {
55 Map<String, String> arguments = PdfmuUtils.getMatcherGroups(m, groupNames);
56 Map<String, Object> argumentsObjects = new LinkedHashMap<>();
57 argumentsObjects.putAll(arguments);
58 return new OperationException(errorType, e, argumentsObjects);
59 }
60 return null;
61 }
62 }