PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/io/VFSFile.java

#
Java | 438 lines | 268 code | 44 blank | 126 comment | 38 complexity | d047f636a00c78cde9cc3a3ec04e7501 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * VFSFile.java - A file residing on a virtual file system
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998, 2005 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.io;
  23. //{{{ Imports
  24. import java.awt.Color;
  25. import java.io.*;
  26. import org.gjt.sp.jedit.*;
  27. import org.gjt.sp.jedit.browser.VFSBrowser;
  28. import org.gjt.sp.util.Log;
  29. import org.gjt.sp.util.IOUtilities;
  30. //}}}
  31. /**
  32. * A directory entry returned from a file listing.
  33. * @since jEdit 4.3pre2
  34. */
  35. public class VFSFile implements Serializable
  36. {
  37. //{{{ findCompletion() method
  38. /**
  39. * Return the index of a file whose name matches the given string,
  40. * in a case-insensitive manner. Exact matches are preferred.
  41. * @param files The list of files
  42. * @param start The start index, inclusive
  43. * @param start The end index, exclusive
  44. * @param str The string to match
  45. * @param dirsOnly Only match directories?
  46. * @since jEdit 4.3pre3
  47. */
  48. public static int findCompletion(VFSFile[] files, int start, int end,
  49. String str, boolean dirsOnly)
  50. {
  51. for(int i = start; i < end; i++)
  52. {
  53. VFSFile file = files[i];
  54. String matchAgainst = (MiscUtilities.isAbsolutePath(str)
  55. ? file.getPath() : file.getName());
  56. if(dirsOnly && file.getType() == VFSFile.FILE)
  57. continue;
  58. /* try exact match first */
  59. else if(matchAgainst.equals(str))
  60. return i;
  61. else if(matchAgainst.regionMatches(true,0,str,0,str.length()))
  62. return i;
  63. }
  64. return -1;
  65. } //}}}
  66. //{{{ findCompletion() method
  67. public static String findCompletion(String path, String complete,
  68. VFSBrowser browser, boolean dirsOnly)
  69. {
  70. Log.log(Log.DEBUG,VFSFile.class,"findCompletion(" + path + "," + complete
  71. + "," + dirsOnly + ")");
  72. if(complete.equals("~"))
  73. return System.getProperty("user.home");
  74. else if(complete.equals("-"))
  75. return browser.getView().getBuffer().getDirectory();
  76. else if(complete.equals(".."))
  77. return MiscUtilities.getParentOfPath(path);
  78. if(MiscUtilities.isAbsolutePath(complete))
  79. {
  80. if(MiscUtilities.isURL(complete))
  81. return complete;
  82. else
  83. path = "roots:";
  84. }
  85. VFS vfs = VFSManager.getVFSForPath(path);
  86. if((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) == 0)
  87. return null;
  88. Object session = vfs.createVFSSession(path,browser);
  89. if(session == null)
  90. return null;
  91. try
  92. {
  93. VFSFile[] files = vfs._listFiles(session,path,browser);
  94. int index = findCompletion(files,0,files.length,complete,dirsOnly);
  95. if(index != -1)
  96. return files[index].path;
  97. }
  98. catch(IOException e)
  99. {
  100. VFSManager.error(e,path,browser);
  101. }
  102. finally
  103. {
  104. try
  105. {
  106. vfs._endVFSSession(session,browser);
  107. }
  108. catch(IOException e)
  109. {
  110. VFSManager.error(e,path,browser);
  111. }
  112. }
  113. return null;
  114. } //}}}
  115. //{{{ File types
  116. public static final int FILE = 0;
  117. public static final int DIRECTORY = 1;
  118. public static final int FILESYSTEM = 2;
  119. //}}}
  120. //{{{ Instance variables
  121. /**
  122. * @deprecated Use the accessor/mutator methods instead.
  123. */
  124. public String name;
  125. /**
  126. * @deprecated Use the accessor/mutator methods instead.
  127. */
  128. public String path;
  129. /**
  130. * @deprecated Use the accessor/mutator methods instead.
  131. */
  132. public String symlinkPath;
  133. /**
  134. * @deprecated Use the accessor/mutator methods instead.
  135. */
  136. public String deletePath;
  137. /**
  138. * @deprecated Use the accessor/mutator methods instead.
  139. */
  140. public int type;
  141. /**
  142. * @deprecated Use the accessor/mutator methods instead.
  143. */
  144. public long length;
  145. /**
  146. * @deprecated Use the accessor/mutator methods instead.
  147. */
  148. public boolean hidden;
  149. /**
  150. * @deprecated Use the accessor/mutator methods instead.
  151. */
  152. public boolean canRead;
  153. /**
  154. * @deprecated Use the accessor/mutator methods instead.
  155. */
  156. public boolean canWrite;
  157. //}}}
  158. //{{{ VFSFile constructor
  159. /**
  160. * @since jEdit 4.3pre2
  161. */
  162. public VFSFile()
  163. {
  164. } //}}}
  165. //{{{ VFSFile constructor
  166. public VFSFile(String name, String path, String deletePath,
  167. int type, long length, boolean hidden)
  168. {
  169. this.name = name;
  170. this.path = path;
  171. this.deletePath = deletePath;
  172. this.symlinkPath = path;
  173. this.type = type;
  174. this.length = length;
  175. this.hidden = hidden;
  176. if(path != null)
  177. {
  178. // maintain backwards compatibility
  179. VFS vfs = VFSManager.getVFSForPath(path);
  180. canRead = ((vfs.getCapabilities() & VFS.READ_CAP) != 0);
  181. canWrite = ((vfs.getCapabilities() & VFS.WRITE_CAP) != 0);
  182. }
  183. } //}}}
  184. //{{{ getVFS() method
  185. /**
  186. * @return The originating virtual file system of this file.
  187. */
  188. public VFS getVFS()
  189. {
  190. return VFSManager.getVFSForPath(path);
  191. } //}}}
  192. //{{{ getName() method
  193. public String getName()
  194. {
  195. return name;
  196. } //}}}
  197. //{{{ setName() method
  198. public void setName(String name)
  199. {
  200. this.name = name;
  201. } //}}}
  202. //{{{ isBinary() method
  203. /**
  204. * Check if a file is binary file.
  205. * To check if a file is binary, we will check the first characters 100
  206. * (jEdit property vfs.binaryCheck.length)
  207. * If more than 1 (jEdit property vfs.binaryCheck.count), the
  208. * file is declared binary.
  209. * This is not 100% because sometimes the autodetection could fail.
  210. *
  211. * @param session the VFS session
  212. * @return <code>true</code> if the file was detected as binary
  213. * @throws IOException IOException If an I/O error occurs
  214. * @since jEdit 4.3pre5
  215. */
  216. public boolean isBinary(Object session)
  217. throws IOException
  218. {
  219. Reader reader = null;
  220. InputStream in = getVFS()._createInputStream(session,getPath(),
  221. false,jEdit.getActiveView());
  222. if(in == null)
  223. throw new IOException("Unable to get a Stream for this file "+this);
  224. try
  225. {
  226. reader = MiscUtilities.autodetect(in, null);
  227. return MiscUtilities.isBinary(reader);
  228. }
  229. finally
  230. {
  231. IOUtilities.closeQuietly(reader);
  232. }
  233. } //}}}
  234. //{{{ getPath() method
  235. public String getPath()
  236. {
  237. return path;
  238. } //}}}
  239. //{{{ setPath() method
  240. public void setPath(String path)
  241. {
  242. this.path = path;
  243. } //}}}
  244. //{{{ getSymlinkPath() method
  245. public String getSymlinkPath()
  246. {
  247. return symlinkPath;
  248. } //}}}
  249. //{{{ setSymlinkPath() method
  250. public void setSymlinkPath(String symlinkPath)
  251. {
  252. this.symlinkPath = symlinkPath;
  253. } //}}}
  254. //{{{ getDeletePath() method
  255. public String getDeletePath()
  256. {
  257. return deletePath;
  258. } //}}}
  259. //{{{ setDeletePath() method
  260. public void setDeletePath(String deletePath)
  261. {
  262. this.deletePath = deletePath;
  263. } //}}}
  264. //{{{ getType() method
  265. public int getType()
  266. {
  267. return type;
  268. } //}}}
  269. //{{{ setType() method
  270. public void setType(int type)
  271. {
  272. this.type = type;
  273. } //}}}
  274. //{{{ getLength() method
  275. public long getLength()
  276. {
  277. return length;
  278. } //}}}
  279. //{{{ setLength() method
  280. public void setLength(long length)
  281. {
  282. this.length = length;
  283. } //}}}
  284. //{{{ isHidden() method
  285. public boolean isHidden()
  286. {
  287. return hidden;
  288. } //}}}
  289. //{{{ setHidden() method
  290. public void setHidden(boolean hidden)
  291. {
  292. this.hidden = hidden;
  293. } //}}}
  294. //{{{ isReadable() method
  295. public boolean isReadable()
  296. {
  297. return canRead;
  298. } //}}}
  299. //{{{ setReadable() method
  300. public void setReadable(boolean canRead)
  301. {
  302. this.canRead = canRead;
  303. } //}}}
  304. //{{{ isWriteable() method
  305. public boolean isWriteable()
  306. {
  307. return canWrite;
  308. } //}}}
  309. //{{{ setWriteable() method
  310. public void setWriteable(boolean canWrite)
  311. {
  312. this.canWrite = canWrite;
  313. } //}}}
  314. protected boolean colorCalculated;
  315. protected Color color;
  316. //{{{ getExtendedAttribute() method
  317. /**
  318. * Returns the value of an extended attribute. Note that this
  319. * returns formatted strings (eg, "10 Mb" for a file size of
  320. * 1048576 bytes). If you need access to the raw data, access
  321. * fields and methods of this class.
  322. * @param name The extended attribute name
  323. * @since jEdit 4.2pre1
  324. */
  325. public String getExtendedAttribute(String name)
  326. {
  327. if(name.equals(VFS.EA_TYPE))
  328. {
  329. switch(getType())
  330. {
  331. case FILE:
  332. return jEdit.getProperty("vfs.browser.type.file");
  333. case DIRECTORY:
  334. return jEdit.getProperty("vfs.browser.type.directory");
  335. case FILESYSTEM:
  336. return jEdit.getProperty("vfs.browser.type.filesystem");
  337. default:
  338. throw new IllegalArgumentException();
  339. }
  340. }
  341. else if(name.equals(VFS.EA_STATUS))
  342. {
  343. if(isReadable())
  344. {
  345. if(isWriteable())
  346. return jEdit.getProperty("vfs.browser.status.rw");
  347. else
  348. return jEdit.getProperty("vfs.browser.status.ro");
  349. }
  350. else
  351. {
  352. if(isWriteable())
  353. return jEdit.getProperty("vfs.browser.status.append");
  354. else
  355. return jEdit.getProperty("vfs.browser.status.no");
  356. }
  357. }
  358. else if(name.equals(VFS.EA_SIZE))
  359. {
  360. if(getType() != FILE)
  361. return null;
  362. else
  363. return MiscUtilities.formatFileSize(getLength());
  364. }
  365. else
  366. return null;
  367. } //}}}
  368. //{{{ getColor() method
  369. public Color getColor()
  370. {
  371. if(!colorCalculated)
  372. {
  373. colorCalculated = true;
  374. color = VFS.getDefaultColorFor(name);
  375. }
  376. return color;
  377. } //}}}
  378. //{{{ toString() method
  379. public String toString()
  380. {
  381. return name;
  382. } //}}}
  383. //{{{ fetchedAttrs() method
  384. protected boolean fetchedAttrs()
  385. {
  386. return fetchedAttrs;
  387. } //}}}
  388. //{{{ fetchAttrs() method
  389. protected void fetchAttrs()
  390. {
  391. fetchedAttrs = true;
  392. } //}}}
  393. private boolean fetchedAttrs;
  394. }