1 23 package org.gjt.sp.jedit.bufferio; 24 25 import org.gjt.sp.jedit.buffer.KillRing; 26 import org.gjt.sp.jedit.buffer.UndoManager; 27 import org.gjt.sp.jedit.jEdit; 28 import org.gjt.sp.jedit.MiscUtilities; 29 import org.gjt.sp.util.Log; 30 import org.gjt.sp.util.XMLUtilities; 31 32 import java.io.*; 33 34 38 public class JEditKillRing extends KillRing 39 { 40 public void load() 42 { 43 String settingsDirectory = jEdit.getSettingsDirectory(); 44 if(settingsDirectory == null) 45 return; 46 47 File killRing = new File(MiscUtilities.constructPath( 48 settingsDirectory,"killring.xml")); 49 if(!killRing.exists()) 50 return; 51 52 killRingModTime = killRing.lastModified(); 53 Log.log(Log.MESSAGE,KillRing.class,"Loading killring.xml"); 54 55 KillRingHandler handler = new KillRingHandler(); 56 try 57 { 58 XMLUtilities.parseXML(new FileInputStream(killRing), handler); 59 } 60 catch (IOException ioe) 61 { 62 Log.log(Log.ERROR, this, ioe); 63 } 64 65 ring = handler.list.toArray( 66 new UndoManager.Remove[handler.list.size()]); 67 count = 0; 68 wrap = true; 69 } 71 public void save() 73 { 74 String settingsDirectory = jEdit.getSettingsDirectory(); 75 if(settingsDirectory == null) 76 return; 77 78 File file1 = new File(MiscUtilities.constructPath( 79 settingsDirectory, "#killring.xml#save#")); 80 File file2 = new File(MiscUtilities.constructPath( 81 settingsDirectory, "killring.xml")); 82 if(file2.exists() && file2.lastModified() != killRingModTime) 83 { 84 Log.log(Log.WARNING,KillRing.class,file2 85 + " changed on disk; will not save recent" 86 + " files"); 87 return; 88 } 89 90 jEdit.backupSettingsFile(file2); 91 92 Log.log(Log.MESSAGE,KillRing.class,"Saving killring.xml"); 93 94 String lineSep = System.getProperty("line.separator"); 95 96 BufferedWriter out = null; 97 98 try 99 { 100 out = new BufferedWriter( 101 new OutputStreamWriter( 102 new FileOutputStream(file1), 103 "UTF-8")); 104 105 out.write("<?xml version=\"1.1\" encoding=\"UTF-8\"?>"); 106 out.write(lineSep); 107 out.write("<!DOCTYPE KILLRING SYSTEM \"killring.dtd\">"); 108 out.write(lineSep); 109 out.write("<KILLRING>"); 110 out.write(lineSep); 111 112 int size = getSize(); 113 for(int i = size - 1; i >=0; i--) 114 { 115 out.write("<ENTRY>"); 116 out.write(XMLUtilities.charsToEntities( 117 getElementAt(i).toString(),true)); 118 out.write("</ENTRY>"); 119 out.write(lineSep); 120 } 121 122 out.write("</KILLRING>"); 123 out.write(lineSep); 124 125 out.close(); 126 127 129 file2.delete(); 130 file1.renameTo(file2); 131 } 132 catch(Exception e) 133 { 134 Log.log(Log.ERROR,KillRing.class,e); 135 } 136 finally 137 { 138 try 139 { 140 if(out != null) 141 out.close(); 142 } 143 catch(IOException e) 144 { 145 } 146 } 147 148 killRingModTime = file2.lastModified(); 149 } } 151 | Popular Tags |