1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.theme.util;
17
18 import org.apache.commons.lang3.StringUtils;
19 import org.codehaus.plexus.util.DirectoryScanner;
20 import org.codehaus.plexus.util.FileUtils;
21 import org.codehaus.plexus.util.SelectorUtils;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Properties;
30
31
32
33
34
35
36 public class ThemeBuilderUtils {
37
38
39
40
41
42
43
44
45
46
47 public static Properties retrieveThemeProperties(String themeDirectory) throws IOException {
48 Properties themeProperties = null;
49
50 FileInputStream fileInputStream = null;
51
52 try {
53 File propertiesFile = new File(themeDirectory, ThemeBuilderConstants.THEME_PROPERTIES_FILE);
54
55 if (propertiesFile.exists()) {
56 fileInputStream = new FileInputStream(propertiesFile);
57
58 themeProperties = new Properties();
59 themeProperties.load(fileInputStream);
60 }
61 } finally {
62 if (fileInputStream != null) {
63 fileInputStream.close();
64 }
65 }
66
67 return themeProperties;
68 }
69
70
71
72
73
74
75
76
77
78 public static void storeThemeProperties(String themeDirectory, Properties themeProperties) throws IOException {
79 File propertiesFile = new File(themeDirectory, ThemeBuilderConstants.THEME_DERIVED_PROPERTIES_FILE);
80
81
82 if (propertiesFile.exists()) {
83 FileUtils.forceDelete(propertiesFile);
84 }
85
86 FileWriter fileWriter = null;
87
88 try {
89 fileWriter = new FileWriter(propertiesFile);
90
91 themeProperties.store(fileWriter, null);
92 } finally {
93 if (fileWriter != null) {
94 fileWriter.close();
95 }
96 }
97 }
98
99
100
101
102
103
104
105
106
107 public static List<String> getPropertyValueAsList(String key, Properties properties) {
108 List<String> propertyValueList = null;
109
110 String[] propertyValueArray = getPropertyValueAsArray(key, properties);
111 if (propertyValueArray != null) {
112 propertyValueList = new ArrayList<String>();
113
114 for (String value : propertyValueArray) {
115 propertyValueList.add(value);
116 }
117 }
118
119 return propertyValueList;
120 }
121
122
123
124
125
126
127
128
129
130 public static String[] getPropertyValueAsArray(String key, Properties properties) {
131 String[] propertyValue = null;
132
133 if (properties.containsKey(key)) {
134 String propertyValueString = properties.getProperty(key);
135
136 if (!StringUtils.isBlank(propertyValueString)) {
137 propertyValue = propertyValueString.split(",");
138 }
139 }
140
141 return propertyValue;
142 }
143
144
145
146
147
148
149
150
151
152 public static void copyProperty(String propertyKey, Properties sourceProperties, Properties targetProperties) {
153 if (targetProperties != null && targetProperties.containsKey(propertyKey) && StringUtils.isNotBlank(
154 targetProperties.getProperty(propertyKey))) {
155 return;
156 }
157
158 if (sourceProperties != null && sourceProperties.containsKey(propertyKey)) {
159 String propertyValue = sourceProperties.getProperty(propertyKey);
160
161 if (targetProperties == null) {
162 targetProperties = new Properties();
163 }
164
165 targetProperties.put(propertyKey, propertyValue);
166 }
167 }
168
169
170
171
172
173
174
175
176 public static void validateFileExistence(List<File> filesToValidate, String exceptionMessage) {
177 if (filesToValidate == null) {
178 return;
179 }
180
181 for (File file : filesToValidate) {
182 if (!file.exists()) {
183 throw new RuntimeException(exceptionMessage + " Path: " + file.getPath());
184 }
185 }
186 }
187
188
189
190
191
192
193
194
195 public static boolean directoryContainsFile(File directory, String fileName) {
196 boolean containsFile = false;
197
198 List<String> directoryContents = getDirectoryContents(directory, null, null);
199
200 for (String directoryFile : directoryContents) {
201 String directoryFilename = FileUtils.filename(directoryFile);
202
203 if (directoryFilename.equals(fileName)) {
204 containsFile = true;
205 }
206 }
207
208 return containsFile;
209 }
210
211
212
213
214
215
216
217
218
219
220 public static List<File> getDirectoryFiles(File baseDirectory, String[] includes, String[] excludes) {
221 List<File> directoryFiles = new ArrayList<File>();
222
223 List<String> directoryFileNames = getDirectoryFileNames(baseDirectory, includes, excludes);
224
225 for (String fileName : directoryFileNames) {
226 directoryFiles.add(new File(baseDirectory, fileName));
227 }
228
229 return directoryFiles;
230 }
231
232
233
234
235
236
237
238
239
240
241 public static List<String> getDirectoryFileNames(File baseDirectory, String[] includes, String[] excludes) {
242 List<String> files = new ArrayList<String>();
243
244 DirectoryScanner scanner = new DirectoryScanner();
245
246 if (includes != null) {
247 scanner.setIncludes(includes);
248 }
249
250 if (excludes != null) {
251 scanner.setExcludes(excludes);
252 }
253
254 scanner.setCaseSensitive(false);
255 scanner.addDefaultExcludes();
256 scanner.setBasedir(baseDirectory);
257
258 scanner.scan();
259
260 for (String includedFilename : scanner.getIncludedFiles()) {
261 files.add(includedFilename);
262 }
263
264 return files;
265 }
266
267
268
269
270
271
272
273
274 public static List<File> getSubDirectories(File baseDirectory, List<String> subDirectoryNames) {
275 List<File> subDirs = null;
276
277 if (subDirectoryNames != null) {
278 subDirs = new ArrayList<File>();
279
280 for (String pluginName : subDirectoryNames) {
281 subDirs.add(new File(baseDirectory, pluginName));
282 }
283 }
284
285 return subDirs;
286 }
287
288
289
290
291
292
293
294
295
296
297 public static List<String> getDirectoryContents(File baseDirectory, String[] includes, String[] excludes) {
298 List<String> contents = new ArrayList<String>();
299
300 DirectoryScanner scanner = new DirectoryScanner();
301
302 if (includes != null) {
303 scanner.setIncludes(includes);
304 }
305
306 if (excludes != null) {
307 scanner.setExcludes(excludes);
308 }
309
310 scanner.setCaseSensitive(false);
311 scanner.addDefaultExcludes();
312 scanner.setBasedir(baseDirectory);
313
314 scanner.scan();
315
316 for (String includedDirectory : scanner.getIncludedDirectories()) {
317 contents.add(includedDirectory);
318 }
319
320 for (String includedFilename : scanner.getIncludedFiles()) {
321 contents.add(includedFilename);
322 }
323
324 return contents;
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341 public static void copyDirectory(String sourceDirectoryPath, String targetDirectoryPath)
342 throws IOException {
343 File sourceDir = new File(sourceDirectoryPath);
344
345 if (!sourceDir.exists()) {
346 return;
347 }
348
349 File targetDir = new File(targetDirectoryPath);
350 if (targetDir.exists()) {
351
352 FileUtils.forceDelete(targetDir);
353 }
354
355 targetDir.mkdir();
356
357 FileUtils.copyDirectoryStructure(sourceDir, targetDir);
358
359
360 DirectoryScanner scanner = new DirectoryScanner();
361 scanner.setBasedir(targetDir);
362
363 scanner.scan();
364
365 for (String includedDirectory : scanner.getIncludedDirectories()) {
366 File subdirectory = new File(targetDir, includedDirectory);
367
368 if (subdirectory.exists() && subdirectory.isDirectory()) {
369 if (subdirectory.getName().startsWith(".")) {
370 FileUtils.forceDelete(subdirectory);
371 }
372 }
373 }
374 }
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391 public static void copyMissingContent(File sourceDirectory, File targetDirectory, List<String> sourceExcludes)
392 throws IOException {
393 String[] copyExcludes = null;
394
395 if ((sourceExcludes != null) && !sourceExcludes.isEmpty()) {
396 copyExcludes = new String[sourceExcludes.size()];
397
398 copyExcludes = sourceExcludes.toArray(copyExcludes);
399 }
400
401 List<String> sourceDirectoryContents = getDirectoryContents(sourceDirectory, null, copyExcludes);
402 List<String> targetDirectoryContents = getDirectoryContents(targetDirectory, null, null);
403
404 for (String sourceContent : sourceDirectoryContents) {
405 if (targetDirectoryContents.contains(sourceContent)) {
406 continue;
407 }
408
409
410 File sourceFile = new File(sourceDirectory, sourceContent);
411 File targetFile = new File(targetDirectory, sourceContent);
412
413 if (sourceFile.isDirectory()) {
414 targetFile.mkdir();
415 } else {
416 FileUtils.copyFile(sourceFile, targetFile);
417 }
418 }
419 }
420
421
422
423
424
425
426
427
428
429 public static boolean inIncludeList(String name, String[] includes) {
430 if ((includes == null) || (includes.length == 0)) {
431 return true;
432 }
433
434 for (String include : includes) {
435 if (SelectorUtils.matchPath(include, name, false)) {
436 return true;
437 }
438 }
439
440 return false;
441 }
442
443
444
445
446
447
448
449
450
451 public static boolean inExcludeList(String name, String[] excludes) {
452 if ((excludes == null) || (excludes.length == 0)) {
453 return false;
454 }
455
456 for (String exclude : excludes) {
457 if (SelectorUtils.matchPath(exclude, name, false)) {
458 return true;
459 }
460 }
461
462 return false;
463 }
464
465
466
467
468
469
470
471
472 public static void addExtensionToPatterns(String[] patterns, String extension) {
473 if (patterns == null) {
474 return;
475 }
476
477 for (int i = 0; i < patterns.length; i++) {
478 String pattern = patterns[i];
479
480 if (!(pattern.endsWith("*") || pattern.endsWith(extension))) {
481 patterns[i] = pattern + extension;
482 }
483 }
484 }
485
486
487
488
489
490
491
492
493
494 public static List<String> getRelativePaths(File parentDirectory, List<File> files) {
495 List<String> relativePaths = new ArrayList<String>();
496
497 for (File file : files) {
498 relativePaths.add(getRelativePath(parentDirectory, file));
499 }
500
501 return relativePaths;
502 }
503
504
505
506
507
508
509
510
511 public static String getRelativePath(File parentDirectory, File file) {
512 String relativePath = null;
513
514 String parentPath = parentDirectory.getPath();
515 String childPath = file.getPath();
516
517 if (childPath.startsWith(parentPath + File.separator)) {
518 relativePath = childPath.substring(parentPath.length() + 1);
519 }
520
521
522 relativePath = relativePath.replaceAll("\\\\", "/");
523
524 return relativePath;
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 public static String calculatePathToFile(File fromFile, File toFile) {
541 String pathToFile = "";
542
543 int directoriesUp = 0;
544 String parentPath = fromFile.getParent();
545
546 while ((parentPath != null) && !fileMatchesPath(parentPath, toFile)) {
547 File parent = new File(parentPath);
548
549 parentPath = parent.getParent();
550 directoriesUp += 1;
551 }
552
553 if (parentPath != null) {
554 for (int i = 0; i < directoriesUp; i++) {
555 pathToFile += "../";
556 }
557
558 String remainingPath = toFile.getPath().replace(parentPath, "");
559
560 if (remainingPath.startsWith(File.separator)) {
561 remainingPath = remainingPath.substring(1);
562 }
563
564
565 remainingPath = remainingPath.replaceAll("\\\\", "/");
566
567
568 if (remainingPath.contains("/")) {
569 int separatorIndex = remainingPath.lastIndexOf("/");
570 remainingPath = remainingPath.substring(0, separatorIndex + 1);
571 } else {
572
573 remainingPath = null;
574 }
575
576 if (remainingPath != null) {
577 pathToFile += remainingPath;
578 }
579 }
580
581 return pathToFile;
582 }
583
584
585
586
587
588
589
590
591
592 protected static boolean fileMatchesPath(String path, File file) {
593 return file.getPath().startsWith(path);
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607 public static List<File> orderFiles(List<File> pluginFiles, List<File> subDirFiles, List<String> loadFirstPatterns,
608 List<String> loadLastPatterns, List<String> pluginLoadOrder, List<String> subDirLoadOrder) {
609 List<File> orderedFiles = new ArrayList<File>();
610
611 List<File> allThemeFiles = new ArrayList<File>();
612 if (pluginFiles != null) {
613 allThemeFiles.addAll(pluginFiles);
614 }
615
616 if (subDirFiles != null) {
617 allThemeFiles.addAll(subDirFiles);
618 }
619
620
621 List<File> endFiles = new ArrayList<File>();
622
623 if (loadLastPatterns != null) {
624 for (String pattern : loadLastPatterns) {
625 endFiles.addAll(matchFiles(allThemeFiles, pattern));
626 }
627 }
628
629
630 if (loadFirstPatterns != null) {
631 for (String pattern : loadFirstPatterns) {
632 List<File> matchedFiles = matchFiles(allThemeFiles, pattern);
633 matchedFiles.removeAll(endFiles);
634
635 orderedFiles.addAll(matchedFiles);
636 }
637 }
638
639
640 if (pluginLoadOrder != null) {
641 for (String pattern : pluginLoadOrder) {
642 List<File> matchedFiles = matchFiles(pluginFiles, pattern);
643 matchedFiles.removeAll(endFiles);
644 matchedFiles.removeAll(orderedFiles);
645
646 orderedFiles.addAll(matchedFiles);
647 }
648 }
649
650
651 if (pluginFiles != null) {
652 for (File pluginFile : pluginFiles) {
653 if (!orderedFiles.contains(pluginFile) && !endFiles.contains(pluginFile)) {
654 orderedFiles.add(pluginFile);
655 }
656 }
657 }
658
659
660 if (subDirLoadOrder != null) {
661 for (String pattern : subDirLoadOrder) {
662 List<File> matchedFiles = matchFiles(subDirFiles, pattern);
663 matchedFiles.removeAll(endFiles);
664 matchedFiles.removeAll(orderedFiles);
665
666 orderedFiles.addAll(matchedFiles);
667 }
668 }
669
670
671 if (subDirFiles != null) {
672 for (File subDirFile : subDirFiles) {
673 if (!orderedFiles.contains(subDirFile) && !endFiles.contains(subDirFile)) {
674 orderedFiles.add(subDirFile);
675 }
676 }
677 }
678
679
680 File[] endFileArray = new File[endFiles.size()];
681 endFileArray = endFiles.toArray(endFileArray);
682
683 for (int i = endFileArray.length - 1; i >= 0; i--) {
684 orderedFiles.add(endFileArray[i]);
685 }
686
687 return orderedFiles;
688 }
689
690
691
692
693
694
695
696
697 public static List<File> matchFiles(List<File> filesToMatch, String pattern) {
698 List<File> matchedFiles = new ArrayList<File>();
699
700 for (File file : filesToMatch) {
701 if (isMatch(file, pattern)) {
702 matchedFiles.add(file);
703 }
704 }
705
706 return matchedFiles;
707 }
708
709
710
711
712
713
714
715
716
717 public static boolean isMatch(File file, String pattern) {
718 boolean isMatch = false;
719
720 String fileBasename = FileUtils.basename(file.getName());
721 if (fileBasename.endsWith(".")) {
722 fileBasename = fileBasename.substring(0, fileBasename.length() - 1);
723 }
724
725 if (SelectorUtils.matchPath(pattern, fileBasename, false)) {
726 isMatch = true;
727 }
728
729 return isMatch;
730 }
731
732
733
734
735
736
737
738
739
740 public static List<File> getContainedFiles(List<File> files, List<File> directories) {
741 List<File> directoryFiles = new ArrayList<File>();
742
743 for (File directory : directories) {
744 for (File file : files) {
745 if (ThemeBuilderUtils.directoryContainsFile(directory, file.getName())) {
746 directoryFiles.add(file);
747 }
748 }
749 }
750
751 return directoryFiles;
752 }
753
754
755
756
757
758
759
760
761
762 public static String[] addToArray(String[] array, String stringToAdd) {
763 String[] arrayToAdd = null;
764
765 if (stringToAdd != null) {
766 arrayToAdd = new String[1];
767 arrayToAdd[0] = stringToAdd;
768 }
769
770 return addToArray(array, arrayToAdd);
771 }
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786 public static String[] addToArray(String[] array, String[] arrayToAdd) {
787 if (array == null) {
788 return arrayToAdd;
789 } else if (arrayToAdd == null) {
790 return array;
791 }
792
793 int combinedArrayLength = array.length + arrayToAdd.length;
794
795 String[] combinedArray = new String[combinedArrayLength];
796
797 for (int i = 0; i < array.length; i++) {
798 combinedArray[i] = array[i];
799 }
800
801 for (int i = 0; i < arrayToAdd.length; i++) {
802 combinedArray[i + array.length] = arrayToAdd[i];
803 }
804
805 return combinedArray;
806 }
807
808
809
810
811
812
813
814 public static String joinFileList(List<File> list) {
815 String joinedString = null;
816
817 if (list != null) {
818 joinedString = "";
819
820 for (File file : list) {
821 if (!"".equals(joinedString)) {
822 joinedString += ",";
823 }
824
825 joinedString += file.getName();
826 }
827 }
828
829 return joinedString;
830 }
831
832 }