1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.theme.postprocessor;
17
18 import com.yahoo.platform.yui.compressor.CssCompressor;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.kuali.rice.krad.theme.util.ThemeBuilderConstants;
22 import org.kuali.rice.krad.theme.util.ThemeBuilderUtils;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.OutputStream;
31 import java.io.OutputStreamWriter;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Properties;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class ThemeCssFilesProcessor extends ThemeFilesProcessor {
53 private static final Logger LOG = Logger.getLogger(ThemeCssFilesProcessor.class);
54
55 protected int linebreak = -1;
56
57 public ThemeCssFilesProcessor(String themeName, File themeDirectory, Properties themeProperties,
58 Map<String, File> themePluginDirsMap, File workingDir, String projectVersion) {
59 super(themeName, themeDirectory, themeProperties, themePluginDirsMap, workingDir, projectVersion);
60 }
61
62
63
64
65 @Override
66 protected String getFileTypeExtension() {
67 return ThemeBuilderConstants.FileExtensions.CSS;
68 }
69
70
71
72
73 @Override
74 protected String getExcludesConfigKey() {
75 return ThemeBuilderConstants.ThemeConfiguration.CSS_EXCLUDES;
76 }
77
78
79
80
81 @Override
82 protected String getFileTypeDirectoryName() {
83 return ThemeBuilderConstants.ThemeDirectories.STYLESHEETS;
84 }
85
86
87
88
89 @Override
90 protected String getFileListingConfigKey() {
91 return ThemeBuilderConstants.DerivedConfiguration.THEME_CSS_FILES;
92 }
93
94
95
96
97 @Override
98 protected void addAdditionalFiles(List<File> themeFiles) {
99
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 @Override
122 protected List<File> sortThemeFiles(List<File> pluginFiles, List<File> subDirFiles) {
123 List<String> loadCssFirst = getThemePropertyValue(ThemeBuilderConstants.ThemeConfiguration.CSS_LOAD_FIRST);
124 List<String> loadCssLast = getThemePropertyValue(ThemeBuilderConstants.ThemeConfiguration.CSS_LOAD_LAST);
125
126 List<String> pluginCssLoadOrder = getThemePropertyValue(
127 ThemeBuilderConstants.ThemeConfiguration.PLUGIN_CSS_LOAD_ORDER);
128 List<String> cssLoadOrder = getThemePropertyValue(
129 ThemeBuilderConstants.ThemeConfiguration.THEME_CSS_LOAD_ORDER);
130
131 return ThemeBuilderUtils.orderFiles(pluginFiles, subDirFiles, loadCssFirst, loadCssLast, pluginCssLoadOrder,
132 cssLoadOrder);
133 }
134
135
136
137
138
139
140 @Override
141 protected String processMergeFileContents(String fileContents, File fileToMerge, File mergedFile)
142 throws IOException {
143 return rewriteCssUrls(fileContents, fileToMerge, mergedFile);
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 protected String rewriteCssUrls(String css, File mergeFile, File mergedFile) throws IOException {
167 String urlAdjustment = ThemeBuilderUtils.calculatePathToFile(mergedFile, mergeFile);
168
169 if (StringUtils.isBlank(urlAdjustment)) {
170
171 return css;
172 }
173
174
175 Pattern urlPattern = Pattern.compile(ThemeBuilderConstants.Patterns.CSS_URL_PATTERN);
176
177 Matcher matcher = urlPattern.matcher(css);
178
179 StringBuffer sb = new StringBuffer();
180 while (matcher.find()) {
181 String cssStatement = matcher.group();
182
183 String cssUrl = null;
184 if (matcher.group(1) != null) {
185 cssUrl = matcher.group(1);
186 } else {
187 cssUrl = matcher.group(2);
188 }
189
190 if (cssUrl != null) {
191
192 String modifiedUrl = cssUrl;
193
194 if (!cssUrl.startsWith("/")) {
195 modifiedUrl = urlAdjustment + cssUrl;
196 }
197
198 String modifiedStatement = Matcher.quoteReplacement(cssStatement.replace(cssUrl, modifiedUrl));
199
200 matcher.appendReplacement(sb, modifiedStatement);
201 }
202 }
203
204 matcher.appendTail(sb);
205
206 return sb.toString();
207 }
208
209
210
211
212
213
214
215
216
217
218
219 @Override
220 protected void minify(File mergedFile, File minifiedFile) throws IOException {
221 InputStream in = null;
222 OutputStream out = null;
223 OutputStreamWriter writer = null;
224 InputStreamReader reader = null;
225
226 LOG.info("Populating minified CSS file: " + minifiedFile.getPath());
227
228 try {
229 out = new FileOutputStream(minifiedFile);
230 writer = new OutputStreamWriter(out);
231
232 in = new FileInputStream(mergedFile);
233 reader = new InputStreamReader(in);
234
235 CssCompressor compressor = new CssCompressor(reader);
236 compressor.compress(writer, this.linebreak);
237
238 writer.flush();
239 } finally {
240 if (in != null) {
241 in.close();
242 }
243
244 if (out != null) {
245 out.close();
246 }
247 }
248 }
249 }