1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * Author: Michael Dewsnip, NZDL Project, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 2006 New Zealand Digital Library Project
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *########################################################################
|
---|
26 | */
|
---|
27 | package org.greenstone.gatherer.greenstone;
|
---|
28 |
|
---|
29 | import java.io.*;
|
---|
30 | import java.util.*;
|
---|
31 | import javax.swing.*;
|
---|
32 | import org.greenstone.gatherer.Configuration;
|
---|
33 | import org.greenstone.gatherer.DebugStream;
|
---|
34 | import org.greenstone.gatherer.Dictionary;
|
---|
35 | import org.greenstone.gatherer.Gatherer;
|
---|
36 | import org.greenstone.gatherer.cdm.Argument;
|
---|
37 | import org.greenstone.gatherer.cdm.Plugin;
|
---|
38 | import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
|
---|
39 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
40 | import org.greenstone.gatherer.util.Utility;
|
---|
41 | import org.greenstone.gatherer.util.XMLTools;
|
---|
42 | import org.w3c.dom.*;
|
---|
43 | import org.xml.sax.*;
|
---|
44 |
|
---|
45 |
|
---|
46 | /** This class is for maintaining a list of known plug-ins, and importing new plugins using the parser. */
|
---|
47 | public class Plugins
|
---|
48 | {
|
---|
49 | // A list of all the plugins in the core Greenstone "perllib/plugins" folder (arguments may not be loaded)
|
---|
50 | static private ArrayList core_greenstone_plugins_list = null;
|
---|
51 | // The name of the loaded collection
|
---|
52 | static private String collection_name = null;
|
---|
53 | // A list of all the plugins in the loaded collection's "perllib/plugins" folder (arguments may not be loaded)
|
---|
54 | static private ArrayList collection_specific_plugins_list = new ArrayList();
|
---|
55 |
|
---|
56 |
|
---|
57 | static public Plugin getPlugin(String plugin_name, boolean arguments_required)
|
---|
58 | {
|
---|
59 | Plugin plugin = null;
|
---|
60 | boolean collection_specific = false;
|
---|
61 |
|
---|
62 | // Check the collection-specific plugins first
|
---|
63 | for (int i = 0; i < collection_specific_plugins_list.size(); i++) {
|
---|
64 | Plugin collection_specific_plugin = (Plugin) collection_specific_plugins_list.get(i);
|
---|
65 | if (collection_specific_plugin.getName().equals(plugin_name)) {
|
---|
66 | plugin = collection_specific_plugin;
|
---|
67 | collection_specific = true;
|
---|
68 | break;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | // Try the core Greenstone plugins if necessary
|
---|
73 | if (plugin == null) {
|
---|
74 | for (int i = 0; i < core_greenstone_plugins_list.size(); i++) {
|
---|
75 | Plugin core_greenstone_plugin = (Plugin) core_greenstone_plugins_list.get(i);
|
---|
76 | if (core_greenstone_plugin.getName().equals(plugin_name)) {
|
---|
77 | plugin = core_greenstone_plugin;
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | // If we've found the plugin, load its arguments now, if required
|
---|
84 | if (plugin != null && arguments_required) {
|
---|
85 | if (!plugin.hasLoadedOptions()) {
|
---|
86 | loadPluginInfo(plugin, collection_specific);
|
---|
87 | }
|
---|
88 | else {
|
---|
89 | DebugStream.println("Already loaded arguments for " + plugin_name + "!");
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | return plugin;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /** Returns a new list from merging the collection-specific and the core Greenstone plugins. */
|
---|
98 | static public ArrayList getPluginsList()
|
---|
99 | {
|
---|
100 | ArrayList plugins_list = new ArrayList();
|
---|
101 | plugins_list.addAll(collection_specific_plugins_list);
|
---|
102 |
|
---|
103 | // Add in the core Greenstone plugins, taking care not to overwrite any collection-specific ones
|
---|
104 | for (int i = 0; i < core_greenstone_plugins_list.size(); i++) {
|
---|
105 | Plugin core_greenstone_plugin = (Plugin) core_greenstone_plugins_list.get(i);
|
---|
106 |
|
---|
107 | boolean found = false;
|
---|
108 | for (int j = 0; j < collection_specific_plugins_list.size(); j++) {
|
---|
109 | Plugin collection_specific_plugin = (Plugin) collection_specific_plugins_list.get(j);
|
---|
110 | if (core_greenstone_plugin.getName().equals(collection_specific_plugin.getName())) {
|
---|
111 | found = true;
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (!found) {
|
---|
117 | plugins_list.add(core_greenstone_plugin);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | return plugins_list;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | static private void loadPluginInfo(Plugin plugin, boolean collection_specific)
|
---|
126 | {
|
---|
127 | DebugStream.println("Loading arguments for " + plugin.getName() + "...");
|
---|
128 |
|
---|
129 | // Run pluginfo.pl to get the list of plugins
|
---|
130 | try {
|
---|
131 | String pluginfo_xml = null;
|
---|
132 | if (Gatherer.isGsdlRemote) {
|
---|
133 | String pluginfo_options = "&plugin=" + plugin;
|
---|
134 | if (collection_specific) {
|
---|
135 | pluginfo_options += "&collection=" + collection_name;
|
---|
136 | }
|
---|
137 | pluginfo_xml = Gatherer.remoteGreenstoneServer.getScriptOptions("pluginfo.pl", pluginfo_options);
|
---|
138 | }
|
---|
139 | else {
|
---|
140 | ArrayList args = new ArrayList();
|
---|
141 | args.add(Configuration.perl_path);
|
---|
142 | args.add("-S");
|
---|
143 | args.add(LocalGreenstone.getBinScriptDirectoryPath() + "pluginfo.pl");
|
---|
144 | if (collection_specific) {
|
---|
145 | args.add("-collection");
|
---|
146 | args.add(collection_name);
|
---|
147 | }
|
---|
148 | args.add("-xml");
|
---|
149 | args.add("-language");
|
---|
150 | args.add(Configuration.getLanguage());
|
---|
151 | args.add(plugin.getName());
|
---|
152 |
|
---|
153 | // Run the pluginfo.pl process
|
---|
154 | Runtime runtime = Runtime.getRuntime();
|
---|
155 | Process process = runtime.exec((String[]) args.toArray(new String[] { }));
|
---|
156 | InputStream input_stream = process.getErrorStream();
|
---|
157 | StringBuffer pluginfo_xml_buffer = XMLTools.readXMLStream(input_stream);
|
---|
158 | if (pluginfo_xml_buffer != null) {
|
---|
159 | pluginfo_xml = pluginfo_xml_buffer.toString();
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | // Check the XML output was obtained successfully
|
---|
164 | if (pluginfo_xml == null || pluginfo_xml.length() == 0) {
|
---|
165 | plugin.setHasLoadedOptions(false); // failure to load options
|
---|
166 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PlugInManager.PlugIn_XML_Parse_Failed", plugin.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
167 | return;
|
---|
168 | } else {
|
---|
169 | plugin.setHasLoadedOptions(true);
|
---|
170 | }
|
---|
171 |
|
---|
172 | parsePluginInfoXML(plugin, pluginfo_xml);
|
---|
173 | }
|
---|
174 | catch (Exception exception) {
|
---|
175 | DebugStream.printStackTrace(exception);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | static public void loadPluginsList(String collection_name_arg)
|
---|
181 | {
|
---|
182 | DebugStream.println("In loadPluginsList()...");
|
---|
183 |
|
---|
184 | // If we're getting the collection-specific plugins, clear the old list no matter what
|
---|
185 | if (collection_name_arg != null) {
|
---|
186 | collection_name = collection_name_arg;
|
---|
187 | collection_specific_plugins_list = new ArrayList();
|
---|
188 | }
|
---|
189 |
|
---|
190 | // Run pluginfo.pl to get the list of plugins
|
---|
191 | try {
|
---|
192 | StringBuffer xml = null;
|
---|
193 | if (Gatherer.isGsdlRemote) {
|
---|
194 | String pluginfo_options = "&listall";
|
---|
195 | if (collection_name != null) {
|
---|
196 | pluginfo_options += "&collection=" + collection_name;
|
---|
197 | }
|
---|
198 | String pluginfo_output = Gatherer.remoteGreenstoneServer.getScriptOptions("pluginfo.pl", pluginfo_options);
|
---|
199 | xml = new StringBuffer(pluginfo_output);
|
---|
200 | }
|
---|
201 | else {
|
---|
202 | ArrayList args = new ArrayList();
|
---|
203 | args.add(Configuration.perl_path);
|
---|
204 | args.add("-S");
|
---|
205 | args.add(LocalGreenstone.getBinScriptDirectoryPath() + "pluginfo.pl");
|
---|
206 | if (collection_name != null) {
|
---|
207 | args.add("-collection");
|
---|
208 | args.add(collection_name);
|
---|
209 | }
|
---|
210 | args.add("-listall");
|
---|
211 | args.add("-xml");
|
---|
212 |
|
---|
213 | // Run the pluginfo.pl process
|
---|
214 | Runtime runtime = Runtime.getRuntime();
|
---|
215 | Process process = runtime.exec((String[]) args.toArray(new String[] { }));
|
---|
216 | InputStream input_stream = process.getErrorStream();
|
---|
217 | xml = XMLTools.readXMLStream(input_stream);
|
---|
218 | }
|
---|
219 |
|
---|
220 | // Check the XML output was obtained successfully
|
---|
221 | if (xml == null || xml.length() == 0) {
|
---|
222 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PluginManager.Plugin_List_XML_Parse_Failed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | if (collection_name != null) {
|
---|
227 | collection_specific_plugins_list = parsePluginsListXML(xml.toString());
|
---|
228 | }
|
---|
229 | else {
|
---|
230 | core_greenstone_plugins_list = parsePluginsListXML(xml.toString());
|
---|
231 | }
|
---|
232 | }
|
---|
233 | catch (Exception exception) {
|
---|
234 | DebugStream.printStackTrace(exception);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | static private void parsePluginInfoXML(Plugin plugin, String xml)
|
---|
240 | {
|
---|
241 | Document document = XMLTools.parseXML(new StringReader(xml));
|
---|
242 | if (document == null) {
|
---|
243 | plugin.setHasLoadedOptions(false); // failure to load the options/failed plugin
|
---|
244 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PlugInManager.PlugIn_XML_Parse_Failed", plugin.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
245 | return;
|
---|
246 | } else {
|
---|
247 | plugin.setHasLoadedOptions(true);
|
---|
248 | }
|
---|
249 |
|
---|
250 | parsePluginInfoXMLNode(plugin, document.getDocumentElement());
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | static private void parsePluginInfoXMLNode(Plugin plugin, Node root_node)
|
---|
255 | {
|
---|
256 | for (Node node = root_node.getFirstChild(); node != null; node = node.getNextSibling()) {
|
---|
257 | String node_name = node.getNodeName();
|
---|
258 |
|
---|
259 | if (node_name.equalsIgnoreCase("Name")) {
|
---|
260 | plugin.setName(XMLTools.getValue(node));
|
---|
261 | }
|
---|
262 | else if (node_name.equals("Desc")) {
|
---|
263 | plugin.setDescription(XMLTools.getValue(node));
|
---|
264 | }
|
---|
265 | else if (node_name.equals("Abstract")) {
|
---|
266 | plugin.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
|
---|
267 | }
|
---|
268 | else if (node_name.equalsIgnoreCase("Explodes")) {
|
---|
269 | plugin.setDoesExplodeMetadataDatabases(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
|
---|
270 | }
|
---|
271 | else if (node_name.equalsIgnoreCase("SourceReplaceable")) { // looking for <SourceReplaceable> tag
|
---|
272 | plugin.setDoesReplaceSrcDocsWithHtml(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
|
---|
273 | }
|
---|
274 | else if (node_name.equalsIgnoreCase("Processes")) {
|
---|
275 | plugin.setDefaultProcessExpression(XMLTools.getValue(node));
|
---|
276 | }
|
---|
277 | else if (node_name.equalsIgnoreCase("Blocks")) {
|
---|
278 | plugin.setDefaultBlockExpression(XMLTools.getValue(node));
|
---|
279 | }
|
---|
280 | // Parse the plugin arguments
|
---|
281 | else if (node_name.equalsIgnoreCase("Arguments")) {
|
---|
282 | for (Node argument_node = node.getFirstChild(); argument_node != null; argument_node = argument_node.getNextSibling()) {
|
---|
283 | // An option
|
---|
284 | if (argument_node.getNodeName().equalsIgnoreCase("Option")) {
|
---|
285 | Argument argument = new Argument();
|
---|
286 | argument.parseXML((Element) argument_node);
|
---|
287 | plugin.addArgument(argument);
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 | // A super plugin class
|
---|
292 | else if (node_name.equalsIgnoreCase("PlugInfo")) {
|
---|
293 | Plugin super_plugin = new Plugin();
|
---|
294 | parsePluginInfoXMLNode(super_plugin, node);
|
---|
295 | plugin.setSuper(super_plugin);
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | static private ArrayList parsePluginsListXML(String xml)
|
---|
302 | {
|
---|
303 | ArrayList plugins_list = new ArrayList();
|
---|
304 |
|
---|
305 | Document document = XMLTools.parseXML(new StringReader(xml));
|
---|
306 | Node root = document.getDocumentElement();
|
---|
307 | for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
|
---|
308 | String node_name = node.getNodeName();
|
---|
309 |
|
---|
310 | if (node_name.equals("PlugInfo")) {
|
---|
311 | Plugin plugin = new Plugin();
|
---|
312 | parsePluginInfoXMLNode(plugin, node);
|
---|
313 | plugins_list.add(plugin);
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | return plugins_list;
|
---|
318 | }
|
---|
319 | }
|
---|