1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  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. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.Task;
  22. /**
  23. * Sets a token filter that is used by the file copy tasks
  24. * to do token substitution. Sets multiple tokens by
  25. * reading these from a file.
  26. *
  27. * @since Ant 1.1
  28. *
  29. * @ant.task category="filesystem"
  30. */
  31. public class Filter extends Task {
  32. private String token;
  33. private String value;
  34. private File filtersFile;
  35. /**
  36. * The token string without @ delimiters.
  37. * @param token token to set
  38. */
  39. public void setToken(String token) {
  40. this.token = token;
  41. }
  42. /**
  43. * The string that should replace the token during filtered copies.
  44. * @param value token replace value
  45. */
  46. public void setValue(String value) {
  47. this.value = value;
  48. }
  49. /**
  50. * The file from which the filters must be read.
  51. * This file must be a formatted as a property file.
  52. *
  53. * @param filtersFile filter file
  54. */
  55. public void setFiltersfile(File filtersFile) {
  56. this.filtersFile = filtersFile;
  57. }
  58. public void execute() throws BuildException {
  59. boolean isFiltersFromFile =
  60. filtersFile != null && token == null && value == null;
  61. boolean isSingleFilter =
  62. filtersFile == null && token != null && value != null;
  63. if (!isFiltersFromFile && !isSingleFilter) {
  64. throw new BuildException("both token and value parameters, or "
  65. + "only a filtersFile parameter is "
  66. + "required", getLocation());
  67. }
  68. if (isSingleFilter) {
  69. getProject().getGlobalFilterSet().addFilter(token, value);
  70. }
  71. if (isFiltersFromFile) {
  72. readFilters();
  73. }
  74. }
  75. protected void readFilters() throws BuildException {
  76. log("Reading filters from " + filtersFile, Project.MSG_VERBOSE);
  77. getProject().getGlobalFilterSet().readFiltersFromFile(filtersFile);
  78. }
  79. }