PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/io/VFS.java

#
Java | 1028 lines | 461 code | 98 blank | 469 comment | 67 complexity | dfa0537ae0d3d4715448f23589d0882e 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. * VFS.java - Virtual filesystem implementation
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2003 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 gnu.regexp.*;
  25. import java.awt.Color;
  26. import java.awt.Component;
  27. import java.io.*;
  28. import java.util.*;
  29. import org.gjt.sp.jedit.buffer.BufferIORequest;
  30. import org.gjt.sp.jedit.msg.PropertiesChanged;
  31. import org.gjt.sp.jedit.*;
  32. import org.gjt.sp.util.Log;
  33. //}}}
  34. /**
  35. * A virtual filesystem implementation.<p>
  36. *
  37. * Plugins can provide virtual file systems by defining entries in their
  38. * <code>services.xml</code> files like so:
  39. *
  40. * <pre>&lt;SERVICE CLASS="org.gjt.sp.jedit.io.VFS" NAME="<i>name</i>"&gt;
  41. * new <i>MyVFS</i>();
  42. *&lt;/SERVICE&gt;</pre>
  43. *
  44. * URLs of the form <code><i>name</i>:<i>path</i></code> will then be handled
  45. * by the VFS named <code><i>name</i></code>.<p>
  46. *
  47. * See {@link org.gjt.sp.jedit.ServiceManager} for details.<p>
  48. *
  49. * <h3>Session objects:</h3>
  50. *
  51. * A session is used to persist things like login information, any network
  52. * sockets, etc. File system implementations that do not need this kind of
  53. * persistence return a dummy object as a session.<p>
  54. *
  55. * Methods whose names are prefixed with "_" expect to be given a
  56. * previously-obtained session object. A session must be obtained from the AWT
  57. * thread in one of two ways:
  58. *
  59. * <ul>
  60. * <li>{@link #createVFSSession(String,Component)}</li>
  61. * <li>{@link #showBrowseDialog(Object[],Component)}</li>
  62. * </ul>
  63. *
  64. * When done, the session must be disposed of using
  65. * {@link #_endVFSSession(Object,Component)}.<p>
  66. *
  67. * <h3>Thread safety:</h3>
  68. *
  69. * The following methods cannot be called from an I/O thread:
  70. *
  71. * <ul>
  72. * <li>{@link #createVFSSession(String,Component)}</li>
  73. * <li>{@link #insert(View,Buffer,String)}</li>
  74. * <li>{@link #load(View,Buffer,String)}</li>
  75. * <li>{@link #save(View,Buffer,String)}</li>
  76. * <li>{@link #showBrowseDialog(Object[],Component)}</li>
  77. * </ul>
  78. *
  79. * All remaining methods are required to be thread-safe in subclasses.
  80. *
  81. * <h3>Implementing a VFS</h3>
  82. *
  83. * You can override as many or as few methods as you want. Make sure
  84. * {@link #getCapabilities()} returns a value reflecting the functionality
  85. * implemented by your VFS.
  86. *
  87. * @see VFSManager#getVFSForPath(String)
  88. * @see VFSManager#getVFSForProtocol(String)
  89. *
  90. * @author Slava Pestov
  91. * @author $Id: VFS.java 4842 2003-08-04 00:23:07Z spestov $
  92. */
  93. public abstract class VFS
  94. {
  95. //{{{ Capabilities
  96. /**
  97. * Read capability.
  98. * @since jEdit 2.6pre2
  99. */
  100. public static final int READ_CAP = 1 << 0;
  101. /**
  102. * Write capability.
  103. * @since jEdit 2.6pre2
  104. */
  105. public static final int WRITE_CAP = 1 << 1;
  106. /**
  107. * @deprecated Do not define this capability.<p>
  108. *
  109. * This was the official API for adding items to a file
  110. * system browser's <b>Plugins</b> menu in jEdit 4.1 and earlier. In
  111. * jEdit 4.2, there is a different way of doing this, you must provide
  112. * a <code>browser.actions.xml</code> file in your plugin JAR, and
  113. * define <code>plugin.<i>class</i>.browser-menu-item</code>
  114. * or <code>plugin.<i>class</i>.browser-menu</code> properties.
  115. * See {@link org.gjt.sp.jedit.EditPlugin} for details.
  116. */
  117. public static final int BROWSE_CAP = 1 << 2;
  118. /**
  119. * Delete file capability.
  120. * @since jEdit 2.6pre2
  121. */
  122. public static final int DELETE_CAP = 1 << 3;
  123. /**
  124. * Rename file capability.
  125. * @since jEdit 2.6pre2
  126. */
  127. public static final int RENAME_CAP = 1 << 4;
  128. /**
  129. * Make directory capability.
  130. * @since jEdit 2.6pre2
  131. */
  132. public static final int MKDIR_CAP = 1 << 5;
  133. /**
  134. * Low latency capability. If this is not set, then a confirm dialog
  135. * will be shown before doing a directory search in this VFS.
  136. * @since jEdit 4.1pre1
  137. */
  138. public static final int LOW_LATENCY_CAP = 1 << 6;
  139. /**
  140. * Case insensitive file system capability.
  141. * @since jEdit 4.1pre1
  142. */
  143. public static final int CASE_INSENSITIVE_CAP = 1 << 7;
  144. //}}}
  145. //{{{ Extended attributes
  146. /**
  147. * File type.
  148. * @since jEdit 4.2pre1
  149. */
  150. public static final String EA_TYPE = "type";
  151. /**
  152. * File status (read only, read write, etc).
  153. * @since jEdit 4.2pre1
  154. */
  155. public static final String EA_STATUS = "status";
  156. /**
  157. * File size.
  158. * @since jEdit 4.2pre1
  159. */
  160. public static final String EA_SIZE = "size";
  161. /**
  162. * File last modified date.
  163. * @since jEdit 4.2pre1
  164. */
  165. public static final String EA_MODIFIED = "modified";
  166. //}}}
  167. //{{{ VFS constructor
  168. /**
  169. * @deprecated Use the form where the constructor takes a capability
  170. * list.
  171. */
  172. public VFS(String name)
  173. {
  174. this(name,0);
  175. } //}}}
  176. //{{{ VFS constructor
  177. /**
  178. * Creates a new virtual filesystem.
  179. * @param name The name
  180. * @param caps The capabilities
  181. */
  182. public VFS(String name, int caps)
  183. {
  184. this.name = name;
  185. this.caps = caps;
  186. // reasonable defaults (?)
  187. this.extAttrs = new String[] { EA_SIZE, EA_TYPE };
  188. } //}}}
  189. //{{{ VFS constructor
  190. /**
  191. * Creates a new virtual filesystem.
  192. * @param name The name
  193. * @param caps The capabilities
  194. * @param extAttrs The extended attributes
  195. * @since jEdit 4.2pre1
  196. */
  197. public VFS(String name, int caps, String[] extAttrs)
  198. {
  199. this.name = name;
  200. this.caps = caps;
  201. this.extAttrs = extAttrs;
  202. } //}}}
  203. //{{{ getName() method
  204. /**
  205. * Returns this VFS's name. The name is used to obtain the
  206. * label stored in the <code>vfs.<i>name</i>.label</code>
  207. * property.
  208. */
  209. public String getName()
  210. {
  211. return name;
  212. } //}}}
  213. //{{{ getCapabilities() method
  214. /**
  215. * Returns the capabilities of this VFS.
  216. * @since jEdit 2.6pre2
  217. */
  218. public int getCapabilities()
  219. {
  220. return caps;
  221. } //}}}
  222. //{{{ getExtendedAttributes() method
  223. /**
  224. * Returns the extended attributes supported by this VFS.
  225. * @since jEdit 4.2pre1
  226. */
  227. public String[] getExtendedAttributes()
  228. {
  229. return extAttrs;
  230. } //}}}
  231. //{{{ showBrowseDialog() method
  232. /**
  233. * Displays a dialog box that should set up a session and return
  234. * the initial URL to browse.
  235. * @param session Where the VFS session will be stored
  236. * @param comp The component that will parent error dialog boxes
  237. * @return The URL
  238. * @since jEdit 2.7pre1
  239. */
  240. public String showBrowseDialog(Object[] session, Component comp)
  241. {
  242. return null;
  243. } //}}}
  244. //{{{ getFileName() method
  245. /**
  246. * Returns the file name component of the specified path.
  247. * @param path The path
  248. * @since jEdit 3.1pre4
  249. */
  250. public String getFileName(String path)
  251. {
  252. if(path.equals("/"))
  253. return path;
  254. if(path.endsWith("/") || path.endsWith(File.separator))
  255. path = path.substring(0,path.length() - 1);
  256. int index = Math.max(path.lastIndexOf('/'),
  257. path.lastIndexOf(File.separatorChar));
  258. if(index == -1)
  259. index = path.indexOf(':');
  260. // don't want getFileName("roots:") to return ""
  261. if(index == -1 || index == path.length() - 1)
  262. return path;
  263. return path.substring(index + 1);
  264. } //}}}
  265. //{{{ getParentOfPath() method
  266. /**
  267. * Returns the parent of the specified path. This must be
  268. * overridden to return a non-null value for browsing of this
  269. * filesystem to work.
  270. * @param path The path
  271. * @since jEdit 2.6pre5
  272. */
  273. public String getParentOfPath(String path)
  274. {
  275. // ignore last character of path to properly handle
  276. // paths like /foo/bar/
  277. int count = Math.max(0,path.length() - 2);
  278. int index = path.lastIndexOf(File.separatorChar,count);
  279. if(index == -1)
  280. index = path.lastIndexOf('/',count);
  281. if(index == -1)
  282. {
  283. // this ensures that getFileParent("protocol:"), for
  284. // example, is "protocol:" and not "".
  285. index = path.lastIndexOf(':');
  286. }
  287. return path.substring(0,index + 1);
  288. } //}}}
  289. //{{{ constructPath() method
  290. /**
  291. * Constructs a path from the specified directory and
  292. * file name component. This must be overridden to return a
  293. * non-null value, otherwise browsing this filesystem will
  294. * not work.<p>
  295. *
  296. * Unless you are writing a VFS, this method should not be called
  297. * directly. To ensure correct behavior, you <b>must</b> call
  298. * {@link org.gjt.sp.jedit.MiscUtilities#constructPath(String,String)}
  299. * instead.
  300. *
  301. * @param parent The parent directory
  302. * @param path The path
  303. * @since jEdit 2.6pre2
  304. */
  305. public String constructPath(String parent, String path)
  306. {
  307. return parent + path;
  308. } //}}}
  309. //{{{ getFileSeparator() method
  310. /**
  311. * Returns the file separator used by this VFS.
  312. * @since jEdit 2.6pre9
  313. */
  314. public char getFileSeparator()
  315. {
  316. return '/';
  317. } //}}}
  318. //{{{ getTwoStageSaveName() method
  319. /**
  320. * Returns a temporary file name based on the given path.
  321. *
  322. * By default jEdit first saves a file to <code>#<i>name</i>#save#</code>
  323. * and then renames it to the original file. However some virtual file
  324. * systems might not support the <code>#</code> character in filenames,
  325. * so this method permits the VFS to override this behavior.
  326. *
  327. * @param path The path name
  328. * @since jEdit 4.1pre7
  329. */
  330. public String getTwoStageSaveName(String path)
  331. {
  332. return MiscUtilities.constructPath(getParentOfPath(path),
  333. '#' + getFileName(path) + "#save#");
  334. } //}}}
  335. //{{{ reloadDirectory() method
  336. /**
  337. * Called before a directory is reloaded by the file system browser.
  338. * Can be used to flush a cache, etc.
  339. * @since jEdit 4.0pre3
  340. */
  341. public void reloadDirectory(String path) {} //}}}
  342. //{{{ createVFSSession() method
  343. /**
  344. * Creates a VFS session. This method is called from the AWT thread,
  345. * so it should not do any I/O. It could, however, prompt for
  346. * a login name and password, for example.
  347. * @param path The path in question
  348. * @param comp The component that will parent any dialog boxes shown
  349. * @return The session
  350. * @since jEdit 2.6pre3
  351. */
  352. public Object createVFSSession(String path, Component comp)
  353. {
  354. return new Object();
  355. } //}}}
  356. //{{{ load() method
  357. /**
  358. * Loads the specified buffer. The default implementation posts
  359. * an I/O request to the I/O thread.
  360. * @param view The view
  361. * @param buffer The buffer
  362. * @param path The path
  363. */
  364. public boolean load(View view, Buffer buffer, String path)
  365. {
  366. if((getCapabilities() & READ_CAP) == 0)
  367. {
  368. VFSManager.error(view,path,"vfs.not-supported.load",new String[] { name });
  369. return false;
  370. }
  371. Object session = createVFSSession(path,view);
  372. if(session == null)
  373. return false;
  374. if((getCapabilities() & WRITE_CAP) == 0)
  375. buffer.setReadOnly(true);
  376. BufferIORequest request = new BufferIORequest(
  377. BufferIORequest.LOAD,view,buffer,session,this,path);
  378. if(buffer.isTemporary())
  379. // this makes HyperSearch much faster
  380. request.run();
  381. else
  382. VFSManager.runInWorkThread(request);
  383. return true;
  384. } //}}}
  385. //{{{ save() method
  386. /**
  387. * Saves the specifies buffer. The default implementation posts
  388. * an I/O request to the I/O thread.
  389. * @param view The view
  390. * @param buffer The buffer
  391. * @param path The path
  392. */
  393. public boolean save(View view, Buffer buffer, String path)
  394. {
  395. if((getCapabilities() & WRITE_CAP) == 0)
  396. {
  397. VFSManager.error(view,path,"vfs.not-supported.save",new String[] { name });
  398. return false;
  399. }
  400. Object session = createVFSSession(path,view);
  401. if(session == null)
  402. return false;
  403. /* When doing a 'save as', the path to save to (path)
  404. * will not be the same as the buffer's previous path
  405. * (buffer.getPath()). In that case, we want to create
  406. * a backup of the new path, even if the old path was
  407. * backed up as well (BACKED_UP property set) */
  408. if(!path.equals(buffer.getPath()))
  409. buffer.unsetProperty(Buffer.BACKED_UP);
  410. VFSManager.runInWorkThread(new BufferIORequest(
  411. BufferIORequest.SAVE,view,buffer,session,this,path));
  412. return true;
  413. } //}}}
  414. //{{{ insert() method
  415. /**
  416. * Inserts a file into the specified buffer. The default implementation
  417. * posts an I/O request to the I/O thread.
  418. * @param view The view
  419. * @param buffer The buffer
  420. * @param path The path
  421. */
  422. public boolean insert(View view, Buffer buffer, String path)
  423. {
  424. if((getCapabilities() & READ_CAP) == 0)
  425. {
  426. VFSManager.error(view,path,"vfs.not-supported.load",new String[] { name });
  427. return false;
  428. }
  429. Object session = createVFSSession(path,view);
  430. if(session == null)
  431. return false;
  432. VFSManager.runInWorkThread(new BufferIORequest(
  433. BufferIORequest.INSERT,view,buffer,session,this,path));
  434. return true;
  435. } //}}}
  436. // A method name that starts with _ requires a session object
  437. //{{{ _canonPath() method
  438. /**
  439. * Returns the canonical form of the specified path name. For example,
  440. * <code>~</code> might be expanded to the user's home directory.
  441. * @param session The session
  442. * @param path The path
  443. * @param comp The component that will parent error dialog boxes
  444. * @exception IOException if an I/O error occurred
  445. * @since jEdit 4.0pre2
  446. */
  447. public String _canonPath(Object session, String path, Component comp)
  448. throws IOException
  449. {
  450. return path;
  451. } //}}}
  452. //{{{ _listDirectory() method
  453. /**
  454. * A convinience method that matches file names against globs, and can
  455. * optionally list the directory recursively.
  456. * @param session The session
  457. * @param directory The directory. Note that this must be a full
  458. * URL, including the host name, path name, and so on. The
  459. * username and password (if needed by the VFS) is obtained from the
  460. * session instance.
  461. * @param glob Only file names matching this glob will be returned
  462. * @param recursive If true, subdirectories will also be listed.
  463. * @param comp The component that will parent error dialog boxes
  464. * @exception IOException if an I/O error occurred
  465. * @since jEdit 4.1pre1
  466. */
  467. public String[] _listDirectory(Object session, String directory,
  468. String glob, boolean recursive, Component comp)
  469. throws IOException
  470. {
  471. Log.log(Log.DEBUG,this,"Listing " + directory);
  472. ArrayList files = new ArrayList(100);
  473. RE filter;
  474. try
  475. {
  476. filter = new RE(MiscUtilities.globToRE(glob),
  477. RE.REG_ICASE);
  478. }
  479. catch(REException e)
  480. {
  481. Log.log(Log.ERROR,this,e);
  482. return null;
  483. }
  484. _listDirectory(session,new ArrayList(),files,directory,filter,
  485. recursive,comp);
  486. String[] retVal = (String[])files.toArray(new String[files.size()]);
  487. Arrays.sort(retVal,new MiscUtilities.StringICaseCompare());
  488. return retVal;
  489. } //}}}
  490. //{{{ _listDirectory() method
  491. /**
  492. * Lists the specified directory.
  493. * @param session The session
  494. * @param directory The directory. Note that this must be a full
  495. * URL, including the host name, path name, and so on. The
  496. * username and password (if needed by the VFS) is obtained from the
  497. * session instance.
  498. * @param comp The component that will parent error dialog boxes
  499. * @exception IOException if an I/O error occurred
  500. * @since jEdit 2.7pre1
  501. */
  502. public DirectoryEntry[] _listDirectory(Object session, String directory,
  503. Component comp)
  504. throws IOException
  505. {
  506. VFSManager.error(comp,directory,"vfs.not-supported.list",new String[] { name });
  507. return null;
  508. } //}}}
  509. //{{{ _getDirectoryEntry() method
  510. /**
  511. * Returns the specified directory entry.
  512. * @param session The session
  513. * @param path The path
  514. * @param comp The component that will parent error dialog boxes
  515. * @exception IOException if an I/O error occurred
  516. * @return The specified directory entry, or null if it doesn't exist.
  517. * @since jEdit 2.7pre1
  518. */
  519. public DirectoryEntry _getDirectoryEntry(Object session, String path,
  520. Component comp)
  521. throws IOException
  522. {
  523. return null;
  524. } //}}}
  525. //{{{ DirectoryEntry class
  526. /**
  527. * A directory entry.
  528. * @since jEdit 2.6pre2
  529. */
  530. public static class DirectoryEntry implements Serializable
  531. {
  532. //{{{ File types
  533. public static final int FILE = 0;
  534. public static final int DIRECTORY = 1;
  535. public static final int FILESYSTEM = 2;
  536. //}}}
  537. //{{{ Instance variables
  538. public String name;
  539. public String path;
  540. public String deletePath;
  541. public int type;
  542. public long length;
  543. public boolean hidden;
  544. public boolean canRead;
  545. public boolean canWrite;
  546. //}}}
  547. //{{{ DirectoryEntry constructor
  548. /**
  549. * @since jEdit 4.2pre2
  550. */
  551. public DirectoryEntry()
  552. {
  553. } //}}}
  554. //{{{ DirectoryEntry constructor
  555. public DirectoryEntry(String name, String path, String deletePath,
  556. int type, long length, boolean hidden)
  557. {
  558. this.name = name;
  559. this.path = path;
  560. this.deletePath = deletePath;
  561. this.type = type;
  562. this.length = length;
  563. this.hidden = hidden;
  564. if(path != null)
  565. {
  566. // maintain backwards compatibility
  567. VFS vfs = VFSManager.getVFSForPath(path);
  568. canRead = ((vfs.getCapabilities() & READ_CAP) != 0);
  569. canWrite = ((vfs.getCapabilities() & WRITE_CAP) != 0);
  570. }
  571. } //}}}
  572. protected boolean colorCalculated;
  573. protected Color color;
  574. //{{{ getExtendedAttribute() method
  575. /**
  576. * Returns the value of an extended attribute. Note that this
  577. * returns formatted strings (eg, "10 Mb" for a file size of
  578. * 1048576 bytes). If you need access to the raw data, access
  579. * fields and methods of this class.
  580. * @param name The extended attribute name
  581. * @since jEdit 4.2pre1
  582. */
  583. public String getExtendedAttribute(String name)
  584. {
  585. if(name.equals(EA_TYPE))
  586. {
  587. switch(type)
  588. {
  589. case FILE:
  590. return jEdit.getProperty("vfs.browser.type.file");
  591. case DIRECTORY:
  592. return jEdit.getProperty("vfs.browser.type.directory");
  593. case FILESYSTEM:
  594. return jEdit.getProperty("vfs.browser.type.filesystem");
  595. default:
  596. throw new IllegalArgumentException();
  597. }
  598. }
  599. else if(name.equals(EA_STATUS))
  600. {
  601. if(canRead)
  602. {
  603. if(canWrite)
  604. return jEdit.getProperty("vfs.browser.status.rw");
  605. else
  606. return jEdit.getProperty("vfs.browser.status.ro");
  607. }
  608. else
  609. {
  610. if(canWrite)
  611. return jEdit.getProperty("vfs.browser.status.append");
  612. else
  613. return jEdit.getProperty("vfs.browser.status.no");
  614. }
  615. }
  616. else if(name.equals(EA_SIZE))
  617. {
  618. if(type != FILE)
  619. return null;
  620. else
  621. return MiscUtilities.formatFileSize(length);
  622. }
  623. else
  624. return null;
  625. } //}}}
  626. //{{{ getColor() method
  627. public Color getColor()
  628. {
  629. if(!colorCalculated)
  630. {
  631. colorCalculated = true;
  632. color = getDefaultColorFor(name);
  633. }
  634. return color;
  635. } //}}}
  636. //{{{ toString() method
  637. public String toString()
  638. {
  639. return name;
  640. } //}}}
  641. } //}}}
  642. //{{{ _delete() method
  643. /**
  644. * Deletes the specified URL.
  645. * @param session The VFS session
  646. * @param path The path
  647. * @param comp The component that will parent error dialog boxes
  648. * @exception IOException if an I/O error occurs
  649. * @since jEdit 2.7pre1
  650. */
  651. public boolean _delete(Object session, String path, Component comp)
  652. throws IOException
  653. {
  654. return false;
  655. } //}}}
  656. //{{{ _rename() method
  657. /**
  658. * Renames the specified URL. Some filesystems might support moving
  659. * URLs between directories, however others may not. Do not rely on
  660. * this behavior.
  661. * @param session The VFS session
  662. * @param from The old path
  663. * @param to The new path
  664. * @param comp The component that will parent error dialog boxes
  665. * @exception IOException if an I/O error occurs
  666. * @since jEdit 2.7pre1
  667. */
  668. public boolean _rename(Object session, String from, String to,
  669. Component comp) throws IOException
  670. {
  671. return false;
  672. } //}}}
  673. //{{{ _mkdir() method
  674. /**
  675. * Creates a new directory with the specified URL.
  676. * @param session The VFS session
  677. * @param directory The directory
  678. * @param comp The component that will parent error dialog boxes
  679. * @exception IOException if an I/O error occurs
  680. * @since jEdit 2.7pre1
  681. */
  682. public boolean _mkdir(Object session, String directory, Component comp)
  683. throws IOException
  684. {
  685. return false;
  686. } //}}}
  687. //{{{ _backup() method
  688. /**
  689. * Backs up the specified file. This should only be overriden by
  690. * the local filesystem VFS.
  691. * @param session The VFS session
  692. * @param path The path
  693. * @param comp The component that will parent error dialog boxes
  694. * @exception IOException if an I/O error occurs
  695. * @since jEdit 3.2pre2
  696. */
  697. public void _backup(Object session, String path, Component comp)
  698. throws IOException
  699. {
  700. } //}}}
  701. //{{{ _createInputStream() method
  702. /**
  703. * Creates an input stream. This method is called from the I/O
  704. * thread.
  705. * @param session the VFS session
  706. * @param path The path
  707. * @param ignoreErrors If true, file not found errors should be
  708. * ignored
  709. * @param comp The component that will parent error dialog boxes
  710. * @exception IOException If an I/O error occurs
  711. * @since jEdit 2.7pre1
  712. */
  713. public InputStream _createInputStream(Object session,
  714. String path, boolean ignoreErrors, Component comp)
  715. throws IOException
  716. {
  717. VFSManager.error(comp,path,"vfs.not-supported.load",new String[] { name });
  718. return null;
  719. } //}}}
  720. //{{{ _createOutputStream() method
  721. /**
  722. * Creates an output stream. This method is called from the I/O
  723. * thread.
  724. * @param session the VFS session
  725. * @param path The path
  726. * @param comp The component that will parent error dialog boxes
  727. * @exception IOException If an I/O error occurs
  728. * @since jEdit 2.7pre1
  729. */
  730. public OutputStream _createOutputStream(Object session,
  731. String path, Component comp)
  732. throws IOException
  733. {
  734. VFSManager.error(comp,path,"vfs.not-supported.save",new String[] { name });
  735. return null;
  736. } //}}}
  737. //{{{ _saveComplete() method
  738. /**
  739. * Called after a file has been saved.
  740. * @param session The VFS session
  741. * @param buffer The buffer
  742. * @param path The path the buffer was saved to (can be different from
  743. * {@link org.gjt.sp.jedit.Buffer#getPath()} if the user invoked the
  744. * <b>Save a Copy As</b> command, for example).
  745. * @param comp The component that will parent error dialog boxes
  746. * @exception IOException If an I/O error occurs
  747. * @since jEdit 4.1pre9
  748. */
  749. public void _saveComplete(Object session, Buffer buffer, String path,
  750. Component comp) throws IOException {} //}}}
  751. //{{{ _endVFSSession() method
  752. /**
  753. * Finishes the specified VFS session. This must be called
  754. * after all I/O with this VFS is complete, to avoid leaving
  755. * stale network connections and such.
  756. * @param session The VFS session
  757. * @param comp The component that will parent error dialog boxes
  758. * @exception IOException if an I/O error occurred
  759. * @since jEdit 2.7pre1
  760. */
  761. public void _endVFSSession(Object session, Component comp)
  762. throws IOException
  763. {
  764. } //}}}
  765. //{{{ getDefaultColorFor() method
  766. /**
  767. * Returns color of the specified file name, by matching it against
  768. * user-specified regular expressions.
  769. * @since jEdit 4.0pre1
  770. */
  771. public static Color getDefaultColorFor(String name)
  772. {
  773. synchronized(lock)
  774. {
  775. if(colors == null)
  776. loadColors();
  777. for(int i = 0; i < colors.size(); i++)
  778. {
  779. ColorEntry entry = (ColorEntry)colors.elementAt(i);
  780. if(entry.re.isMatch(name))
  781. return entry.color;
  782. }
  783. return null;
  784. }
  785. } //}}}
  786. //{{{ DirectoryEntryCompare class
  787. /**
  788. * Implementation of {@link org.gjt.sp.jedit.MiscUtilities.Compare}
  789. * interface that compares {@link VFS.DirectoryEntry} instances.
  790. * @since jEdit 4.2pre1
  791. */
  792. public static class DirectoryEntryCompare implements MiscUtilities.Compare
  793. {
  794. private boolean sortIgnoreCase, sortMixFilesAndDirs;
  795. /**
  796. * Creates a new <code>DirectoryEntryCompare</code>.
  797. * @param sortMixFilesAndDirs If false, directories are
  798. * put at the top of the listing.
  799. * @param sortIgnoreCase If false, upper case comes before
  800. * lower case.
  801. */
  802. public DirectoryEntryCompare(boolean sortMixFilesAndDirs,
  803. boolean sortIgnoreCase)
  804. {
  805. this.sortMixFilesAndDirs = sortMixFilesAndDirs;
  806. this.sortIgnoreCase = sortIgnoreCase;
  807. }
  808. public int compare(Object obj1, Object obj2)
  809. {
  810. VFS.DirectoryEntry file1 = (VFS.DirectoryEntry)obj1;
  811. VFS.DirectoryEntry file2 = (VFS.DirectoryEntry)obj2;
  812. if(!sortMixFilesAndDirs)
  813. {
  814. if(file1.type != file2.type)
  815. return file2.type - file1.type;
  816. }
  817. return MiscUtilities.compareStrings(file1.name,
  818. file2.name,sortIgnoreCase);
  819. }
  820. } //}}}
  821. //{{{ Private members
  822. private String name;
  823. private int caps;
  824. private String[] extAttrs;
  825. private static Vector colors;
  826. private static Object lock = new Object();
  827. //{{{ Class initializer
  828. static
  829. {
  830. EditBus.addToBus(new EBComponent()
  831. {
  832. public void handleMessage(EBMessage msg)
  833. {
  834. if(msg instanceof PropertiesChanged)
  835. {
  836. synchronized(lock)
  837. {
  838. colors = null;
  839. }
  840. }
  841. }
  842. });
  843. } //}}}
  844. //{{{ _listDirectory() method
  845. private void _listDirectory(Object session, ArrayList stack,
  846. ArrayList files, String directory, RE glob, boolean recursive,
  847. Component comp) throws IOException
  848. {
  849. if(stack.contains(directory))
  850. {
  851. Log.log(Log.ERROR,this,
  852. "Recursion in _listDirectory(): "
  853. + directory);
  854. return;
  855. }
  856. else
  857. stack.add(directory);
  858. VFS.DirectoryEntry[] _files = _listDirectory(session,directory,
  859. comp);
  860. if(_files == null || _files.length == 0)
  861. return;
  862. for(int i = 0; i < _files.length; i++)
  863. {
  864. VFS.DirectoryEntry file = _files[i];
  865. if(file.type == VFS.DirectoryEntry.DIRECTORY
  866. || file.type == VFS.DirectoryEntry.FILESYSTEM)
  867. {
  868. if(recursive)
  869. {
  870. // resolve symlinks to avoid loops
  871. String canonPath = _canonPath(session,file.path,comp);
  872. if(!MiscUtilities.isURL(canonPath))
  873. canonPath = MiscUtilities.resolveSymlinks(canonPath);
  874. _listDirectory(session,stack,files,
  875. canonPath,glob,recursive,
  876. comp);
  877. }
  878. }
  879. else
  880. {
  881. if(!glob.isMatch(file.name))
  882. continue;
  883. Log.log(Log.DEBUG,this,file.path);
  884. files.add(file.path);
  885. }
  886. }
  887. } //}}}
  888. //{{{ loadColors() method
  889. private static void loadColors()
  890. {
  891. synchronized(lock)
  892. {
  893. colors = new Vector();
  894. if(!jEdit.getBooleanProperty("vfs.browser.colorize"))
  895. return;
  896. String glob;
  897. int i = 0;
  898. while((glob = jEdit.getProperty("vfs.browser.colors." + i + ".glob")) != null)
  899. {
  900. try
  901. {
  902. colors.addElement(new ColorEntry(
  903. new RE(MiscUtilities.globToRE(glob)),
  904. jEdit.getColorProperty(
  905. "vfs.browser.colors." + i + ".color",
  906. Color.black)));
  907. }
  908. catch(REException e)
  909. {
  910. Log.log(Log.ERROR,VFS.class,"Invalid regular expression: "
  911. + glob);
  912. Log.log(Log.ERROR,VFS.class,e);
  913. }
  914. i++;
  915. }
  916. }
  917. } //}}}
  918. //{{{ ColorEntry class
  919. static class ColorEntry
  920. {
  921. RE re;
  922. Color color;
  923. ColorEntry(RE re, Color color)
  924. {
  925. this.re = re;
  926. this.color = color;
  927. }
  928. } //}}}
  929. //}}}
  930. }