PageRenderTime 78ms CodeModel.GetById 64ms app.highlight 10ms 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
  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
 23package org.gjt.sp.jedit.io;
 24
 25//{{{ Imports
 26import java.awt.Color;
 27import java.io.*;
 28import org.gjt.sp.jedit.*;
 29import org.gjt.sp.jedit.browser.VFSBrowser;
 30import org.gjt.sp.util.Log;
 31import org.gjt.sp.util.IOUtilities;
 32//}}}
 33
 34/**
 35 * A directory entry returned from a file listing.
 36 * @since jEdit 4.3pre2
 37 */
 38public class VFSFile implements Serializable
 39{
 40	//{{{ findCompletion() method
 41	/**
 42	 * Return the index of a file whose name matches the given string,
 43	 * in a case-insensitive manner. Exact matches are preferred.
 44	 * @param files The list of files
 45	 * @param start The start index, inclusive
 46	 * @param start The end index, exclusive
 47	 * @param str The string to match
 48	 * @param dirsOnly Only match directories?
 49	 * @since jEdit 4.3pre3
 50	 */
 51	public static int findCompletion(VFSFile[] files, int start, int end,
 52		String str, boolean dirsOnly)
 53	{
 54		for(int i = start; i < end; i++)
 55		{
 56			VFSFile file = files[i];
 57			String matchAgainst = (MiscUtilities.isAbsolutePath(str)
 58				? file.getPath() : file.getName());
 59
 60			if(dirsOnly && file.getType() == VFSFile.FILE)
 61				continue;
 62			/* try exact match first */
 63			else if(matchAgainst.equals(str))
 64				return i;
 65			else if(matchAgainst.regionMatches(true,0,str,0,str.length()))
 66				return i;
 67		}
 68
 69		return -1;
 70	} //}}}
 71
 72	//{{{ findCompletion() method
 73	public static String findCompletion(String path, String complete,
 74		VFSBrowser browser, boolean dirsOnly)
 75	{
 76		Log.log(Log.DEBUG,VFSFile.class,"findCompletion(" + path + "," + complete
 77			+ "," + dirsOnly + ")");
 78
 79		if(complete.equals("~"))
 80			return System.getProperty("user.home");
 81		else if(complete.equals("-"))
 82			return browser.getView().getBuffer().getDirectory();
 83		else if(complete.equals(".."))
 84			return MiscUtilities.getParentOfPath(path);
 85
 86		if(MiscUtilities.isAbsolutePath(complete))
 87		{
 88			if(MiscUtilities.isURL(complete))
 89				return complete;
 90			else
 91				path = "roots:";
 92		}
 93
 94		VFS vfs = VFSManager.getVFSForPath(path);
 95		if((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) == 0)
 96			return null;
 97		Object session = vfs.createVFSSession(path,browser);
 98		if(session == null)
 99			return null;
100
101		try
102		{
103			VFSFile[] files = vfs._listFiles(session,path,browser);
104			int index = findCompletion(files,0,files.length,complete,dirsOnly);
105			if(index != -1)
106				return files[index].path;
107		}
108		catch(IOException e)
109		{
110			VFSManager.error(e,path,browser);
111		}
112		finally
113		{
114			try
115			{
116				vfs._endVFSSession(session,browser);
117			}
118			catch(IOException e)
119			{
120				VFSManager.error(e,path,browser);
121			}
122		}
123		
124		return null;
125	} //}}}
126
127	//{{{ File types
128	public static final int FILE = 0;
129	public static final int DIRECTORY = 1;
130	public static final int FILESYSTEM = 2;
131	//}}}
132
133	//{{{ Instance variables
134	/**
135	 * @deprecated Use the accessor/mutator methods instead.
136	 */
137	public String name;
138	/**
139	 * @deprecated Use the accessor/mutator methods instead.
140	 */
141	public String path;
142	/**
143	 * @deprecated Use the accessor/mutator methods instead.
144	 */
145	public String symlinkPath;
146	/**
147	 * @deprecated Use the accessor/mutator methods instead.
148	 */
149	public String deletePath;
150	/**
151	 * @deprecated Use the accessor/mutator methods instead.
152	 */
153	public int type;
154	/**
155	 * @deprecated Use the accessor/mutator methods instead.
156	 */
157	public long length;
158	/**
159	 * @deprecated Use the accessor/mutator methods instead.
160	 */
161	public boolean hidden;
162	/**
163	 * @deprecated Use the accessor/mutator methods instead.
164	 */
165	public boolean canRead;
166	/**
167	 * @deprecated Use the accessor/mutator methods instead.
168	 */
169	public boolean canWrite;
170	//}}}
171
172	//{{{ VFSFile constructor
173	/**
174	 * @since jEdit 4.3pre2
175	 */
176	public VFSFile()
177	{
178	} //}}}
179
180	//{{{ VFSFile constructor
181	public VFSFile(String name, String path, String deletePath,
182		int type, long length, boolean hidden)
183	{
184		this.name = name;
185		this.path = path;
186		this.deletePath = deletePath;
187		this.symlinkPath = path;
188		this.type = type;
189		this.length = length;
190		this.hidden = hidden;
191		if(path != null)
192		{
193			// maintain backwards compatibility
194			VFS vfs = VFSManager.getVFSForPath(path);
195			canRead = ((vfs.getCapabilities() & VFS.READ_CAP) != 0);
196			canWrite = ((vfs.getCapabilities() & VFS.WRITE_CAP) != 0);
197		}
198	} //}}}
199
200	//{{{ getVFS() method
201	/**
202	 * @return The originating virtual file system of this file.
203	 */
204	public VFS getVFS()
205	{
206		return VFSManager.getVFSForPath(path);
207	} //}}}
208	
209	//{{{ getName() method
210	public String getName()
211	{
212		return name;
213	} //}}}
214
215	//{{{ setName() method
216	public void setName(String name)
217	{
218		this.name = name;
219	} //}}}
220
221	//{{{ isBinary() method
222	/**
223	 * Check if a file is binary file.
224	 * To check if a file is binary, we will check the first characters 100
225	 * (jEdit property vfs.binaryCheck.length)
226	 * If more than 1 (jEdit property vfs.binaryCheck.count), the
227	 * file is declared binary.
228	 * This is not 100% because sometimes the autodetection could fail.
229	 *
230	 * @param session the VFS session
231	 * @return <code>true</code> if the file was detected as binary
232	 * @throws IOException IOException If an I/O error occurs
233	 * @since jEdit 4.3pre5
234	 */
235	public boolean isBinary(Object session)
236		throws IOException
237	{
238		Reader reader = null;
239		InputStream in = getVFS()._createInputStream(session,getPath(),
240			false,jEdit.getActiveView());
241		if(in == null)
242			throw new IOException("Unable to get a Stream for this file "+this);
243
244		try
245		{
246			reader = MiscUtilities.autodetect(in, null);
247			return MiscUtilities.isBinary(reader);
248		}
249		finally
250		{
251			IOUtilities.closeQuietly(reader);
252		}
253	} //}}}
254
255	//{{{ getPath() method
256	public String getPath()
257	{
258		return path;
259	} //}}}
260
261	//{{{ setPath() method
262	public void setPath(String path)
263	{
264		this.path = path;
265	} //}}}
266
267	//{{{ getSymlinkPath() method
268	public String getSymlinkPath()
269	{
270		return symlinkPath;
271	} //}}}
272
273	//{{{ setSymlinkPath() method
274	public void setSymlinkPath(String symlinkPath)
275	{
276		this.symlinkPath = symlinkPath;
277	} //}}}
278
279	//{{{ getDeletePath() method
280	public String getDeletePath()
281	{
282		return deletePath;
283	} //}}}
284
285	//{{{ setDeletePath() method
286	public void setDeletePath(String deletePath)
287	{
288		this.deletePath = deletePath;
289	} //}}}
290
291	//{{{ getType() method
292	public int getType()
293	{
294		return type;
295	} //}}}
296
297	//{{{ setType() method
298	public void setType(int type)
299	{
300		this.type = type;
301	} //}}}
302
303	//{{{ getLength() method
304	public long getLength()
305	{
306		return length;
307	} //}}}
308
309	//{{{ setLength() method
310	public void setLength(long length)
311	{
312		this.length = length;
313	} //}}}
314
315	//{{{ isHidden() method
316	public boolean isHidden()
317	{
318		return hidden;
319	} //}}}
320
321	//{{{ setHidden() method
322	public void setHidden(boolean hidden)
323	{
324		this.hidden = hidden;
325	} //}}}
326
327	//{{{ isReadable() method
328	public boolean isReadable()
329	{
330		return canRead;
331	} //}}}
332
333	//{{{ setReadable() method
334	public void setReadable(boolean canRead)
335	{
336		this.canRead = canRead;
337	} //}}}
338
339	//{{{ isWriteable() method
340	public boolean isWriteable()
341	{
342		return canWrite;
343	} //}}}
344
345	//{{{ setWriteable() method
346	public void setWriteable(boolean canWrite)
347	{
348		this.canWrite = canWrite;
349	} //}}}
350
351	protected boolean colorCalculated;
352	protected Color color;
353
354	//{{{ getExtendedAttribute() method
355	/**
356	 * Returns the value of an extended attribute. Note that this
357	 * returns formatted strings (eg, "10 Mb" for a file size of
358	 * 1048576 bytes). If you need access to the raw data, access
359	 * fields and methods of this class.
360	 * @param name The extended attribute name
361	 * @since jEdit 4.2pre1
362	 */
363	public String getExtendedAttribute(String name)
364	{
365		if(name.equals(VFS.EA_TYPE))
366		{
367			switch(getType())
368			{
369			case FILE:
370				return jEdit.getProperty("vfs.browser.type.file");
371			case DIRECTORY:
372				return jEdit.getProperty("vfs.browser.type.directory");
373			case FILESYSTEM:
374				return jEdit.getProperty("vfs.browser.type.filesystem");
375			default:
376				throw new IllegalArgumentException();
377			}
378		}
379		else if(name.equals(VFS.EA_STATUS))
380		{
381			if(isReadable())
382			{
383				if(isWriteable())
384					return jEdit.getProperty("vfs.browser.status.rw");
385				else
386					return jEdit.getProperty("vfs.browser.status.ro");
387			}
388			else
389			{
390				if(isWriteable())
391					return jEdit.getProperty("vfs.browser.status.append");
392				else
393					return jEdit.getProperty("vfs.browser.status.no");
394			}
395		}
396		else if(name.equals(VFS.EA_SIZE))
397		{
398			if(getType() != FILE)
399				return null;
400			else
401				return MiscUtilities.formatFileSize(getLength());
402		}
403		else
404			return null;
405	} //}}}
406
407	//{{{ getColor() method
408	public Color getColor()
409	{
410		if(!colorCalculated)
411		{
412			colorCalculated = true;
413			color = VFS.getDefaultColorFor(name);
414		}
415
416		return color;
417	} //}}}
418
419	//{{{ toString() method
420	public String toString()
421	{
422		return name;
423	} //}}}
424	
425	//{{{ fetchedAttrs() method
426	protected boolean fetchedAttrs()
427	{
428		return fetchedAttrs;
429	} //}}}
430	
431	//{{{ fetchAttrs() method
432	protected void fetchAttrs()
433	{
434		fetchedAttrs = true;
435	} //}}}
436	
437	private boolean fetchedAttrs;
438}