1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.configuration;
20
21 import java.io.File;
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.InvocationTargetException;
24 import java.net.URI;
25 import java.util.Arrays;
26
27 import org.apache.rat.ConfigurationException;
28
29
30
31
32
33 public enum Format {
34
35 XML(XMLConfigurationReader.class, "xml"),
36
37 TXT(null, "txt", "text");
38
39
40 private final String[] suffix;
41
42
43 private Constructor<MatcherReader> matcherReader;
44
45 private Constructor<LicenseReader> licenseReader;
46
47 @SuppressWarnings("unchecked")
48 Format(final Class<?> reader, final String... suffix) {
49 if (reader != null) {
50 try {
51 matcherReader = MatcherReader.class.isAssignableFrom(reader)
52 ? (Constructor<MatcherReader>) reader.getConstructor()
53 : null;
54 licenseReader = LicenseReader.class.isAssignableFrom(reader)
55 ? (Constructor<LicenseReader>) reader.getConstructor()
56 : null;
57 } catch (NoSuchMethodException | SecurityException e) {
58 throw new ConfigurationException("Error retrieving no argument constructor for " + reader.getName(), e);
59 }
60 }
61 this.suffix = suffix;
62 }
63
64
65
66
67 public MatcherReader matcherReader() {
68 try {
69 return matcherReader == null ? null : matcherReader.newInstance();
70 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
71 | InvocationTargetException e) {
72 throw new ConfigurationException("Can not instantiate MatcherReader for " + this.name(), e);
73 }
74 }
75
76
77
78
79 public LicenseReader licenseReader() {
80 try {
81 return licenseReader == null ? null : licenseReader.newInstance();
82 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
83 | InvocationTargetException e) {
84 throw new ConfigurationException("Can not instantiate LicenseReader for " + this.name(), e);
85 }
86 }
87
88
89
90
91
92
93 public static Format from(final String name) {
94 int pos = name.lastIndexOf('.');
95 String suffix = name.substring(pos + 1);
96 for (Format f : Format.values()) {
97 if (Arrays.asList(f.suffix).contains(suffix)) {
98 return f;
99 }
100 }
101 throw new IllegalArgumentException(String.format("No such suffix: %s", suffix));
102 }
103
104
105
106
107
108
109 public static Format from(final URI uri) {
110 return Format.from(uri.toString());
111 }
112
113
114
115
116
117
118 public static Format from(final File file) {
119 return Format.from(file.getName());
120 }
121 }