1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.creadur.tentacles;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Collections;
24 import java.util.Map;
25
26 public class Licenses {
27
28 private final IOSystem ioSystem;
29 private final Map<String, String> licenses;
30
31 public Licenses(final Map<String, String> licenses, final Platform platform) {
32 super();
33 this.ioSystem = platform.getIoSystem();
34 this.licenses = Collections.unmodifiableMap(licenses);
35 }
36
37 public License from(final File document) throws IOException {
38 return license(this.ioSystem.slurp(document));
39 }
40
41 private License license(final String text) {
42 final String key = toKey(text);
43 return new License(key, normalize(text));
44 }
45
46 private String toKey(final String text) {
47 return text.replaceAll("[ \\n\\t\\r]+", "").toLowerCase().intern();
48 }
49
50 private String normalize(String text) {
51 for (final Map.Entry<String, String> license : this.licenses.entrySet()) {
52 text =
53 text.replace(
54 license.getValue(),
55 String.format("---[%s - full text]---\n\n",
56 license.getKey()));
57 }
58 text = text.intern();
59 return text;
60 }
61 }