View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.filtering;
20  
21  import java.util.ArrayList;
22  import java.util.LinkedHashSet;
23  import java.util.List;
24  import java.util.Properties;
25  import java.util.function.Consumer;
26  
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.interpolation.Interpolator;
30  
31  /**
32   * @since 1.0-beta-3
33   */
34  public class AbstractMavenFilteringRequest {
35  
36      private MavenProject mavenProject;
37  
38      private List<String> filters;
39  
40      private boolean escapeWindowsPaths = true;
41  
42      private MavenSession mavenSession;
43  
44      /**
45       * List of Strings considered as expressions which contains values in the project/pom: pom project Default value
46       * will be pom and project.
47       *
48       * @since 1.0-beta-2
49       */
50      private List<String> projectStartExpressions = new ArrayList<>();
51  
52      /**
53       * String which will escape interpolation mechanism: foo \${foo.bar} -> foo ${foo.bar}
54       *
55       * @since 1.0-beta-2
56       */
57      private String escapeString;
58  
59      /**
60       * @since 1.0-beta-3
61       */
62      private Properties additionalProperties;
63  
64      /**
65       * @since 1.0-beta-3
66       */
67      private boolean injectProjectBuildFilters = false;
68  
69      /**
70       * Set of expression delimiter specifications to use during filtering. Delimiter specifications are given in the
71       * form 'BEGIN*END' or, for symmetrical delimiters, simply 'TOKEN'. The default values are '${*}' and '@'.
72       *
73       * @since 1.0-beta-3
74       */
75      private LinkedHashSet<String> delimiters = new LinkedHashSet<>();
76  
77      /**
78       * Do not stop trying to filter tokens when reaching EOL.
79       *
80       * @since 1.0
81       */
82      private boolean supportMultiLineFiltering;
83  
84      private Consumer<Interpolator> interpolatorCustomizer;
85  
86      /**
87       * Create instance.
88       */
89      protected AbstractMavenFilteringRequest() {
90          initDefaults();
91      }
92  
93      /**
94       * Create instance with given parameters
95       *
96       * @param mavenProject The instance of MavenProject.
97       * @param filters The list of filters.
98       * @param mavenSession The MavenSession.
99       */
100     protected AbstractMavenFilteringRequest(
101             MavenProject mavenProject, List<String> filters, MavenSession mavenSession) {
102         initDefaults();
103         this.mavenProject = mavenProject;
104         this.filters = filters;
105         this.mavenSession = mavenSession;
106     }
107 
108     private void initDefaults() {
109         projectStartExpressions.add("pom");
110         projectStartExpressions.add("project");
111 
112         delimiters.add("${*}");
113         delimiters.add("@");
114     }
115 
116     /**
117      * @return The MavenProject
118      */
119     public MavenProject getMavenProject() {
120         return mavenProject;
121     }
122 
123     /**
124      * Set the MavenProject.
125      *
126      * @param mavenProject The MavenProject to be set.
127      */
128     public void setMavenProject(MavenProject mavenProject) {
129         this.mavenProject = mavenProject;
130     }
131 
132     /**
133      * The list of filters.
134      *
135      * @return The list of currently set filters.
136      */
137     public List<String> getFilters() {
138         return filters;
139     }
140 
141     /**
142      * Set the filters.
143      *
144      * @param filters Set the list of filters
145      */
146     public void setFilters(List<String> filters) {
147         this.filters = filters;
148     }
149 
150     /**
151      * Alias for {@link #getFilters()}.
152      *
153      * @return The list of filters.
154      */
155     public List<String> getFileFilters() {
156         return getFilters();
157     }
158 
159     /**
160      * Alias for {@link #setFilters(List)}
161      *
162      * @param paramfilters The list of filters to be set.
163      */
164     public void setFileFilters(List<String> paramfilters) {
165         setFilters(paramfilters);
166     }
167 
168     /**
169      * @since 1.0-beta-3
170      * @return true if escape is activated false otherwise.
171      */
172     public boolean isEscapeWindowsPaths() {
173         return escapeWindowsPaths;
174     }
175 
176     /**
177      * @since 1.0-beta-3
178      * @param escapedBackslashesInFilePath true or false.
179      */
180     public void setEscapeWindowsPaths(boolean escapedBackslashesInFilePath) {
181         this.escapeWindowsPaths = escapedBackslashesInFilePath;
182     }
183 
184     /**
185      * Alias for {@link #isEscapeWindowsPaths()}
186      *
187      * @return The current value of {@link #isEscapeWindowsPaths()}
188      */
189     public boolean isEscapedBackslashesInFilePath() {
190         return isEscapeWindowsPaths();
191     }
192 
193     /**
194      * Alias for {@link #setEscapeWindowsPaths(boolean)}
195      *
196      * @param escape activate or deactivate escaping.
197      */
198     public void setEscapedBackslashesInFilePath(boolean escape) {
199         setEscapeWindowsPaths(escape);
200     }
201 
202     /**
203      * @return Current value of mavenSession
204      */
205     public MavenSession getMavenSession() {
206         return mavenSession;
207     }
208 
209     /**
210      * @param mavenSession Set new value for the MavenSession of the instance.
211      */
212     public void setMavenSession(MavenSession mavenSession) {
213         this.mavenSession = mavenSession;
214     }
215 
216     /**
217      * @return the additional properties.
218      * @since 1.0-beta-3
219      */
220     public Properties getAdditionalProperties() {
221         return additionalProperties;
222     }
223 
224     /**
225      * @param additionalProperties The additional properties to be set.
226      * @since 1.0-beta-3
227      */
228     public void setAdditionalProperties(Properties additionalProperties) {
229         this.additionalProperties = additionalProperties;
230     }
231 
232     /**
233      * @return the current value of injectProjectBuildFilters.
234      * @since 1.0-beta-3
235      */
236     public boolean isInjectProjectBuildFilters() {
237         return injectProjectBuildFilters;
238     }
239 
240     /**
241      * @param injectProjectBuildFilters true or false.
242      * @since 1.0-beta-3
243      */
244     public void setInjectProjectBuildFilters(boolean injectProjectBuildFilters) {
245         this.injectProjectBuildFilters = injectProjectBuildFilters;
246     }
247 
248     /**
249      * @return Current value of escapeString.
250      * @since 1.0-beta-2
251      */
252     public String getEscapeString() {
253         return escapeString;
254     }
255 
256     /**
257      * @param escapeString The escape string to use
258      * @since 1.0-beta-2
259      */
260     public void setEscapeString(String escapeString) {
261         this.escapeString = escapeString;
262     }
263 
264     /**
265      * @return The list of project start expressions.
266      * @since 1.0-beta-2
267      */
268     public List<String> getProjectStartExpressions() {
269         return projectStartExpressions;
270     }
271 
272     /**
273      * @param projectStartExpressions The start expressions
274      * @since 1.0-beta-2
275      */
276     public void setProjectStartExpressions(List<String> projectStartExpressions) {
277         this.projectStartExpressions = projectStartExpressions;
278     }
279 
280     /**
281      * See {@link AbstractMavenFilteringRequest#delimiters} for more information and default values.
282      *
283      * @return Not allowed to be <code>null</code> or empty.
284      * @since 1.0-beta-3
285      */
286     public LinkedHashSet<String> getDelimiters() {
287         return delimiters;
288     }
289 
290     /**
291      * Set the delimiter specifications to use during filtering. Specifications should be of the form: 'BEGIN*END' for
292      * asymmetrical delimiters, or 'TOKEN' for symmetrical delimiters. See
293      * {@link AbstractMavenFilteringRequest#delimiters} for more information and default values.
294      *
295      * @param delimiters If <code>null</code>, reset delimiters to '${*}' only. Otherwise, use the provided value.
296      * @since 1.0-beta-3
297      */
298     public void setDelimiters(LinkedHashSet<String> delimiters) {
299         if (delimiters == null || delimiters.isEmpty()) {
300             this.delimiters.clear();
301             this.delimiters.add("${*}");
302         } else {
303             this.delimiters = delimiters;
304         }
305     }
306 
307     /**
308      * @param delimiters If {@code null} than nothing will happen. If not {@code null} the delimiters will be set
309      *            according to the contents. If delimiter entries are {@code null} those entries will be set to '${*}'.
310      * @param useDefaultDelimiters true if the default delimiters will be used false otherwise.
311      */
312     public void setDelimiters(LinkedHashSet<String> delimiters, boolean useDefaultDelimiters) {
313         if (delimiters != null && !delimiters.isEmpty()) {
314             LinkedHashSet<String> delims = new LinkedHashSet<>();
315             if (useDefaultDelimiters) {
316                 delims.addAll(this.getDelimiters());
317             }
318 
319             for (String delim : delimiters) {
320                 if (delim == null) {
321                     // FIXME: ${filter:*} could also trigger this condition. Need a better long-term solution.
322                     delims.add("${*}");
323                 } else {
324                     delims.add(delim);
325                 }
326             }
327 
328             this.setDelimiters(delims);
329         }
330     }
331 
332     /**
333      * @return If support multiple line filtering is active or not.
334      */
335     public boolean isSupportMultiLineFiltering() {
336         return supportMultiLineFiltering;
337     }
338 
339     /**
340      * @param supportMultiLineFiltering activate or deactivate multiple line filtering support.
341      */
342     public void setSupportMultiLineFiltering(boolean supportMultiLineFiltering) {
343         this.supportMultiLineFiltering = supportMultiLineFiltering;
344     }
345 
346     /**
347      *
348      * @return the customizer which is supposed to be used by filters creating an {@link Interpolator} like those based on {@link BaseFilter}.
349      * @since 3.4.0
350      */
351     public Consumer<Interpolator> getInterpolatorCustomizer() {
352         return interpolatorCustomizer;
353     }
354 
355     /**
356      *
357      * @param interpolatorCustomizer the customizer which is supposed to be used by filters creating an {@link Interpolator} like those based on {@link BaseFilter}.
358      * @since 3.4.0
359      */
360     public void setInterpolatorCustomizer(Consumer<Interpolator> interpolatorCustomizer) {
361         this.interpolatorCustomizer = interpolatorCustomizer;
362     }
363 }