VelocityWhitespaceFilter.java

  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. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import org.apache.commons.io.FilenameUtils;
  22. import org.apache.maven.shared.filtering.DefaultMavenFileFilter;
  23. import org.apache.maven.shared.filtering.MavenFileFilter;
  24. import org.apache.maven.shared.filtering.MavenFilteringException;
  25. import org.apache.maven.shared.utils.io.FileUtils;
  26. import org.codehaus.plexus.component.annotations.Component;

  27. /**
  28.  * Simple resource filter that is used to remove excess whitespace from Velocity
  29.  * Templates using standard Maven resource filtering. Leading whitespace is
  30.  * removed from lines and a trailing Velocity comment (##) is appended to each
  31.  * line to swallow the new line from the resulting output.
  32.  *
  33.  * @author Jeremy Long
  34.  */
  35. @Component(role = MavenFileFilter.class, hint = "default")
  36. public class VelocityWhitespaceFilter extends DefaultMavenFileFilter {

  37.     /**
  38.      * The extensions that are supported.
  39.      */
  40.     private final List<String> extensions = Arrays.asList("vm", "vtl", "vsl");

  41.     /**
  42.      * Whether or not the given file should be filtered.
  43.      *
  44.      * @param from the file to test
  45.      * @return true if this filter should transform the file, otherwise false
  46.      */
  47.     protected boolean shouldFilter(File from) {
  48.         if (from == null) {
  49.             return false;
  50.         }
  51.         final String ext = FilenameUtils.getExtension(from.getName());
  52.         return extensions.contains(ext.toLowerCase());
  53.     }

  54.     /**
  55.      * {@inheritDoc} Copies the given file using the
  56.      * {@link org.owasp.maven.tools.VelocityWhitespaceFilteringReader} so that
  57.      * the Velocity Template copied will output less whitespace.
  58.      */
  59.     @Override
  60.     public void copyFile(File from, File to, boolean filtering, List<FileUtils.FilterWrapper> filterWrappers,
  61.             String encoding, boolean overwrite) throws MavenFilteringException {
  62.         List<FileUtils.FilterWrapper> wrappers = filterWrappers;
  63.         if (filtering && shouldFilter(from)) {
  64.             wrappers = new ArrayList<>(filterWrappers);
  65.             wrappers.add(new VelocityWhitespaceFilterWrapper());
  66.         }
  67.         super.copyFile(from, to, filtering, wrappers, encoding, overwrite);
  68.     }
  69. }