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