source: trunk/gli/src/org/greenstone/gatherer/greenstone/Plugins.java@ 12646

Last change on this file since 12646 was 12646, checked in by mdewsnip, 18 years ago

Support for collection-specific plugins and classifiers with the new code.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
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 */
27package org.greenstone.gatherer.greenstone;
28
29import java.io.*;
30import java.util.*;
31import javax.swing.*;
32import org.greenstone.gatherer.Configuration;
33import org.greenstone.gatherer.DebugStream;
34import org.greenstone.gatherer.Dictionary;
35import org.greenstone.gatherer.Gatherer;
36import org.greenstone.gatherer.LocalGreenstone;
37import org.greenstone.gatherer.cdm.Argument;
38import org.greenstone.gatherer.cdm.Plugin;
39import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
40import org.greenstone.gatherer.util.StaticStrings;
41import org.greenstone.gatherer.util.Utility;
42import org.greenstone.gatherer.util.XMLTools;
43import org.w3c.dom.*;
44import org.xml.sax.*;
45
46
47/** This class is for maintaining a list of known plug-ins, and importing new plugins using the parser. */
48public class Plugins
49{
50 // A list of all the plugins in the core Greenstone "perllib/plugins" folder (arguments may not be loaded)
51 static private ArrayList core_greenstone_plugins_list = null;
52 // The name of the loaded collection
53 static private String collection_name = null;
54 // A list of all the plugins in the loaded collection's "perllib/plugins" folder (arguments may not be loaded)
55 static private ArrayList collection_specific_plugins_list = null;
56
57
58 static public Plugin getPlugin(String plugin_name, boolean arguments_required)
59 {
60 Plugin plugin = null;
61 boolean collection_specific = false;
62
63 // Check the collection-specific plugins first
64 for (int i = 0; i < collection_specific_plugins_list.size(); i++) {
65 Plugin collection_specific_plugin = (Plugin) collection_specific_plugins_list.get(i);
66 if (collection_specific_plugin.getName().equals(plugin_name)) {
67 plugin = collection_specific_plugin;
68 collection_specific = true;
69 break;
70 }
71 }
72
73 // Try the core Greenstone plugins if necessary
74 if (plugin == null) {
75 for (int i = 0; i < core_greenstone_plugins_list.size(); i++) {
76 Plugin core_greenstone_plugin = (Plugin) core_greenstone_plugins_list.get(i);
77 if (core_greenstone_plugin.getName().equals(plugin_name)) {
78 plugin = core_greenstone_plugin;
79 break;
80 }
81 }
82 }
83
84 // If we've found the plugin, load its arguments now, if required
85 if (plugin != null && arguments_required) {
86 if (plugin.getArguments().size() == 0) {
87 loadPluginInfo(plugin, collection_specific);
88 }
89 else {
90 DebugStream.println("Already loaded arguments for " + plugin_name + "!");
91 }
92 }
93
94 return plugin;
95 }
96
97
98 /** Returns a new list from merging the collection-specific and the core Greenstone plugins. */
99 static public ArrayList getPluginsList()
100 {
101 ArrayList plugins_list = new ArrayList();
102 plugins_list.addAll(collection_specific_plugins_list);
103
104 // Add in the core Greenstone plugins, taking care not to overwrite any collection-specific ones
105 for (int i = 0; i < core_greenstone_plugins_list.size(); i++) {
106 Plugin core_greenstone_plugin = (Plugin) core_greenstone_plugins_list.get(i);
107
108 boolean found = false;
109 for (int j = 0; j < collection_specific_plugins_list.size(); j++) {
110 Plugin collection_specific_plugin = (Plugin) collection_specific_plugins_list.get(j);
111 if (core_greenstone_plugin.getName().equals(collection_specific_plugin.getName())) {
112 found = true;
113 break;
114 }
115 }
116
117 if (!found) {
118 plugins_list.add(core_greenstone_plugin);
119 }
120 }
121
122 return plugins_list;
123 }
124
125
126 static private void loadPluginInfo(Plugin plugin, boolean collection_specific)
127 {
128 DebugStream.println("Loading arguments for " + plugin.getName() + "...");
129
130 // Run pluginfo.pl to get the list of plugins
131 try {
132 StringBuffer xml = null;
133 if (Gatherer.isGsdlRemote) {
134 String pluginfo_output = RemoteGreenstoneServer.getScriptOptions("pluginfo.pl", "&plugin=" + plugin);
135 xml = new StringBuffer(pluginfo_output);
136 }
137 else {
138 ArrayList args = new ArrayList();
139 if (Utility.isWindows()) {
140 args.add(Configuration.perl_path);
141 args.add("-S");
142 }
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 xml = XMLTools.readXMLStream(input_stream);
158 }
159
160 // Check the XML output was obtained successfully
161 if (xml == null || xml.length() == 0) {
162 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PlugInManager.PlugIn_XML_Parse_Failed", plugin.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
163 return;
164 }
165
166 parsePluginInfoXML(plugin, xml.toString());
167 }
168 catch (Exception exception) {
169 DebugStream.printStackTrace(exception);
170 }
171 }
172
173
174 static public void loadPluginsList(String collection_name_arg)
175 {
176 DebugStream.println("In loadPluginsList()...");
177
178 // If we're getting the collection-specific plugins, clear the old list no matter what
179 if (collection_name_arg != null) {
180 collection_name = collection_name_arg;
181 collection_specific_plugins_list = new ArrayList();
182 }
183
184 // Run pluginfo.pl to get the list of plugins
185 try {
186 StringBuffer xml = null;
187 if (Gatherer.isGsdlRemote) {
188 String pluginfo_output = RemoteGreenstoneServer.getScriptOptions("pluginfo.pl", "&listall");
189 xml = new StringBuffer(pluginfo_output);
190 }
191 else {
192 ArrayList args = new ArrayList();
193 if (Utility.isWindows()) {
194 args.add(Configuration.perl_path);
195 args.add("-S");
196 }
197 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "pluginfo.pl");
198 if (collection_name != null) {
199 args.add("-collection");
200 args.add(collection_name);
201 }
202 args.add("-listall");
203 args.add("-xml");
204
205 // Run the pluginfo.pl process
206 Runtime runtime = Runtime.getRuntime();
207 Process process = runtime.exec((String[]) args.toArray(new String[] { }));
208 InputStream input_stream = process.getErrorStream();
209 xml = XMLTools.readXMLStream(input_stream);
210 }
211
212 // Check the XML output was obtained successfully
213 if (xml == null || xml.length() == 0) {
214 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PluginManager.Plugin_List_XML_Parse_Failed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
215 return;
216 }
217
218 if (collection_name != null) {
219 collection_specific_plugins_list = parsePluginsListXML(xml.toString());
220 }
221 else {
222 core_greenstone_plugins_list = parsePluginsListXML(xml.toString());
223 }
224 }
225 catch (Exception exception) {
226 DebugStream.printStackTrace(exception);
227 }
228 }
229
230
231 static private void parsePluginInfoXML(Plugin plugin, String xml)
232 {
233 Document document = XMLTools.parseXML(new StringReader(xml));
234 parsePluginInfoXMLNode(plugin, document.getDocumentElement());
235 }
236
237
238 static private void parsePluginInfoXMLNode(Plugin plugin, Node root_node)
239 {
240 for (Node node = root_node.getFirstChild(); node != null; node = node.getNextSibling()) {
241 String node_name = node.getNodeName();
242
243 if (node_name.equalsIgnoreCase("Name")) {
244 plugin.setName(XMLTools.getValue(node));
245 }
246 else if (node_name.equals("Desc")) {
247 plugin.setDescription(XMLTools.getValue(node));
248 }
249 else if (node_name.equals("Abstract")) {
250 plugin.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
251 }
252 else if (node_name.equalsIgnoreCase("Explodes")) {
253 plugin.setDoesExplodeMetadataDatabases(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
254 }
255 else if (node_name.equalsIgnoreCase("Processes")) {
256 plugin.setDefaultProcessExpression(XMLTools.getValue(node));
257 }
258 else if (node_name.equalsIgnoreCase("Blocks")) {
259 plugin.setDefaultBlockExpression(XMLTools.getValue(node));
260 }
261 // Parse the plugin arguments
262 else if (node_name.equalsIgnoreCase("Arguments")) {
263 for (Node argument_node = node.getFirstChild(); argument_node != null; argument_node = argument_node.getNextSibling()) {
264 // An option
265 if (argument_node.getNodeName().equalsIgnoreCase("Option")) {
266 Argument argument = new Argument();
267 argument.parseXML((Element) argument_node);
268 plugin.addArgument(argument);
269 }
270 }
271 }
272 // A super plugin class
273 else if (node_name.equalsIgnoreCase("PlugInfo")) {
274 Plugin super_plugin = new Plugin();
275 parsePluginInfoXMLNode(super_plugin, node);
276 plugin.setSuper(super_plugin);
277 }
278 }
279 }
280
281
282 static private ArrayList parsePluginsListXML(String xml)
283 {
284 ArrayList plugins_list = new ArrayList();
285
286 Document document = XMLTools.parseXML(new StringReader(xml));
287 Node root = document.getDocumentElement();
288 for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
289 String node_name = node.getNodeName();
290
291 if (node_name.equals("PlugInfo")) {
292 Plugin plugin = new Plugin();
293 parsePluginInfoXMLNode(plugin, node);
294 plugins_list.add(plugin);
295 }
296 }
297
298 return plugins_list;
299 }
300}
Note: See TracBrowser for help on using the repository browser.