PageRenderTime 83ms CodeModel.GetById 66ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/JEditKillRing.java

#
Java | 224 lines | 164 code | 28 blank | 32 comment | 17 complexity | dbf5b3f5e348a817f9e6094ad649643b MD5 | raw file
  1/*
  2 * :tabSize=8:indentSize=8:noTabs=false:
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * Copyright (C) 2003, 2005 Slava Pestov
  6 * Portions copyright (C) 2006, 2011 Matthieu Casanova
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22package org.gjt.sp.jedit;
 23
 24import java.io.File;
 25import java.util.List;
 26import java.util.LinkedList;
 27import java.io.IOException;
 28
 29import org.xml.sax.Attributes;
 30import org.xml.sax.InputSource;
 31import org.xml.sax.helpers.DefaultHandler;
 32
 33import org.gjt.sp.jedit.buffer.KillRing;
 34import org.gjt.sp.util.Log;
 35import org.gjt.sp.util.XMLUtilities;
 36import org.gjt.sp.util.IOUtilities;
 37
 38import javax.swing.*;
 39
 40/**
 41 * The basic KillRing of jEdit.
 42 * @author Matthieu Casanova
 43 * @version $Id: ParserRuleSet.java 5471 2006-06-22 06:31:23Z kpouer $
 44 */
 45class JEditKillRing extends KillRing
 46{
 47	//{{{ Constructor
 48	JEditKillRing()
 49	{
 50		String settingsDirectory = jEdit.getSettingsDirectory();
 51		if(settingsDirectory != null)
 52		{
 53			killringXML = new SettingsXML(settingsDirectory, "killring");
 54		}
 55	} //}}}
 56
 57	//{{{ load() method
 58	@Override
 59	public void load()
 60	{
 61		if(killringXML == null)
 62			return;
 63
 64		if(!killringXML.fileExists())
 65			return;
 66
 67		Log.log(Log.MESSAGE,KillRing.class,"Loading " + killringXML);
 68
 69		KillRingHandler handler = new KillRingHandler();
 70		try
 71		{
 72			killringXML.load(handler);
 73		}
 74		catch (OutOfMemoryError oem)
 75		{
 76			Log.log(Log.ERROR, this, "Unable to load entire Killring, too low memory, increase your jvm max heap size");
 77			String start = jEdit.getProperty("killring.start");
 78			String deleteKillRing = jEdit.getProperty("killring.delete");
 79			String stop = jEdit.getProperty("killring.stop");
 80			int selected = GUIUtilities.option(null, "killring.load-memoryerror", null,
 81							      JOptionPane.ERROR_MESSAGE,
 82							      new Object[]{start, deleteKillRing, stop}, start);
 83			if (selected == 2)
 84			{
 85				System.exit(-1);
 86			}
 87			else if (selected == 1)
 88			{
 89				new File(MiscUtilities.constructPath(
 90					jEdit.getSettingsDirectory(),"killring.xml")).delete();
 91				return;
 92			}
 93
 94		}
 95		catch (IOException ioe)
 96		{
 97			Log.log(Log.ERROR, this, ioe);
 98		}
 99		reset(handler.list);
100	} //}}}
101
102	//{{{ save() method
103	@Override
104	public void save()
105	{
106		if(killringXML == null)
107			return;
108
109		if(killringXML.hasChangedOnDisk())
110		{
111			Log.log(Log.WARNING,KillRing.class,killringXML
112				+ " changed on disk; will not save killring"
113				+ " files");
114			return;
115		}
116
117		Log.log(Log.MESSAGE,KillRing.class,"Saving " + killringXML);
118
119		String lineSep = System.getProperty("line.separator");
120
121		SettingsXML.Saver out = null;
122
123		try
124		{
125			out = killringXML.openSaver();
126			out.writeXMLDeclaration("1.1");
127
128			out.write("<!DOCTYPE KILLRING SYSTEM \"killring.dtd\">");
129			out.write(lineSep);
130			out.write("<KILLRING>");
131			out.write(lineSep);
132
133			int size = getSize();
134			for(int i = size - 1; i >=0; i--)
135			{
136				out.write("<ENTRY>");
137				out.write(XMLUtilities.charsToEntities(
138					getElementAt(i).toString(),true));
139				out.write("</ENTRY>");
140				out.write(lineSep);
141			}
142
143			out.write("</KILLRING>");
144			out.write(lineSep);
145
146			out.finish();
147		}
148		catch(Exception e)
149		{
150			Log.log(Log.ERROR,KillRing.class,e);
151		}
152		finally
153		{
154			IOUtilities.closeQuietly(out);
155		}
156	} //}}}
157
158	//{{{ Private members
159	private SettingsXML killringXML;
160
161	//{{{ KillRingHandler class
162	private static class KillRingHandler extends DefaultHandler
163	{
164		public List<String> list = new LinkedList<String>();
165
166		@Override
167		public InputSource resolveEntity(String publicId, String systemId)
168		{
169			return XMLUtilities.findEntity(systemId, "killring.dtd", getClass());
170		}
171
172		@Override
173		public void startElement(String uri, String localName,
174					 String qName, Attributes attrs)
175		{
176			inEntry = qName.equals("ENTRY");
177		}
178
179		@Override
180		public void endElement(String uri, String localName, String name)
181		{
182			if(name.equals("ENTRY"))
183			{
184				list.add(charData.toString());
185				inEntry = false;
186				charData.setLength(0);
187			}
188		}
189
190		@Override
191		public void characters(char[] ch, int start, int length)
192		{
193			if (inEntry)
194				charData.append(ch, start, length);
195		}
196
197		@Override
198		public void processingInstruction(String target, String data)
199		{
200			if ("illegal-xml-character".equals(target))
201			{
202				char ch;
203				try
204				{
205					ch = (char)Integer.parseInt(data.trim());
206				}
207				catch (Exception e)
208				{
209					Log.log(Log.ERROR, this,
210						"Failed to get character from PI"
211							+ "\"" + target + "\""
212							+ " with \"" + data + "\""
213							+ ": " + e);
214					return;
215				}
216				characters(new char[] {ch}, 0, 1);
217			}
218		}
219
220		private final StringBuilder charData = new StringBuilder();
221		private boolean inEntry;
222	} //}}}
223	//}}}
224}