001: /*
002: * PluginListHandler.java - XML handler for the plugin list
003: * Copyright (C) 2001 Slava Pestov
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package org.gjt.sp.jedit.pluginmgr;
021:
022: import java.util.Stack;
023:
024: import org.xml.sax.Attributes;
025: import org.xml.sax.InputSource;
026: import org.xml.sax.helpers.DefaultHandler;
027:
028: import org.gjt.sp.util.Log;
029: import org.gjt.sp.util.XMLUtilities;
030:
031: /**
032: * @version $Id: PluginListHandler.java 8303 2007-01-02 21:50:43Z kpouer $
033: */
034:
035: class PluginListHandler extends DefaultHandler {
036: PluginListHandler(PluginList pluginList, String path) {
037: this .pluginList = pluginList;
038: this .path = path;
039:
040: author = new StringBuffer();
041: description = new StringBuffer();
042: pluginSetEntry = new StringBuffer();
043: download = new StringBuffer();
044: downloadSource = new StringBuffer();
045: }
046:
047: public InputSource resolveEntity(String publicId, String systemId) {
048: return XMLUtilities.findEntity(systemId, "plugins.dtd",
049: getClass());
050: }
051:
052: public void attribute(String aname, String value,
053: boolean isSpecified) {
054: if (aname == "NAME")
055: name = value;
056: else if (aname == "JAR")
057: jar = value;
058: else if (aname == "VERSION")
059: version = value;
060: else if (aname == "DATE")
061: date = value;
062: else if (aname == "OBSOLETE")
063: obsolete = ("TRUE".equals(value));
064: else if (aname == "WHAT")
065: depWhat = value;
066: else if (aname == "FROM")
067: depFrom = value;
068: else if (aname == "TO")
069: depTo = value;
070: else if (aname == "PLUGIN")
071: depPlugin = value;
072: else if (aname == "SIZE") {
073: size = Integer.parseInt(value);
074: if (size == 0)
075: Log.log(Log.WARNING, this , "SIZE = 0");
076: }
077: }
078:
079: public void characters(char[] c, int off, int len) {
080: String tag = peekElement();
081:
082: if (tag.equals("DESCRIPTION")) {
083: description.append(c, off, len);
084: } else if (tag.equals("PLUGIN_SET_ENTRY"))
085: pluginSetEntry.append(c, off, len);
086: else if (tag.equals("AUTHOR")) {
087: if (author.length() != 0)
088: author.append(", ");
089: author.append(c, off, len);
090: } else if (tag.equals("DOWNLOAD"))
091: download.append(c, off, len);
092: else if (tag.equals("DOWNLOAD_SOURCE"))
093: downloadSource.append(c, off, len);
094: }
095:
096: public void startElement(String uri, String localName, String tag,
097: Attributes attrs) {
098: for (int i = 0; i < attrs.getLength(); i++) {
099: String aName = attrs.getQName(i);
100: String aValue = attrs.getValue(i);
101: attribute(aName, aValue, true);
102: }
103:
104: tag = pushElement(tag);
105:
106: if (tag.equals("PLUGIN_SET")) {
107: description.setLength(0);
108: pluginSet = new PluginList.PluginSet();
109: pluginSet.name = name;
110: } else if (tag.equals("PLUGIN")) {
111: description.setLength(0);
112: author.setLength(0);
113: branch = null;
114: plugin = new PluginList.Plugin();
115: } else if (tag.equals("BRANCH")) {
116: download.setLength(0);
117: branch = new PluginList.Branch();
118: } else if (tag.equals("DOWNLOAD"))
119: downloadSize = size;
120: else if (tag.equals("DOWNLOAD_SOURCE"))
121: downloadSourceSize = size;
122: }
123:
124: public void endElement(String uri, String localName, String tag) {
125: popElement();
126:
127: if (tag.equals("PLUGIN_SET")) {
128: pluginList.addPluginSet(pluginSet);
129: pluginSet = null;
130: pluginSetEntry.setLength(0);
131: } else if (tag.equals("PLUGIN_SET_ENTRY")) {
132: pluginSet.plugins.add(pluginSetEntry.toString());
133: pluginSetEntry.setLength(0);
134: } else if (tag.equals("PLUGIN")) {
135: plugin.jar = jar;
136: plugin.name = name;
137: plugin.author = author.toString();
138: plugin.description = description.toString();
139: pluginList.addPlugin(plugin);
140: jar = null;
141: name = null;
142: author.setLength(0);
143: description.setLength(0);
144: } else if (tag.equals("BRANCH")) {
145: branch.version = version;
146: branch.date = date;
147: branch.download = download.toString();
148: branch.downloadSize = downloadSize;
149: branch.downloadSource = downloadSource.toString();
150: branch.downloadSourceSize = downloadSourceSize;
151: branch.obsolete = obsolete;
152: plugin.branches.add(branch);
153: version = null;
154: download.setLength(0);
155: downloadSource.setLength(0);
156: obsolete = false;
157: } else if (tag.equals("DEPEND")) {
158: PluginList.Dependency dep = new PluginList.Dependency(
159: depWhat, depFrom, depTo, depPlugin);
160: branch.deps.add(dep);
161: depWhat = null;
162: depFrom = null;
163: depTo = null;
164: depPlugin = null;
165: }
166: }
167:
168: public void startDocument() {
169: try {
170: pushElement(null);
171: } catch (Exception e) {
172: e.printStackTrace();
173: }
174: }
175:
176: public void endDocument() {
177: pluginList.finished();
178: }
179:
180: // end HandlerBase implementation
181:
182: // private members
183: private String path;
184:
185: private PluginList pluginList;
186:
187: private PluginList.PluginSet pluginSet;
188: private StringBuffer pluginSetEntry;
189:
190: private PluginList.Plugin plugin;
191: private String jar;
192: private StringBuffer author;
193:
194: private PluginList.Branch branch;
195: private boolean obsolete;
196: private String version;
197: private String date;
198: private StringBuffer download;
199: private int downloadSize;
200: private StringBuffer downloadSource;
201: private int downloadSourceSize;
202: private int size;
203: private String depWhat;
204: private String depFrom;
205: private String depTo;
206: private String depPlugin;
207:
208: private String name;
209: private StringBuffer description;
210:
211: private final Stack<String> stateStack = new Stack<String>();
212:
213: private String pushElement(String name) {
214: stateStack.push(name);
215: return name;
216: }
217:
218: private String peekElement() {
219: return stateStack.peek();
220: }
221:
222: private String popElement() {
223: return stateStack.pop();
224: }
225: }
|