/plugins/ProjectViewer/tags/pv_2_1_3_2/projectviewer/importer/CVSEntriesFilter.java

# · Java · 195 lines · 101 code · 25 blank · 69 comment · 22 complexity · 1848397623c6b06804b028dc715c75f4 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.io.FileReader;
  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.FileNotFoundException;
  26. import java.util.HashMap;
  27. import java.util.HashSet;
  28. import com.microstar.xml.XmlParser;
  29. import com.microstar.xml.HandlerBase;
  30. import org.gjt.sp.util.Log;
  31. import org.gjt.sp.jedit.jEdit;
  32. //}}}
  33. /**
  34. * Filter that uses the CVS/Entries file to decide if a file should be accepted
  35. * or not. The filter behaves a little differently depending on where it's
  36. * being used: if inside a JFileChooser, it accepts directories regardless of
  37. * them being on the CVS/Entries file or not, so the user can navigate freely.
  38. *
  39. * <p>For the java.io.FilenameFilter implementation, the CVS/Entries listing is
  40. * strictly enforced, even for directories. This way, no directories that are
  41. * not listed there are going to be imported into the project.</p>
  42. *
  43. * <p>"Entries" files read are kept in an internal cache so that subsequent
  44. * visits to the same directory are faster.</p>
  45. *
  46. * <p>Since PV 2.1.1, this filter also looks for .svn/entries files when
  47. * they're available.</p>
  48. *
  49. * @author Marcelo Vanzin
  50. * @version $Id: CVSEntriesFilter.java 6346 2005-08-21 19:37:00Z vanza $
  51. */
  52. public class CVSEntriesFilter extends ImporterFileFilter {
  53. //{{{ Private members
  54. private HashMap entries = new HashMap();
  55. //}}}
  56. //{{{ +getDescription() : String
  57. public String getDescription() {
  58. return jEdit.getProperty("projectviewer.import.filter.cvs.desc");
  59. } //}}}
  60. //{{{ +accept(File) : boolean
  61. /**
  62. * accept() method for the Swing JFileChooser. Accepts files only if they
  63. * are in the CVS/Entries file for the directory. All directories not named
  64. * "CVS" are accepted, so the user can navigate freely.
  65. */
  66. public boolean accept(File file) {
  67. return (file.isDirectory() && !file.getName().equals("CVS"))
  68. || accept(file.getParentFile(), file.getName());
  69. } //}}}
  70. //{{{ +accept(File, String) : boolean
  71. /**
  72. * accept() method for the FilenameFilter implementation. Accepts only
  73. * files and directories that are listed in the CVS/Entries file.
  74. */
  75. public boolean accept(File file, String fileName) {
  76. File f = new File(file.getAbsolutePath(), fileName);
  77. if ((fileName.equals("CVS") || fileName.equals(".svn"))
  78. && f.isDirectory())
  79. {
  80. return false;
  81. }
  82. return (f.isDirectory()
  83. || getEntries(file.getAbsolutePath()).contains(fileName));
  84. } //}}}
  85. //{{{ -getEntries(String) : HashSet
  86. /**
  87. * Returns the set of files from the CVS/Entries file for the given path.
  88. * In case the file has not yet been read, parse it.
  89. */
  90. private HashSet getEntries(String dirPath) {
  91. HashSet h = (HashSet) entries.get(dirPath);
  92. if (h == null) {
  93. // parse file
  94. BufferedReader br = null;
  95. try {
  96. h = new HashSet();
  97. String fPath = dirPath + File.separator + "CVS" +
  98. File.separator + "Entries";
  99. br = new BufferedReader(new FileReader(fPath));
  100. String line;
  101. while ( (line = br.readLine()) != null ) {
  102. int idx1, idx2;
  103. idx1 = line.indexOf('/');
  104. if (idx1 != -1) {
  105. idx2 = line.indexOf('/', idx1 + 1);
  106. h.add(line.substring(idx1 + 1, idx2));
  107. }
  108. }
  109. } catch (FileNotFoundException fnfe) {
  110. // no CVS/Entries. Try .svn/Entries
  111. getSubversionEntries(h, dirPath);
  112. } catch (IOException ioe) {
  113. //shouldn't happen
  114. Log.log(Log.ERROR,this,ioe);
  115. } finally {
  116. if (br != null) try { br.close(); } catch (Exception e) { }
  117. entries.put(dirPath, h);
  118. }
  119. }
  120. return h;
  121. } //}}}
  122. //{{{ -getSubversionEntries(String) : HashSet
  123. private void getSubversionEntries(HashSet target, String dirPath) {
  124. try {
  125. String fPath = dirPath + File.separator + ".svn" +
  126. File.separator + "entries";
  127. XmlParser parser = new XmlParser();
  128. parser.setHandler(new SubversionEntriesHandler(target));
  129. parser.parse(null, null, new FileReader(fPath));
  130. } catch (FileNotFoundException fnfe) {
  131. // no .svn/Entries
  132. } catch (Exception e) {
  133. //shouldn't happen
  134. Log.log(Log.ERROR,this,e);
  135. }
  136. } //}}}
  137. //{{{ +getRecurseDescription() : String
  138. public String getRecurseDescription() {
  139. return jEdit.getProperty("projectviewer.import.filter.cvs.rdesc");
  140. } //}}}
  141. //{{{ -class _SubversionEntriesHandler_
  142. private static class SubversionEntriesHandler extends HandlerBase {
  143. private HashSet target;
  144. private boolean addEntry;
  145. private String path;
  146. //{{{ +SubversionEntriesHandler(HashSet) : <init>
  147. public SubversionEntriesHandler(HashSet target) {
  148. this.target = target;
  149. this.addEntry = false;
  150. this.path = null;
  151. } //}}}
  152. //{{{ +attribute(String, String, boolean) : void
  153. public void attribute(String name, String value, boolean spec) {
  154. if (name.equals("name")) {
  155. this.path = value;
  156. } else if (name.equals("kind") && value.equals("file")) {
  157. this.addEntry = true;
  158. }
  159. } //}}}
  160. //{{{ +endElement(String) : void
  161. public void endElement(String qName) {
  162. if (qName.equals("entry")) {
  163. if (this.addEntry && this.path != null)
  164. this.target.add(this.path);
  165. this.addEntry = false;
  166. this.path = null;
  167. }
  168. } //}}}
  169. } //}}}
  170. }