KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > FileHistory


1 /*
2  * FileHistory.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: FileHistory.java,v 1.5 2003/06/29 00:19:34 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.XMLReader JavaDoc;
33 import org.xml.sax.helpers.DefaultHandler JavaDoc;
34
35 public final class FileHistory extends DefaultHandler JavaDoc implements ContentHandler JavaDoc
36 {
37     private static final int MAX_ENTRIES = 100;
38
39     // Singleton.
40
private static FileHistory fileHistory;
41
42     private ArrayList JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc 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         // Add new entry.
90
list.add(0, newEntry);
91     }
92
93     private void load()
94     {
95         XMLReader JavaDoc xmlReader = Utilities.getDefaultXMLReader();
96         if (xmlReader != null) {
97             xmlReader.setContentHandler(this);
98             try {
99                 InputSource JavaDoc inputSource = new InputSource JavaDoc(file.getInputStream());
100                 xmlReader.parse(inputSource);
101             }
102             catch (Exception JavaDoc e) {
103                 Log.error(e);
104             }
105         }
106     }
107
108     private FileHistoryEntry currentEntry = null;
109
110     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
111         Attributes JavaDoc attributes) throws SAXException JavaDoc
112     {
113         if (localName.equals("files") || qName.equals("files")) {
114             String JavaDoc version = attributes.getValue("version");
115             if (!version.equals(getVersion()))
116                 throw new SAXException JavaDoc("Unknown file history format");
117         } else if (localName.equals("file") || qName.equals("file")) {
118             // Start a new entry.
119
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 JavaDoc e) {
127                 Log.error(e);
128             }
129         } else if (localName.equals("property") || qName.equals("property")) {
130             Debug.assertTrue(currentEntry != null);
131             String JavaDoc key = attributes.getValue("", "name");
132             String JavaDoc 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 JavaDoc uri, String JavaDoc localName, String JavaDoc 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 JavaDoc writer =
151                 new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(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 JavaDoc e) {
167             Log.error(e);
168         }
169     }
170
171     private static final String JavaDoc getVersion()
172     {
173         return "2";
174     }
175 }
176
Popular Tags