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
24 import org.apache.commons.cli.Converter;
25 import org.apache.commons.lang3.tuple.Pair;
26 import org.apache.rat.ConfigurationException;
27 import org.apache.rat.document.DocumentName;
28 import org.apache.rat.report.claim.ClaimStatistic;
29
30 import static java.lang.String.format;
31
32
33
34
35 public final class Converters {
36
37 private Converters() {
38
39 }
40
41
42
43
44 public static final FileConverter FILE_CONVERTER = new FileConverter();
45
46
47
48
49 public static final Converter<Pair<ClaimStatistic.Counter, Integer>, ConfigurationException> COUNTER_CONVERTER = arg -> {
50 String[] parts = arg.split(":");
51 try {
52 ClaimStatistic.Counter counter = ClaimStatistic.Counter.valueOf(parts[0].toUpperCase());
53 Integer limit = Integer.parseInt(parts[1]);
54 return Pair.of(counter, limit);
55 } catch (NumberFormatException e) {
56 throw new ConfigurationException(format("'%s' is not a valid integer", parts[1]), e);
57 } catch (IllegalArgumentException e) {
58 throw new ConfigurationException(format("'%s' is not a valid Counter", parts[0]), e);
59 }
60 };
61
62
63
64
65 public static final class FileConverter implements Converter<File, NullPointerException> {
66
67 private DocumentName workingDirectory;
68
69
70
71
72 private FileConverter() {
73
74 }
75
76
77
78
79
80 public void setWorkingDirectory(final DocumentName workingDirectory) {
81 this.workingDirectory = workingDirectory;
82 }
83
84
85
86
87
88
89
90 public File apply(final String fileName) throws NullPointerException {
91 File file = new File(fileName);
92
93 if (!fileName.startsWith(File.separator)) {
94
95 if (!DocumentName.FSInfo.getDefault().rootFor(fileName).isPresent()) {
96
97 file = new File(workingDirectory.resolve(fileName).getName()).getAbsoluteFile();
98 }
99 }
100 try {
101 return file.getCanonicalFile();
102 } catch (IOException e) {
103 return file.getAbsoluteFile();
104 }
105 }
106 }
107 }