1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.commandline;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Arrays;
24
25 import org.apache.commons.cli.Converter;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.apache.rat.ConfigurationException;
28 import org.apache.rat.document.DocumentName;
29 import org.apache.rat.report.claim.ClaimStatistic;
30
31 import static java.lang.String.format;
32
33
34
35
36 public final class Converters {
37
38 private Converters() {
39
40 }
41
42
43
44
45 public static final FileConverter FILE_CONVERTER = new FileConverter();
46
47
48
49
50 public static final Converter<Pair<ClaimStatistic.Counter, Integer>, ConfigurationException> COUNTER_CONVERTER = arg -> {
51 String[] parts = arg.split(":");
52 try {
53 ClaimStatistic.Counter counter = ClaimStatistic.Counter.valueOf(parts[0].toUpperCase());
54 Integer limit = Integer.parseInt(parts[1]);
55 return Pair.of(counter, limit);
56 } catch (NumberFormatException e) {
57 throw new ConfigurationException(format("'%s' is not a valid integer", parts[1]), e);
58 } catch (IllegalArgumentException e) {
59 throw new ConfigurationException(format("'%s' is not a valid Counter", parts[0]), e);
60 }
61 };
62
63
64
65
66 public static final Converter<String[], ConfigurationException> TEXT_LIST_CONVERTER = arg -> {
67 if (arg == null) {
68 return null;
69 }
70 return Arrays.stream(arg.split(",")).map(String::trim).toArray(String[]::new);
71 };
72
73
74
75
76 public static final class FileConverter implements Converter<File, NullPointerException> {
77
78 private DocumentName workingDirectory;
79
80
81
82
83 private FileConverter() {
84
85 }
86
87
88
89
90
91 public void setWorkingDirectory(final DocumentName workingDirectory) {
92 this.workingDirectory = workingDirectory;
93 }
94
95
96
97
98
99
100
101 public File apply(final String fileName) throws NullPointerException {
102 File file = new File(fileName);
103
104 if (!fileName.startsWith(File.separator)) {
105
106 if (!DocumentName.FSInfo.getDefault().rootFor(fileName).isPresent()) {
107
108 file = new File(workingDirectory.resolve(fileName).getName()).getAbsoluteFile();
109 }
110 }
111 try {
112 return file.getCanonicalFile();
113 } catch (IOException e) {
114 return file.getAbsoluteFile();
115 }
116 }
117 }
118 }