001: /*
002: * :tabSize=8:indentSize=8:noTabs=false:
003: * :folding=explicit:collapseFolds=1:
004: *
005: * Copyright (C) 2006 Matthieu Casanova
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 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: package org.gjt.sp.jedit;
022:
023: import org.gjt.sp.util.Log;
024: import org.gjt.sp.util.XMLUtilities;
025: import org.gjt.sp.util.IOUtilities;
026: import org.xml.sax.helpers.DefaultHandler;
027: import org.xml.sax.InputSource;
028: import org.xml.sax.Attributes;
029:
030: import java.io.IOException;
031:
032: /**
033: * The concrete RegisterSaver for jEdit.
034: * @author Matthieu Casanova
035: * @version $Id: FoldHandler.java 5568 2006-07-10 20:52:23Z kpouer $
036: */
037: class JEditRegisterSaver implements RegisterSaver {
038: //{{{ Constructor
039: public JEditRegisterSaver() {
040: String settingsDirectory = jEdit.getSettingsDirectory();
041: if (settingsDirectory != null) {
042: registersXML = new SettingsXML(settingsDirectory,
043: "registers");
044: }
045: } //}}}
046:
047: //{{{ loadRegisters() method
048: public void loadRegisters() {
049: if (registersXML == null)
050: return;
051:
052: if (!registersXML.fileExists())
053: return;
054:
055: Log.log(Log.MESSAGE, jEdit.class, "Loading " + registersXML);
056:
057: RegistersHandler handler = new RegistersHandler();
058: try {
059: Registers.setLoading(true);
060: registersXML.load(handler);
061: } catch (IOException ioe) {
062: Log.log(Log.ERROR, Registers.class, ioe);
063: } finally {
064: Registers.setLoading(false);
065: }
066: } //}}}
067:
068: //{{{ saveRegisters() method
069: public void saveRegisters() {
070: if (registersXML == null)
071: return;
072:
073: if (registersXML.hasChangedOnDisk()) {
074: Log.log(Log.WARNING, Registers.class, registersXML
075: + " changed on disk; will not save registers");
076: return;
077: }
078:
079: Log.log(Log.MESSAGE, Registers.class, "Saving " + registersXML);
080:
081: String lineSep = System.getProperty("line.separator");
082:
083: SettingsXML.Saver out = null;
084:
085: try {
086: out = registersXML.openSaver();
087: out.writeXMLDeclaration();
088:
089: out.write("<!DOCTYPE REGISTERS SYSTEM \"registers.dtd\">");
090: out.write(lineSep);
091: out.write("<REGISTERS>");
092: out.write(lineSep);
093:
094: Registers.Register[] registers = Registers.getRegisters();
095: for (int i = 0; i < registers.length; i++) {
096: Registers.Register register = registers[i];
097: if (register == null || i == '$' || i == '%'
098: || register.toString().length() == 0)
099: continue;
100:
101: out.write("<REGISTER NAME=\"");
102: if (i == '"')
103: out.write(""");
104: else
105: out.write((char) i);
106: out.write("\">");
107:
108: out.write(XMLUtilities.charsToEntities(register
109: .toString(), false));
110:
111: out.write("</REGISTER>");
112: out.write(lineSep);
113: }
114:
115: out.write("</REGISTERS>");
116: out.write(lineSep);
117:
118: out.finish();
119: } catch (Exception e) {
120: Log.log(Log.ERROR, Registers.class, e);
121: } finally {
122: IOUtilities.closeQuietly(out);
123: }
124: } //}}}
125:
126: //{{{ Private members
127: private SettingsXML registersXML;
128:
129: //{{{ RegistersHandler class
130: private static class RegistersHandler extends DefaultHandler {
131: //{{{ resolveEntity() method
132: public InputSource resolveEntity(String publicId,
133: String systemId) {
134: return XMLUtilities.findEntity(systemId, "registers.dtd",
135: getClass());
136: } //}}}
137:
138: //{{{ startElement() method
139: public void startElement(String uri, String localName,
140: String qName, Attributes attrs) {
141: registerName = attrs.getValue("NAME");
142: inRegister = "REGISTER".equals(qName);
143: } //}}}
144:
145: //{{{ endElement() method
146: public void endElement(String uri, String localName, String name) {
147: if (name.equals("REGISTER")) {
148: if (registerName == null || registerName.length() != 1)
149: Log.log(Log.ERROR, this , "Malformed NAME: "
150: + registerName);
151: else
152: Registers.setRegister(registerName.charAt(0),
153: charData.toString());
154: inRegister = false;
155: charData.setLength(0);
156: }
157: } //}}}
158:
159: //{{{ characters() method
160: public void characters(char[] ch, int start, int length) {
161: if (inRegister)
162: charData.append(ch, start, length);
163: } //}}}
164:
165: //{{{ Private members
166: private String registerName;
167: private StringBuffer charData = new StringBuffer();
168: private boolean inRegister;
169: //}}}
170: } //}}}
171: //}}}
172: }
|