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

Last change on this file since 25963 was 25963, checked in by ak19, 12 years ago

Kathy asked me to adjust the code used to launch pluginfo.pl from GLI to take a new cmd line option called gs_version that should be accompanied by the value 3 for gs3mode. I'm setting this flag and an appropriate value for Greenstone 2 as well. Have also updated the gliserver.pl file for RemoteGreenstone, to similarly launch pluginfo.pl with the new gs_version flag.

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