source: gli/branches/rtl-gli/src/org/greenstone/gatherer/greenstone/Classifiers.java@ 18356

Last change on this file since 18356 was 18356, checked in by kjdon, 15 years ago

updated the rtl-gli branch with files from trunk. Result of a merge 14807:18318

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