/plugins/ProjectViewer/tags/pv_2_1_3_7/projectviewer/importer/GlobFilter.java

# · Java · 174 lines · 99 code · 22 blank · 53 comment · 16 complexity · e04488a6d6ec8b453b43f843c60fc323 MD5 · raw file

  1. /*
  2. * :tabSize=4:indentSize=4:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package projectviewer.importer;
  20. //{{{ Imports
  21. import java.io.File;
  22. import java.util.StringTokenizer;
  23. import java.util.regex.Pattern;
  24. import java.util.regex.PatternSyntaxException;
  25. import org.gjt.sp.jedit.jEdit;
  26. import org.gjt.sp.jedit.MiscUtilities;
  27. import org.gjt.sp.jedit.io.VFS;
  28. import org.gjt.sp.jedit.io.VFSManager;
  29. import org.gjt.sp.util.Log;
  30. import projectviewer.config.ProjectViewerConfig;
  31. //}}}
  32. /**
  33. * Filter that uses the settings provided by the user (in jEdit's options)
  34. * to select the files.
  35. *
  36. * @author Marcelo Vanzin
  37. * @version $Id: GlobFilter.java 6410 2006-06-12 01:53:11Z vanza $
  38. * @since PV 2.1.1
  39. */
  40. public class GlobFilter extends ImporterFileFilter {
  41. //{{{ +_getImportSettingsFilter()_ : GlobFilter
  42. /**
  43. * Returns a glob filter with the settings taken from the global
  44. * ProjectViewer "import settings".
  45. */
  46. public static GlobFilter getImportSettingsFilter() {
  47. ProjectViewerConfig config = ProjectViewerConfig.getInstance();
  48. return new GlobFilter(
  49. jEdit.getProperty("projectviewer.import.filter.settings.desc"),
  50. jEdit.getProperty("projectviewer.import.filter.settings.rdesc"),
  51. config.getImportGlobs(),
  52. config.getExcludeDirs());
  53. } //}}}
  54. //{{{ Private members
  55. private String description;
  56. private String recurseDesc;
  57. private String fileGlobs;
  58. private String dirGlobs;
  59. private Pattern file_positive;
  60. private Pattern file_negative;
  61. private Pattern dir_negative;
  62. //}}}
  63. //{{{ +GlobFilter(String, String) : <init>
  64. /**
  65. * Creates a new GlobFilter based on the given parameters.
  66. *
  67. * @param fileGlobs List of globs of files to accept (or reject if
  68. * the glob starts with !). space-separated.
  69. * @param dirGlobs List of globs of directory names to ignore.
  70. */
  71. public GlobFilter(String fileGlobs, String dirGlobs) {
  72. this(null, null, fileGlobs, dirGlobs);
  73. } //}}}
  74. //{{{ +GlobFilter(String, String, String, String) : <init>
  75. /**
  76. * Creates a new GlobFilter based on the given parameters with some
  77. * description texts. Mainly used internally by PV for the
  78. * {@link #getImportSettingsFilter() import settings filter}.
  79. */
  80. public GlobFilter(String description, String recurseDescription,
  81. String fileGlobs, String dirGlobs)
  82. {
  83. this.description = description;
  84. this.recurseDesc = recurseDescription;
  85. this.fileGlobs = fileGlobs;
  86. this.dirGlobs = dirGlobs;
  87. } //}}}
  88. //{{{ +getDescription() : String
  89. public String getDescription() {
  90. return description;
  91. } //}}}
  92. //{{{ +accept(File) : boolean
  93. public boolean accept(File file) {
  94. return accept(file.getParentFile(), file.getName());
  95. } //}}}
  96. //{{{ +accept(File, String) : boolean
  97. public boolean accept(File dir, String fileName) {
  98. if (file_positive == null) {
  99. StringTokenizer globs = new StringTokenizer(fileGlobs);
  100. StringBuffer fPos = new StringBuffer();
  101. StringBuffer fNeg = new StringBuffer();
  102. while (globs.hasMoreTokens()) {
  103. String token = globs.nextToken();
  104. if (token.startsWith("!")) {
  105. fNeg.append(MiscUtilities.globToRE(token.substring(1)));
  106. fNeg.append("|");
  107. } else {
  108. fPos.append(MiscUtilities.globToRE(token));
  109. fPos.append("|");
  110. }
  111. }
  112. if (fNeg.length() > 0)
  113. fNeg.setLength(fNeg.length() - 1);
  114. if (fPos.length() > 0)
  115. fPos.setLength(fPos.length() - 1);
  116. globs = new StringTokenizer(dirGlobs);
  117. StringBuffer dirs = new StringBuffer();
  118. while (globs.hasMoreTokens()) {
  119. dirs.append(MiscUtilities.globToRE(globs.nextToken()));
  120. dirs.append("|");
  121. }
  122. if (dirs.length() > 0)
  123. dirs.setLength(dirs.length() - 1);
  124. try {
  125. if ((VFSManager.getFileVFS().getCapabilities()
  126. & VFS.CASE_INSENSITIVE_CAP) != 0) {
  127. file_positive = Pattern.compile(fPos.toString(), Pattern.CASE_INSENSITIVE);
  128. file_negative = Pattern.compile(fNeg.toString(), Pattern.CASE_INSENSITIVE);
  129. dir_negative = Pattern.compile(dirs.toString(), Pattern.CASE_INSENSITIVE);
  130. } else {
  131. file_positive = Pattern.compile(fPos.toString());
  132. file_negative = Pattern.compile(fNeg.toString());
  133. dir_negative = Pattern.compile(dirs.toString());
  134. }
  135. } catch (PatternSyntaxException re) {
  136. Log.log(Log.ERROR, this, re);
  137. }
  138. }
  139. File child = new File(dir, fileName);
  140. if (child.isFile()) {
  141. return file_positive.matcher(fileName).matches()
  142. && !file_negative.matcher(fileName).matches();
  143. } else if (child.isDirectory()) {
  144. return !dir_negative.matcher(fileName).matches();
  145. }
  146. return false;
  147. } //}}}
  148. //{{{ +getRecurseDescription() : String
  149. public String getRecurseDescription() {
  150. return recurseDesc;
  151. } //}}}
  152. }