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

Last change on this file since 15111 was 15111, checked in by ak19, 16 years ago

Added code to work with new replace_srcdoc_with_html.pl script

  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 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.getArguments().size() == 0 && plugin.didLoadingOptionsFail() == false) {
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 = 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.setLoadingOptionsFailed();
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 }
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 = RemoteGreenstoneServer.getScriptOptions("pluginfo.pl", pluginfo_options);
199 xml = new StringBuffer(pluginfo_output);
200 }
201 else {
202 ArrayList args = new ArrayList();
203 if (Utility.isWindows()) {
204 args.add(Configuration.perl_path);
205 args.add("-S");
206 }
207 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "pluginfo.pl");
208 if (collection_name != null) {
209 args.add("-collection");
210 args.add(collection_name);
211 }
212 args.add("-listall");
213 args.add("-xml");
214
215 // Run the pluginfo.pl process
216 Runtime runtime = Runtime.getRuntime();
217 Process process = runtime.exec((String[]) args.toArray(new String[] { }));
218 InputStream input_stream = process.getErrorStream();
219 xml = XMLTools.readXMLStream(input_stream);
220 }
221
222 // Check the XML output was obtained successfully
223 if (xml == null || xml.length() == 0) {
224 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PluginManager.Plugin_List_XML_Parse_Failed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
225 return;
226 }
227
228 if (collection_name != null) {
229 collection_specific_plugins_list = parsePluginsListXML(xml.toString());
230 }
231 else {
232 core_greenstone_plugins_list = parsePluginsListXML(xml.toString());
233 }
234 }
235 catch (Exception exception) {
236 DebugStream.printStackTrace(exception);
237 }
238 }
239
240
241 static private void parsePluginInfoXML(Plugin plugin, String xml)
242 {
243 Document document = XMLTools.parseXML(new StringReader(xml));
244 if (document == null) {
245 plugin.setLoadingOptionsFailed();
246 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.PlugInManager.PlugIn_XML_Parse_Failed", plugin.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
247 return;
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}
Note: See TracBrowser for help on using the repository browser.