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

Last change on this file since 12647 was 12647, checked in by mdewsnip, 18 years ago

The last of the new plugins and classifiers code: collection-specific plugins and classifiers with remote building.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 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.LocalGreenstone;
37import org.greenstone.gatherer.cdm.Argument;
38import org.greenstone.gatherer.cdm.Classifier;
39import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
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 = null;
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) {
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 StringBuffer xml = null;
132 if (Gatherer.isGsdlRemote) {
133 String classinfo_options = "&classifier=" + classifier;
134 if (collection_specific) {
135 classinfo_options += "&collection=" + collection_name;
136 }
137 String classinfo_output = RemoteGreenstoneServer.getScriptOptions("classinfo.pl", classinfo_options);
138 xml = new StringBuffer(classinfo_output);
139 }
140 else {
141 ArrayList args = new ArrayList();
142 if (Utility.isWindows()) {
143 args.add(Configuration.perl_path);
144 args.add("-S");
145 }
146 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "classinfo.pl");
147 if (collection_specific) {
148 args.add("-collection");
149 args.add(collection_name);
150 }
151 args.add("-xml");
152 args.add("-language");
153 args.add(Configuration.getLanguage());
154 args.add(classifier.getName());
155
156 // Run the classinfo.pl process
157 Runtime runtime = Runtime.getRuntime();
158 Process process = runtime.exec((String[]) args.toArray(new String[] { }));
159 InputStream input_stream = process.getErrorStream();
160 xml = XMLTools.readXMLStream(input_stream);
161 }
162
163 // Check the XML output was obtained successfully
164 if (xml == null || xml.length() == 0) {
165 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_XML_Parse_Failed", classifier.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
166 return;
167 }
168
169 parseClassifierInfoXML(classifier, xml.toString());
170 }
171 catch (Exception exception) {
172 DebugStream.printStackTrace(exception);
173 }
174 }
175
176
177 static public void loadClassifiersList(String collection_name_arg)
178 {
179 DebugStream.println("In loadClassifiersList()...");
180
181 // If we're getting the collection-specific classifiers, clear the old list no matter what
182 if (collection_name_arg != null) {
183 collection_name = collection_name_arg;
184 collection_specific_classifiers_list = new ArrayList();
185 }
186
187 // Run classifierfo.pl to get the list of classifiers
188 try {
189 StringBuffer xml = null;
190 if (Gatherer.isGsdlRemote) {
191 String classinfo_options = "&listall";
192 if (collection_name != null) {
193 classinfo_options += "&collection=" + collection_name;
194 }
195 String classinfo_output = RemoteGreenstoneServer.getScriptOptions("classinfo.pl", classinfo_options);
196 xml = new StringBuffer(classinfo_output);
197 }
198 else {
199 ArrayList args = new ArrayList();
200 if (Utility.isWindows()) {
201 args.add(Configuration.perl_path);
202 args.add("-S");
203 }
204 args.add(LocalGreenstone.getBinScriptDirectoryPath() + "classinfo.pl");
205 if (collection_name != null) {
206 args.add("-collection");
207 args.add(collection_name);
208 }
209 args.add("-listall");
210 args.add("-xml");
211
212 // Run the classinfo.pl process
213 Runtime runtime = Runtime.getRuntime();
214 Process process = runtime.exec((String[]) args.toArray(new String[] { }));
215 InputStream input_stream = process.getErrorStream();
216 xml = XMLTools.readXMLStream(input_stream);
217 }
218
219 // Check the XML output was obtained successfully
220 if (xml == null || xml.length() == 0) {
221 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_List_XML_Parse_Failed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
222 return;
223 }
224
225 if (collection_name != null) {
226 collection_specific_classifiers_list = parseClassifiersListXML(xml.toString());
227 }
228 else {
229 core_greenstone_classifiers_list = parseClassifiersListXML(xml.toString());
230 }
231 }
232 catch (Exception exception) {
233 DebugStream.printStackTrace(exception);
234 }
235 }
236
237
238 static private void parseClassifierInfoXML(Classifier classifier, String xml)
239 {
240 Document document = XMLTools.parseXML(new StringReader(xml));
241 parseClassifierInfoXMLNode(classifier, document.getDocumentElement());
242 }
243
244
245 static private void parseClassifierInfoXMLNode(Classifier classifier, Node root_node)
246 {
247 for (Node node = root_node.getFirstChild(); node != null; node = node.getNextSibling()) {
248 String node_name = node.getNodeName();
249
250 if (node_name.equalsIgnoreCase("Name")) {
251 classifier.setName(XMLTools.getValue(node));
252 }
253 else if (node_name.equals("Desc")) {
254 classifier.setDescription(XMLTools.getValue(node));
255 }
256 else if (node_name.equals("Abstract")) {
257 classifier.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
258 }
259 // Parse the classifier arguments
260 else if (node_name.equalsIgnoreCase("Arguments")) {
261 for (Node argument_node = node.getFirstChild(); argument_node != null; argument_node = argument_node.getNextSibling()) {
262 // An option
263 if (argument_node.getNodeName().equalsIgnoreCase("Option")) {
264 Argument argument = new Argument();
265 argument.parseXML((Element) argument_node);
266 classifier.addArgument(argument);
267 }
268 }
269 }
270 // A super classifier class
271 else if (node_name.equalsIgnoreCase("ClassInfo")) {
272 Classifier super_classifier = new Classifier();
273 parseClassifierInfoXMLNode(super_classifier, node);
274 classifier.setSuper(super_classifier);
275 }
276 }
277 }
278
279
280 static private ArrayList parseClassifiersListXML(String xml)
281 {
282 ArrayList classifiers_list = new ArrayList();
283
284 Document document = XMLTools.parseXML(new StringReader(xml));
285 Node root = document.getDocumentElement();
286 for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
287 String node_name = node.getNodeName();
288
289 if (node_name.equals("ClassInfo")) {
290 Classifier classifier = new Classifier();
291 parseClassifierInfoXMLNode(classifier, node);
292 classifiers_list.add(classifier);
293 }
294 }
295
296 return classifiers_list;
297 }
298}
Note: See TracBrowser for help on using the repository browser.