001: /*
002: * Copyright (C) 2001-2003 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: Plugin.java$
021: * $FileID: 4682$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 8/8/03 11:24 PM$
026: * $Comment: Add ability to save values to XML Element.$
027: */
028: package org.sourcejammer.client.gui.conf;
029:
030: import java.util.Enumeration;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.StringTokenizer;
034:
035: import org.sourcejammer.client.plugin.SJClientPlugin;
036: import org.sourcejammer.util.ConfigurationException;
037: import org.sourcejammer.util.EnumeratedType;
038: import org.sourcejammer.xml.XMLNodeDoesNotExistException;
039: import org.sourcejammer.xml.XMLUtil;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.Node;
042: import org.w3c.dom.NodeList;
043:
044: /**
045: * Title: $FileName: Plugin.java$
046: * @version $VerNum: 3$
047: * @author $AuthorName: Rob MacGrogan$<br><br>
048: *
049: * $Description: $<br>
050: * $KeyWordsOff: $<br>
051: */
052: public class Plugin {
053:
054: private String name = null;
055: private String pluginClass = null;
056: private HashMap types = new HashMap();
057: private HashMap params = new HashMap();
058: private SJClientPlugin plugin = null;
059: private Element definition = null;
060:
061: public static final class PluginType {
062: public static final String ACTION = "Action";
063: public static final String FILE_EVENT = "FileEvent";
064: public static final String PROJECT_EVENT = "ProjectEvent";
065: }
066:
067: public static final class XMLNodes {
068: public static final String PLUGINS = "plugins";
069: public static final String PLUGIN = "plugin";
070: public static final String PLUGIN_NAME = "name";
071: public static final String PLUGIN_CLASS = "class";
072: public static final String PLUGIN_TYPE = "type";
073: public static final String PLUGIN_PARAMS = "params";
074: public static final String PLUGIN_PARAM = "param";
075: public static final String PLUGIN_PARAM_NAME = "param-name";
076: public static final String PLUGIN_PARAM_VALUE = "param-value";
077: }
078:
079: public Plugin(Element pluginElement) {
080: definition = pluginElement;
081: name = pluginElement.getAttribute(XMLNodes.PLUGIN_NAME);
082: if (name == null || name.equals("")) {
083: throw new ConfigurationException(
084: "Name attribute missing in plugin definition.");
085: }
086:
087: pluginClass = pluginElement.getAttribute(XMLNodes.PLUGIN_CLASS);
088: if (pluginClass == null || pluginClass.equals("")) {
089: throw new ConfigurationException(
090: "Class attribute missing in plugin definition.");
091: }
092:
093: String types = pluginElement.getAttribute(XMLNodes.PLUGIN_TYPE);
094: if (types == null || types.equals("")) {
095: throw new ConfigurationException(
096: "Type attribute missing in plugin definition.");
097: }
098: parseTypes(types);
099:
100: //Try to load the class
101: try {
102: Class cl = Class.forName(pluginClass);
103: Object o = cl.newInstance();
104: if (o instanceof SJClientPlugin) {
105: plugin = (SJClientPlugin) o;
106: plugin.initPlugin(params);
107: } else {
108: throw new ConfigurationException(
109: "Specified plugin class, "
110: + pluginClass
111: + ", does not implement SJClientPlugin.");
112: }
113: } catch (ClassNotFoundException e) {
114: throw new ConfigurationException(
115: "Could not load plugin class " + pluginClass + ".",
116: e);
117: } catch (InstantiationException e) {
118: throw new ConfigurationException(
119: "Could not load plugin class " + pluginClass + ".",
120: e);
121: } catch (IllegalAccessException e) {
122: throw new ConfigurationException(
123: "Could not load plugin class " + pluginClass + ".",
124: e);
125: }
126: }
127:
128: private void parseTypes(String types) {
129: //Space separated.
130: StringTokenizer tokenizer = new StringTokenizer(types, " ");
131: while (tokenizer.hasMoreTokens()) {
132: String type = tokenizer.nextToken();
133: this .types.put(type, "placeholder");
134: }
135: }
136:
137: private void loadParams(Element pluginElement) {
138: try {
139: Element elmParams = XMLUtil.getChildElement(
140: XMLNodes.PLUGIN_PARAMS, pluginElement);
141: NodeList paramList = elmParams.getChildNodes();
142: for (int i = 0; i < paramList.getLength(); i++) {
143: Node nd = paramList.item(i);
144: if (nd instanceof Element) {
145: Element param = (Element) nd;
146: String sName = XMLUtil.getValue(
147: XMLNodes.PLUGIN_PARAM_NAME, param);
148: String sValue = XMLUtil.getValue(
149: XMLNodes.PLUGIN_PARAM_VALUE, param);
150: params.put(sName, sValue);
151: }
152: }
153: } catch (XMLNodeDoesNotExistException ex) {
154: //ignore
155: }
156: }
157:
158: public Element writePluginElement(Element pluginsElement) {
159: Element pluginElement = XMLUtil.addNewChildElement(
160: XMLNodes.PLUGIN, pluginsElement);
161: pluginElement.setAttribute(XMLNodes.PLUGIN_NAME, name);
162: pluginElement.setAttribute(XMLNodes.PLUGIN_CLASS, pluginClass);
163:
164: Iterator itr = types.keySet().iterator();
165: StringBuffer str = new StringBuffer();
166: boolean first = true;
167: while (itr.hasNext()) {
168: if (!first) {
169: str.append(" ");
170: }
171: str.append((String) itr.next());
172: first = false;
173: }
174: pluginElement
175: .setAttribute(XMLNodes.PLUGIN_TYPE, str.toString());
176:
177: if (params.size() > 0) {
178: Element elmParams = XMLUtil.addNewChildElement(
179: XMLNodes.PLUGIN_PARAMS, pluginElement);
180: itr = params.keySet().iterator();
181: while (itr.hasNext()) {
182: Element elmParam = XMLUtil.addNewChildElement(
183: XMLNodes.PLUGIN_PARAM, elmParams);
184: String paramName = (String) itr.next();
185: XMLUtil.addNewChildElement(XMLNodes.PLUGIN_PARAM_NAME,
186: paramName, elmParam);
187: String paramValue = (String) params.get(paramName);
188: XMLUtil.addNewChildElement(XMLNodes.PLUGIN_PARAM_VALUE,
189: paramValue, elmParam);
190: }
191: }
192:
193: return pluginElement;
194: }
195:
196: /**
197: * Returns the definition.
198: * @return Element
199: */
200: public Element getDefinition() {
201: return definition;
202: }
203:
204: /**
205: * Returns the name.
206: * @return String
207: */
208: public String getName() {
209: return name;
210: }
211:
212: /**
213: * Returns the params.
214: * @return HashMap
215: */
216: public HashMap getParams() {
217: return params;
218: }
219:
220: /**
221: * Returns the plugin.
222: * @return SJClientPlugin
223: */
224: public SJClientPlugin getPlugin() {
225: return plugin;
226: }
227:
228: /**
229: * Returns the pluginClass.
230: * @return String
231: */
232: public String getPluginClass() {
233: return pluginClass;
234: }
235:
236: public boolean isType(String type) {
237: Object o = types.get(type);
238: return (o != null);
239: }
240:
241: }
|