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