1 21 22 package org.armedbear.j; 23 24 import java.io.BufferedWriter ; 25 import java.io.IOException ; 26 import java.io.OutputStreamWriter ; 27 import java.util.ArrayList ; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.ContentHandler ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.XMLReader ; 33 import org.xml.sax.helpers.DefaultHandler ; 34 35 public final class FileHistory extends DefaultHandler implements ContentHandler 36 { 37 private static final int MAX_ENTRIES = 100; 38 39 private static FileHistory fileHistory; 41 42 private ArrayList list = new ArrayList (); 43 44 private File file; 45 46 private FileHistory() 47 { 48 file = File.getInstance(Directories.getEditorDirectory(), "files.xml"); 49 if (file.isFile()) 50 load(); 51 } 52 53 public static synchronized FileHistory getFileHistory() 54 { 55 if (fileHistory == null) { 56 fileHistory = new FileHistory(); 57 Editor.protect(fileHistory); 58 } 59 return fileHistory; 60 } 61 62 public synchronized FileHistoryEntry findEntry(String canonicalPath) 63 { 64 final int limit = list.size(); 65 for (int i = 0; i < limit; i++) { 66 FileHistoryEntry entry = (FileHistoryEntry) list.get(i); 67 if (entry.getName().equals(canonicalPath)) 68 return entry; 69 } 70 return null; 71 } 72 73 public synchronized void store(FileHistoryEntry newEntry) 74 { 75 Debug.assertTrue(newEntry != null); 76 Debug.assertTrue(newEntry.getName() != null); 77 final int limit = list.size(); 78 for (int i = 0; i < limit; i++) { 79 FileHistoryEntry entry = (FileHistoryEntry) list.get(i); 80 if (entry.getName().equals(newEntry.getName())) { 81 if (i == 0) { 82 list.set(0, newEntry); 83 return; 84 } 85 list.remove(i); 86 break; 87 } 88 } 89 list.add(0, newEntry); 91 } 92 93 private void load() 94 { 95 XMLReader xmlReader = Utilities.getDefaultXMLReader(); 96 if (xmlReader != null) { 97 xmlReader.setContentHandler(this); 98 try { 99 InputSource inputSource = new InputSource (file.getInputStream()); 100 xmlReader.parse(inputSource); 101 } 102 catch (Exception e) { 103 Log.error(e); 104 } 105 } 106 } 107 108 private FileHistoryEntry currentEntry = null; 109 110 public void startElement(String uri, String localName, String qName, 111 Attributes attributes) throws SAXException 112 { 113 if (localName.equals("files") || qName.equals("files")) { 114 String version = attributes.getValue("version"); 115 if (!version.equals(getVersion())) 116 throw new SAXException ("Unknown file history format"); 117 } else if (localName.equals("file") || qName.equals("file")) { 118 currentEntry = new FileHistoryEntry(); 120 currentEntry.setName(attributes.getValue("", "name")); 121 currentEntry.setEncoding(attributes.getValue("", "encoding")); 122 currentEntry.setMode(attributes.getValue("", "mode")); 123 try { 124 currentEntry.setWhen(Long.parseLong(attributes.getValue("when"))); 125 } 126 catch (NumberFormatException e) { 127 Log.error(e); 128 } 129 } else if (localName.equals("property") || qName.equals("property")) { 130 Debug.assertTrue(currentEntry != null); 131 String key = attributes.getValue("", "name"); 132 String value = attributes.getValue("", "value"); 133 Property property = Property.findProperty(key); 134 if (property != null) 135 currentEntry.setPropertyFromString(property, value); 136 } 137 } 138 139 public void endElement(String uri, String localName, String qName) 140 { 141 if (localName.equals("file") || qName.equals("file")) { 142 list.add(currentEntry); 143 currentEntry = null; 144 } 145 } 146 147 public synchronized void save() 148 { 149 try { 150 BufferedWriter writer = 151 new BufferedWriter (new OutputStreamWriter (file.getOutputStream())); 152 writer.write("<?xml version=\"1.0\"?>"); 153 writer.newLine(); 154 writer.write("<files version=\"" + getVersion() + "\">"); 155 writer.newLine(); 156 final int limit = Math.min(list.size(), MAX_ENTRIES); 157 for (int i = 0; i < limit; i++) { 158 FileHistoryEntry entry = (FileHistoryEntry) list.get(i); 159 writer.write(entry.toXml()); 160 writer.newLine(); 161 } 162 writer.write("</files>"); 163 writer.flush(); 164 writer.close(); 165 } 166 catch (IOException e) { 167 Log.error(e); 168 } 169 } 170 171 private static final String getVersion() 172 { 173 return "2"; 174 } 175 } 176 | Popular Tags |