1 /**
2 * This file Copyright (c) 2015-2018 Magnolia International
3 * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4 *
5 *
6 * This file is dual-licensed under both the Magnolia
7 * Network Agreement and the GNU General Public License.
8 * You may elect to use one or the other of these licenses.
9 *
10 * This file is distributed in the hope that it will be
11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14 * Redistribution, except as permitted by whichever of the GPL
15 * or MNA you select, is prohibited.
16 *
17 * 1. For the GPL license (GPL), you can redistribute and/or
18 * modify this file under the terms of the GNU General
19 * Public License, Version 3, as published by the Free Software
20 * Foundation. You should have received a copy of the GNU
21 * General Public License, Version 3 along with this program;
22 * if not, write to the Free Software Foundation, Inc., 51
23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 * 2. For the Magnolia Network Agreement (MNA), this file
26 * and the accompanying materials are made available under the
27 * terms of the MNA which accompanies this distribution, and
28 * is available at http://www.magnolia-cms.com/mna.html
29 *
30 * Any modifications to this file must keep this entire header
31 * intact.
32 *
33 */
34 package info.magnolia.resourceloader.util;
35
36 import info.magnolia.resourceloader.Resource;
37 import info.magnolia.resourceloader.ResourceVisitor;
38
39 import java.util.function.Function;
40 import java.util.function.Predicate;
41
42 /**
43 * A {@link ResourceVisitor} constructed with {@link Predicate}s and a {@link Function} to determine its behavior.
44 */
45 public class PredicatedResourceVisitor implements ResourceVisitor {
46 /**
47 * Visits everything and applies the given {@link Function} to everything.
48 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
49 * @see VoidFunction
50 */
51 public static PredicatedResourceVisitor with(Function<Resource, Void> function) {
52 return with(dir -> true, dir -> true, file -> true, function);
53 }
54
55 /**
56 * Visits according to the given {@code visitAndApply} predicate and applies the given {@link Function} according to {@code visitAndApply}.
57 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
58 * @see VoidFunction
59 *
60 * TODO: visitAndApply is used for both visiting and applying, so e.g pattern /foo/.*\.text won't match anything because "/foo" doesn't match the expression
61 */
62 public static PredicatedResourceVisitor with(Predicate<Resource> visitAndApply, Function<Resource, Void> function) {
63 return with(visitAndApply, visitAndApply, visitAndApply, function);
64 }
65
66 /**
67 * Visits according to the given {@code visitDirectory} predicate and applies the given {@link Function} according to {@code applyFunction}.
68 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
69 * @see VoidFunction
70 */
71 public static PredicatedResourceVisitor with(Predicate<Resource> visitDirectory, Predicate<Resource> applyFunction, Function<Resource, Void> function) {
72 return new PredicatedResourceVisitor(visitDirectory, applyFunction, applyFunction, function);
73 }
74
75 /**
76 * Visits according to the given {@code visitDirectory} predicate and applies the given {@link Function} according to {@code applyToDirectory} and {@code applyToFile} depending on the resource's type.
77 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
78 * @see VoidFunction
79 */
80 public static PredicatedResourceVisitor with(Predicate<Resource> visitDirectory, Predicate<Resource> applyToDirectory, Predicate<Resource> applyToFile, Function<Resource, Void> function) {
81 return new PredicatedResourceVisitor(visitDirectory, applyToDirectory, applyToFile, function);
82 }
83
84 /**
85 * Traverses all directories, and applies the given {@link Function} to all files, according to {@code applyToFile}. Does not apply the function to directory resources.
86 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
87 * @see VoidFunction
88 */
89 public static PredicatedResourceVisitor onAllMatchingFiles(Predicate<Resource> applyToFile, Function<Resource, Void> function) {
90 return new PredicatedResourceVisitor(dir -> true, dir -> false, applyToFile, function);
91 }
92
93 private final Predicate<Resource> visitDirectory;
94 private final Predicate<Resource> applyToDirectory;
95 private final Predicate<Resource> applyToFile;
96 private final Function<Resource, Void> function;
97
98 protected PredicatedResourceVisitor(Predicate<Resource> visitDirectory, Predicate<Resource> applyToDirectory, Predicate<Resource> applyToFile, Function<Resource, Void> function) {
99 // Always visit root so `visitDirectory` can be e.g pathStartsWith("/foo")
100 Predicate<Resource> isRootPath = dir -> "/".equals(dir.getPath());
101 this.visitDirectory = isRootPath.or(visitDirectory);
102 this.applyToDirectory = applyToDirectory;
103 this.applyToFile = applyToFile;
104 this.function = function;
105 }
106
107 @Override
108 public boolean visitDirectory(Resource resource) {
109 if (applyToDirectory.test(resource)) {
110 function.apply(resource);
111 }
112 return visitDirectory.test(resource);
113 }
114
115 @Override
116 public void visitFile(Resource resource) {
117 if (applyToFile.test(resource)) {
118 function.apply(resource);
119 }
120 }
121
122 /**
123 * Can be useful to e.g. retrieve resources collected by the function.
124 */
125 protected Function<Resource, Void> getFunction() {
126 return function;
127 }
128 }