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 | */
|
---|
27 | package org.greenstone.gatherer.greenstone;
|
---|
28 |
|
---|
29 | import java.io.*;
|
---|
30 | import java.util.*;
|
---|
31 | import javax.swing.*;
|
---|
32 | import org.greenstone.gatherer.Configuration;
|
---|
33 | import org.greenstone.gatherer.DebugStream;
|
---|
34 | import org.greenstone.gatherer.Dictionary;
|
---|
35 | import org.greenstone.gatherer.Gatherer;
|
---|
36 | import org.greenstone.gatherer.cdm.Argument;
|
---|
37 | import org.greenstone.gatherer.cdm.Classifier;
|
---|
38 | import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
|
---|
39 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
40 | import org.greenstone.gatherer.util.Utility;
|
---|
41 | import org.greenstone.gatherer.util.XMLTools;
|
---|
42 | import org.w3c.dom.*;
|
---|
43 | import org.xml.sax.*;
|
---|
44 |
|
---|
45 |
|
---|
46 | public 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 | args.add(Configuration.perl_path);
|
---|
141 | args.add("-S");
|
---|
142 | args.add(LocalGreenstone.getBinScriptDirectoryPath() + "classinfo.pl");
|
---|
143 | if (collection_specific) {
|
---|
144 | args.add("-collection");
|
---|
145 | args.add(collection_name);
|
---|
146 | }
|
---|
147 | args.add("-xml");
|
---|
148 | args.add("-language");
|
---|
149 | args.add(Configuration.getLanguage());
|
---|
150 | args.add(classifier.getName());
|
---|
151 |
|
---|
152 | // Run the classinfo.pl process
|
---|
153 | Runtime runtime = Runtime.getRuntime();
|
---|
154 | Process process = runtime.exec((String[]) args.toArray(new String[] { }));
|
---|
155 | InputStream input_stream = process.getErrorStream();
|
---|
156 | StringBuffer classinfo_xml_buffer = XMLTools.readXMLStream(input_stream);
|
---|
157 | if (classinfo_xml_buffer != null) {
|
---|
158 | classinfo_xml = classinfo_xml_buffer.toString();
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | // Check the XML output was obtained successfully
|
---|
163 | if (classinfo_xml == null || classinfo_xml.length() == 0) {
|
---|
164 | classifier.setLoadingOptionsFailed();
|
---|
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, classinfo_xml);
|
---|
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 = Gatherer.remoteGreenstoneServer.getScriptOptions("classinfo.pl", classinfo_options);
|
---|
196 | xml = new StringBuffer(classinfo_output);
|
---|
197 | }
|
---|
198 | else {
|
---|
199 | ArrayList args = new ArrayList();
|
---|
200 | args.add(Configuration.perl_path);
|
---|
201 | args.add("-S");
|
---|
202 | args.add(LocalGreenstone.getBinScriptDirectoryPath() + "classinfo.pl");
|
---|
203 | if (collection_name != null) {
|
---|
204 | args.add("-collection");
|
---|
205 | args.add(collection_name);
|
---|
206 | }
|
---|
207 | args.add("-listall");
|
---|
208 | args.add("-xml");
|
---|
209 |
|
---|
210 | // Run the classinfo.pl process
|
---|
211 | Runtime runtime = Runtime.getRuntime();
|
---|
212 | Process process = runtime.exec((String[]) args.toArray(new String[] { }));
|
---|
213 | InputStream input_stream = process.getErrorStream();
|
---|
214 | xml = XMLTools.readXMLStream(input_stream);
|
---|
215 | }
|
---|
216 |
|
---|
217 | // Check the XML output was obtained successfully
|
---|
218 | if (xml == null || xml.length() == 0) {
|
---|
219 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_List_XML_Parse_Failed"), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
220 | return;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (collection_name != null) {
|
---|
224 | collection_specific_classifiers_list = parseClassifiersListXML(xml.toString());
|
---|
225 | }
|
---|
226 | else {
|
---|
227 | core_greenstone_classifiers_list = parseClassifiersListXML(xml.toString());
|
---|
228 | }
|
---|
229 | }
|
---|
230 | catch (Exception exception) {
|
---|
231 | DebugStream.printStackTrace(exception);
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | static private void parseClassifierInfoXML(Classifier classifier, String xml)
|
---|
237 | {
|
---|
238 | Document document = XMLTools.parseXML(new StringReader(xml));
|
---|
239 | if (document == null) {
|
---|
240 | classifier.setLoadingOptionsFailed();
|
---|
241 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.ClassifierManager.Classifier_XML_Parse_Failed", classifier.getName()), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
242 | return;
|
---|
243 | }
|
---|
244 |
|
---|
245 | parseClassifierInfoXMLNode(classifier, document.getDocumentElement());
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | static private void parseClassifierInfoXMLNode(Classifier classifier, Node root_node)
|
---|
250 | {
|
---|
251 | for (Node node = root_node.getFirstChild(); node != null; node = node.getNextSibling()) {
|
---|
252 | String node_name = node.getNodeName();
|
---|
253 |
|
---|
254 | if (node_name.equalsIgnoreCase("Name")) {
|
---|
255 | classifier.setName(XMLTools.getValue(node));
|
---|
256 | }
|
---|
257 | else if (node_name.equals("Desc")) {
|
---|
258 | classifier.setDescription(XMLTools.getValue(node));
|
---|
259 | }
|
---|
260 | else if (node_name.equals("Abstract")) {
|
---|
261 | classifier.setIsAbstract(XMLTools.getValue(node).equalsIgnoreCase(StaticStrings.YES_STR));
|
---|
262 | }
|
---|
263 | // Parse the classifier arguments
|
---|
264 | else if (node_name.equalsIgnoreCase("Arguments")) {
|
---|
265 | for (Node argument_node = node.getFirstChild(); argument_node != null; argument_node = argument_node.getNextSibling()) {
|
---|
266 | // An option
|
---|
267 | if (argument_node.getNodeName().equalsIgnoreCase("Option")) {
|
---|
268 | Argument argument = new Argument();
|
---|
269 | argument.parseXML((Element) argument_node);
|
---|
270 | classifier.addArgument(argument);
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|
274 | // A super classifier class
|
---|
275 | else if (node_name.equalsIgnoreCase("ClassInfo")) {
|
---|
276 | Classifier super_classifier = new Classifier();
|
---|
277 | parseClassifierInfoXMLNode(super_classifier, node);
|
---|
278 | classifier.setSuper(super_classifier);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | static private ArrayList parseClassifiersListXML(String xml)
|
---|
285 | {
|
---|
286 | ArrayList classifiers_list = new ArrayList();
|
---|
287 |
|
---|
288 | Document document = XMLTools.parseXML(new StringReader(xml));
|
---|
289 | Node root = document.getDocumentElement();
|
---|
290 | for (Node node = root.getFirstChild(); node != null; node = node.getNextSibling()) {
|
---|
291 | String node_name = node.getNodeName();
|
---|
292 |
|
---|
293 | if (node_name.equals("ClassInfo")) {
|
---|
294 | Classifier classifier = new Classifier();
|
---|
295 | parseClassifierInfoXMLNode(classifier, node);
|
---|
296 | classifiers_list.add(classifier);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | return classifiers_list;
|
---|
301 | }
|
---|
302 | }
|
---|