source: main/trunk/gli/src/org/greenstone/gatherer/greenstone/Classifiers.java@ 31636

Last change on this file since 31636 was 31636, checked in by ak19, 7 years ago

First phase of shifting gli code to use SafeProcess instead of Java Process. This phase takes care of all the easy cases.

  • 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.Classifier;
38import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
39import org.greenstone.gatherer.util.SafeProcess;
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
47public class Classifiers
48{
49 // A list of all the classifiers in the core Greenstone "perllib/classify" folder (arguments may not be loaded)
50 static private ArrayList core_greenstone_classifiers_list = null;
51 // The name of the loaded collection
52 static private String collection_name = null;
53 // A list of all the classifiers in the loaded collection's "perllib/classify" folder (arguments may not be loaded)
54 static private ArrayList collection_specific_classifiers_list = new ArrayList();
55
56
57 static public Classifier getClassifier(String classifier_name, boolean arguments_required)
58 {
59 Classifier classifier = null;
60 boolean collection_specific = false;
61
62 // Check the collection-specific classifiers first
63 for (int i = 0; i < collection_specific_classifiers_list.size(); i++) {
64 Classifier collection_specific_classifier = (Classifier) collection_specific_classifiers_list.get(i);
65 if (collection_specific_classifier.getName().equals(classifier_name)) {
66 classifier = collection_specific_classifier;
67 collection_specific = true;
68 break;
69 }
70 }
71
72 // Try the core Greenstone classifiers if necessary
73 if (classifier == null) {
74 for (int i = 0; i < core_greenstone_classifiers_list.size(); i++) {
75 Classifier core_greenstone_classifier = (Classifier) core_greenstone_classifiers_list.get(i);
76 if (core_greenstone_classifier.getName().equals(classifier_name)) {
77 classifier = core_greenstone_classifier;
78 break;
79 }
80 }
81 }
82
83 // If we've found the classifier, load its arguments now, if required
84 if (classifier != null && arguments_required) {
85 if (classifier.getArguments().size() == 0 && classifier.didLoadingOptionsFail() == false) {
86 loadClassifierInfo(classifier, collection_specific);
87 }
88 else {
89 DebugStream.println("Already loaded arguments for " + classifier_name + "!");
90 }
91 }
92
93 return classifier;
94 }
95
96
97 /** Returns a new list from merging the collection-specific and the core Greenstone classifiers. */
98 static public ArrayList getClassifiersList()
99 {
100 ArrayList classifiers_list = new ArrayList();
101 classifiers_list.addAll(collection_specific_classifiers_list);
102
103 // Add in the core Greenstone classifiers, taking care not to overwrite any collection-specific ones
104 for (int i = 0; i < core_greenstone_classifiers_list.size(); i++) {
105 Classifier core_greenstone_classifier = (Classifier) core_greenstone_classifiers_list.get(i);
106
107 boolean found = false;
108 for (int j = 0; j < collection_specific_classifiers_list.size(); j++) {
109 Classifier collection_specific_classifier = (Classifier) collection_specific_classifiers_list.get(j);
110 if (core_greenstone_classifier.getName().equals(collection_specific_classifier.getName())) {
111 found = true;
112 break;
113 }
114 }
115
116 if (!found) {
117 classifiers_list.add(core_greenstone_classifier);
118 }
119 }
120
121 return classifiers_list;
122 }
123
124
125 static private void loadClassifierInfo(Classifier classifier, boolean collection_specific)
126 {
127 DebugStream.println("Loading arguments for " + classifier.getName() + "...");
128
129 // Run classifierfo.pl to get the list of classifiers
130 try {
131 String classinfo_xml = null;
132 if (Gatherer.isGsdlRemote) {
133 String classinfo_options = "&classifier=" + classifier;
134 if (collection_specific) {
135 classinfo_options += "&collection=" + collection_name;
136 }
137 classinfo_xml = Gatherer.remoteGreenstoneServer.getScriptOptions("classinfo.pl", classinfo_options);
138 }
139 else {
140 ArrayList args = new ArrayList();
141 args.add(Configuration.perl_path);
142 args.add("-S");
143 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "classinfo.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(classifier.getName());
152
153
154 // Run the classinfo.pl process:
155 // Create the process.
156 SafeProcess process = new SafeProcess((String[]) args.toArray(new String[] { }));
157
158 // run the SafeProcess
159 int exitVal = process.runProcess();
160 if(exitVal != 0) {
161 throw new Exception("*** Error running classify.pl loadClassifierInfo, process exited with: "
162 + exitVal);
163 }
164 // get the result: We expect XML to have come out of the process std error stream.
165 classinfo_xml = process.getStdError();
166 ///System.err.println("*********\nClassifierInfo, got:\n" + classinfo_xml + "\n**********\n");
167 }
168
169 // Check the XML output was obtained successfully
170 if (classinfo_xml == null || classinfo_xml.length() == 0) {
171 classifier.setLoadingOptionsFailed();
172 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_XML_Parse_Failed", classifier.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
173 return;
174 }
175
176 parseClassifierInfoXML(classifier, classinfo_xml);
177 }
178 catch (Exception exception) {
179 DebugStream.printStackTrace(exception);
180 }
181 }
182
183
184 static public void loadClassifiersList(String collection_name_arg)
185 {
186 DebugStream.println("In loadClassifiersList()...");
187
188 // If we're getting the collection-specific classifiers, clear the old list no matter what
189 if (collection_name_arg != null) {
190 collection_name = collection_name_arg;
191 collection_specific_classifiers_list = new ArrayList();
192 }
193
194 // Run classifierfo.pl to get the list of classifiers
195 try {
196 StringBuffer xml = null;
197 if (Gatherer.isGsdlRemote) {
198 String classinfo_options = "&listall";
199 if (collection_name != null) {
200 classinfo_options += "&collection=" + collection_name;
201 }
202 String classinfo_output = Gatherer.remoteGreenstoneServer.getScriptOptions("classinfo.pl", classinfo_options);
203 xml = new StringBuffer(classinfo_output);
204 }
205 else {
206 ArrayList args = new ArrayList();
207 args.add(Configuration.perl_path);
208 args.add("-S");
209 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "classinfo.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 classinfo.pl process:
218 // Create the process.
219 SafeProcess process = new SafeProcess((String[]) args.toArray(new String[] { }));
220
221 // run the SafeProcess
222 int exitVal = process.runProcess();
223 if(exitVal != 0) {
224 throw new Exception("*** Error running classify.pl loadClassifiersList, process exited with: "
225 + exitVal);
226 }
227 // get the result: We expect XML to have come out of the process std error stream.
228 xml = new StringBuffer(process.getStdError());
229 ///System.err.println("*********\nClassifierList, got:\n" + xml + "\n**********\n");
230 }
231
232 // Check the XML output was obtained successfully
233 if (xml == null || xml.length() == 0) {
234 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_List_XML_Parse_Failed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
235 return;
236 }
237
238 if (collection_name != null) {
239 collection_specific_classifiers_list = parseClassifiersListXML(xml.toString());
240 }
241 else {
242 core_greenstone_classifiers_list = parseClassifiersListXML(xml.toString());
243 }
244 }
245 catch (Exception exception) {
246 DebugStream.printStackTrace(exception);
247 }
248 }
249
250
251 static private void parseClassifierInfoXML(Classifier classifier, String xml)
252 {
253 Document document = XMLTools.parseXML(new StringReader(xml));
254 if (document == null) {
255 classifier.setLoadingOptionsFailed();
256 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_XML_Parse_Failed", classifier.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
257 return;
258 }
259
260 parseClassifierInfoXMLNode(classifier, document.getDocumentElement());
261 }
262
263
264 static private void parseClassifierInfoXMLNode(Classifier classifier, Node root_node)
265 {
266 for (Node node = root_node.getFirstChild(); node != null; node = node.getNextSibling()) {
267 String node_name = node.getNodeName();
268
269 if (node_name.equalsIgnoreCase("Name")) {
270 classifier.setName(XMLTools.getValue(node));
271 }
272 else if (node_name.equals("Desc")) {
273 classifier.setDescription(XMLTools.getValue(node));
274 }
275 else if (node_name.equals("Abstract")) {
276 classifier.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
277 }
278 // Parse the classifier arguments
279 else if (node_name.equalsIgnoreCase("Arguments")) {
280 for (Node argument_node = node.getFirstChild(); argument_node != null; argument_node = argument_node.getNextSibling()) {
281 // An option
282 if (argument_node.getNodeName().equalsIgnoreCase("Option")) {
283 Argument argument = new Argument();
284 argument.parseXML((Element) argument_node);
285 classifier.addArgument(argument);
286 }
287 }
288 }
289 // A super classifier class
290 else if (node_name.equalsIgnoreCase("ClassInfo")) {
291 Classifier super_classifier = new Classifier();
292 parseClassifierInfoXMLNode(super_classifier, node);
293 classifier.setSuper(super_classifier);
294 }
295 }
296 }
297
298
299 static private ArrayList parseClassifiersListXML(String xml)
300 {
301 ArrayList classifiers_list = new ArrayList();
302
303 Document document = XMLTools.parseXML(new StringReader(xml));
304 Node root = document.getDocumentElement();
305 for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
306 String node_name = node.getNodeName();
307
308 if (node_name.equals("ClassInfo")) {
309 Classifier classifier = new Classifier();
310 parseClassifierInfoXMLNode(classifier, node);
311 classifiers_list.add(classifier);
312 }
313 }
314
315 return classifiers_list;
316 }
317}
Note: See TracBrowser for help on using the repository browser.