001: /*
002: * :tabSize=8:indentSize=8:noTabs=false:
003: * :folding=explicit:collapseFolds=1:
004: *
005: * Copyright (C) 2003, 2005 Slava Pestov
006: * Portions copyright (C) 2006 Matthieu Casanova
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package org.gjt.sp.jedit;
023:
024: import java.util.List;
025: import java.util.LinkedList;
026: import java.io.IOException;
027:
028: import org.xml.sax.Attributes;
029: import org.xml.sax.InputSource;
030: import org.xml.sax.helpers.DefaultHandler;
031:
032: import org.gjt.sp.jedit.buffer.KillRing;
033: import org.gjt.sp.util.Log;
034: import org.gjt.sp.util.XMLUtilities;
035: import org.gjt.sp.util.IOUtilities;
036:
037: /**
038: * The basic KillRing of jEdit.
039: * @author Matthieu Casanova
040: * @version $Id: ParserRuleSet.java 5471 2006-06-22 06:31:23Z kpouer $
041: */
042: class JEditKillRing extends KillRing {
043: //{{{ Constructor
044: public JEditKillRing() {
045: String settingsDirectory = jEdit.getSettingsDirectory();
046: if (settingsDirectory != null) {
047: killringXML = new SettingsXML(settingsDirectory, "killring");
048: }
049: } //}}}
050:
051: //{{{ load() method
052: public void load() {
053: if (killringXML == null)
054: return;
055:
056: if (!killringXML.fileExists())
057: return;
058:
059: Log.log(Log.MESSAGE, KillRing.class, "Loading " + killringXML);
060:
061: KillRingHandler handler = new KillRingHandler();
062: try {
063: killringXML.load(handler);
064: } catch (IOException ioe) {
065: Log.log(Log.ERROR, this , ioe);
066: }
067: reset(handler.list);
068: } //}}}
069:
070: //{{{ save() method
071: public void save() {
072: if (killringXML == null)
073: return;
074:
075: if (killringXML.hasChangedOnDisk()) {
076: Log.log(Log.WARNING, KillRing.class, killringXML
077: + " changed on disk; will not save killring"
078: + " files");
079: return;
080: }
081:
082: Log.log(Log.MESSAGE, KillRing.class, "Saving " + killringXML);
083:
084: String lineSep = System.getProperty("line.separator");
085:
086: SettingsXML.Saver out = null;
087:
088: try {
089: out = killringXML.openSaver();
090: out.writeXMLDeclaration("1.1");
091:
092: out.write("<!DOCTYPE KILLRING SYSTEM \"killring.dtd\">");
093: out.write(lineSep);
094: out.write("<KILLRING>");
095: out.write(lineSep);
096:
097: int size = getSize();
098: for (int i = size - 1; i >= 0; i--) {
099: out.write("<ENTRY>");
100: out.write(XMLUtilities.charsToEntities(getElementAt(i)
101: .toString(), true));
102: out.write("</ENTRY>");
103: out.write(lineSep);
104: }
105:
106: out.write("</KILLRING>");
107: out.write(lineSep);
108:
109: out.finish();
110: } catch (Exception e) {
111: Log.log(Log.ERROR, KillRing.class, e);
112: } finally {
113: IOUtilities.closeQuietly(out);
114: }
115: } //}}}
116:
117: //{{{ Private members
118: private SettingsXML killringXML;
119:
120: //{{{ KillRingHandler class
121: private static class KillRingHandler extends DefaultHandler {
122: public List<String> list = new LinkedList<String>();
123:
124: public InputSource resolveEntity(String publicId,
125: String systemId) {
126: return XMLUtilities.findEntity(systemId, "killring.dtd",
127: getClass());
128: }
129:
130: public void startElement(String uri, String localName,
131: String qName, Attributes attrs) {
132: inEntry = qName.equals("ENTRY");
133: }
134:
135: public void endElement(String uri, String localName, String name) {
136: if (name.equals("ENTRY")) {
137: list.add(charData.toString());
138: inEntry = false;
139: charData.setLength(0);
140: }
141: }
142:
143: public void characters(char[] ch, int start, int length) {
144: if (inEntry)
145: charData.append(ch, start, length);
146: }
147:
148: private StringBuffer charData = new StringBuffer();
149: private boolean inEntry;
150: } //}}}
151: //}}}
152: }
|