001: /*
002: * FileHistory.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: FileHistory.java,v 1.5 2003/06/29 00:19:34 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.BufferedWriter;
025: import java.io.IOException;
026: import java.io.OutputStreamWriter;
027: import java.util.ArrayList;
028: import org.xml.sax.Attributes;
029: import org.xml.sax.ContentHandler;
030: import org.xml.sax.InputSource;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.XMLReader;
033: import org.xml.sax.helpers.DefaultHandler;
034:
035: public final class FileHistory extends DefaultHandler implements
036: ContentHandler {
037: private static final int MAX_ENTRIES = 100;
038:
039: // Singleton.
040: private static FileHistory fileHistory;
041:
042: private ArrayList list = new ArrayList();
043:
044: private File file;
045:
046: private FileHistory() {
047: file = File.getInstance(Directories.getEditorDirectory(),
048: "files.xml");
049: if (file.isFile())
050: load();
051: }
052:
053: public static synchronized FileHistory getFileHistory() {
054: if (fileHistory == null) {
055: fileHistory = new FileHistory();
056: Editor.protect(fileHistory);
057: }
058: return fileHistory;
059: }
060:
061: public synchronized FileHistoryEntry findEntry(String canonicalPath) {
062: final int limit = list.size();
063: for (int i = 0; i < limit; i++) {
064: FileHistoryEntry entry = (FileHistoryEntry) list.get(i);
065: if (entry.getName().equals(canonicalPath))
066: return entry;
067: }
068: return null;
069: }
070:
071: public synchronized void store(FileHistoryEntry newEntry) {
072: Debug.assertTrue(newEntry != null);
073: Debug.assertTrue(newEntry.getName() != null);
074: final int limit = list.size();
075: for (int i = 0; i < limit; i++) {
076: FileHistoryEntry entry = (FileHistoryEntry) list.get(i);
077: if (entry.getName().equals(newEntry.getName())) {
078: if (i == 0) {
079: list.set(0, newEntry);
080: return;
081: }
082: list.remove(i);
083: break;
084: }
085: }
086: // Add new entry.
087: list.add(0, newEntry);
088: }
089:
090: private void load() {
091: XMLReader xmlReader = Utilities.getDefaultXMLReader();
092: if (xmlReader != null) {
093: xmlReader.setContentHandler(this );
094: try {
095: InputSource inputSource = new InputSource(file
096: .getInputStream());
097: xmlReader.parse(inputSource);
098: } catch (Exception e) {
099: Log.error(e);
100: }
101: }
102: }
103:
104: private FileHistoryEntry currentEntry = null;
105:
106: public void startElement(String uri, String localName,
107: String qName, Attributes attributes) throws SAXException {
108: if (localName.equals("files") || qName.equals("files")) {
109: String version = attributes.getValue("version");
110: if (!version.equals(getVersion()))
111: throw new SAXException("Unknown file history format");
112: } else if (localName.equals("file") || qName.equals("file")) {
113: // Start a new entry.
114: currentEntry = new FileHistoryEntry();
115: currentEntry.setName(attributes.getValue("", "name"));
116: currentEntry.setEncoding(attributes
117: .getValue("", "encoding"));
118: currentEntry.setMode(attributes.getValue("", "mode"));
119: try {
120: currentEntry.setWhen(Long.parseLong(attributes
121: .getValue("when")));
122: } catch (NumberFormatException e) {
123: Log.error(e);
124: }
125: } else if (localName.equals("property")
126: || qName.equals("property")) {
127: Debug.assertTrue(currentEntry != null);
128: String key = attributes.getValue("", "name");
129: String value = attributes.getValue("", "value");
130: Property property = Property.findProperty(key);
131: if (property != null)
132: currentEntry.setPropertyFromString(property, value);
133: }
134: }
135:
136: public void endElement(String uri, String localName, String qName) {
137: if (localName.equals("file") || qName.equals("file")) {
138: list.add(currentEntry);
139: currentEntry = null;
140: }
141: }
142:
143: public synchronized void save() {
144: try {
145: BufferedWriter writer = new BufferedWriter(
146: new OutputStreamWriter(file.getOutputStream()));
147: writer.write("<?xml version=\"1.0\"?>");
148: writer.newLine();
149: writer.write("<files version=\"" + getVersion() + "\">");
150: writer.newLine();
151: final int limit = Math.min(list.size(), MAX_ENTRIES);
152: for (int i = 0; i < limit; i++) {
153: FileHistoryEntry entry = (FileHistoryEntry) list.get(i);
154: writer.write(entry.toXml());
155: writer.newLine();
156: }
157: writer.write("</files>");
158: writer.flush();
159: writer.close();
160: } catch (IOException e) {
161: Log.error(e);
162: }
163: }
164:
165: private static final String getVersion() {
166: return "2";
167: }
168: }
|