1. /*
  2. * Copyright 2002-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.condition;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.util.FileUtils;
  22. /**
  23. * Compares two files for bitwise equality based on size and
  24. * content. Timestamps are not at all looked at.
  25. *
  26. * @version $Revision: 1.12.2.4 $
  27. * @since Ant 1.5
  28. */
  29. public class FilesMatch implements Condition {
  30. /**
  31. * files to compare
  32. */
  33. private File file1, file2;
  34. /**
  35. * Helper that provides the file comparison method.
  36. */
  37. private FileUtils fu = FileUtils.newFileUtils();
  38. /**
  39. * Sets the File1 attribute
  40. *
  41. * @param file1 The new File1 value
  42. */
  43. public void setFile1(File file1) {
  44. this.file1 = file1;
  45. }
  46. /**
  47. * Sets the File2 attribute
  48. *
  49. * @param file2 The new File2 value
  50. */
  51. public void setFile2(File file2) {
  52. this.file2 = file2;
  53. }
  54. /**
  55. * comparison method of the interface
  56. *
  57. * @return true if the files are equal
  58. * @exception BuildException if it all went pear-shaped
  59. */
  60. public boolean eval()
  61. throws BuildException {
  62. //validate
  63. if (file1 == null || file2 == null) {
  64. throw new BuildException("both file1 and file2 are required in "
  65. + "filesmatch");
  66. }
  67. //#now match the files
  68. boolean matches = false;
  69. try {
  70. matches = fu.contentEquals(file1, file2);
  71. } catch (IOException ioe) {
  72. throw new BuildException("when comparing files: "
  73. + ioe.getMessage(), ioe);
  74. }
  75. return matches;
  76. }
  77. }