1 /**
2 * This file Copyright (c) 2015-2016 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 static info.magnolia.resourceloader.util.Functions.always;
37
38 import info.magnolia.resourceloader.Resource;
39 import info.magnolia.resourceloader.ResourceVisitor;
40
41 import com.google.common.base.Function;
42 import com.google.common.base.Predicate;
43 import com.google.common.base.Predicates;
44
45 /**
46 * A {@link ResourceVisitor} constructed with {@link Predicate}s and a {@link Function} to determine its behavior.
47 */
48 public class PredicatedResourceVisitor implements ResourceVisitor {
49 /**
50 * Visits everything and applies the given {@link Function} to everything.
51 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
52 * @see VoidFunction
53 */
54 public static PredicatedResourceVisitor with(Function<Resource, Void> function) {
55 return with(always(), always(), always(), function);
56 }
57
58 /**
59 * Visits according to the given {@code visitAndApply} predicate and applies the given {@link Function} according to {@code visitAndApply}.
60 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
61 * @see VoidFunction
62 *
63 * 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
64 */
65 public static PredicatedResourceVisitor with(Predicate<Resource> visitAndApply, Function<Resource, Void> function) {
66 return with(visitAndApply, visitAndApply, visitAndApply, function);
67 }
68
69 /**
70 * Visits according to the given {@code visitDirectory} predicate and applies the given {@link Function} according to {@code applyFunction}.
71 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
72 * @see VoidFunction
73 */
74 public static PredicatedResourceVisitor with(Predicate<Resource> visitDirectory, Predicate<Resource> applyFunction, Function<Resource, Void> function) {
75 return new PredicatedResourceVisitor(visitDirectory, applyFunction, applyFunction, function);
76 }
77
78 /**
79 * 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.
80 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
81 * @see VoidFunction
82 */
83 public static PredicatedResourceVisitor with(Predicate<Resource> visitDirectory, Predicate<Resource> applyToDirectory, Predicate<Resource> applyToFile, Function<Resource, Void> function) {
84 return new PredicatedResourceVisitor(visitDirectory, applyToDirectory, applyToFile, function);
85 }
86
87 /**
88 * Traverses all directories, and applies the given {@link Function} to all files, according to {@code applyToFile}. Does not apply the function to directory resources.
89 * @see Functions which provides a couple of helpful predicates and functions to use with Resource.
90 * @see VoidFunction
91 */
92 public static PredicatedResourceVisitor onAllMatchingFiles(Predicate<Resource> applyToFile, Function<Resource, Void> function) {
93 return new PredicatedResourceVisitor(Functions.always(), Functions.never(), applyToFile, function);
94 }
95
96 private final Predicate<Resource> visitDirectory;
97 private final Predicate<Resource> applyToDirectory;
98 private final Predicate<Resource> applyToFile;
99 private final Function<Resource, Void> function;
100
101 protected PredicatedResourceVisitor(Predicate<Resource> visitDirectory, Predicate<Resource> applyToDirectory, Predicate<Resource> applyToFile, Function<Resource, Void> function) {
102 // Always visit root so `visitDirectory` can be e.g pathStartsWith("/foo")
103 this.visitDirectory = Predicates.or(Functions.pathEquals("/"), visitDirectory);
104 this.applyToDirectory = applyToDirectory;
105 this.applyToFile = applyToFile;
106 this.function = function;
107 }
108
109 @Override
110 public boolean visitDirectory(Resource resource) {
111 if (applyToDirectory.apply(resource)) {
112 function.apply(resource);
113 }
114 return visitDirectory.apply(resource);
115 }
116
117 @Override
118 public void visitFile(Resource resource) {
119 if (applyToFile.apply(resource)) {
120 function.apply(resource);
121 }
122 }
123
124 /**
125 * Can be useful to e.g. retrieve resources collected by the function.
126 */
127 protected Function<Resource, Void> getFunction() {
128 return function;
129 }
130 }