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