001: /*
002: * JdbSession.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: JdbSession.java,v 1.5 2003/06/29 17:50:41 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.jdb;
023:
024: import java.io.BufferedWriter;
025: import java.io.EOFException;
026: import java.io.IOException;
027: import java.io.OutputStream;
028: import java.io.OutputStreamWriter;
029: import java.util.ArrayList;
030: import java.util.Enumeration;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Properties;
034: import org.armedbear.j.Directories;
035: import org.armedbear.j.Editor;
036: import org.armedbear.j.File;
037: import org.armedbear.j.Log;
038: import org.armedbear.j.Utilities;
039: import org.xml.sax.Attributes;
040: import org.xml.sax.ContentHandler;
041: import org.xml.sax.InputSource;
042: import org.xml.sax.SAXException;
043: import org.xml.sax.XMLReader;
044: import org.xml.sax.helpers.DefaultHandler;
045:
046: public final class JdbSession extends Properties {
047: private List breakpointSpecifications;
048: private List breakpoints;
049:
050: public JdbSession() {
051: }
052:
053: public String getName() {
054: return getProperty("name", "");
055: }
056:
057: private static File jdbDir;
058:
059: private static File getSettingsDirectory() {
060: if (jdbDir == null) {
061: jdbDir = File.getInstance(Directories.getEditorDirectory(),
062: "jdb");
063: if (!jdbDir.isDirectory())
064: jdbDir.mkdirs();
065: }
066: return jdbDir;
067: }
068:
069: private static File sessionDir;
070:
071: private File getDefaultSessionFile() {
072: return File.getInstance(getSettingsDirectory(), "defaults.xml");
073: }
074:
075: public static String[] getSessionNames() {
076: return sessionDir.list();
077: }
078:
079: public static void deleteSession(String name) {
080: File file = File.getInstance(sessionDir, name);
081: if (file.isFile())
082: file.delete();
083: }
084:
085: public String getMainClass() {
086: return getProperty("mainClass", "");
087: }
088:
089: public void setMainClass(String s) {
090: put("mainClass", s);
091: }
092:
093: public String getMainClassArgs() {
094: return getProperty("mainClassArgs", "");
095: }
096:
097: public void setMainClassArgs(String s) {
098: put("mainClassArgs", s);
099: }
100:
101: public String getClassPath() {
102: return getProperty("classPath", "");
103: }
104:
105: public void setClassPath(String s) {
106: put("classPath", s);
107: }
108:
109: public String getJavaHome() {
110: return getProperty("javaHome", "");
111: }
112:
113: public void setJavaHome(String s) {
114: put("javaHome", s);
115: }
116:
117: public String getJavaExecutable() {
118: return getProperty("javaExecutable", "");
119: }
120:
121: public void setJavaExecutable(String s) {
122: put("javaExecutable", s);
123: }
124:
125: public String getVMArgs() {
126: return getProperty("vmArgs", "");
127: }
128:
129: public void setVMArgs(String s) {
130: put("vmArgs", s);
131: }
132:
133: public boolean getStartSuspended() {
134: String s = getProperty("startSuspended");
135: return s != null && s.equals("true");
136: }
137:
138: public void setStartSuspended(boolean b) {
139: put("startSuspended", b ? "true" : "false");
140: }
141:
142: public String getSourcePath() {
143: return getProperty("sourcePath", "");
144: }
145:
146: public void setSourcePath(String s) {
147: put("sourcePath", s);
148: }
149:
150: public void saveDefaults() {
151: save(getDefaultSessionFile());
152: }
153:
154: public void loadDefaults() {
155: clear();
156: File file = getDefaultSessionFile();
157: if (file != null && file.isFile())
158: load(file);
159: }
160:
161: private void save(File file) {
162: try {
163: OutputStream out = file.getOutputStream();
164: BufferedWriter writer = new BufferedWriter(
165: new OutputStreamWriter(out));
166: writer.write("<?xml version=\"1.0\"?>");
167: writer.newLine();
168: writer.write("<session version=\"" + getVersion() + "\">");
169: writer.newLine();
170: Enumeration propertyNames = propertyNames();
171: while (propertyNames.hasMoreElements()) {
172: String name = (String) propertyNames.nextElement();
173: String value = getProperty(name);
174: writer.write(" ");
175: writer.write(Utilities.propertyToXml(name, value));
176: writer.newLine();
177: }
178: saveBreakpoints(writer);
179: writer.write("</session>");
180: writer.flush();
181: writer.close();
182: } catch (IOException e) {
183: Log.error(e);
184: }
185: }
186:
187: private void saveBreakpoints(BufferedWriter writer) {
188: if (breakpoints != null && breakpoints.size() > 0) {
189: try {
190: writer.write(" <breakpoints>");
191: writer.newLine();
192: Iterator iter = breakpoints.iterator();
193: while (iter.hasNext()) {
194: Object obj = iter.next();
195: if (obj instanceof MethodBreakpoint) {
196: MethodBreakpoint bp = (MethodBreakpoint) obj;
197: if (!bp.isTemporary())
198: writer.write(bp.toXml());
199: } else if (obj instanceof LineNumberBreakpoint) {
200: LineNumberBreakpoint bp = (LineNumberBreakpoint) obj;
201: if (!bp.isTemporary())
202: writer.write(bp.toXml());
203: }
204: }
205: writer.write(" </breakpoints>");
206: writer.newLine();
207: } catch (IOException e) {
208: Log.error(e);
209: }
210: }
211: }
212:
213: private void load(File file) {
214: XMLReader xmlReader = Utilities.getDefaultXMLReader();
215: if (xmlReader != null) {
216: xmlReader.setContentHandler(new Handler());
217: try {
218: InputSource inputSource = new InputSource(file
219: .getInputStream());
220: xmlReader.parse(inputSource);
221: } catch (EOFException ignored) {
222: } catch (Exception e) {
223: Log.error(e);
224: }
225: }
226: }
227:
228: public List getBreakpointSpecifications() {
229: return breakpointSpecifications;
230: }
231:
232: public void setBreakpoints(List breakpoints) {
233: this .breakpoints = breakpoints;
234: }
235:
236: private static final String getVersion() {
237: return "1";
238: }
239:
240: private class Handler extends DefaultHandler implements
241: ContentHandler {
242: public void startElement(String uri, String localName,
243: String qName, Attributes attributes)
244: throws SAXException {
245: if (localName.equals("session") || qName.equals("session")) {
246: String version = attributes.getValue("version");
247: if (!version.equals(getVersion()))
248: throw new SAXException("Unknown session format");
249: } else if (localName.equals("property")
250: || qName.equals("property")) {
251: // Session property.
252: String propertyName = attributes.getValue("name");
253: String value = attributes.getValue("value");
254: setProperty(propertyName, value);
255: } else if (localName.equals("breakpoints")
256: || qName.equals("breakpoints")) {
257: breakpointSpecifications = new ArrayList();
258: } else if (localName.equals("breakpoint")
259: || qName.equals("breakpoint")) {
260: BreakpointSpecification spec = new BreakpointSpecification(
261: attributes);
262: breakpointSpecifications.add(spec);
263: }
264: }
265: }
266: }
|