1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.modules.resources.templating; |
35 |
|
|
36 |
|
import info.magnolia.module.resources.ResourceLinker; |
37 |
|
import info.magnolia.resourceloader.Resource; |
38 |
|
import info.magnolia.resourceloader.ResourceOrigin; |
39 |
|
import info.magnolia.resourceloader.ResourceVisitor; |
40 |
|
|
41 |
|
import java.util.Arrays; |
42 |
|
import java.util.LinkedHashMap; |
43 |
|
import java.util.LinkedList; |
44 |
|
import java.util.List; |
45 |
|
import java.util.Map; |
46 |
|
import java.util.regex.PatternSyntaxException; |
47 |
|
|
48 |
|
import javax.inject.Inject; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@link |
54 |
|
@link@link@link@link@link |
55 |
|
@link@link |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@see |
77 |
|
|
|
|
| 93.9% |
Uncovered Elements: 3 (49) |
Complexity: 20 |
Complexity Density: 0.69 |
|
78 |
|
public class ResourcesTemplatingFunctions { |
79 |
|
|
80 |
|
public static final String NAME = "resfn"; |
81 |
|
|
82 |
|
protected static final String CSS_SUFFIX = "\"/>"; |
83 |
|
protected static final String JS_PREFIX = "<script src=\""; |
84 |
|
protected static final String JS_SUFFIX = "\"></script>"; |
85 |
|
|
86 |
|
private final ResourceOrigin origin; |
87 |
|
private final ResourceLinker linker; |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
89 |
8 |
@Inject... |
90 |
|
public ResourcesTemplatingFunctions(ResourceOrigin resourceOrigin, ResourceLinker linker) { |
91 |
8 |
this.origin = resourceOrigin; |
92 |
8 |
this.linker = linker; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
98 |
4 |
public String css(String pattern) {... |
99 |
4 |
return css(Arrays.asList(pattern)); |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
2 |
public String css(String pattern, String otherAttributes) {... |
106 |
2 |
return css(Arrays.asList(pattern), otherAttributes); |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
112 |
5 |
public String css(final List<String> patterns) {... |
113 |
5 |
return css(patterns, null); |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
7 |
public String css(List<String> patterns, String otherAttributes) {... |
120 |
7 |
return generate(patterns, getCssPrefix(otherAttributes), CSS_SUFFIX, false); |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
126 |
1 |
public String cachedCss(String pattern) {... |
127 |
1 |
return cachedCss(Arrays.asList(pattern)); |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
0 |
public String cachedCss(String pattern, String otherAttributes) {... |
134 |
0 |
return cachedCss(Arrays.asList(pattern), otherAttributes); |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
140 |
1 |
public String cachedCss(final List<String> patterns) {... |
141 |
1 |
return cachedCss(patterns, null); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
147 |
1 |
public String cachedCss(List<String> patterns, String otherAttributes) {... |
148 |
1 |
return generate(patterns, getCssPrefix(otherAttributes), CSS_SUFFIX, true); |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
1 |
public String js(String pattern) {... |
155 |
1 |
return js(Arrays.asList(pattern)); |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
161 |
2 |
public String js(final List<String> patterns) {... |
162 |
2 |
return generate(patterns, JS_PREFIX, JS_SUFFIX, false); |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
168 |
1 |
public String cachedJs(String pattern) {... |
169 |
1 |
return cachedJs(Arrays.asList(pattern)); |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
175 |
1 |
public String cachedJs(final List<String> patterns) {... |
176 |
1 |
return generate(patterns, JS_PREFIX, JS_SUFFIX, true); |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
182 |
11 |
private String generate(List<String> patterns, String prefix, String suffix, final boolean isCached) {... |
183 |
|
|
184 |
11 |
Map<String, List<String>> intermediates = new LinkedHashMap<>(); |
185 |
11 |
patterns.forEach(pattern -> intermediates.put(pattern, new LinkedList<>())); |
186 |
11 |
origin.traverseWith(new ResourceVisitor() { |
187 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
188 |
88 |
@Override... |
189 |
|
public void visitFile(final Resource resource) { |
190 |
88 |
patterns.stream() |
191 |
|
.filter(pattern -> matchSafely(resource.getPath(), pattern)) |
192 |
|
.map(intermediates::get) |
193 |
|
.forEach(results -> results.add(prefix + linker.linkTo(resource.getPath(), isCached) + suffix)); |
194 |
|
} |
195 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
196 |
104 |
private boolean matchSafely(String resource, String pattern) {... |
197 |
104 |
try { |
198 |
104 |
return resource.matches(pattern); |
199 |
|
} catch (PatternSyntaxException e) { |
200 |
|
|
201 |
|
} |
202 |
0 |
return false; |
203 |
|
} |
204 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
205 |
66 |
@Override... |
206 |
|
public boolean visitDirectory(Resource resource) { |
207 |
66 |
return true; |
208 |
|
} |
209 |
|
}); |
210 |
|
|
211 |
11 |
StringBuilder results = new StringBuilder(); |
212 |
11 |
List<String> hits = new LinkedList<>(); |
213 |
|
|
214 |
11 |
intermediates.values() |
215 |
|
.forEach(patternResults -> patternResults.stream() |
216 |
|
.filter(potentialHit -> !hits.contains(potentialHit)) |
217 |
|
.forEach(hit -> { |
218 |
20 |
results.append(hit).append("\n"); |
219 |
20 |
hits.add(hit); |
220 |
|
})); |
221 |
|
|
222 |
11 |
return results.toString(); |
223 |
|
} |
224 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
225 |
8 |
private static String getCssPrefix(String otherAttributes) {... |
226 |
8 |
return "<link rel=\"stylesheet\" type=\"text/css\" " + (otherAttributes == null ? "" : otherAttributes + " ") + "href=\""; |
227 |
|
} |
228 |
|
|
229 |
|
} |