source: gli/branches/rtl-gli/src/org/greenstone/gatherer/greenstone/Plugins.java@ 18356

Last change on this file since 18356 was 18356, checked in by kjdon, 15 years ago

updated the rtl-gli branch with files from trunk. Result of a merge 14807:18318

  • Property svn:keywords set to Author Date Id Revision
File size: 11.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.cdm.Argument;
37import org.greenstone.gatherer.cdm.Plugin;
38import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
39import org.greenstone.gatherer.util.StaticStrings;
40import org.greenstone.gatherer.util.Utility;
41import org.greenstone.gatherer.util.XMLTools;
42import org.w3c.dom.*;
43import org.xml.sax.*;
44
45
46/** This class is for maintaining a list of known plug-ins, and importing new plugins using the parser. */
47public 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 if (Utility.isWindows()) {
142 args.add(Configuration.perl_path);
143 args.add("-S");
144 }
145 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "pluginfo.pl");
146 if (collection_specific) {
147 args.add("-collection");
148 args.add(collection_name);
149 }
150 args.add("-xml");
151 args.add("-language");
152 args.add(Configuration.getLanguage());
153 args.add(plugin.getName());
154
155 // Run the pluginfo.pl process
156 Runtime runtime = Runtime.getRuntime();
157 Process process = runtime.exec((String[]) args.toArray(new String[] { }));
158 InputStream input_stream = process.getErrorStream();
159 StringBuffer pluginfo_xml_buffer = XMLTools.readXMLStream(input_stream);
160 if (pluginfo_xml_buffer != null) {
161 pluginfo_xml = pluginfo_xml_buffer.toString();
162 }
163 }
164
165 // Check the XML output was obtained successfully
166 if (pluginfo_xml == null || pluginfo_xml.length() == 0) {
167 plugin.setHasLoadedOptions(false); // failure to load options
168 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PlugInManager.PlugIn_XML_Parse_Failed", plugin.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
169 return;
170 } else {
171 plugin.setHasLoadedOptions(true);
172 }
173
174 parsePluginInfoXML(plugin, pluginfo_xml);
175 }
176 catch (Exception exception) {
177 DebugStream.printStackTrace(exception);
178 }
179 }
180
181
182 static public void loadPluginsList(String collection_name_arg)
183 {
184 DebugStream.println("In loadPluginsList()...");
185
186 // If we're getting the collection-specific plugins, clear the old list no matter what
187 if (collection_name_arg != null) {
188 collection_name = collection_name_arg;
189 collection_specific_plugins_list = new ArrayList();
190 }
191
192 // Run pluginfo.pl to get the list of plugins
193 try {
194 StringBuffer xml = null;
195 if (Gatherer.isGsdlRemote) {
196 String pluginfo_options = "&listall";
197 if (collection_name != null) {
198 pluginfo_options += "&collection=" + collection_name;
199 }
200 String pluginfo_output = Gatherer.remoteGreenstoneServer.getScriptOptions("pluginfo.pl", pluginfo_options);
201 xml = new StringBuffer(pluginfo_output);
202 }
203 else {
204 ArrayList args = new ArrayList();
205 if (Utility.isWindows()) {
206 args.add(Configuration.perl_path);
207 args.add("-S");
208 }
209 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "pluginfo.pl");
210 if (collection_name != null) {
211 args.add("-collection");
212 args.add(collection_name);
213 }
214 args.add("-listall");
215 args.add("-xml");
216
217 // Run the pluginfo.pl process
218 Runtime runtime = Runtime.getRuntime();
219 Process process = runtime.exec((String[]) args.toArray(new String[] { }));
220 InputStream input_stream = process.getErrorStream();
221 xml = XMLTools.readXMLStream(input_stream);
222 }
223
224 // Check the XML output was obtained successfully
225 if (xml == null || xml.length() == 0) {
226 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PluginManager.Plugin_List_XML_Parse_Failed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
227 return;
228 }
229
230 if (collection_name != null) {
231 collection_specific_plugins_list = parsePluginsListXML(xml.toString());
232 }
233 else {
234 core_greenstone_plugins_list = parsePluginsListXML(xml.toString());
235 }
236 }
237 catch (Exception exception) {
238 DebugStream.printStackTrace(exception);
239 }
240 }
241
242
243 static private void parsePluginInfoXML(Plugin plugin, String xml)
244 {
245 Document document = XMLTools.parseXML(new StringReader(xml));
246 if (document == null) {
247 plugin.setHasLoadedOptions(false); // failure to load the options/failed plugin
248 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PlugInManager.PlugIn_XML_Parse_Failed", plugin.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
249 return;
250 } else {
251 plugin.setHasLoadedOptions(true);
252 }
253
254 parsePluginInfoXMLNode(plugin, document.getDocumentElement());
255 }
256
257
258 static private void parsePluginInfoXMLNode(Plugin plugin, Node root_node)
259 {
260 for (Node node = root_node.getFirstChild(); node != null; node = node.getNextSibling()) {
261 String node_name = node.getNodeName();
262
263 if (node_name.equalsIgnoreCase("Name")) {
264 plugin.setName(XMLTools.getValue(node));
265 }
266 else if (node_name.equals("Desc")) {
267 plugin.setDescription(XMLTools.getValue(node));
268 }
269 else if (node_name.equals("Abstract")) {
270 plugin.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
271 }
272 else if (node_name.equalsIgnoreCase("Explodes")) {
273 plugin.setDoesExplodeMetadataDatabases(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
274 }
275 else if (node_name.equalsIgnoreCase("SourceReplaceable")) { // looking for <SourceReplaceable> tag
276 plugin.setDoesReplaceSrcDocsWithHtml(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
277 }
278 else if (node_name.equalsIgnoreCase("Processes")) {
279 plugin.setDefaultProcessExpression(XMLTools.getValue(node));
280 }
281 else if (node_name.equalsIgnoreCase("Blocks")) {
282 plugin.setDefaultBlockExpression(XMLTools.getValue(node));
283 }
284 // Parse the plugin arguments
285 else if (node_name.equalsIgnoreCase("Arguments")) {
286 for (Node argument_node = node.getFirstChild(); argument_node != null; argument_node = argument_node.getNextSibling()) {
287 // An option
288 if (argument_node.getNodeName().equalsIgnoreCase("Option")) {
289 Argument argument = new Argument();
290 argument.parseXML((Element) argument_node);
291 plugin.addArgument(argument);
292 }
293 }
294 }
295 // A super plugin class
296 else if (node_name.equalsIgnoreCase("PlugInfo")) {
297 Plugin super_plugin = new Plugin();
298 parsePluginInfoXMLNode(super_plugin, node);
299 plugin.setSuper(super_plugin);
300 }
301 }
302 }
303
304
305 static private ArrayList parsePluginsListXML(String xml)
306 {
307 ArrayList plugins_list = new ArrayList();
308
309 Document document = XMLTools.parseXML(new StringReader(xml));
310 Node root = document.getDocumentElement();
311 for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
312 String node_name = node.getNodeName();
313
314 if (node_name.equals("PlugInfo")) {
315 Plugin plugin = new Plugin();
316 parsePluginInfoXMLNode(plugin, node);
317 plugins_list.add(plugin);
318 }
319 }
320
321 return plugins_list;
322 }
323}
Note: See TracBrowser for help on using the repository browser.