You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValidFileFilter.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2000 Xerox Corporation.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing.util;
  13. import java.io.File;
  14. import java.io.FileFilter;
  15. /**
  16. * FileFilter that accepts existing files
  17. * with static singleton variants
  18. * made from inner subclasses.
  19. */
  20. public class ValidFileFilter implements FileFilter {
  21. //----------------------------- singleton variants
  22. public static final FileFilter EXIST = new ValidFileFilter();
  23. public static final FileFilter FILE_EXISTS = new FilesOnlyFilter();
  24. public static final FileFilter DIR_EXISTS = new DirsOnlyFilter();
  25. public static final FileFilter CLASS_FILE = new ClassOnlyFilter();
  26. public static final FileFilter JAVA_FILE = new JavaOnlyFilter();
  27. public static final FileFilter RESOURCE = new ResourcesOnlyFilter();
  28. //----------------------------- members
  29. protected final FileFilter delegate;
  30. protected ValidFileFilter(){
  31. this(null);
  32. }
  33. protected ValidFileFilter(FileFilter delegate){
  34. this.delegate = delegate;
  35. }
  36. /**
  37. * Implement <code>FileFilter.accept(File)</code> by checking
  38. * taht input is not null, exists, and is accepted by any delegate.
  39. */
  40. public boolean accept(File f) {
  41. return ((null != f) && (f.exists())
  42. && ((null == delegate) || delegate.accept(f)));
  43. }
  44. //----------------------------- inner subclasses
  45. static class FilesOnlyFilter extends ValidFileFilter {
  46. public boolean accept(File f) {
  47. return (super.accept(f) && (!f.isDirectory()));
  48. }
  49. }
  50. static class ResourcesOnlyFilter extends FilesOnlyFilter {
  51. public boolean accept(File f) {
  52. return (super.accept(f) && (FileUtil.isResourcePath(f.getPath())));
  53. }
  54. }
  55. static class DirsOnlyFilter extends ValidFileFilter {
  56. public final boolean accept(File f) {
  57. return (super.accept(f) && (f.isDirectory()));
  58. }
  59. }
  60. // todo: StringsFileFilter, accepts String[] variants for each
  61. static class StringFileFilter extends ValidFileFilter {
  62. public static final boolean IGNORE_CASE = true;
  63. protected final String prefix;
  64. protected final String substring;
  65. protected final String suffix;
  66. protected final boolean ignoreCase;
  67. /** true if one of the String specifiers is not null */
  68. protected final boolean haveSpecifier;
  69. public StringFileFilter(String prefix, String substring,
  70. String suffix, boolean ignoreCase) {
  71. this.ignoreCase = ignoreCase;
  72. this.prefix = preprocess(prefix);
  73. this.substring = preprocess(substring);
  74. this.suffix = preprocess(suffix);
  75. haveSpecifier = ((null != prefix) || (null != substring)
  76. || (null != suffix));
  77. }
  78. private final String preprocess(String input) {
  79. if ((null != input) && ignoreCase) {
  80. input = input.toLowerCase();
  81. }
  82. return input;
  83. }
  84. public boolean accept(File f) {
  85. if (!(super.accept(f))) {
  86. return false;
  87. } else if (haveSpecifier) {
  88. String path = preprocess(f.getPath());
  89. if ((null == path) || (0 == path.length())) {
  90. return false;
  91. }
  92. if ((null != prefix) && (!(path.startsWith(prefix)))) {
  93. return false;
  94. }
  95. if ((null != substring) && (!path.contains(substring))) {
  96. return false;
  97. }
  98. if ((null != suffix) && (!(path.endsWith(suffix)))) {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. } // class StringFileFilter
  105. static class ClassOnlyFilter extends StringFileFilter {
  106. ClassOnlyFilter() {
  107. super(null, null, ".class", IGNORE_CASE);
  108. }
  109. }
  110. static class JavaOnlyFilter extends StringFileFilter {
  111. JavaOnlyFilter() {
  112. super(null, null, ".java", IGNORE_CASE);
  113. }
  114. }
  115. } // class ValidFileFilter