KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > bufferio > BufferLoadRequest


1 /*
2  * BufferLoadRequest.java - I/O request
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2000, 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
23 package org.gjt.sp.jedit.bufferio;
24
25 //{{{ Imports
26
import java.io.*;
27 import java.nio.charset.*;
28 import org.gjt.sp.jedit.io.*;
29 import org.gjt.sp.jedit.*;
30 import org.gjt.sp.jedit.buffer.JEditBuffer;
31 import org.gjt.sp.util.*;
32 //}}}
33

34 /**
35  * A buffer load request.
36  * @author Slava Pestov
37  * @version $Id: BufferLoadRequest.java 8133 2006-11-25 19:50:58Z ezust $
38  */

39 public class BufferLoadRequest extends BufferIORequest
40 {
41     //{{{ BufferLoadRequest constructor
42
/**
43      * Creates a new buffer I/O request.
44      * @param view The view
45      * @param buffer The buffer
46      * @param session The VFS session
47      * @param vfs The VFS
48      * @param path The path
49      */

50     public BufferLoadRequest(View view, Buffer buffer,
51         Object JavaDoc session, VFS vfs, String JavaDoc path)
52     {
53         super(view,buffer,session,vfs,path);
54     } //}}}
55

56     //{{{ run() method
57
public void run()
58     {
59         try
60         {
61             InputStream contents = null;
62             try
63             {
64                 String JavaDoc[] args = { vfs.getFileName(path) };
65                 setAbortable(true);
66                 if(!buffer.isTemporary())
67                 {
68                     setStatus(jEdit.getProperty("vfs.status.load",args));
69                     setValue(0L);
70                 }
71
72                 path = vfs._canonPath(session,path,view);
73
74                 VFSFile entry = vfs._getFile(
75                     session,path,view);
76                 long length;
77                 if(entry != null)
78                     length = entry.getLength();
79                 else
80                     length = 0L;
81
82                 contents = vfs._createInputStream(session,path,
83                     false,view);
84                 if(contents == null)
85                 {
86                     buffer.setBooleanProperty(ERROR_OCCURRED,true);
87                     return;
88                 }
89
90                 read(autodetect(contents),length,false);
91                 buffer.setNewFile(false);
92             }
93             catch(CharConversionException e)
94             {
95                 handleEncodingError(e);
96             }
97             catch(CharacterCodingException e)
98             {
99                 handleEncodingError(e);
100             }
101             catch(UnsupportedEncodingException e)
102             {
103                 handleEncodingError(e);
104             }
105             catch(UnsupportedCharsetException e)
106             {
107                 handleEncodingError(e);
108             }
109             catch(IOException io)
110             {
111                 Log.log(Log.ERROR,this,io);
112                 Object JavaDoc[] pp = { io.toString() };
113                 VFSManager.error(view,path,"ioerror.read-error",pp);
114
115                 buffer.setBooleanProperty(ERROR_OCCURRED,true);
116             }
117             catch(OutOfMemoryError JavaDoc oom)
118             {
119                 Log.log(Log.ERROR,this,oom);
120                 VFSManager.error(view,path,"out-of-memory-error",null);
121
122                 buffer.setBooleanProperty(ERROR_OCCURRED,true);
123             }
124             finally
125             {
126                 IOUtilities.closeQuietly(contents);
127             }
128
129             if(jEdit.getBooleanProperty("persistentMarkers"))
130             {
131                 InputStream markers = null;
132                 try
133                 {
134                     String JavaDoc[] args = { vfs.getFileName(path) };
135                     if(!buffer.isTemporary())
136                         setStatus(jEdit.getProperty("vfs.status.load-markers",args));
137                     setAbortable(true);
138
139                     markers = vfs._createInputStream(session,markersPath,true,view);
140                     if(markers != null)
141                         readMarkers(buffer,markers);
142                 }
143                 catch(IOException io)
144                 {
145                     // ignore
146
}
147                 finally
148                 {
149                     IOUtilities.closeQuietly(markers);
150                 }
151             }
152         }
153         catch(WorkThread.Abort a)
154         {
155             buffer.setBooleanProperty(ERROR_OCCURRED,true);
156         }
157         finally
158         {
159             try
160             {
161                 vfs._endVFSSession(session,view);
162             }
163             catch(IOException io)
164             {
165                 Log.log(Log.ERROR,this,io);
166                 String JavaDoc[] pp = { io.toString() };
167                 VFSManager.error(view,path,"ioerror.read-error",pp);
168
169                 buffer.setBooleanProperty(ERROR_OCCURRED,true);
170             }
171             catch(WorkThread.Abort a)
172             {
173                 buffer.setBooleanProperty(ERROR_OCCURRED,true);
174             }
175         }
176     } //}}}
177

178     //{{{ handleEncodingError() method
179
private void handleEncodingError(Exception JavaDoc e)
180     {
181         Log.log(Log.ERROR,this,e);
182         Object JavaDoc[] pp = { buffer.getProperty(JEditBuffer.ENCODING),
183             e.toString() };
184         VFSManager.error(view,path,"ioerror.encoding-error",pp);
185
186         buffer.setBooleanProperty(ERROR_OCCURRED,true);
187     } //}}}
188

189     //{{{ readMarkers() method
190
private static void readMarkers(Buffer buffer, InputStream _in)
191         throws IOException
192     {
193         // For `reload' command
194
buffer.removeAllMarkers();
195
196         BufferedReader in = new BufferedReader(new InputStreamReader(_in));
197
198         try
199         {
200             String JavaDoc line;
201             while((line = in.readLine()) != null)
202             {
203                 // malformed marks file?
204
if(line.length() == 0)
205                     continue;
206                 
207                 // compatibility kludge for jEdit 3.1 and earlier
208
if(line.charAt(0) != '!')
209                     continue;
210
211
212                 char shortcut = line.charAt(1);
213                 int start = line.indexOf(';');
214                 int end = line.indexOf(';',start + 1);
215                 int position = Integer.parseInt(line.substring(start + 1,end));
216                 buffer.addMarker(shortcut,position);
217             }
218             buffer.setMarkersChanged(false);
219         }
220         finally
221         {
222             in.close();
223         }
224     } //}}}
225
}
226
Popular Tags