View Javadoc
1   /*
2    * Copyright 2018 Jeremy Long.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.owasp.maven.tools;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  import org.apache.commons.io.FilenameUtils;
23  import org.apache.maven.shared.filtering.DefaultMavenFileFilter;
24  import org.apache.maven.shared.filtering.MavenFileFilter;
25  import org.apache.maven.shared.filtering.MavenFilteringException;
26  import org.apache.maven.shared.utils.io.FileUtils;
27  import org.codehaus.plexus.component.annotations.Component;
28  
29  /**
30   * Simple resource filter that is used to remove excess whitespace from Velocity
31   * Templates using standard Maven resource filtering. Leading whitespace is
32   * removed from lines and a trailing Velocity comment (##) is appended to each
33   * line to swallow the new line from the resulting output.
34   *
35   * @author Jeremy Long
36   */
37  @Component(role = MavenFileFilter.class, hint = "default")
38  public class VelocityWhitespaceFilter extends DefaultMavenFileFilter {
39  
40      /**
41       * The extensions that are supported.
42       */
43      private final List<String> extensions = Arrays.asList("vm", "vtl", "vsl");
44  
45      /**
46       * Whether or not the given file should be filtered.
47       *
48       * @param from the file to test
49       * @return true if this filter should transform the file, otherwise false
50       */
51      protected boolean shouldFilter(File from) {
52          if (from == null) {
53              return false;
54          }
55          final String ext = FilenameUtils.getExtension(from.getName());
56          return extensions.contains(ext.toLowerCase());
57      }
58  
59      /**
60       * {@inheritDoc} Copies the given file using the
61       * {@link org.owasp.maven.tools.VelocityWhitespaceFilteringReader} so that
62       * the Velocity Template copied will output less whitespace.
63       */
64      @Override
65      public void copyFile(File from, File to, boolean filtering, List<FileUtils.FilterWrapper> filterWrappers,
66              String encoding, boolean overwrite) throws MavenFilteringException {
67          List<FileUtils.FilterWrapper> wrappers = filterWrappers;
68          if (filtering && shouldFilter(from)) {
69              wrappers = new ArrayList<>(filterWrappers);
70              wrappers.add(new VelocityWhitespaceFilterWrapper());
71          }
72          super.copyFile(from, to, filtering, wrappers, encoding, overwrite);
73      }
74  }