1 package com.github.mikkoi.maven.plugins.enforcer.rule.propertyusage;
2
3 import org.apache.maven.plugin.logging.Log;
4
5 import javax.annotation.Nonnull;
6 import java.io.IOException;
7 import java.nio.charset.Charset;
8 import java.nio.file.Files;
9 import java.nio.file.Paths;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19
20
21
22 class UsageFiles {
23
24 private final Log log;
25
26 UsageFiles(final Log log) {
27 this.log = log;
28 }
29
30
31
32
33
34
35
36
37
38 @Nonnull
39 Set<String> readDefinedUsagesFromFiles(
40 @Nonnull final Collection<String> filenames,
41 @Nonnull final Map<String,String> templatesAndProperties,
42 @Nonnull final Charset charset)
43 throws IOException {
44 final Set<String> results = new HashSet<>();
45 final Map<Pattern,String> tplPatterns = new HashMap<>();
46 templatesAndProperties.forEach((tpl,property) -> tplPatterns.put(Pattern.compile(tpl, Pattern.COMMENTS | Pattern.UNICODE_CHARACTER_CLASS),property));
47 for (final String filename : filenames) {
48 log.debug("Reading file '" + filename + "'.");
49 final String allFile = String.join("", Files.readAllLines(Paths.get(filename), charset));
50 tplPatterns.forEach((tplP, property) -> {
51 log.debug(" Matching pattern '" + tplP.pattern() + "'.");
52 log.debug(" Matching with allFile '" + allFile + "'.");
53 final Matcher matcher = tplP.matcher(allFile);
54 if (matcher.find()) {
55 log.debug(" Pattern match found (" + filename + ")" + ", pattern '" + tplP.pattern() + "'.");
56 results.add(property);
57 }
58 });
59 }
60 return results;
61 }
62
63
64
65
66
67
68
69 @Nonnull
70 Set<UsageLocation> readAllUsagesFromFiles(
71 @Nonnull final Collection<String> filenames,
72 @Nonnull final Set<String> templates,
73 @Nonnull final Charset charset)
74 throws IOException {
75 final Set<UsageLocation> foundProperties = new HashSet<>();
76 final ArrayList<Pattern> tplPatterns = new ArrayList<>();
77 templates.forEach(tpl -> tplPatterns.add(Pattern.compile(tpl, Pattern.COMMENTS | Pattern.UNICODE_CHARACTER_CLASS)));
78 for (final String filename : filenames) {
79 log.debug("Reading file '" + filename + "'.");
80 final Collection<String> lines = Files.readAllLines(Paths.get(filename), charset);
81 tplPatterns.forEach(tplP -> {
82 log.debug(" Matching pattern '" + tplP.pattern() + "'.");
83 int rowNr = 1;
84 for (final String row : lines) {
85 log.debug(" Matching with row '" + row + "'.");
86 final Matcher matcher = tplP.matcher(row);
87 if (matcher.find()) {
88 log.debug(" Pattern match found (" + filename + ":" + rowNr + ")" + ", pattern '" + tplP.pattern() + "'.");
89 final String propname = matcher.group(1);
90 log.debug(" Extracted property '" + propname + "'.");
91 foundProperties.add(new UsageLocation(propname, rowNr, filename));
92 }
93 rowNr += 1;
94 }
95 });
96 }
97 return foundProperties;
98 }
99
100 static class UsageLocation {
101
102 @Nonnull
103 private String property;
104
105 private int row;
106
107 @Nonnull
108 private String filename;
109
110
111
112
113
114
115 UsageLocation(@Nonnull final String propertyVal, final int rowVal, @Nonnull final String filenameVal) {
116 property = propertyVal;
117 row = rowVal;
118 filename = filenameVal;
119 }
120
121 @Nonnull
122 public String getProperty() {
123 return property;
124 }
125
126 int getRow() {
127 return row;
128 }
129
130 @Nonnull
131 String getFilename() {
132 return filename;
133 }
134 }
135 }