/**
   * @param from
   * @param to
   * @param encoding
   * @param wrappers
   * @param filterProperties
   * @throws IOException TO DO: Remove this method when Maven moves to plexus-utils version 1.4
   */
  private static void copyFilteredFile(
      File from, File to, String encoding, FilterWrapper[] wrappers, Properties filterProperties)
      throws IOException {
    // buffer so it isn't reading a byte at a time!
    Reader fileReader = null;
    Writer fileWriter = null;
    try {
      // fix for MWAR-36, ensures that the parent dir are created first
      to.getParentFile().mkdirs();

      if (encoding == null || encoding.length() < 1) {
        fileReader = new BufferedReader(new FileReader(from));
        fileWriter = new FileWriter(to);
      } else {
        FileInputStream instream = new FileInputStream(from);

        FileOutputStream outstream = new FileOutputStream(to);

        fileReader = new BufferedReader(new InputStreamReader(instream, encoding));

        fileWriter = new OutputStreamWriter(outstream, encoding);
      }

      Reader reader = fileReader;
      for (FilterWrapper wrapper : wrappers) {
        reader = wrapper.getReader(reader, filterProperties);
      }

      IOUtil.copy(reader, fileWriter);
    } finally {
      IOUtil.close(fileReader);
      IOUtil.close(fileWriter);
    }
  }
  /**
   * Copy the specified file if the target location has not yet already been used and filter its
   * content with the configureed filter properties.
   *
   * <p>The <tt>targetFileName</tt> is the relative path according to the root of the generated web
   * application.
   *
   * @param sourceId the source id
   * @param context the context to use
   * @param file the file to copy
   * @param targetFilename the relative path according to the root of the webapp
   * @return true if the file has been copied, false otherwise
   * @throws IOException if an error occured while copying
   * @throws MojoExecutionException if an error occured while retrieving the filter properties
   */
  protected boolean copyFilteredFile(
      String sourceId, AmpPackagingContext context, File file, String targetFilename)
      throws IOException, MojoExecutionException {

    if (context.getAmpStructure().registerFile(sourceId, targetFilename)) {
      final File targetFile = new File(context.getAmpDirectory(), targetFilename);
      // buffer so it isn't reading a byte at a time!
      Reader fileReader = null;
      Writer fileWriter = null;
      try {
        // fix for MWAR-36, ensures that the parent dir are created first
        targetFile.getParentFile().mkdirs();

        fileReader = new BufferedReader(new FileReader(file));
        fileWriter = new FileWriter(targetFile);

        Reader reader = fileReader;
        for (int i = 0; i < getFilterWrappers().length; i++) {
          FilterWrapper wrapper = getFilterWrappers()[i];
          reader = wrapper.getReader(reader, context.getFilterProperties());
        }

        IOUtil.copy(reader, fileWriter);
      } finally {
        IOUtil.close(fileReader);
        IOUtil.close(fileWriter);
      }
      // Add the file to the protected list
      context.getLog().debug(" + " + targetFilename + " has been copied.");
      return true;
    } else {
      context
          .getLog()
          .debug(" - " + targetFilename + " wasn't copied because it has already been packaged.");
      return false;
    }
  }