PageRenderTime 89ms CodeModel.GetById 60ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/BufferHistory.java

#
Java | 339 lines | 268 code | 47 blank | 24 comment | 36 complexity | c8115e540f0d7c2ec41b87c8392359cb MD5 | raw file
  1/*
  2 * BufferHistory.java - Remembers caret positions 
  3 * Copyright (C) 2000, 2001 Slava Pestov
  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
 20package org.gjt.sp.jedit;
 21
 22import com.microstar.xml.*;
 23import java.io.*;
 24import java.util.*;
 25import org.gjt.sp.jedit.textarea.*;
 26import org.gjt.sp.util.Log;
 27
 28public class BufferHistory
 29{
 30	public static Entry getEntry(String path)
 31	{
 32		Enumeration enum = history.elements();
 33		while(enum.hasMoreElements())
 34		{
 35			Entry entry = (Entry)enum.nextElement();
 36			if(pathsCaseInsensitive)
 37			{
 38				if(entry.path.equalsIgnoreCase(path))
 39					return entry;
 40			}
 41			else
 42			{
 43				if(entry.path.equals(path))
 44					return entry;
 45			}
 46		}
 47
 48		return null;
 49	}
 50
 51	public static void setEntry(String path, int caret, Selection[] selection,
 52		String encoding)
 53	{
 54		removeEntry(path);
 55		addEntry(new Entry(path,caret,selectionToString(selection),encoding));
 56	}
 57
 58	public static Vector getBufferHistory()
 59	{
 60		return history;
 61	}
 62
 63	public static void load(File file)
 64	{
 65		max = jEdit.getIntegerProperty("recentFiles",50);
 66
 67		Log.log(Log.MESSAGE,jEdit.class,"Loading recent file list " + file);
 68
 69		RecentHandler handler = new RecentHandler();
 70		XmlParser parser = new XmlParser();
 71		parser.setHandler(handler);
 72		try
 73		{
 74			BufferedReader in = new BufferedReader(new FileReader(file));
 75			parser.parse(null, null, in);
 76		}
 77		catch(XmlException xe)
 78		{
 79			int line = xe.getLine();
 80			String message = xe.getMessage();
 81			Log.log(Log.ERROR,BufferHistory.class,file + ":" + line
 82				+ ": " + message);
 83		}
 84		catch(FileNotFoundException fnf)
 85		{
 86			Log.log(Log.DEBUG,BufferHistory.class,fnf);
 87		}
 88		catch(Exception e)
 89		{
 90			Log.log(Log.ERROR,BufferHistory.class,e);
 91		}
 92	}
 93
 94	public static void save(File file)
 95	{
 96		String lineSep = System.getProperty("line.separator");
 97
 98		try
 99		{
100			BufferedWriter out = new BufferedWriter(
101				new FileWriter(file));
102
103			out.write("<?xml version=\"1.0\"?>");
104			out.write(lineSep);
105			out.write("<!DOCTYPE RECENT SYSTEM \"recent.dtd\">");
106			out.write(lineSep);
107			out.write("<RECENT>");
108			out.write(lineSep);
109
110			Enumeration enum = history.elements();
111			while(enum.hasMoreElements())
112			{
113				out.write("<ENTRY>");
114				out.write(lineSep);
115
116				Entry entry = (Entry)enum.nextElement();
117
118				out.write("<PATH><![CDATA[");
119				out.write(entry.path);
120				out.write("]]></PATH>");
121				out.write(lineSep);
122
123				out.write("<CARET>");
124				out.write(String.valueOf(entry.caret));
125				out.write("</CARET>");
126				out.write(lineSep);
127
128				if(entry.selection != null
129					&& entry.selection.length() > 0)
130				{
131					out.write("<SELECTION>");
132					out.write(entry.selection);
133					out.write("</SELECTION>");
134					out.write(lineSep);
135				}
136
137				if(entry.encoding != null)
138				{
139					out.write("<ENCODING>");
140					out.write(entry.encoding);
141					out.write("</ENCODING>");
142					out.write(lineSep);
143				}
144
145				out.write("</ENTRY>");
146				out.write(lineSep);
147			}
148
149			out.write("</RECENT>");
150			out.write(lineSep);
151
152			out.close();
153		}
154		catch(Exception e)
155		{
156			Log.log(Log.ERROR,BufferHistory.class,e);
157		}
158	}
159
160	// private members
161	private static Vector history;
162	private static boolean pathsCaseInsensitive;
163	private static int max;
164
165	static
166	{
167		history = new Vector();
168		pathsCaseInsensitive = (File.separatorChar == '\\'
169			|| File.separatorChar == ':');
170	}
171
172	/* private */ static void addEntry(Entry entry)
173	{
174		history.addElement(entry);
175		while(history.size() > max)
176			history.removeElementAt(0);
177	}
178
179	/* private */ static void removeEntry(String path)
180	{
181		Enumeration enum = history.elements();
182		for(int i = 0; i < history.size(); i++)
183		{
184			Entry entry = (Entry)history.elementAt(i);
185			if(entry.path.equals(path))
186			{
187				history.removeElementAt(i);
188				return;
189			}
190		}
191	}
192
193	private static String selectionToString(Selection[] s)
194	{
195		if(s == null)
196			return null;
197
198		StringBuffer buf = new StringBuffer();
199
200		for(int i = 0; i < s.length; i++)
201		{
202			if(i != 0)
203				buf.append(' ');
204
205			Selection sel = s[i];
206			if(sel instanceof Selection.Range)
207				buf.append("range ");
208			else //if(sel instanceof Selection.Rect)
209				buf.append("rect ");
210			buf.append(sel.getStart());
211			buf.append(' ');
212			buf.append(sel.getEnd());
213		}
214
215		return buf.toString();
216	}
217
218	private static Selection[] stringToSelection(String s)
219	{
220		if(s == null)
221			return null;
222
223		Vector selection = new Vector();
224		StringTokenizer st = new StringTokenizer(s);
225
226		while(st.hasMoreTokens())
227		{
228			String type = st.nextToken();
229			int start = Integer.parseInt(st.nextToken());
230			int end = Integer.parseInt(st.nextToken());
231			if(end < start)
232			{
233				// I'm not sure when this can happen,
234				// but it does sometimes, witness the
235				// jEdit bug tracker.
236				continue;
237			}
238
239			Selection sel;
240			if(type.equals("range"))
241				sel = new Selection.Range(start,end);
242			else //if(type.equals("rect"))
243				sel = new Selection.Rect(start,end);
244
245			selection.addElement(sel);
246		}
247
248		Selection[] returnValue = new Selection[selection.size()];
249		selection.copyInto(returnValue);
250		return returnValue;
251	}
252
253	public static class Entry
254	{
255		public String path;
256		public int caret;
257		public String selection;
258		public String encoding;
259
260		public Selection[] getSelection()
261		{
262			return stringToSelection(selection);
263		}
264
265		public Entry(String path, int caret, String selection, String encoding)
266		{
267			this.path = path;
268			this.caret = caret;
269			this.selection = selection;
270			this.encoding = encoding;
271		}
272	}
273
274	static class RecentHandler extends HandlerBase
275	{
276		public Object resolveEntity(String publicId, String systemId)
277		{
278			if("recent.dtd".equals(systemId))
279			{
280				try
281				{
282					return new BufferedReader(new InputStreamReader(
283						getClass().getResourceAsStream("recent.dtd")));
284				}
285				catch(Exception e)
286				{
287					Log.log(Log.ERROR,this,"Error while opening"
288						+ " recent.dtd:");
289					Log.log(Log.ERROR,this,e);
290				}
291			}
292
293			return null;
294		}
295
296		public void doctypeDecl(String name, String publicId,
297			String systemId) throws Exception
298		{
299			if("RECENT".equals(name))
300				return;
301
302			Log.log(Log.ERROR,this,"recent.xml: DOCTYPE must be RECENT");
303		}
304
305		public void endElement(String name)
306		{
307			if(name.equals("ENTRY"))
308			{
309				addEntry(new Entry(path,caret,selection,encoding));
310				path = null;
311				caret = 0;
312				selection = null;
313				encoding = null;
314			}
315			else if(name.equals("PATH"))
316				path = charData;
317			else if(name.equals("CARET"))
318				caret = Integer.parseInt(charData);
319			else if(name.equals("SELECTION"))
320				selection = charData;
321			else if(name.equals("ENCODING"))
322				encoding = charData;
323		}
324
325		public void charData(char[] ch, int start, int length)
326		{
327			charData = new String(ch,start,length);
328		}
329
330		// end HandlerBase implementation
331
332		// private members
333		private String path;
334		private int caret;
335		private String selection;
336		private String encoding;
337		private String charData;
338	}
339}