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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
89 @Inject
90 public ResourcesTemplatingFunctions(ResourceOrigin resourceOrigin, ResourceLinker linker) {
91 this.origin = resourceOrigin;
92 this.linker = linker;
93 }
94
95
96
97
98
99 public String css(String pattern) {
100 return css(Arrays.asList(new String[] { pattern }));
101 }
102
103
104
105
106 public String css(String pattern, String otherAttributes) {
107 return css(Arrays.asList(new String[] { pattern }), otherAttributes);
108 }
109
110
111
112
113 public String css(final List<String> patterns) {
114 return css(patterns, null);
115 }
116
117
118
119
120 public String css(List<String> patterns, String otherAttributes) {
121 return generate(patterns, getCssPrefix(otherAttributes), CSS_SUFFIX, false);
122 }
123
124
125
126
127 public String cachedCss(String pattern) {
128 return cachedCss(Arrays.asList(new String[] { pattern }));
129 }
130
131
132
133
134 public String cachedCss(String pattern, String otherAttributes) {
135 return cachedCss(Arrays.asList(new String[] { pattern }), otherAttributes);
136 }
137
138
139
140
141 public String cachedCss(final List<String> patterns) {
142 return cachedCss(patterns, null);
143 }
144
145
146
147
148 public String cachedCss(List<String> patterns, String otherAttributes) {
149 return generate(patterns, getCssPrefix(otherAttributes), CSS_SUFFIX, true);
150 }
151
152
153
154
155 public String js(String pattern) {
156 return js(Arrays.asList(new String[] { pattern }));
157 }
158
159
160
161
162 public String js(final List<String> patterns) {
163 return generate(patterns, JS_PREFIX, JS_SUFFIX, false);
164 }
165
166
167
168
169 public String cachedJs(String pattern) {
170 return cachedJs(Arrays.asList(new String[] { pattern }));
171 }
172
173
174
175
176 public String cachedJs(final List<String> patterns) {
177 return generate(patterns, JS_PREFIX, JS_SUFFIX, true);
178 }
179
180
181
182
183 private String generate(List<String> patterns, String prefix, String suffix, final boolean isCached) {
184
185 Map<String, List<String>> intermediates = new LinkedHashMap<>();
186 patterns.stream()
187 .forEach(pattern -> intermediates.put(pattern, new LinkedList<>()));
188 origin.traverseWith(new ResourceVisitor() {
189
190 @Override
191 public void visitFile(final Resource resource) {
192 patterns.stream()
193 .filter(pattern -> matchSafely(resource.getPath(), pattern))
194 .map(pattern -> intermediates.get(pattern))
195 .forEach(results -> results.add(prefix + linker.linkTo(resource.getPath(), isCached) + suffix));
196 }
197
198 private boolean matchSafely(String resource, String pattern) {
199 try {
200 return resource.matches(pattern);
201 } catch (PatternSyntaxException e) {
202
203 }
204 return false;
205 }
206
207 @Override
208 public boolean visitDirectory(Resource resource) {
209 return true;
210 }
211 });
212
213 StringBuilder results = new StringBuilder();
214 List<String> hits = new LinkedList<>();
215
216 intermediates.values().stream()
217 .forEach(patternResults -> patternResults.stream()
218 .filter(potentialHit -> !hits.contains(potentialHit))
219 .forEach(hit -> {
220 results.append(hit).append("\n");
221 hits.add(hit);
222 }));
223
224 return results.toString();
225 }
226
227 private static String getCssPrefix(String otherAttributes) {
228 return "<link rel=\"stylesheet\" type=\"text/css\" " + (otherAttributes == null ? "" : otherAttributes + " ") + "href=\"";
229 }
230
231 }