source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/greenstone/Plugins.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

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