source: gli/trunk/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java@ 20440

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

starting to remove code that strips off extracted namespace - this gets written out to config file now.

  • Property svn:keywords set to Author Date Id Revision
File size: 205.4 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: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 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.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.io.*;
32import java.util.*;
33import javax.swing.*;
34import org.greenstone.gatherer.Configuration;
35import org.greenstone.gatherer.DebugStream;
36import org.greenstone.gatherer.Gatherer;
37import org.greenstone.gatherer.collection.CollectionManager;
38import org.greenstone.gatherer.greenstone.LocalLibraryServer;
39import org.greenstone.gatherer.gui.GLIButton;
40import org.greenstone.gatherer.metadata.MetadataElement;
41import org.greenstone.gatherer.metadata.MetadataSetManager;
42import org.greenstone.gatherer.metadata.MetadataTools;
43import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
44import org.greenstone.gatherer.util.DOMTree;
45import org.greenstone.gatherer.util.Codec;
46import org.greenstone.gatherer.util.StaticStrings;
47import org.greenstone.gatherer.util.XMLTools;
48import org.greenstone.gatherer.util.Utility;
49import org.w3c.dom.*;
50
51/** This class provides access to an xml-type view of the collect.cfg file. This is useful as it allows the manipulation and free form editing of a collect.cfg file while still allowing the various CDM data managers to base themselves directly on this model (whereas they used to be independant ListModels which clobbered the ordering of unparsed commands).
52 * @author John Thompson, Greenstone Digital Library, University of Waikato
53 * @version 2.3d
54 */
55public class CollectionConfiguration {
56 static final private String ENCODING = "UTF-8";
57 static final private String NEWLINE_ELEMENT = "NewLine";
58 static final private String PLUGOUT_ELEMENT = "plugout";//used by building flax collections
59
60 static private Document document;
61 static private StringBuffer saved_collect_cfg_string_buffer = null;
62
63
64 static public Element createElement (String element_name) {
65 return document.createElement (element_name);
66 }
67
68
69 /** Find the best insertion position for the given DOM Element. This should try to match command tag, and if found should then try to group by name or type (eg CollectionMeta), or append to end is no such grouping exists (eg Plugins). Failing a command match it will check against the command order for the best insertion location.
70 * @param target_element the command Element to be inserted
71 * @return the Element which the given command should be inserted before, or null to append to end of list
72 */
73 static public Node findInsertionPoint (Element target_element) {
74 ///ystem.err.println("Find insertion point: " + target_element.getNodeName());
75 String target_element_name = target_element.getNodeName ();
76 Element document_element = document.getDocumentElement ();
77 // Try to find commands with the same tag.
78 NodeList matching_elements = document_element.getElementsByTagName (target_element_name);
79 // If we found matching elements, then we have our most likely insertion location, so check within for groupings
80 if(matching_elements.getLength () != 0) {
81 ///ystem.err.println("Found matching elements.");
82 // Only CollectionMeta are grouped.
83 if(target_element_name.equals (StaticStrings.COLLECTIONMETADATA_ELEMENT)) {
84 ///ystem.err.println("Dealing with collection metadata");
85 // Special case: CollectionMeta can be added at either the start or end of a collection configuration file. However the start position is reserved for special metadata, so if no non-special metadata can be found we must append to the end.
86 // So if the command to be added is special add it immediately after any other special command
87 if(target_element.getAttribute (StaticStrings.SPECIAL_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
88 int index = 0;
89 Element matched_element = (Element) matching_elements.item (index);
90 Element sibling_element = (Element) matched_element.getNextSibling ();
91 while(sibling_element.getAttribute (StaticStrings.SPECIAL_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
92 index++;
93 matched_element = (Element) matching_elements.item (index);
94 sibling_element = (Element) matched_element.getNextSibling ();
95 }
96 if(sibling_element.getNodeName ().equals (NEWLINE_ELEMENT)) {
97 Element newline_element = document.createElement (NEWLINE_ELEMENT);
98 document_element.insertBefore (newline_element, sibling_element);
99 }
100 return sibling_element;
101 }
102 // Otherwise try to find a matching 'name' and add after the last one in that group.
103 else {
104 int index = 0;
105 target_element_name = target_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
106 boolean found = false;
107 // Skip all of the special metadata
108 Element matched_element = (Element) matching_elements.item (index);
109 while(matched_element.getAttribute (StaticStrings.SPECIAL_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
110 index++;
111 matched_element = (Element) matching_elements.item (index);
112 }
113 // Begin search
114 while(!found && matched_element != null) {
115 if(matched_element.getAttribute (StaticStrings.NAME_ATTRIBUTE).equals (target_element_name)) {
116 found = true;
117 }
118 else {
119 index++;
120 matched_element = (Element) matching_elements.item (index);
121 }
122 }
123 // If we found a match, we need to continue checking until we find the last name match.
124 if(found) {
125 index++;
126 Element previous_sibling = matched_element;
127 Element sibling_element = (Element) matching_elements.item (index);
128 while(sibling_element != null && sibling_element.getAttribute (StaticStrings.NAME_ATTRIBUTE).equals (target_element_name)) {
129 previous_sibling = sibling_element;
130 index++;
131 sibling_element = (Element) matching_elements.item (index);
132 }
133 // Previous sibling now holds the command immediately before where we want to add, so find its next sibling and add to that. In this one case we can ignore new lines!
134 return previous_sibling.getNextSibling ();
135 }
136 // If not found we just add after last metadata element
137 else {
138 Element last_element = (Element) matching_elements.item (matching_elements.getLength () - 1);
139 return last_element.getNextSibling ();
140 }
141 }
142
143 }
144 else {
145 ///ystem.err.println("Not dealing with collection meta.");
146 Element matched_element = (Element) matching_elements.item (matching_elements.getLength () - 1);
147 // One final quick test. If the matched element is immediately followed by a NewLine command, then we insert another NewLine after the matched command, then return the NewLine instead (thus the about to be inserted command will be placed between the two NewLines)
148 Node sibling_element = matched_element.getNextSibling ();
149 if(sibling_element != null && sibling_element.getNodeName ().equals (NEWLINE_ELEMENT)) {
150 Element newline_element = document.createElement (NEWLINE_ELEMENT);
151 document_element.insertBefore (newline_element, sibling_element);
152 }
153 return sibling_element; // Note that this may be null
154 }
155 }
156 ///ystem.err.println("No matching elements found.");
157 // Locate where this command is in the ordering
158 int command_index = -1;
159 for(int i = 0; command_index == -1 && i < COMMAND_ORDER.length; i++) {
160 if(COMMAND_ORDER[i].equals (target_element_name)) {
161 command_index = i;
162 }
163 }
164 ///ystem.err.println("Command index is: " + command_index);
165 // Now move forward, checking for existing elements in each of the preceeding command orders.
166 int preceeding_index = command_index - 1;
167 ///ystem.err.println("Searching before the target command.");
168 while(preceeding_index >= 0) {
169 matching_elements = document_element.getElementsByTagName (COMMAND_ORDER[preceeding_index]);
170 // If we've found a match
171 if(matching_elements.getLength () > 0) {
172 // We add after the last element
173 Element matched_element = (Element) matching_elements.item (matching_elements.getLength () - 1);
174 // One final quick test. If the matched element is immediately followed by a NewLine command, then we insert another NewLine after the matched command, then return the NewLine instead (thus the about to be inserted command will be placed between the two NewLines)
175 Node sibling_element = matched_element.getNextSibling ();
176 if(sibling_element != null && sibling_element.getNodeName ().equals (NEWLINE_ELEMENT)) {
177 Element newline_element = document.createElement (NEWLINE_ELEMENT);
178 document_element.insertBefore (newline_element, sibling_element);
179 }
180 return sibling_element; // Note that this may be null
181 }
182 preceeding_index--;
183 }
184 // If all that fails, we now move backwards through the commands
185 int susceeding_index = command_index + 1;
186 ///ystem.err.println("Searching after the target command.");
187 while(susceeding_index < COMMAND_ORDER.length) {
188 matching_elements = document_element.getElementsByTagName (COMMAND_ORDER[susceeding_index]);
189 // If we've found a match
190 if(matching_elements.getLength () > 0) {
191 // We add before the first element
192 Element matched_element = (Element) matching_elements.item (0);
193 // One final quick test. If the matched element is immediately preceeded by a NewLine command, then we insert another NewLine before the matched command, then return this new NewLine instead (thus the about to be inserted command will be placed between the two NewLines)
194 Node sibling_element = matched_element.getPreviousSibling ();
195 if(sibling_element != null && sibling_element.getNodeName ().equals (NEWLINE_ELEMENT)) {
196 Element newline_element = document.createElement (NEWLINE_ELEMENT);
197 document_element.insertBefore (newline_element, sibling_element);
198 }
199 return sibling_element; // Note that this may be null
200 }
201 susceeding_index++;
202 }
203 // Well. Apparently there are no other commands in this collection configuration. So append away...
204 return null;
205 }
206
207
208 static public NodeList getElementsByTagName (String element_name) {
209 return document.getDocumentElement ().getElementsByTagName (element_name);
210 }
211
212
213 static public String toString (Element command_element, boolean show_extracted_namespace) {
214 String command_element_name = command_element.getNodeName ();
215 if(command_element_name.equals (StaticStrings.CLASSIFY_ELEMENT)) {
216 return classifyToString (command_element);
217 }
218 else if(command_element_name.equals (StaticStrings.FORMAT_ELEMENT)) {
219 return formatToString (command_element);
220 }
221 else if(command_element_name.equals (StaticStrings.INDEXES_ELEMENT)) {
222 return indexesToString (command_element);
223 }
224 else if(command_element_name.equals (StaticStrings.INDEX_DEFAULT_ELEMENT)) {
225 return indexDefaultToString (command_element);
226 }
227 else if(command_element_name.equals (StaticStrings.LANGUAGES_ELEMENT)) {
228 return languagesToString (command_element);
229 }
230 else if(command_element_name.equals (StaticStrings.LANGUAGE_DEFAULT_ELEMENT)) {
231 return languageDefaultToString (command_element);
232 }
233 else if (command_element_name.equals (StaticStrings.LANGUAGE_METADATA_ELEMENT)) {
234 return languageMetadataToString (command_element);
235 }
236 else if(command_element_name.equals (StaticStrings.INDEXOPTIONS_ELEMENT)) {
237 return indexOptionsToString (command_element);
238 }
239 else if(command_element_name.equals (StaticStrings.INDEXOPTION_DEFAULT_ELEMENT)) {
240 return indexOptionDefaultToString (command_element);
241 }
242 else if(command_element_name.equals (StaticStrings.COLLECTIONMETADATA_ELEMENT)) {
243 return metadataToString (command_element, show_extracted_namespace);
244 }
245 else if(command_element_name.equals (StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT)) {
246 return metadataToString (command_element, show_extracted_namespace);
247 }
248 else if(command_element_name.equals (StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT)) {
249 return metadataToString (command_element, show_extracted_namespace);
250 }
251 else if(command_element_name.equals (StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT)) {
252 return metadataToString (command_element, show_extracted_namespace);
253 }
254 else if (command_element_name.equals (StaticStrings.BUILDTYPE_ELEMENT)) {
255 return metadataToString (command_element, show_extracted_namespace);
256 }
257 else if(command_element_name.equals (StaticStrings.PLUGIN_ELEMENT)) {
258 return pluginToString (command_element);
259 }
260 else if(command_element_name.equals (StaticStrings.SUBCOLLECTION_ELEMENT)) {
261 return subcollectionToString (command_element);
262 }
263 else if(command_element_name.equals (StaticStrings.SUBCOLLECTION_DEFAULT_INDEX_ELEMENT)) {
264 return subcollectionDefaultIndexToString (command_element);
265 }
266 else if(command_element_name.equals (StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT)) {
267 return subcollectionIndexesToString (command_element);
268 }
269 else if(command_element_name.equals (StaticStrings.SUPERCOLLECTION_ELEMENT)) {
270 return supercollectionToString (command_element);
271 }
272 else if(command_element_name.equals (StaticStrings.UNKNOWN_ELEMENT)) {
273 return unknownToString (command_element);
274 }
275 return "";
276 }
277
278 /** Parses arguments from a tokenizer and returns a HashMap of mappings. The tricky bit here is that not all entries in the HashMap are name->value pairs, as some arguments are boolean and are turned on by their presence. Arguments are denoted by a '-' prefix.
279 * @param tokenizer a CommandTokenizer based on the unconsumed portion of a command string
280 * @return a HashMap containing the arguments parsed
281 */
282 static public HashMap parseArguments (CommandTokenizer tokenizer) {
283 HashMap arguments = new HashMap ();
284 String name = null;
285 String value = null;
286 while(tokenizer.hasMoreTokens () || name != null) {
287 // First we retrieve a name if we need one.
288 if(name == null) {
289 name = tokenizer.nextToken ();
290 }
291 // Now we attempt to retrieve a value
292 if(tokenizer.hasMoreTokens ()) {
293 value = tokenizer.nextToken ();
294 // Test if the value is actually a name, and if so add the name by itself, then put value into name so that it is parsed correctly during the next loop.
295 // The value is not a name if it contains a space character: it's a quoted value
296 if (value.startsWith(StaticStrings.MINUS_CHARACTER) && value.indexOf(StaticStrings.SPACE_CHARACTER) == -1) {
297 arguments.put (name, null);
298 name = value;
299 }
300 // Otherwise we have a typical name->value pair ready to go
301 else {
302 arguments.put (name, value);
303 name = null;
304 }
305 }
306 // Otherwise its a binary flag
307 else {
308 arguments.put (name, null);
309 name = null;
310 }
311 }
312 return arguments;
313 }
314
315 /** Gives the preferred ordering of commands */
316 static final private String[] COMMAND_ORDER =
317 {StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT, StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT, StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT, StaticStrings.BUILDTYPE_ELEMENT, StaticStrings.PLUGIN_ELEMENT, StaticStrings.INDEXES_ELEMENT, StaticStrings.INDEX_DEFAULT_ELEMENT, StaticStrings.INDEXOPTIONS_ELEMENT, StaticStrings.INDEXOPTION_DEFAULT_ELEMENT, StaticStrings.LANGUAGES_ELEMENT, StaticStrings.LANGUAGE_DEFAULT_ELEMENT, StaticStrings.LANGUAGE_METADATA_ELEMENT, StaticStrings.SUBCOLLECTION_ELEMENT, StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT, StaticStrings.SUBCOLLECTION_DEFAULT_INDEX_ELEMENT, StaticStrings.SUPERCOLLECTION_ELEMENT, StaticStrings.CLASSIFY_ELEMENT, StaticStrings.FORMAT_ELEMENT, StaticStrings.COLLECTIONMETADATA_ELEMENT};
318
319 /** ************************** Public Data Members ***************************/
320
321 /** ************************** Private Data Members ***************************/
322
323 private File collect_cfg_file;
324
325 /** ************************** Public Methods ***************************/
326
327
328 /** This debug facility shows the currently loaded collect.cfg or CollectConfig.xml file as a DOM tree. */
329 public void display () {
330 JDialog dialog = new JDialog (Gatherer.g_man, "Collection Configuration", false);
331 dialog.setSize (400,400);
332 JPanel content_pane = (JPanel) dialog.getContentPane ();
333 final DOMTree tree = new DOMTree (document);
334 JButton refresh_button = new GLIButton ("Refresh Tree");
335 refresh_button.addActionListener (new ActionListener () {
336 public void actionPerformed (ActionEvent event) {
337 tree.setDocument (document);
338 }
339 });
340 content_pane.setBorder (BorderFactory.createEmptyBorder (5,5,5,5));
341 content_pane.setLayout (new BorderLayout ());
342 content_pane.add (new JScrollPane (tree), BorderLayout.CENTER);
343 content_pane.add (refresh_button, BorderLayout.SOUTH);
344 dialog.setVisible (true);
345 }
346
347
348 public Element getCreator () {
349 Element element = getOrCreateElementByTagName (StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT, null, null);
350 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.COLLECTIONMETADATA_CREATOR_STR);
351 element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
352 return element;
353 }
354
355 public Element getDocumentElement () {
356 return document.getDocumentElement ();
357 }
358
359 public File getFile () {
360 return collect_cfg_file;
361 }
362
363 /** Retrieve or create the languages Element. */
364 public Element getLanguages () {
365 return getOrCreateElementByTagName (StaticStrings.LANGUAGES_ELEMENT, null, null);
366 }
367
368 public Element getLanguageMetadata () {
369 return getOrCreateElementByTagName (StaticStrings.LANGUAGE_METADATA_ELEMENT, null, null);
370 }
371
372 public Element getLevels () {
373 return getOrCreateElementByTagName (StaticStrings.INDEXOPTIONS_ELEMENT, StaticStrings.NAME_ATTRIBUTE, StaticStrings.LEVELS_STR);
374 }
375
376 public Element getLevelDefault () {
377 return getOrCreateElementByTagName (StaticStrings.INDEXOPTION_DEFAULT_ELEMENT, StaticStrings.NAME_ATTRIBUTE, StaticStrings.LEVEL_DEFAULT_STR);
378 }
379
380 public Element getIndexOptions () {
381 return getOrCreateElementByTagName (StaticStrings.INDEXOPTIONS_ELEMENT, StaticStrings.NAME_ATTRIBUTE, StaticStrings.INDEXOPTIONS_STR);
382 }
383
384
385 public Element getMaintainer () {
386 Element element = getOrCreateElementByTagName (StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT, null, null);
387 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR);
388 element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
389 return element;
390 }
391
392 /** Retrieve or create the indexes Element. Note that this method behaves differently from the other getBlah methods, in that it also has to keep in mind that indexes come in two flavours, MG and MGPP. */
393 public Element getMGIndexes () {
394 return getOrCreateElementByTagName (StaticStrings.INDEXES_ELEMENT, StaticStrings.MGPP_ATTRIBUTE, StaticStrings.FALSE_STR);
395 }
396
397 public Element getMGPPIndexes () {
398 return getOrCreateElementByTagName (StaticStrings.INDEXES_ELEMENT, StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
399 }
400
401 public Element getPublic () {
402 Element element = getOrCreateElementByTagName (StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT, null, null);
403 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.COLLECTIONMETADATA_PUBLIC_STR);
404 element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
405 return element;
406 }
407
408 public Element getBuildType () {
409 Element element = getOrCreateElementByTagName (StaticStrings.BUILDTYPE_ELEMENT, null, null);
410 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.BUILDTYPE_STR);
411 element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
412 return element;
413
414 }
415
416 /** Retrieve or create the subindexes Element. */
417 public Element getSubIndexes () {
418 return getOrCreateElementByTagName (StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT, null, null);
419 }
420
421 /** Retrieve or create the supercollections Element. */
422 public Element getSuperCollection () {
423 return getOrCreateElementByTagName (StaticStrings.SUPERCOLLECTION_ELEMENT, null, null);
424 }
425
426 public boolean ready () {
427 return document != null;
428 }
429
430
431
432
433 /** ************************** Private Methods ***************************/
434
435 static private String classifyToString (Element command_element) {
436 StringBuffer text = new StringBuffer (StaticStrings.CLASSIFY_STR);
437 text.append (StaticStrings.TAB_CHARACTER);
438 text.append (command_element.getAttribute (StaticStrings.TYPE_ATTRIBUTE));
439 NodeList option_elements = command_element.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
440 int option_elements_length = option_elements.getLength ();
441 for(int j = 0; j < option_elements_length; j++) {
442 Element option_element = (Element) option_elements.item (j);
443 if(option_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
444 text.append (StaticStrings.SPACE_CHARACTER);
445 text.append (StaticStrings.MINUS_CHARACTER);
446 text.append (option_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
447 String value_str = XMLTools.getValue (option_element);
448
449 // value_str = modifyMetadataArgumentValue(value_str);
450 if (value_str.length () > 0) {
451 text.append (StaticStrings.SPACE_CHARACTER);
452 if(value_str.indexOf (StaticStrings.SPACE_CHARACTER) != -1) {
453 // enclose in quotes
454 text.append(StaticStrings.SPEECH_CHARACTER);
455 text.append(value_str);
456 text.append(StaticStrings.SPEECH_CHARACTER);
457 } else {
458
459 text.append(value_str);
460 }
461 }
462
463 value_str = null;
464 }
465 option_element = null;
466 }
467 option_elements = null;
468 return text.toString ();
469 }
470
471 static private String formatToString (Element command_element) {
472 StringBuffer text = new StringBuffer (StaticStrings.FORMAT_STR);
473 text.append (StaticStrings.SPACE_CHARACTER);
474 text.append (command_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
475 text.append (StaticStrings.SPACE_CHARACTER);
476 String value_str = command_element.getAttribute (StaticStrings.VALUE_ATTRIBUTE);
477 if(value_str.length () != 0) {
478 text.append (value_str);
479 }
480 else {
481 // Remember to encode format string to Greenstone specification
482 value_str = Codec.transform (XMLTools.getValue (command_element), Codec.DOM_TO_GREENSTONE);
483 text.append (StaticStrings.SPEECH_CHARACTER);
484 text.append (value_str);
485 text.append (StaticStrings.SPEECH_CHARACTER);
486 }
487 value_str = null;
488 return text.toString ();
489 }
490
491 /** Retrieve or create the indexes Element. */
492 static private Element getOrCreateElementByTagName (String name, String conditional_attribute, String required_value) {
493 Element document_element = document.getDocumentElement ();
494 NodeList elements = document_element.getElementsByTagName (name);
495 int elements_length = elements.getLength ();
496 if(elements_length > 0) {
497 if(conditional_attribute == null) {
498 document_element = null;
499 return (Element) elements.item (0);
500 }
501 else {
502 for(int i = 0; i < elements_length; i++) {
503 Element element = (Element) elements.item (i);
504 if(element.getAttribute (conditional_attribute).equals (required_value)) {
505 document_element = null;
506 return element;
507 }
508 element = null;
509 }
510 }
511 }
512 // Create the element
513 Element element = document.createElement (name);
514 // If there was a property set it
515 if(conditional_attribute != null) {
516 element.setAttribute (conditional_attribute, required_value);
517 }
518 Node target_node = findInsertionPoint (element);
519 if(target_node != null) {
520 document_element.insertBefore (element, target_node);
521 }
522 else {
523 document_element.appendChild (element);
524 }
525 document_element = null;
526 return element;
527 }
528
529 static private String indexesToString (Element command_element) {
530 boolean comment_only = false;
531 StringBuffer text = new StringBuffer ("");
532 if(command_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
533 text.append ("#");
534 comment_only = true;
535 }
536 text.append (StaticStrings.INDEX_STR);
537 text.append (StaticStrings.TAB_CHARACTER);
538 if(!comment_only) {
539 text.append (StaticStrings.TAB_CHARACTER);
540 }
541 NodeList index_elements = command_element.getElementsByTagName (StaticStrings.INDEX_ELEMENT);
542 if (index_elements.getLength () == 0) { // no indexes
543 return "";
544 }
545 // For each index, write its level, a colon, then concatenate its child content elements into a single comma separated list
546 int index_elements_length = index_elements.getLength ();
547 for(int j = 0; j < index_elements_length; j++) {
548 Element index_element = (Element) index_elements.item (j);
549 String level_str = index_element.getAttribute (StaticStrings.LEVEL_ATTRIBUTE);
550 if(level_str.length () > 0) {
551 text.append (level_str);
552 text.append (StaticStrings.COLON_CHARACTER);
553 }
554 NodeList content_elements = index_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
555 int content_elements_length = content_elements.getLength ();
556 // Don't output anything if no indexes are set
557 if(content_elements_length == 0) {
558 return null;
559 }
560 for(int k = 0; k < content_elements_length; k++) {
561 Element content_element = (Element) content_elements.item (k);
562 String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
563 text.append (name_str);
564 name_str = null;
565 if(k < content_elements_length - 1) {
566 text.append (StaticStrings.COMMA_CHARACTER);
567 }
568 content_element = null;
569 }
570 if(j < index_elements_length - 1) {
571 text.append (StaticStrings.SPACE_CHARACTER);
572 }
573 content_elements = null;
574 index_element = null;
575 }
576 index_elements = null;
577 return text.toString ();
578 }
579
580 static private String indexDefaultToString (Element command_element) {
581 StringBuffer text = new StringBuffer ("");
582 if(command_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
583 text.append ("#");
584 }
585 text.append (StaticStrings.INDEX_DEFAULT_STR);
586 text.append (StaticStrings.TAB_CHARACTER);
587 if (!command_element.getAttribute (StaticStrings.LEVEL_ATTRIBUTE).equals ("")) {
588 text.append (command_element.getAttribute (StaticStrings.LEVEL_ATTRIBUTE));
589 text.append (StaticStrings.COLON_CHARACTER);
590 }
591 NodeList content_elements = command_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
592 int content_elements_length = content_elements.getLength ();
593 for(int j = 0; j < content_elements_length; j++) {
594 Element content_element = (Element) content_elements.item (j);
595 String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
596 text.append (name_str);
597 name_str = null;
598 if(j < content_elements_length - 1) {
599 text.append (StaticStrings.COMMA_CHARACTER);
600 }
601 content_element = null;
602 }
603 content_elements = null;
604 return text.toString ();
605 }
606
607 static private String languagesToString (Element command_element) {
608 StringBuffer text = new StringBuffer (StaticStrings.LANGUAGES_STR);
609 text.append (StaticStrings.TAB_CHARACTER);
610 // Retrieve all the languages and write them out in a space separated list
611 NodeList language_elements = command_element.getElementsByTagName (StaticStrings.LANGUAGE_ELEMENT);
612 int language_elements_length = language_elements.getLength ();
613 if(language_elements_length == 0) {
614 return null;
615 }
616 for(int j = 0; j < language_elements_length; j++) {
617 Element language_element = (Element) language_elements.item (j);
618 text.append (language_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
619 if(j < language_elements_length - 1) {
620 text.append (StaticStrings.SPACE_CHARACTER);
621 }
622 }
623 return text.toString ();
624 }
625
626 static private String languageDefaultToString (Element command_element) {
627 StringBuffer text = new StringBuffer (StaticStrings.LANGUAGE_DEFAULT_STR);
628 text.append (StaticStrings.TAB_CHARACTER);
629 text.append (command_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
630 return text.toString ();
631 }
632
633 static private String languageMetadataToString (Element command_element) {
634 if (!command_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
635 return "";
636 }
637 StringBuffer text = new StringBuffer (StaticStrings.LANGUAGE_METADATA_STR);
638 text.append (StaticStrings.TAB_CHARACTER);
639 String name_str = command_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
640 text.append (name_str);
641 return text.toString ();
642 }
643
644 static private String indexOptionsToString (Element command_element) {
645 StringBuffer text = new StringBuffer ("");
646 if(command_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
647 text.append ("#");
648 }
649 text.append (command_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
650 text.append (StaticStrings.TAB_CHARACTER);
651 NodeList content_elements = command_element.getElementsByTagName (StaticStrings.INDEXOPTION_ELEMENT);
652 int content_elements_length = content_elements.getLength ();
653 // Don't output anything if no options are set.
654 if(content_elements_length == 0) {
655 return null;
656 }
657 for(int i = 0; i < content_elements_length; i++) {
658 Element content_element = (Element) content_elements.item (i);
659 text.append (content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
660 text.append (StaticStrings.SPACE_CHARACTER);
661 }
662 return text.substring (0, text.length () - 1);
663 }
664
665 static private String indexOptionDefaultToString (Element command_element) {
666 // Don't bother if there is no value
667 if (command_element.getAttribute (StaticStrings.VALUE_ATTRIBUTE).equals ("")) {
668 return "";
669 }
670 StringBuffer text = new StringBuffer ("");
671 if(command_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
672 text.append ("#");
673 }
674 text.append (command_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
675 text.append (StaticStrings.TAB_CHARACTER);
676 text.append (command_element.getAttribute (StaticStrings.VALUE_ATTRIBUTE));
677 return text.toString ();
678 }
679
680 static private String metadataToString (Element command_element, boolean text_value) {
681 // lets first check the value - if its empty, don't bother sticking it in the config file
682 String value_str = XMLTools.getValue (command_element);
683 if (value_str.equals ("")) {
684 return "";
685 }
686 boolean special = false;
687
688 StringBuffer text = new StringBuffer ("");
689 String name_str = command_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
690 // If the name is one of the special four, we don't write the collectionmeta first. Note maintainer and buildtype are singled out for 'prittying' reasons.
691 if(name_str.equals (StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)|| name_str.equals (StaticStrings.BUILDTYPE_STR) ) {
692 text.append (name_str);
693 text.append (StaticStrings.TAB_CHARACTER);
694 special = true;
695 }
696 else if (name_str.equals (StaticStrings.COLLECTIONMETADATA_CREATOR_STR) || name_str.equals (StaticStrings.COLLECTIONMETADATA_PUBLIC_STR) ) {
697 text.append (name_str);
698 text.append (StaticStrings.TAB_CHARACTER);
699 text.append (StaticStrings.TAB_CHARACTER);
700 special = true;
701 }
702 else {
703 text.append (StaticStrings.COLLECTIONMETADATA_STR);
704 text.append (StaticStrings.TAB_CHARACTER);
705 text.append (name_str);
706 text.append (StaticStrings.SPACE_CHARACTER);
707 String language_str = command_element.getAttribute (StaticStrings.LANGUAGE_ATTRIBUTE);
708 text.append (StaticStrings.LBRACKET_CHARACTER);
709 text.append (StaticStrings.LANGUAGE_ARGUMENT);
710 text.append (language_str);
711 text.append (StaticStrings.RBRACKET_CHARACTER);
712 text.append (StaticStrings.SPACE_CHARACTER);
713 }
714 name_str = null;
715
716 // The value string we retrieved will be encoded for xml, so we now decode it - to text if text_value set. This parameter was originally show_extracted_namespace, but sincethis is only true for 'toString()' commands from within the CDM, its good enough to determine if this toString() will be used to display on screen, or write to collect.cfg
717 if(text_value == CollectionMeta.TEXT) {
718 value_str = Codec.transform (value_str, Codec.DOM_TO_TEXT);
719 }
720 else {
721 value_str = Codec.transform (value_str, Codec.DOM_TO_GREENSTONE);
722 }
723
724 // We don't wrap the email addresses in quotes, nor the other special metadata
725 if(special) {
726 text.append (value_str);
727 }
728 else {
729 text.append (StaticStrings.SPEECH_CHARACTER);
730 text.append (value_str);
731 text.append (StaticStrings.SPEECH_CHARACTER);
732 }
733 value_str = null;
734 return text.toString ();
735 }
736
737 /** Parse a collect.cfg into a DOM model representation.
738 * note we are ignoring 2.39 compatibility now. */
739 private void parse (File collect_cfg_file) {
740 // hack for pre 2.71 compatibility - we need to add in a
741 // build type if there is not one there
742 boolean search_types_parsed = false;
743 boolean build_types_parsed = false;
744 try {
745 saved_collect_cfg_string_buffer = new StringBuffer ();
746
747 Element collect_cfg_element = document.getDocumentElement ();
748 // Read in the file one command at a time.
749 InputStream istream = new FileInputStream (collect_cfg_file);
750 Reader in_reader = new InputStreamReader (istream, ENCODING);
751 BufferedReader in = new BufferedReader (in_reader);
752 String command_str = null;
753 while((command_str = in.readLine ()) != null) {
754 saved_collect_cfg_string_buffer.append (command_str + "\n");
755
756 boolean append_element = true;
757 Element command_element = null;
758 // A command may be broken over several lines.
759 command_str = command_str.trim ();
760 boolean eof = false;
761 while(!eof && command_str.endsWith (StaticStrings.NEWLINE_CHARACTER)) {
762 String next_line = in.readLine ();
763 if(next_line != null) {
764 next_line = next_line.trim ();
765 if(next_line.length () > 0) {
766 // Remove the new line character
767 command_str = command_str.substring (0, command_str.lastIndexOf (StaticStrings.NEWLINE_CHARACTER));
768 // And append the next line, which due to the test above must be non-zero length
769 command_str = command_str + next_line;
770 }
771 next_line = null;
772 }
773 // If we've reached the end of the file theres nothing more we can do
774 else {
775 eof = true;
776 }
777 }
778 // If there is still a new line character, then we remove it and hope for the best
779 if(command_str.endsWith (StaticStrings.NEWLINE_CHARACTER)) {
780 command_str = command_str.substring (0, command_str.lastIndexOf (StaticStrings.NEWLINE_CHARACTER));
781 }
782 // Now we've either got a command to parse...
783 if(command_str.length () != 0) {
784 // Start trying to figure out what it is
785 //StringTokenizer tokenizer = new StringTokenizer(command_str);
786 // Instead of a standard string tokenizer I'm going to use the new version of CommandTokenizer, which is not only smart enough to correctly notice speech marks and correctly parse them out, but now also takes the input stream so it can rebuild tokens that stretch over several lines.
787 CommandTokenizer tokenizer = new CommandTokenizer (command_str, in);
788 String command_type = tokenizer.nextToken ().toLowerCase ();
789 // Why can't you switch on strings eh? We pass it to the various subparsers who each have a bash at parsing the command. If none can parse the command, an unknown element is created
790 if(command_element == null && command_type.equals (StaticStrings.CLASSIFY_STR)) {
791 command_element = parseClassify (command_str);
792 }
793 if(command_element == null && command_type.equals (StaticStrings.FORMAT_STR)) {
794 command_element = parseFormat (tokenizer); // Revised to handle multiple lines
795 }
796 if(command_element == null && (command_type.equals (StaticStrings.INDEX_STR) || command_type.equals (StaticStrings.COMMENTED_INDEXES_STR))) {
797 command_element = parseIndex (command_str);
798 }
799 if(command_element == null && (command_type.equals (StaticStrings.INDEX_DEFAULT_STR) || command_type.equals (StaticStrings.COMMENTED_INDEX_DEFAULT_STR))) {
800
801 command_element = parseIndexDefault (command_str);
802 }
803 if(command_element == null && command_type.equals (StaticStrings.LANGUAGES_STR)) {
804 command_element = parseLanguage (command_str);
805 }
806 if(command_element == null && command_type.equals (StaticStrings.LANGUAGE_DEFAULT_STR)) {
807 command_element = parseLanguageDefault (command_str);
808 }
809 if (command_element == null && command_type.equals (StaticStrings.LANGUAGE_METADATA_STR)) {
810 command_element = parseLanguageMetadata (command_str);
811 }
812 if(command_element == null && command_type.equals (StaticStrings.LEVELS_STR)) {
813 command_element = parseIndexOptions (command_str, StaticStrings.LEVELS_STR, true);
814 }
815 if (command_element == null && command_type.equals (StaticStrings.COMMENTED_LEVELS_STR)) {
816 command_element = parseIndexOptions (command_str, StaticStrings.LEVELS_STR, false);
817 }
818 if(command_element == null && command_type.equals (StaticStrings.LEVEL_DEFAULT_STR)) {
819 command_element = parseIndexOptionDefault (command_str, StaticStrings.LEVEL_DEFAULT_STR, true);
820 }
821 if(command_element == null && command_type.equals (StaticStrings.COMMENTED_LEVEL_DEFAULT_STR)) {
822 command_element = parseIndexOptionDefault (command_str, StaticStrings.LEVEL_DEFAULT_STR, false);
823 }
824 if (command_element == null && command_type.equals (StaticStrings.INDEXOPTIONS_STR)) {
825 command_element = parseIndexOptions (command_str, StaticStrings.INDEXOPTIONS_STR, true);
826 }
827 if (command_element == null && command_type.equals (StaticStrings.COMMENTED_INDEXOPTIONS_STR)) {
828 command_element = parseIndexOptions (command_str, StaticStrings.INDEXOPTIONS_STR, false);
829 }
830 if(command_element == null && command_type.equals (StaticStrings.COLLECTIONMETADATA_STR)) {
831 command_element = parseMetadata (tokenizer); // Revised to handle multiple lines
832 }
833 if(command_element == null && (command_type.equals (StaticStrings.COLLECTIONMETADATA_PUBLIC_STR) || command_type.equals (StaticStrings.COLLECTIONMETADATA_CREATOR_STR) || command_type.equals (StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR) || command_type.equals (StaticStrings.BUILDTYPE_STR))) {
834 command_element = parseMetadataSpecial (command_str);
835 // pre 2.71 hack
836 if (command_type.equals (StaticStrings.BUILDTYPE_STR)) {
837 build_types_parsed = true;
838 }
839 }
840 if(command_element == null && command_type.equals (StaticStrings.PLUGIN_STR)) {
841 command_element = parsePlugin (command_str);
842 }
843 // leave here for backwards compatibility
844 if(command_element == null && command_type.equals (StaticStrings.SEARCHTYPE_STR)) {
845 command_element = parseSearchType (command_str);
846 // pre 2.71 hack
847 search_types_parsed = true;
848
849 }
850 if(command_element == null && command_type.equals (StaticStrings.SUBCOLLECTION_STR)) {
851 command_element = parseSubCollection (command_str);
852 }
853 if(command_element == null && command_type.equals (StaticStrings.SUBCOLLECTION_DEFAULT_INDEX_STR)) {
854 command_element = parseSubCollectionDefaultIndex (command_str);
855 }
856 if(command_element == null && command_type.equals (StaticStrings.SUBCOLLECTION_INDEX_STR)) {
857 command_element = parseSubCollectionIndex (command_str);
858 }
859 if(command_element == null && (command_type.equals (StaticStrings.SUPERCOLLECTION_STR) || command_type.equals (StaticStrings.CCS_STR))) {
860 command_element = parseSuperCollection (command_str);
861 }
862 // Doesn't match any known type
863 command_type = null;
864 if(command_element == null) {
865 // No-one knows what to do with this command, so we create an Unknown command element
866 command_element = document.createElement (StaticStrings.UNKNOWN_ELEMENT);
867 XMLTools.setValue (command_element, command_str);
868 }
869 }
870 // Or an empty line to remember for later
871 else {
872 command_element = document.createElement (NEWLINE_ELEMENT);
873 }
874 // Now command element shouldn't be null so we append it to the collection config DOM, but only if we haven't been told not to add it
875 //if(append_element) {
876 collect_cfg_element.appendChild (command_element);
877 //}
878 }
879 if (!build_types_parsed) {
880 String buildtype_type = BuildTypeManager.BUILD_TYPE_MG;
881 if (search_types_parsed) {
882 buildtype_type = BuildTypeManager.BUILD_TYPE_MGPP;
883 }
884 Element command_element = parseMetadataSpecial (StaticStrings.BUILDTYPE_STR+" "+buildtype_type);
885 Node target_node = findInsertionPoint (command_element);
886 if(target_node != null) {
887 collect_cfg_element.insertBefore (command_element, target_node);
888 }
889 else {
890 collect_cfg_element.appendChild (command_element);
891 }
892
893 }
894 }
895 catch(Exception exception) {
896 DebugStream.println ("Error in CollectionConfiguration.parse(java.io.File): " + exception);
897 DebugStream.printStackTrace (exception);
898 }
899 }
900
901
902 private Element parseClassify (String command_str) {
903 Element command_element = null;
904 try {
905 CommandTokenizer tokenizer = new CommandTokenizer (command_str);
906 // Check the token count. The token count from a command tokenizer isn't guarenteed to be correct, but it does give the maximum number of available tokens according to the underlying StringTokenizer (some of which may actually be append together by the CommandTokenizer as being a single argument).
907 if(tokenizer.countTokens () >= 2) { // Must support "classify Phind" (no args)
908 command_element = document.createElement (StaticStrings.CLASSIFY_ELEMENT);
909 // First token is classify
910 tokenizer.nextToken ();
911 // The next token is the classifier type
912 command_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, tokenizer.nextToken ());
913 // Now we parse out the remaining arguments into a hashmapping from name to value
914 HashMap arguments = parseArguments (tokenizer);
915 // Assign the arguments as Option elements, but watch out for the metadata argument as we treat that differently
916 Iterator names = arguments.keySet ().iterator ();
917 while(names.hasNext ()) {
918 String name = (String) names.next ();
919 String value = (String) arguments.get (name); // Can be null
920 // The metadata argument gets added as the content attribute
921 /* if (name.equals (StaticStrings.METADATA_ARGUMENT) && value != null) {
922 // Add the extracted namespace onto un-namespaced metadata names
923 StringTokenizer string_tokenizer = new StringTokenizer (value, ",");
924 value = "";
925 while (string_tokenizer.hasMoreElements ()) {
926 String token = (String) string_tokenizer.nextElement ();
927
928 if (token.indexOf (StaticStrings.NS_SEP) == -1) {
929 token = StaticStrings.EXTRACTED_NAMESPACE + token;
930 }
931 else {
932 MetadataElement metadata_element = MetadataTools.getMetadataElementWithName (token);
933 if (metadata_element != null) {
934 token = metadata_element.getDisplayName ();
935 }
936 }
937
938 if (!value.equals ("")) {
939 value = value + ",";
940 }
941 value = value + token;
942 }
943 }*/
944 // Everything else is an Option Element
945 Element option_element = document.createElement (StaticStrings.OPTION_ELEMENT);
946 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name.substring (1));
947 if(value != null) {
948 // Remove any speech marks appended in strings containing whitespace
949 if(value.startsWith (StaticStrings.SPEECH_CHARACTER) && value.endsWith (StaticStrings.SPEECH_CHARACTER)) {
950 value = value.substring (1, value.length () - 1);
951 }
952 XMLTools.setValue (option_element, value);
953 }
954 option_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
955 command_element.appendChild (option_element);
956 option_element = null;
957 name = null;
958 value = null;
959 }
960 names = null;
961 arguments = null;
962 }
963 tokenizer = null;
964 }
965 catch(Exception error) {
966 }
967 return command_element;
968 }
969
970 private Element parseFormat (CommandTokenizer tokenizer) {
971 Element command_element = null;
972 try {
973 command_element = document.createElement (StaticStrings.FORMAT_ELEMENT);
974 String name_str = tokenizer.nextToken ();
975 String value_str = tokenizer.nextToken ();
976 if(name_str != null && value_str != null) {
977 command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
978 // If the value is true or false we add it as an attribute
979 if(value_str.equalsIgnoreCase (StaticStrings.TRUE_STR) || value_str.equalsIgnoreCase (StaticStrings.FALSE_STR)) {
980 command_element.setAttribute (StaticStrings.VALUE_ATTRIBUTE, value_str.toLowerCase ());
981 }
982 // Otherwise it gets added as a text node
983 else {
984 // Ready the value str (which can contain all sorts of funky characters) for writing as a DOM value
985 value_str = Codec.transform (value_str, Codec.GREENSTONE_TO_DOM);
986 XMLTools.setValue (command_element, value_str);
987 }
988 }
989 else {
990 command_element = null;
991 }
992 name_str = null;
993 value_str = null;
994 }
995 catch (Exception exception) {
996 DebugStream.printStackTrace (exception);
997 command_element = null;
998 }
999 return command_element;
1000 }
1001
1002 private Element parseIndex (String command_str) {
1003 Element command_element = null;
1004 try {
1005 StringTokenizer tokenizer = new StringTokenizer (command_str);
1006 String command = tokenizer.nextToken ();
1007 command_element = document.createElement (StaticStrings.INDEXES_ELEMENT);
1008 command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, (command.equals (StaticStrings.INDEX_STR) ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
1009 command = null;
1010 if(!tokenizer.hasMoreTokens ()) {
1011
1012 // there are no indexes
1013 command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
1014 command_element.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.FALSE_STR); // for now
1015 tokenizer = null;
1016 return command_element;
1017 }
1018
1019 while(tokenizer.hasMoreTokens ()) {
1020 Element index_element = document.createElement (StaticStrings.INDEX_ELEMENT);
1021 String index_str = tokenizer.nextToken ();
1022 // There are two types of index we have to consider. Old G2.38 and earlier use level:source tuplets while G2.39+ have just a single, non-comma separated list where order is important.
1023 boolean old_index;
1024 if(index_str.indexOf (StaticStrings.COLON_CHARACTER) != -1) {
1025 old_index = true;
1026 index_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE, index_str.substring (0, index_str.indexOf (StaticStrings.COLON_CHARACTER)));
1027 index_str = index_str.substring (index_str.indexOf (StaticStrings.COLON_CHARACTER) + 1);
1028 command_element.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.FALSE_STR);
1029 }
1030 else {
1031 command_element.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
1032 old_index = false;
1033 }
1034 StringTokenizer content_tokenizer = new StringTokenizer (index_str, StaticStrings.COMMA_CHARACTER);
1035 while(content_tokenizer.hasMoreTokens ()) {
1036 Element content_element = document.createElement (StaticStrings.CONTENT_ELEMENT);
1037 String content_str = content_tokenizer.nextToken ();
1038 // Since the contents of indexes have to be certain keywords, or metadata elements, if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
1039 if(content_str.indexOf (StaticStrings.NS_SEP) == -1) {
1040 if(content_str.equals (StaticStrings.TEXT_STR) || (!old_index && content_str.equals (StaticStrings.ALLFIELDS_STR))) {
1041 // Our special strings are OK.
1042 }
1043 else {
1044 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
1045 }
1046 }
1047 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_str);
1048 index_element.appendChild (content_element);
1049 content_element = null;
1050 }
1051 content_tokenizer = null;
1052 index_str = null;
1053 command_element.appendChild (index_element);
1054 index_element = null;
1055 }
1056 tokenizer = null;
1057 }
1058 catch (Exception exception) {
1059 exception.printStackTrace ();
1060 }
1061 return command_element;
1062 }
1063
1064 private Element parseIndexDefault (String command_str) {
1065 Element command_element = null;
1066 try {
1067 StringTokenizer tokenizer = new StringTokenizer (command_str);
1068 if(tokenizer.countTokens () >= 2) {
1069 command_element = document.createElement (StaticStrings.INDEX_DEFAULT_ELEMENT);
1070 command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, ((tokenizer.nextToken ()).equals (StaticStrings.INDEX_DEFAULT_STR) ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
1071 String index_str = tokenizer.nextToken ();
1072 String level="";
1073 if (index_str.indexOf (StaticStrings.COLON_CHARACTER) !=-1) {
1074 level = index_str.substring (0, index_str.indexOf (StaticStrings.COLON_CHARACTER));
1075 }
1076
1077 command_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE,level);
1078
1079 String content_str = index_str;
1080
1081 if (index_str.indexOf (StaticStrings.COLON_CHARACTER) !=-1) {
1082 content_str = index_str.substring (index_str.indexOf (StaticStrings.COLON_CHARACTER) + 1);
1083 }
1084
1085 StringTokenizer content_tokenizer = new StringTokenizer (content_str, StaticStrings.COMMA_CHARACTER);
1086 while(content_tokenizer.hasMoreTokens ()) {
1087 Element content_element = document.createElement (StaticStrings.CONTENT_ELEMENT);
1088 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_tokenizer.nextToken ());
1089 command_element.appendChild (content_element);
1090 content_element = null;
1091 }
1092 content_tokenizer = null;
1093 content_str = null;
1094 content_str = null;
1095 index_str = null;
1096 }
1097 tokenizer = null;
1098 }
1099 catch (Exception exception) {
1100 }
1101 return command_element;
1102 }
1103
1104 private Element parseLanguage (String command_str) {
1105 Element command_element = null;
1106 try {
1107 StringTokenizer tokenizer = new StringTokenizer (command_str);
1108 tokenizer.nextToken ();
1109 if(tokenizer.hasMoreTokens ()) {
1110 command_element = document.createElement (StaticStrings.LANGUAGES_ELEMENT);
1111 while(tokenizer.hasMoreTokens ()) {
1112 Element language_element = document.createElement (StaticStrings.LANGUAGE_ELEMENT);
1113 language_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, tokenizer.nextToken ());
1114 command_element.appendChild (language_element);
1115 language_element = null;
1116 }
1117 }
1118 tokenizer = null;
1119 }
1120 catch (Exception exception) {
1121 }
1122 return command_element;
1123 }
1124
1125 private Element parseLanguageDefault (String command_str) {
1126 Element command_element = null;
1127 try {
1128 StringTokenizer tokenizer = new StringTokenizer (command_str);
1129 if(tokenizer.countTokens () >= 2) {
1130 command_element = document.createElement (StaticStrings.LANGUAGE_DEFAULT_ELEMENT);
1131 tokenizer.nextToken ();
1132 String default_language_str = tokenizer.nextToken ();
1133 command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, default_language_str);
1134 command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1135 default_language_str = null;
1136 }
1137 tokenizer = null;
1138 }
1139 catch (Exception exception) {
1140 }
1141 return command_element;
1142 }
1143
1144 private Element parseLanguageMetadata (String command_str) {
1145 Element command_element = null;
1146 try {
1147 StringTokenizer tokenizer = new StringTokenizer (command_str);
1148 if(tokenizer.countTokens () >= 2) {
1149 command_element = document.createElement (StaticStrings.LANGUAGE_METADATA_ELEMENT);
1150 tokenizer.nextToken ();
1151 String language_metadata_str = tokenizer.nextToken ();
1152 if (language_metadata_str.indexOf (StaticStrings.NS_SEP) == -1) {
1153 language_metadata_str = StaticStrings.EXTRACTED_NAMESPACE + language_metadata_str;
1154 }
1155 command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, language_metadata_str);
1156 command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1157 language_metadata_str = null;
1158 }
1159 tokenizer = null;
1160
1161 }
1162 catch (Exception exception) {
1163 }
1164 return command_element;
1165 }
1166
1167 private Element parseIndexOptions (String command_str, String type, boolean assigned) {
1168 Element command_element = null;
1169 try {
1170 StringTokenizer tokenizer = new StringTokenizer (command_str);
1171 // First token is command type
1172 String command = tokenizer.nextToken ();
1173 if(tokenizer.hasMoreTokens ()) {
1174 command_element = document.createElement (StaticStrings.INDEXOPTIONS_ELEMENT);
1175 command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE,type);
1176 command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
1177 while(tokenizer.hasMoreTokens ()) {
1178 Element option_element = document.createElement (StaticStrings.INDEXOPTION_ELEMENT);
1179 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, tokenizer.nextToken ());
1180 command_element.appendChild (option_element);
1181 option_element = null;
1182 }
1183 }
1184 command = null;
1185 }
1186 catch(Exception exception) {
1187 }
1188 return command_element;
1189 }
1190
1191 private Element parseIndexOptionDefault (String command_str, String type, boolean assigned) {
1192 Element command_element = null;
1193 try {
1194 StringTokenizer tokenizer = new StringTokenizer (command_str);
1195 // First token is command type
1196 String command = tokenizer.nextToken ();
1197 if(tokenizer.hasMoreTokens ()) {
1198 command_element = document.createElement (StaticStrings.INDEXOPTION_DEFAULT_ELEMENT);
1199 command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, (assigned ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); // is it commented out or not?
1200 command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, type);
1201 command_element.setAttribute (StaticStrings.VALUE_ATTRIBUTE, tokenizer.nextToken ());
1202 }
1203
1204 tokenizer = null;
1205 }
1206 catch (Exception exception) {
1207 }
1208 return command_element;
1209 }
1210
1211 private Element parseMetadata (CommandTokenizer tokenizer) {
1212 Element command_element = null;
1213 try {
1214 command_element = document.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
1215 String name_str = tokenizer.nextToken ();
1216 String value_str = tokenizer.nextToken ();
1217 if(name_str != null && value_str != null) {
1218 String language_str = Configuration.getLanguage ();
1219 // Check if the value string is actually a language string
1220 if(value_str.startsWith (StaticStrings.LBRACKET_CHARACTER) && value_str.endsWith (StaticStrings.RBRACKET_CHARACTER)) {
1221 language_str = value_str.substring (value_str.indexOf (StaticStrings.LANGUAGE_ARGUMENT) + 2, value_str.length () - 1);
1222 value_str = tokenizer.nextToken ();
1223 }
1224 if(value_str != null) {
1225 // Ready the value str (which can contain all sorts of funky characters) for writing as a DOM value
1226 value_str = Codec.transform (value_str, Codec.GREENSTONE_TO_DOM);
1227 command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
1228 command_element.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, language_str);
1229 command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1230 XMLTools.setValue (command_element, value_str);
1231 }
1232 else {
1233 command_element = null;
1234 }
1235 language_str = null;
1236 }
1237 else {
1238 command_element = null;
1239 }
1240 name_str = null;
1241 value_str = null;
1242 }
1243 catch (Exception exception) {
1244 DebugStream.printStackTrace (exception);
1245 command_element = null;
1246 }
1247 return command_element;
1248 }
1249
1250 private Element parseMetadataSpecial (String command_str) {
1251 Element command_element = null;
1252 try {
1253 StringTokenizer tokenizer = new StringTokenizer (command_str);
1254 if(tokenizer.countTokens () >= 2) {
1255 String name_str = tokenizer.nextToken ();
1256 String value_str = tokenizer.nextToken ();
1257 if (name_str.equals (StaticStrings.COLLECTIONMETADATA_CREATOR_STR)) {
1258 command_element = document.createElement (StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT);
1259 }
1260 else if(name_str.equals (StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR)) {
1261 command_element = document.createElement (StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT);
1262 }
1263 else if(name_str.equals (StaticStrings.COLLECTIONMETADATA_PUBLIC_STR)) {
1264 command_element = document.createElement (StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT);
1265 }
1266 else if (name_str.equals (StaticStrings.BUILDTYPE_STR)) {
1267 command_element = document.createElement (StaticStrings.BUILDTYPE_ELEMENT);
1268 }
1269 if(command_element != null) {
1270 command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
1271 command_element.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
1272 command_element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
1273 command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1274 if(value_str.startsWith (StaticStrings.SPEECH_CHARACTER) && value_str.endsWith (StaticStrings.SPEECH_CHARACTER)) {
1275 value_str = value_str.substring (1, value_str.length () - 1);
1276 }
1277 XMLTools.setValue (command_element, value_str);
1278 }
1279 value_str = null;
1280 name_str = null;
1281 }
1282 tokenizer = null;
1283 }
1284 catch (Exception exception) {
1285 }
1286 return command_element;
1287 }
1288
1289 private Element parsePlugin (String command_str) {
1290 Element command_element = null;
1291 try {
1292 CommandTokenizer tokenizer = new CommandTokenizer (command_str);
1293 // Check the token count. The token count from a command tokenizer isn't guarenteed to be correct, but it does give the maximum number of available tokens according to the underlying StringTokenizer (some of which may actually be append together by the CommandTokenizer as being a single argument).
1294 if(tokenizer.countTokens () >= 2) {
1295 command_element = document.createElement (StaticStrings.PLUGIN_ELEMENT);
1296 // First token is plugin
1297 tokenizer.nextToken ();
1298 // The next token is the type
1299 String type = tokenizer.nextToken ();
1300 type = ensureNewPluginName(type);
1301 command_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, type);
1302 // Now we parse out the remaining arguments into a hashmapping from name to value
1303 HashMap arguments = parseArguments (tokenizer);
1304 // Assign the arguments as Option elements, but watch out for the metadata argument as we treat that differently
1305 // also watch out for the deprecated -use_metadata_files option to RecPlug and remove it
1306 Iterator names = arguments.keySet ().iterator ();
1307 while(names.hasNext ()) {
1308 String name = (String) names.next ();
1309 String value = (String) arguments.get (name); // Can be null
1310
1311 if(type.equals (StaticStrings.RECPLUG_STR) && name.substring (1).equals (StaticStrings.USE_METADATA_FILES_ARGUMENT)) {
1312 continue; // ignore this option
1313 }
1314 Element option_element = document.createElement (StaticStrings.OPTION_ELEMENT);
1315 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name.substring (1));
1316 option_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1317 if(value != null) {
1318 // Remove any speech marks appended in strings containing whitespace
1319 if(value.startsWith (StaticStrings.SPEECH_CHARACTER) && value.endsWith (StaticStrings.SPEECH_CHARACTER)) {
1320 value = value.substring (1, value.length () - 1);
1321 }
1322 if(name.equals (StaticStrings.METADATA_ARGUMENT)) {
1323 // The metadata argument must be the fully qualified name of a metadata element, so if it doesn't yet have a namespace, append the extracted metadata namespace.
1324 if(value.indexOf (StaticStrings.NS_SEP) == -1) {
1325 value = StaticStrings.EXTRACTED_NAMESPACE + value;
1326 }
1327 }
1328 XMLTools.setValue (option_element, value);
1329 }
1330 command_element.appendChild (option_element);
1331 option_element = null;
1332 name = null;
1333 value = null;
1334 }
1335
1336 type = null;
1337 names = null;
1338 arguments = null;
1339 }
1340 tokenizer = null;
1341 }
1342 catch(Exception exception) {
1343 // This catch clause had been left empty. If this is deliberate then
1344 // we should have a comment here explaining why there is no need to
1345 // print anything out. Am assuming this is mistake for now, and
1346 // have added in a call to printStackTrace()
1347 System.err.println("Malformed plugin statement");
1348 exception.printStackTrace();
1349 }
1350 return command_element;
1351 }
1352
1353 /* search types are now handled as formats - leave this here to convert in case we have an old config file */
1354 private Element parseSearchType (String command_str) {
1355 Element command_element = null;
1356 try {
1357 StringTokenizer tokenizer = new StringTokenizer (command_str);
1358 // First token is command type (searchtype)
1359 tokenizer.nextToken ();
1360 if(tokenizer.hasMoreTokens ()) {
1361 command_element = document.createElement (StaticStrings.FORMAT_ELEMENT);
1362 command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, "SearchTypes");
1363 String value = tokenizer.nextToken ();
1364 while(tokenizer.hasMoreTokens ()) {
1365 value += ","+tokenizer.nextToken ();
1366 }
1367 value = Codec.transform (value, Codec.GREENSTONE_TO_DOM);
1368 XMLTools.setValue (command_element, value);
1369 }
1370 }
1371 catch(Exception exception) {
1372 }
1373 return command_element;
1374 }
1375
1376 private Element parseSubCollection (String command_str) {
1377 Element command_element = null;
1378 try {
1379 CommandTokenizer tokenizer = new CommandTokenizer (command_str);
1380 if(tokenizer.countTokens () >= 3) {
1381 command_element = document.createElement (StaticStrings.SUBCOLLECTION_ELEMENT);
1382 // First token is command type
1383 tokenizer.nextToken ();
1384 // Then subcollection identifier
1385 command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, tokenizer.nextToken ());
1386 // Then finally the pattern used to build the subcollection partition
1387 String full_pattern_str = tokenizer.nextToken ();
1388 // Set inclusion/exclusion flag and remove any exclamation mark
1389 boolean exclusion = full_pattern_str.startsWith (StaticStrings.EXCLAMATION_CHARACTER);
1390 if (exclusion) {
1391 full_pattern_str = full_pattern_str.substring (1, full_pattern_str.length ());
1392 command_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, StaticStrings.EXCLUDE_STR);
1393 }
1394 else {
1395 command_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, StaticStrings.INCLUDE_STR);
1396 }
1397
1398 // Let's make sure it is a valid Greenstone configuration line
1399 String[] results = full_pattern_str.split("\\" + StaticStrings.SEPARATOR_CHARACTER, 3);
1400
1401 if (results.length >= 2) {
1402 String content_str = results[0];
1403 // Since the contents of indexes have to be certain keywords, or metadata elements, if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
1404 if (!content_str.equals (StaticStrings.FILENAME_STR) && content_str.indexOf (StaticStrings.NS_SEP) == -1) {
1405 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
1406 }
1407 command_element.setAttribute (StaticStrings.CONTENT_ATTRIBUTE, content_str);
1408 XMLTools.setValue (command_element, results[1]);
1409 if (results.length >= 3) {
1410 command_element.setAttribute (StaticStrings.OPTIONS_ATTRIBUTE, results[2]);
1411 }
1412 }
1413 }
1414 }
1415 catch(Exception exception) {
1416 exception.printStackTrace ();
1417 }
1418 return command_element;
1419 }
1420
1421 private Element parseSubCollectionDefaultIndex (String command_str) {
1422 Element command_element = null;
1423 try {
1424 StringTokenizer tokenizer = new StringTokenizer (command_str);
1425 if(tokenizer.countTokens () == 2) {
1426 command_element = document.createElement (StaticStrings.SUBCOLLECTION_DEFAULT_INDEX_ELEMENT);
1427 tokenizer.nextToken ();
1428 //command_element.setAttribute(CONTENT_ATTRIBUTE, tokenizer.nextToken());
1429 String content_str = tokenizer.nextToken ();
1430 StringTokenizer content_tokenizer = new StringTokenizer (content_str, StaticStrings.COMMA_CHARACTER);
1431 while(content_tokenizer.hasMoreTokens ()) {
1432 Element content_element = document.createElement (StaticStrings.CONTENT_ELEMENT);
1433 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_tokenizer.nextToken ());
1434 command_element.appendChild (content_element);
1435 content_element = null;
1436 }
1437 content_tokenizer = null;
1438 content_str = null;
1439 }
1440 tokenizer = null;
1441 }
1442 catch(Exception exception) {
1443 }
1444 return command_element;
1445 }
1446
1447 private Element parseSubCollectionIndex (String command_str) {
1448 Element command_element = null;
1449 try {
1450 StringTokenizer tokenizer = new StringTokenizer (command_str);
1451 tokenizer.nextToken ();
1452 if(tokenizer.hasMoreTokens ()) {
1453 command_element = document.createElement (StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT);
1454 }
1455 while(tokenizer.hasMoreTokens ()) {
1456 Element subcollectionindex_element = document.createElement (StaticStrings.INDEX_ELEMENT);
1457 //command_element.setAttribute(CONTENT_ATTRIBUTE, tokenizer.nextToken());
1458 String content_str = tokenizer.nextToken ();
1459 StringTokenizer content_tokenizer = new StringTokenizer (content_str, StaticStrings.COMMA_CHARACTER);
1460 while(content_tokenizer.hasMoreTokens ()) {
1461 Element content_element = document.createElement (StaticStrings.CONTENT_ELEMENT);
1462 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_tokenizer.nextToken ());
1463 subcollectionindex_element.appendChild (content_element);
1464 content_element = null;
1465 }
1466 content_tokenizer = null;
1467 content_str = null;
1468 command_element.appendChild (subcollectionindex_element);
1469 subcollectionindex_element = null;
1470 }
1471 tokenizer = null;
1472 }
1473 catch (Exception exception) {
1474 }
1475 return command_element;
1476 }
1477
1478 private Element parseSuperCollection (String command_str) {
1479 Element command_element = null;
1480 try {
1481 StringTokenizer tokenizer = new StringTokenizer (command_str);
1482 if(tokenizer.countTokens () >= 3) {
1483 command_element = document.createElement (StaticStrings.SUPERCOLLECTION_ELEMENT);
1484 tokenizer.nextToken ();
1485 while(tokenizer.hasMoreTokens ()) {
1486 Element collection_element = document.createElement (StaticStrings.COLLECTION_ELEMENT);
1487 collection_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, tokenizer.nextToken ());
1488 command_element.appendChild (collection_element);
1489 collection_element = null;
1490 }
1491 }
1492 tokenizer = null;
1493 }
1494 catch(Exception exception) {
1495 }
1496 return command_element;
1497 }
1498
1499 static private String pluginToString (Element command_element) {
1500 if(command_element.getAttribute (StaticStrings.SEPARATOR_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
1501 return "";
1502 }
1503 StringBuffer text = new StringBuffer (StaticStrings.PLUGIN_STR);
1504 text.append (StaticStrings.TAB_CHARACTER);
1505 text.append (command_element.getAttribute (StaticStrings.TYPE_ATTRIBUTE));
1506 // Retrieve, and output, the arguments
1507 NodeList option_elements = command_element.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
1508 int option_elements_length = option_elements.getLength ();
1509 if(option_elements_length > 0) {
1510 for(int j = 0; j < option_elements_length; j++) {
1511 Element option_element = (Element) option_elements.item (j);
1512 if(option_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
1513 text.append (StaticStrings.SPACE_CHARACTER);
1514 text.append (StaticStrings.MINUS_CHARACTER);
1515 text.append (option_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
1516 String value_str = XMLTools.getValue (option_element);
1517 // turn display name into proper name
1518 //value_str = modifyMetadataArgumentValue(value_str);
1519 if (value_str.length () > 0) {
1520 text.append (StaticStrings.SPACE_CHARACTER);
1521 if(value_str.indexOf (StaticStrings.SPACE_CHARACTER) != -1) {
1522 // enclose in quotes
1523 text.append(StaticStrings.SPEECH_CHARACTER);
1524 text.append(value_str);
1525 text.append(StaticStrings.SPEECH_CHARACTER);
1526 } else {
1527
1528 text.append(value_str);
1529 }
1530 }
1531
1532 value_str = null;
1533 }
1534 option_element = null;
1535 }
1536 }
1537 option_elements = null;
1538
1539 return text.toString ();
1540 }
1541
1542 // Convert metadata element names to internal names, and remove extracted metadata namespaces
1543 /* static private String modifyMetadataArgumentValue(String value_str) {
1544 if (value_str.length () == 0) {
1545 return value_str;
1546 }
1547
1548 // final true arg to return delims as tokens
1549 StringTokenizer string_tokenizer = new StringTokenizer (value_str, ",;", true);
1550 StringBuffer value_buffer = new StringBuffer ();
1551 while (string_tokenizer.hasMoreElements ()) {
1552 String raw_token = (String) string_tokenizer.nextElement ();
1553 String token = raw_token.trim ();
1554 boolean modified_token = false;
1555 // not a delimiter token
1556 if (!raw_token.equals(",") && !raw_token.equals(";")) {
1557 //MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName (token);
1558 //if (metadata_element != null) {
1559 //token = metadata_element.getFullName ();
1560 //modified_token = true;
1561 //}
1562
1563 // if (token.startsWith (StaticStrings.EXTRACTED_NAMESPACE)) {
1564 // token = token.substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
1565 // modified_token = true;
1566 //}
1567 }
1568 if (modified_token) {
1569 value_buffer.append (token);
1570 } else {
1571 // we may have had whitespace etc that was part of the string
1572 value_buffer.append (raw_token);
1573 }
1574 }
1575
1576
1577 if(value_buffer.length () == 0) {
1578 return "";
1579 }
1580 if(value_str.indexOf (StaticStrings.SPACE_CHARACTER) == -1) {
1581 return value_buffer.toString();
1582 }
1583
1584 // it must contain a space, so we surround the value with quotes
1585 value_buffer.insert(0, StaticStrings.SPEECH_CHARACTER);
1586 value_buffer.append(StaticStrings.SPEECH_CHARACTER);
1587 return value_buffer.toString();
1588
1589 }*/
1590
1591 static private String searchtypeToString (Element command_element) {
1592 if(command_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
1593 StringBuffer text = new StringBuffer (StaticStrings.SEARCHTYPE_STR);
1594 text.append (StaticStrings.TAB_CHARACTER);
1595 NodeList search_elements = command_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
1596 int search_elements_length = search_elements.getLength ();
1597 for(int i = 0; i < search_elements_length; i++) {
1598 Element search_element = (Element) search_elements.item (i);
1599 text.append (search_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
1600 text.append (StaticStrings.SPACE_CHARACTER);
1601 }
1602 return text.substring (0, text.length () - 1);
1603 }
1604 else {
1605 return null;
1606 }
1607 }
1608
1609 static private String subcollectionToString (Element command_element) {
1610 StringBuffer text = new StringBuffer (StaticStrings.SUBCOLLECTION_STR);
1611 text.append (StaticStrings.SPACE_CHARACTER);
1612 text.append (command_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
1613 text.append (StaticStrings.SPACE_CHARACTER);
1614 text.append (StaticStrings.TAB_CHARACTER);
1615 text.append (StaticStrings.SPEECH_CHARACTER);
1616 if(command_element.getAttribute (StaticStrings.TYPE_ATTRIBUTE).equals (StaticStrings.EXCLUDE_STR)) {
1617 text.append (StaticStrings.EXCLAMATION_CHARACTER);
1618 }
1619 text.append (command_element.getAttribute (StaticStrings.CONTENT_ATTRIBUTE));
1620 text.append (StaticStrings.SEPARATOR_CHARACTER);
1621 text.append (XMLTools.getValue (command_element));
1622 text.append (StaticStrings.SEPARATOR_CHARACTER);
1623 String options_str = command_element.getAttribute (StaticStrings.OPTIONS_ATTRIBUTE);
1624 if(options_str.length () > 0) {
1625 text.append (options_str);
1626 }
1627 options_str = null;
1628 text.append (StaticStrings.SPEECH_CHARACTER);
1629 return text.toString ();
1630 }
1631
1632 static private String subcollectionDefaultIndexToString (Element command_element) {
1633 StringBuffer text = new StringBuffer (StaticStrings.SUBCOLLECTION_DEFAULT_INDEX_STR);
1634 text.append (StaticStrings.TAB_CHARACTER);
1635 NodeList content_elements = command_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
1636 int content_elements_length = content_elements.getLength ();
1637 for(int j = 0; j < content_elements_length; j++) {
1638 Element content_element = (Element) content_elements.item (j);
1639 text.append (content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
1640 if(j < content_elements_length - 1) {
1641 text.append (StaticStrings.COMMA_CHARACTER);
1642 }
1643 }
1644 return text.toString ();
1645 }
1646
1647 static private String subcollectionIndexesToString (Element command_element) {
1648 StringBuffer text = new StringBuffer (StaticStrings.SUBCOLLECTION_INDEX_STR);
1649 text.append (StaticStrings.TAB_CHARACTER);
1650 // Retrieve all of the subcollection index partitions
1651 NodeList subcollectionindex_elements = command_element.getElementsByTagName (StaticStrings.INDEX_ELEMENT);
1652 int subcollectionindex_elements_length = subcollectionindex_elements.getLength ();
1653 if(subcollectionindex_elements_length == 0) {
1654 return null;
1655 }
1656 for(int j = 0; j < subcollectionindex_elements_length; j++) {
1657 Element subcollectionindex_element = (Element) subcollectionindex_elements.item (j);
1658 NodeList content_elements = subcollectionindex_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
1659 int content_elements_length = content_elements.getLength ();
1660 for(int k = 0; k < content_elements_length; k++) {
1661 Element content_element = (Element) content_elements.item (k);
1662 text.append (content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
1663 if(k < content_elements_length - 1) {
1664 text.append (StaticStrings.COMMA_CHARACTER);
1665 }
1666 }
1667 if(j < subcollectionindex_elements_length - 1) {
1668 text.append (StaticStrings.SPACE_CHARACTER);
1669 }
1670 }
1671 return text.toString ();
1672 }
1673
1674 static private String supercollectionToString (Element command_element) {
1675 NodeList content_elements = command_element.getElementsByTagName (StaticStrings.COLLECTION_ELEMENT);
1676 int content_elements_length = content_elements.getLength ();
1677 if(content_elements_length > 1) {
1678 StringBuffer text = new StringBuffer (StaticStrings.SUPERCOLLECTION_STR);
1679 text.append (StaticStrings.TAB_CHARACTER);
1680 for(int j = 0; j < content_elements_length; j++) {
1681 Element content_element = (Element) content_elements.item (j);
1682 text.append (content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE));
1683 if(j < content_elements_length - 1) {
1684 text.append (StaticStrings.SPACE_CHARACTER);
1685 }
1686 }
1687 return text.toString ();
1688 }
1689 return null;
1690 }
1691
1692 static private String unknownToString (Element command_element) {
1693 return XMLTools.getValue (command_element);
1694 }
1695
1696 /** Write the text to the buffer. This is used so we don't have to worry about storing intermediate String values just so we can calaulate length and offset.
1697 * @param writer the BufferedWriter to which the str will be written
1698 * @param str the String to be written
1699 */
1700 private void write (BufferedWriter writer, String str)
1701 throws IOException {
1702 writer.write (str, 0, str.length ());
1703 }
1704
1705 /** ********************************************************************************************************
1706 The code from this point below are used for greenstone 3 collection configuration, i.e., read ColletionConfig.xml
1707 * into the internal DOM tree, and convert the internal DOM tree back to CollectionConfig.xml.
1708 *
1709 Methods named 'doXXXX' are for convert collectionConfig.xml into the internal configuration xml structure;
1710 Methods named 'convertXXXX' are for convert the internal configuration xml structure back to collectionConfig.xml.
1711 ************************************************************************************************************ */
1712
1713 /**Arguments: metadataListNode->the 'displayItemList' element in collectionConfig.xml
1714 name_value->the value of the 'name' attribute of 'index' element;
1715 att_value->the value of the 'name' attribute of 'displayItem' element
1716 return: an ArrayList of the contructed 'CollectionMetadata' elements
1717 */
1718 private ArrayList doDisplayItemList (Document to, Node displayListNode, String att_value, String name_value) {
1719 Element toElement = to.getDocumentElement ();
1720 ArrayList display_item_list = new ArrayList ();
1721 ArrayList item_list = XMLTools.getNamedElementList ((Element)displayListNode,
1722 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, att_value);
1723 if (item_list == null) {
1724 return null;
1725 }
1726
1727 for (int i=0; i<item_list.size (); i++) {
1728 Element item = (Element)item_list.get (i);
1729 String text = XMLTools.getNodeText (item);
1730
1731 //If there is nothing to display, don't bother creating the element
1732 if (text == "") {
1733 continue;
1734 }
1735 //get the value in 'lang=value'
1736 String lang = item.getAttribute (StaticStrings.LANG_STR);
1737
1738 Element e = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
1739 e.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1740 e.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_value);
1741 e.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
1742 XMLTools.setNodeText (e, text);
1743 display_item_list.add (e);
1744 }
1745 return display_item_list;
1746 }
1747
1748 private ArrayList doMetadataList (Document to, Node metadataListNode, String ele_name, String att_value) {
1749 Element toElement = to.getDocumentElement ();
1750 ArrayList metadata_list = new ArrayList ();
1751
1752 ArrayList item_list = XMLTools.getNamedElementList ((Element)metadataListNode,
1753 StaticStrings.METADATA_STR, StaticStrings.NAME_ATTRIBUTE, att_value);
1754 if (item_list == null) {
1755 return null;
1756 }
1757
1758 for (int i=0; i<item_list.size (); i++) {
1759 Element item = (Element)item_list.get (i);
1760 String text = XMLTools.getNodeText (item);
1761
1762 //If there is nothing to display, don't bother creating the element
1763 if (text == "") {
1764 continue;
1765 }
1766 //get the value in 'lang=value'
1767 String lang = item.getAttribute (StaticStrings.LANG_STR);
1768
1769 Element element = to.createElement (ele_name);
1770 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, att_value);
1771 element.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
1772 element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1773 element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
1774 XMLTools.setNodeText (element, text);
1775
1776 metadata_list.add (element);
1777 }
1778 return metadata_list;
1779 }
1780 // 'to' is the internal structure
1781 private void doMGIndexes (Document to, Node searchNode) {
1782 Element toElement = to.getDocumentElement ();
1783 Element indexes_element = to.createElement (StaticStrings.INDEXES_ELEMENT);//<Indexes>
1784 indexes_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1785 indexes_element.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.FALSE_STR);
1786
1787 NodeList index_children = ((Element)searchNode).getElementsByTagName (StaticStrings.INDEX_LOW_STR);//index
1788 int num_nodes = index_children.getLength ();
1789
1790 for (int i=0; i<num_nodes; i++) {
1791 Element e = (Element)index_children.item (i);
1792 String index_str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1793 String index_str_display = index_str;//used for creating collectionmetadata for this index
1794 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);//<Index>
1795
1796 // For mg, it's the 'Old G2.38 and earlier' that use level:source tuplets, but we double check it anyway
1797 boolean old_index = true;
1798 if(index_str.indexOf (StaticStrings.COLON_CHARACTER) == -1) {
1799 // It doesn't contain ':' character
1800 System.err.println ("Something is wrong! the index should be level:source tuplets.");
1801 old_index = false;
1802 }
1803 else {
1804 // Handling 'index' element
1805 index_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE,
1806 index_str.substring (0, index_str.indexOf (StaticStrings.COLON_CHARACTER)));
1807 index_str = index_str.substring (index_str.indexOf (StaticStrings.COLON_CHARACTER) + 1);
1808
1809 //Each index may have a list of comma-separated strings.
1810 //split them into 'content' elements in the internal structure
1811 StringTokenizer content_tokenizer = new StringTokenizer (index_str, StaticStrings.COMMA_CHARACTER);
1812 //index_str = "";
1813 while(content_tokenizer.hasMoreTokens ()) {
1814 // Replace index_str to be qualified name, eg. dc.Subject and keywords insread of dc.Subject.
1815
1816
1817 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
1818 String content_str = content_tokenizer.nextToken ();
1819 // Since the contents of indexes have to be certain keywords, or metadata elements,
1820 //if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
1821 if(content_str.indexOf (StaticStrings.NS_SEP) == -1) {
1822 if(content_str.equals (StaticStrings.TEXT_STR) ||
1823 (!old_index && content_str.equals (StaticStrings.ALLFIELDS_STR))) {
1824 // in this case, do nothing
1825 }
1826 else {
1827 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
1828 }
1829 }
1830
1831 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_str);
1832 index_element.appendChild (content_element);
1833 content_element = null;
1834 } // while ends
1835
1836 indexes_element.appendChild (index_element);
1837
1838 // Handling 'displayItem' elements and Constructing 'collectionmetadata' elements
1839 // Use the fully qualified index names
1840 ArrayList collectionmetadata_list = doDisplayItemList (to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
1841 appendArrayList (toElement, collectionmetadata_list);
1842 } //else ends
1843 } //for loop ends
1844 appendProperly (toElement, indexes_element);
1845
1846 //***//
1847 // create another set of <indexes> which will be used when user switches to MGPP/LUCENE
1848 // i.e. we build a default index set for a start
1849
1850 String []index_strs =
1851 {StaticStrings.TEXT_STR,
1852 StaticStrings.EXTRACTED_NAMESPACE + StaticStrings.TITLE_ELEMENT,
1853 StaticStrings.EXTRACTED_NAMESPACE + StaticStrings.SOURCE_ELEMENT};
1854
1855 Element mgpp_indexes = to.createElement (StaticStrings.INDEXES_ELEMENT);//<Indexes>
1856 mgpp_indexes.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
1857 mgpp_indexes.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
1858 for (int i=0; i<index_strs.length; i++) {
1859 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);//<Index>
1860 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
1861 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_strs[i]);
1862 index_element.appendChild (content_element);
1863 mgpp_indexes.appendChild (index_element);
1864
1865 // Contructing 'collectionmetadata' elements for 'mgpp' indexes
1866 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
1867 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1868 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_strs[i]);
1869 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
1870 if (index_strs[i].indexOf (StaticStrings.NS_SEP) != -1) {
1871 index_strs[i] = index_strs[i].substring (index_strs[i].indexOf (StaticStrings.NS_SEP) + 1);
1872 }
1873 XMLTools.setNodeText (collectionmetadata, index_strs[i]);
1874
1875 appendProperly (toElement, collectionmetadata);
1876
1877 }
1878 appendProperly (toElement, mgpp_indexes);
1879 }
1880
1881 //This is actually doing indexes for both mgpp and lucene
1882 private void doMGPPIndexes (Document to, Node searchNode) {
1883 Element toElement = to.getDocumentElement ();
1884 Element indexes_element = to.createElement (StaticStrings.INDEXES_ELEMENT);//<Indexes>
1885 indexes_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1886 indexes_element.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
1887
1888 NodeList index_children = ((Element)searchNode).getElementsByTagName (StaticStrings.INDEX_LOW_STR);//index
1889 int num_nodes = index_children.getLength ();
1890
1891 for (int i=0; i<num_nodes; i++) {
1892
1893 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);//<Index>
1894 Element e = (Element)index_children.item (i);
1895 String index_str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1896 String index_str_display = index_str;//for creating collectionmetadata for this index
1897
1898 // Handling 'index' element
1899 // Double check to make sure it's not colon separated style index.
1900 boolean old_index = false;
1901 if(index_str.indexOf (StaticStrings.COLON_CHARACTER) != -1) {
1902 System.err.println ("Something is wrong! the index should NOT be level:source tuplets style.");
1903 old_index = true;
1904 }
1905 //Each index may have a list of comma-separated strings.
1906 //split them into 'content' elements in the internal structure
1907 StringTokenizer content_tokenizer = new StringTokenizer (index_str, StaticStrings.COMMA_CHARACTER);
1908 //index_str = "";
1909 while(content_tokenizer.hasMoreTokens ()) {
1910 // Replace index_str to be qualified name, eg. dc.Subject and keywords insread of dc.Subject.
1911
1912 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
1913 String content_str = content_tokenizer.nextToken ();
1914 // Since the contents of indexes have to be certain keywords, or metadata elements, if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
1915 if(content_str.indexOf (StaticStrings.NS_SEP) == -1) {
1916 if(content_str.equals (StaticStrings.TEXT_STR)) {
1917 // in this case, do nothing
1918 }
1919 else {
1920 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
1921 }
1922 }
1923 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_str);
1924 index_element.appendChild (content_element);
1925 content_element = null;
1926 } //while ends
1927
1928 indexes_element.appendChild (index_element);
1929
1930
1931 index_element = null;
1932
1933 // Handling 'displayItem' element of this 'index' element
1934 // 'e' is the parent element 'index' of 'displayItem' element
1935 ArrayList collectionmetadata_list = doDisplayItemList (to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
1936 appendArrayList (toElement, collectionmetadata_list);
1937
1938
1939 } // for loop ends
1940 toElement.appendChild (indexes_element);
1941
1942 // create another set of <indexes> which will be used when user switches to MG
1943 // i.e. we build a default index set for a start
1944 Element mg_indexes = to.createElement (StaticStrings.INDEXES_ELEMENT);//<Indexes>
1945 mg_indexes.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
1946 mg_indexes.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.FALSE_STR);
1947
1948 //put the namespace '.ex' as prefix to the indexes
1949 String []index_strs =
1950 {StaticStrings.TEXT_STR,
1951 StaticStrings.EXTRACTED_NAMESPACE + StaticStrings.TITLE_ELEMENT,
1952 StaticStrings.EXTRACTED_NAMESPACE + StaticStrings.SOURCE_ELEMENT};
1953 for (int i=0; i<index_strs.length; i++) {
1954 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);//<Index>
1955 index_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE, StaticStrings.DOCUMENT_STR);
1956 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
1957 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_strs[i]);
1958 index_element.appendChild (content_element);
1959
1960 mg_indexes.appendChild (index_element);
1961
1962 // Contructing 'collectionmetadata' elements for 'mg' indexes
1963 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
1964 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1965 String temp = StaticStrings.DOCUMENT_STR.concat (StaticStrings.COLON_CHARACTER).concat (index_strs[i]);
1966 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, temp);
1967 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
1968 if (index_strs[i].indexOf (StaticStrings.NS_SEP) != -1) {
1969 index_strs[i] = index_strs[i].substring (index_strs[i].indexOf (StaticStrings.NS_SEP) + 1);
1970 }
1971 XMLTools.setNodeText (collectionmetadata, index_strs[i]);
1972
1973 appendProperly (toElement, collectionmetadata);
1974
1975 }
1976 toElement.appendChild (mg_indexes);
1977
1978 }
1979
1980 private void doDisplayFormat (Document to, Element from) {
1981 //display element in the xml file
1982 Element de = (Element)XMLTools.getChildByTagName (from, StaticStrings.DISPLAY_STR);
1983 if (de == null) {
1984 return;
1985 }
1986 //format element in the display element
1987 Element fe = (Element)XMLTools.getChildByTagName (de, StaticStrings.FORMAT_STR);
1988
1989 to.getDocumentElement ().appendChild (doFormat(to, fe, StaticStrings.DISPLAY_STR));
1990 }
1991
1992 //construct 'DefaultIndex' element in the internal structure from collectionConfig.xml
1993 private void doDefaultIndex (Document to, Node searchNode) {
1994 Element toElement = to.getDocumentElement ();
1995 Element default_index_element = to.createElement (StaticStrings.INDEX_DEFAULT_ELEMENT);
1996 default_index_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1997
1998 Element e = (Element)XMLTools.getChildByTagName (searchNode, StaticStrings.INDEX_DEFAULT_ELEMENT_LOWERCASE);//defaultIndex
1999 if (e == null) {
2000 return;
2001 }
2002 String index_str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2003
2004 boolean old_index = false;
2005 if(index_str.indexOf (StaticStrings.COLON_CHARACTER) != -1) {
2006 //The index is 'level:source tuplets' which is for mg. Take out 'level'
2007 old_index = true;
2008 default_index_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE,
2009 index_str.substring (0, index_str.indexOf (StaticStrings.COLON_CHARACTER)));
2010 index_str = index_str.substring (index_str.indexOf (StaticStrings.COLON_CHARACTER) + 1);
2011 } else {
2012 default_index_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE, "");
2013 }
2014
2015 //Each index may have a list of comma-separated strings.
2016 //split them into 'content' elements in the internal structure
2017 StringTokenizer content_tokenizer = new StringTokenizer (index_str, StaticStrings.COMMA_CHARACTER);
2018 while(content_tokenizer.hasMoreTokens ()) {
2019 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
2020 String content_str = content_tokenizer.nextToken ();
2021 // Since the contents of indexes have to be certain keywords, or metadata elements, if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
2022 if(content_str.indexOf (StaticStrings.NS_SEP) == -1) {
2023 if(content_str.equals (StaticStrings.TEXT_STR) ||
2024 (!old_index && content_str.equals (StaticStrings.ALLFIELDS_STR))) {
2025 // in this case, do nothing
2026 }
2027 else {
2028 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
2029 }
2030 }
2031
2032 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_str);
2033 default_index_element.appendChild (content_element);
2034 content_element = null;
2035 }
2036 appendProperly (toElement, default_index_element);
2037 }
2038 // For mg, this method is still called, but make it 'assigned=false'
2039 private void doDefaultLevel (Document to, Node searchNode) {
2040 Element toElement = to.getDocumentElement ();
2041 Element default_index_option = to.createElement (StaticStrings.INDEXOPTION_DEFAULT_ELEMENT);
2042 default_index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.LEVEL_DEFAULT_STR);
2043
2044 Element e = (Element)XMLTools.getChildByTagName (searchNode, StaticStrings.LEVEL_DEFAULT_ELEMENT);
2045 if (e != null) {
2046 default_index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2047 String level = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2048 default_index_option.setAttribute (StaticStrings.VALUE_ATTRIBUTE, level);
2049 } else {
2050 //In the case of mg, there's no level! build a default one using 'assigned=false value=document'
2051 default_index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
2052 default_index_option.setAttribute (StaticStrings.VALUE_ATTRIBUTE, StaticStrings.DOCUMENT_STR);
2053 }
2054 appendProperly (toElement, default_index_option);
2055 }
2056
2057 // Transform plugins (pluginListNode) of collectionConfig.xml into the internal structure (i.e. Document to)
2058 private void doPlugin (Document to, Node pluginListNode) {
2059 Element toElement = to.getDocumentElement ();
2060 NodeList plugin_children = ((Element)pluginListNode).getElementsByTagName (StaticStrings.PLUGIN_STR);
2061 int plugin_nodes = plugin_children.getLength ();
2062
2063 if (plugin_nodes < 1) {
2064 return;
2065 }
2066
2067 for (int i=0; i<plugin_nodes; i++) {
2068 Element e = (Element)plugin_children.item (i);
2069 String str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2070 str = ensureNewPluginName(str);
2071 Element plugin_element = to.createElement (StaticStrings.PLUGIN_ELEMENT);
2072 plugin_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, str);
2073
2074
2075 NodeList option_children = e.getElementsByTagName (StaticStrings.OPTION_STR);
2076
2077 for (int j=0; j<option_children.getLength (); j++) {
2078 Element el = (Element)option_children.item (j);
2079 String name_str = el.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2080 if (name_str.startsWith (StaticStrings.MINUS_CHARACTER)) {
2081 name_str = name_str.substring (1);
2082 }
2083 String value_str = el.getAttribute (StaticStrings.VALUE_ATTRIBUTE);
2084 Element option_element = null;
2085
2086 if (name_str.equals ("") && !value_str.equals ("")) {
2087 continue;
2088 }
2089
2090
2091 option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
2092 option_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2093 if(name_str.equals (StaticStrings.RECPLUG_STR) && value_str.equals (StaticStrings.USE_METADATA_FILES_ARGUMENT)) {
2094 continue; // ignore this option
2095 }
2096
2097 if(value_str != null) {
2098 // Remove any speech marks appended in strings containing whitespace
2099 if(value_str.startsWith (StaticStrings.SPEECH_CHARACTER) && value_str.endsWith (StaticStrings.SPEECH_CHARACTER)) {
2100 value_str = value_str.substring (1, value_str.length () - 1);
2101 }
2102 if(name_str.equals (StaticStrings.METADATA_STR)) {
2103 // The metadata argument must be the fully qualified name of a metadata element, so if it doesn't yet have a namespace, append the extracted metadata namespace.
2104 String[] values = value_str.split (StaticStrings.COMMA_CHARACTER);
2105 value_str = "";
2106 for (int k=0; k<=values.length-1; k++) {
2107 if (values[k].indexOf (StaticStrings.NS_SEP) == -1) {
2108 values[k] = StaticStrings.EXTRACTED_NAMESPACE + values[k];
2109 }
2110
2111 if (k < values.length-1) {
2112 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
2113
2114 } else {
2115 value_str = value_str + values[k];
2116 }
2117 }
2118 }
2119 }
2120 if (!name_str.equals ("")) {
2121 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
2122 }
2123 if (!value_str.equals ("")) {
2124 XMLTools.setNodeText (option_element, value_str);
2125 }
2126 plugin_element.appendChild (option_element);
2127
2128 }
2129
2130 appendProperly (toElement, plugin_element);
2131 }
2132
2133 }
2134
2135 //Handle classifiers
2136 private void doClassifier (Document to, Node browseNode) {
2137 Element toElement = to.getDocumentElement ();
2138 NodeList classifier_children = ((Element)browseNode).getElementsByTagName (StaticStrings.CLASSIFIER_STR);
2139 int num_nodes = classifier_children.getLength ();
2140
2141 if (num_nodes < 1) {
2142 return;
2143 }
2144
2145 for (int i=0; i<num_nodes; i++) {
2146 Element e = (Element)classifier_children.item (i);
2147 String str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2148 Element classify_element = to.createElement (StaticStrings.CLASSIFY_ELEMENT);
2149 classify_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, str);
2150
2151 String options_str = "";
2152 NodeList option_children = e.getElementsByTagName (StaticStrings.OPTION_STR);
2153 for (int j=0; j<option_children.getLength (); j++) {
2154 Element el = (Element)option_children.item (j);
2155 String name_str = el.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2156 options_str = options_str + ((name_str.equals(""))? "" : (" " + name_str));
2157 if (name_str.startsWith (StaticStrings.MINUS_CHARACTER)) {
2158 name_str = name_str.substring (1);
2159 }
2160 String value_str = el.getAttribute (StaticStrings.VALUE_ATTRIBUTE);
2161 options_str = options_str + ((name_str.equals(""))? "" : (" " + value_str));
2162 Element option_element = null;
2163
2164 if (name_str.equals ("") && !value_str.equals ("")) {
2165 continue; //invalid condition
2166 }
2167
2168 option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
2169 option_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2170
2171 if (!name_str.equals ("")) {
2172 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
2173 }
2174
2175 if (!value_str.equals ("") && name_str.equals (StaticStrings.METADATA_STR)) {
2176 // The metadata argument must be the fully qualified name of a metadata element, so if it doesn't yet have a namespace, append the extracted metadata namespace.
2177 String[] values = value_str.split (StaticStrings.COMMA_CHARACTER);
2178 value_str = "";
2179 for (int k=0; k<=values.length-1; k++) {
2180 if (values[k].indexOf (StaticStrings.NS_SEP) == -1) {
2181 values[k] = StaticStrings.EXTRACTED_NAMESPACE + values[k];
2182 }
2183 else {
2184 MetadataElement metadata_element = MetadataTools.getMetadataElementWithName (values[k]);
2185 if (metadata_element != null) {
2186 values[k] = metadata_element.getDisplayName ();
2187 }
2188 }
2189 if (k < values.length-1) {
2190 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
2191 } else {
2192 value_str = value_str + values[k];
2193 }
2194 }
2195 }
2196
2197 if (value_str != null && !value_str.equals ("")) {
2198 XMLTools.setNodeText (option_element, value_str);
2199 }
2200 classify_element.appendChild (option_element);
2201 }
2202 //format element for this classifier
2203 Element format = (Element)XMLTools.getChildByTagName (e, StaticStrings.FORMAT_STR);
2204 if(format != null) {
2205 classify_element.appendChild (doFormat(to, format, null));
2206 }
2207 appendProperly (toElement, classify_element);
2208 }
2209
2210 // default format statement for all classifiers
2211 Element default_classifier_format = (Element)XMLTools.getChildByTagName (browseNode, StaticStrings.FORMAT_STR);
2212
2213 to.getDocumentElement ().appendChild (doFormat(to, default_classifier_format, StaticStrings.BROWSE_STR));
2214 }
2215 private Element doFormat(Document to, Element format, String name_str) {
2216 Element format_element = to.createElement (StaticStrings.FORMAT_STR);
2217 if (name_str != null) {
2218 format_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
2219 }
2220 if (format != null) {
2221 String gsf_text = XMLTools.xmlNodeToStringWithoutIndenting(format);
2222
2223 if (gsf_text.startsWith("<") && (gsf_text.indexOf("<") != gsf_text.lastIndexOf("<"))) {
2224 gsf_text = gsf_text.substring(gsf_text.indexOf("<gsf"),
2225 gsf_text.indexOf(StaticStrings.FORMAT_END_TAG));
2226 }
2227
2228 XMLTools.setNodeText(format_element, gsf_text);
2229 }
2230 return format_element;
2231 }
2232 // Handling 'subcollection' elements in 'search' element of 'collectionConfig.xml'
2233 private void doSubcollection (Document to, Node searchNode) {
2234 Element toElement = to.getDocumentElement ();
2235 NodeList sub_children = ((Element)searchNode).getElementsByTagName (StaticStrings.SUBCOLLECTION_STR);
2236 int sub_nodes = sub_children.getLength ();
2237
2238 // There is no subcollection
2239 if (sub_nodes < 1) {
2240 return;
2241 }
2242
2243 for (int i=0; i<sub_nodes; i++) {
2244 Element sub_child = (Element)sub_children.item (i);
2245 String name_str = sub_child.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2246 String filter_str = sub_child.getAttribute (StaticStrings.FILTER_ATTRIBUTE);
2247
2248 // filter_str is in the form '<! (if set)><metadata>/<metadata value>/<flag (if any)>'
2249
2250 int pos = filter_str.indexOf (StaticStrings.SEPARATOR_CHARACTER);
2251 String meta_str = "";
2252 String meta_value_str = "";
2253 String clude_str = "";
2254 String flag_str = "";
2255 if (pos == -1) {
2256
2257 meta_str = meta_value_str = filter_str;
2258 clude_str = StaticStrings.INCLUDE_STR;
2259 } else {
2260 clude_str = StaticStrings.INCLUDE_STR;
2261 if (filter_str.startsWith (StaticStrings.EXCLAMATION_CHARACTER)) {
2262 clude_str = StaticStrings.EXCLUDE_STR;
2263 // Peel off "!"
2264 filter_str = filter_str.substring (StaticStrings.EXCLAMATION_CHARACTER.length ());
2265 }
2266
2267 String[] strs = filter_str.split (StaticStrings.SEPARATOR_CHARACTER);
2268 if (strs[0] != null && strs[0] != "") {
2269 meta_str = strs[0];
2270 }
2271 if(!meta_str.equals (StaticStrings.FILENAME_STR) && meta_str.indexOf (StaticStrings.NS_SEP) == -1) {
2272 meta_str = StaticStrings.EXTRACTED_NAMESPACE + meta_str;
2273 }
2274
2275 if (strs[1] != null && strs[1] != "") {
2276 meta_value_str = strs[1];
2277 }
2278 if (strs.length > 2) {
2279 //This means there has been set a flag
2280 if (strs[2] != null && strs[2] != "") {
2281 flag_str = strs[2];
2282 }
2283 }
2284 }
2285 Element subcollection_element = to.createElement (StaticStrings.SUBCOLLECTION_ELEMENT);
2286 subcollection_element.setAttribute (StaticStrings.NAME_STR, name_str);
2287 subcollection_element.setAttribute (StaticStrings.CONTENT_ATTRIBUTE, meta_str);
2288 subcollection_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, clude_str);
2289 if (flag_str != "") {
2290 subcollection_element.setAttribute (StaticStrings.OPTIONS_ATTRIBUTE, flag_str);
2291 }
2292 XMLTools.setNodeText (subcollection_element, meta_value_str);
2293
2294 toElement.appendChild (subcollection_element);
2295 }
2296 }
2297 //Handle levels (document, section). In the internal structure, the element is called 'IndexOption'
2298 private void doLevel (Document to, Node searchNode) {
2299 Element toElement = to.getDocumentElement ();
2300 NodeList level_children = ((Element)searchNode).getElementsByTagName (StaticStrings.LEVEL_ATTRIBUTE);
2301 int level_nodes = level_children.getLength ();
2302
2303 // it's mg, there's no level. So we construct a default 'indexOption' in the internal structure
2304 if (level_nodes < 1) {
2305 Element index_option = to.createElement (StaticStrings.INDEXOPTIONS_ELEMENT);
2306 index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
2307 index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.LEVELS_STR);
2308
2309 Element option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
2310 option_element.setAttribute (StaticStrings.NAME_STR, StaticStrings.DOCUMENT_STR);
2311 index_option.appendChild (option_element);
2312
2313 appendProperly (toElement, index_option);
2314
2315 return;
2316 }
2317
2318 Element index_option = to.createElement (StaticStrings.INDEXOPTIONS_ELEMENT);
2319 index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2320 index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.LEVELS_STR);
2321
2322 for (int i=0; i<level_nodes; i++) {
2323 Element level_element = (Element)level_children.item (i);
2324 String level_str = level_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2325 Element option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
2326 option_element.setAttribute (StaticStrings.NAME_STR, level_str);
2327 index_option.appendChild (option_element);
2328
2329
2330 // Contructing 'collectionmetadata' elements from the 'displayItem' of this 'level' element
2331 ArrayList displayItem_list = XMLTools.getNamedElementList (level_element,
2332 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
2333 if (displayItem_list == null) {
2334 return ;
2335 }
2336 for (int j=0; j<displayItem_list.size (); j++) {
2337 Element item = (Element)displayItem_list.get (j);
2338 String text = XMLTools.getNodeText (item);
2339 String lang = item.getAttribute (StaticStrings.LANG_ATTRIBUTE);
2340
2341 //If there is nothing to display, don't bother creating the element
2342 if (text == "") {
2343 continue;
2344 }
2345 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
2346 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2347 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, level_str);
2348 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
2349 XMLTools.setNodeText (collectionmetadata, text);
2350
2351 appendProperly (toElement, collectionmetadata);
2352 }
2353 }
2354 appendProperly (toElement, index_option);
2355 }
2356 //Handle 'indexSubcollection' element of collectionConfig.xml, which is called 'SubcollectionIndexes' in the internal structure. These contain the subcollection indexes (i.e. the filter or filter combinations), and displayed words for the filter names.
2357 private void doIndexSubcollection (Document to, Node searchNode) {
2358 Element toElement = to.getDocumentElement ();
2359 NodeList index_sub_children = ((Element)searchNode).getElementsByTagName (StaticStrings.SUBCOLLECTION_INDEX_ELEMENT);
2360 int num_nodes = index_sub_children.getLength ();
2361
2362 // there is no subcollection index
2363 if (num_nodes < 1) {
2364 return;
2365 }
2366
2367 Element subcollection_indexes = to.createElement (StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT);
2368
2369 for (int i=0; i<num_nodes; i++) {
2370 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);
2371 Element index_sub_child = (Element)index_sub_children.item (i);
2372 String name_str = index_sub_child.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2373
2374 // name_str is in the form of comma separated strings, each of which is a subcollection filter name
2375 String[] filters = name_str.split (StaticStrings.COMMA_CHARACTER);
2376 for (int j=0; j<filters.length; j++) {
2377
2378 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
2379 content_element.setAttribute (StaticStrings.NAME_STR, filters[j]);
2380 index_element.appendChild (content_element);
2381 }
2382 subcollection_indexes.appendChild (index_element);
2383
2384 // Contructing 'collectionmetadata' elements from the 'displayItem' of this 'indexSubcollection' element
2385 ArrayList displayItem_list = XMLTools.getNamedElementList (index_sub_child,
2386 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
2387 if (displayItem_list == null) {
2388 // there is no display item for this element
2389 continue;
2390 }
2391 for (int j=0; j<displayItem_list.size (); j++) {
2392 Element item = (Element)displayItem_list.get (j);
2393 String text = XMLTools.getNodeText (item);
2394 String lang = item.getAttribute (StaticStrings.LANG_ATTRIBUTE);
2395
2396 //If there is nothing to display, don't bother creating the element
2397 if (text == "") {
2398 continue;
2399 }
2400 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
2401 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2402 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
2403 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
2404 XMLTools.setNodeText (collectionmetadata, text);
2405
2406 appendProperly (toElement, collectionmetadata);
2407 }
2408 }
2409 appendProperly (toElement, subcollection_indexes);
2410 }
2411 //Handle 'indexLanguage' element of collectionConfig.xml, which is called 'Languages' in the internal structure. These contain the language indexes, and displayed words for those language index names.
2412 private void doIndexLanguage (Document to, Node searchNode) {
2413 Element toElement = to.getDocumentElement ();
2414 NodeList index_sub_children = ((Element)searchNode).getElementsByTagName (StaticStrings.LANGUAGE_INDEX_ELEMENT);
2415 int num_nodes = index_sub_children.getLength ();
2416
2417 // there is no subcollection index
2418 if (num_nodes < 1) {
2419 return;
2420 }
2421
2422 Element language_indexes = to.createElement (StaticStrings.LANGUAGES_ELEMENT);
2423
2424 for (int i=0; i<num_nodes; i++) {
2425 Element language_element = to.createElement (StaticStrings.LANGUAGE_ELEMENT);
2426 Element index_sub_child = (Element)index_sub_children.item (i);
2427 String name_str = index_sub_child.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2428 language_element.setAttribute (StaticStrings.NAME_STR, name_str);
2429 language_indexes.appendChild (language_element);
2430
2431 // Contructing 'collectionmetadata' elements from the 'displayItem' of this 'indexLanguage' element
2432 ArrayList displayItem_list = XMLTools.getNamedElementList (index_sub_child,
2433 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
2434 if (displayItem_list == null) {
2435 // there is no display item for this element
2436 continue;
2437 }
2438 for (int j=0; j<displayItem_list.size (); j++) {
2439 Element item = (Element)displayItem_list.get (j);
2440 String text = XMLTools.getNodeText (item);
2441 String lang = item.getAttribute (StaticStrings.LANG_ATTRIBUTE);
2442
2443 //If there is nothing to display, don't bother creating the element
2444 if (text == "") {
2445 continue;
2446 }
2447 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
2448 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2449 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
2450 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
2451 XMLTools.setNodeText (collectionmetadata, text);
2452
2453 appendProperly (toElement, collectionmetadata);
2454 }
2455 }
2456 toElement.appendChild (language_indexes);
2457 }
2458 // Handling both searchType and search format statement
2459 private void doSearchFormat (Document to, Node searchNode) {
2460 //There are two elements called <format> in <search>: one is for searchType; another is for format statement
2461 NodeList format_children = ((Element)searchNode).getElementsByTagName (StaticStrings.FORMAT_STR);
2462 int format_nodes = format_children.getLength ();
2463 if (format_nodes < 1) {
2464 return;
2465 }
2466 Element format = null;
2467 Element search_type = null;
2468 for(int i=0; i<format_nodes; i++) {
2469 Node e = format_children.item (i);
2470 if (e.hasAttributes () == false) {
2471 //The format element for format statement has no any attribute
2472 format = (Element)e;
2473 }
2474 else if (((Element)e).getAttribute (StaticStrings.NAME_ATTRIBUTE).equals(StaticStrings.SEARCHTYPE_ELEMENT)) {
2475 search_type = (Element)e;
2476 }
2477 }
2478 //format statement for search
2479 if (format != null) {
2480 (to.getDocumentElement ()).appendChild (doFormat(to, format, StaticStrings.SEARCH_STR));
2481 }
2482 // searchType in search
2483 if(search_type != null) {
2484 String searchtype_str = XMLTools.getNodeText(search_type);
2485
2486 Element search_type_element = to.createElement (StaticStrings.FORMAT_STR);
2487 search_type_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCHTYPE_ELEMENT);
2488 XMLTools.setNodeText(search_type_element, searchtype_str);
2489
2490
2491 appendProperly (to.getDocumentElement (), search_type_element);
2492 }
2493 }
2494 // Handling defaultIndexLanguage and languageMetadata in collectionConfig.xml ('elementNameFrom'); in the internal structure, they are called 'DefaultLanguage' and 'LanguageMetadata' ('elementNameTo') respectively.
2495 // Converting from collectionConfig.xml to the internal xml structure.
2496 private void doLanguageMetadata (Document to, Node searchNode) {
2497 Element toElement = to.getDocumentElement ();
2498 String elementNameFrom = StaticStrings.LANGUAGE_METADATA_ELEMENT_STR;
2499 String elementNameTo = StaticStrings.LANGUAGE_METADATA_ELEMENT;
2500 Node from_element = XMLTools.getChildByTagName (searchNode, elementNameFrom);
2501 if (from_element == null) {
2502 return; // such an element not found
2503 }
2504
2505 Element to_element = to.createElement (elementNameTo);
2506
2507 String name_str = ((Element)from_element).getAttribute (StaticStrings.NAME_ATTRIBUTE);
2508 if (name_str.indexOf (StaticStrings.NS_SEP) == -1) {
2509 name_str = StaticStrings.EXTRACTED_NAMESPACE + name_str;
2510 }
2511 to_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
2512 to_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2513
2514 toElement.appendChild (to_element);
2515 }
2516 private void doReplaceListRef (Document to, Element from) {
2517 Element toElement = to.getDocumentElement ();
2518
2519 Node replace_element = XMLTools.getChildByTagName (from, StaticStrings.REPLACELISTREF_STR);
2520 if (replace_element == null) {
2521 return; // such an element not found
2522 }
2523
2524 Element to_element = XMLTools.duplicateElement (to, (Element)replace_element, true);
2525 toElement.appendChild (to_element);
2526 }
2527 private void convertReplaceListRef (Document from, Document to) {
2528 Element toElement = to.getDocumentElement ();
2529
2530 Node replace_element = XMLTools.getChildByTagName (from.getDocumentElement (), StaticStrings.REPLACELISTREF_STR);
2531 if (replace_element == null) {
2532 return; // such an element not found
2533 }
2534
2535 Element to_element = XMLTools.duplicateElement (to, (Element)replace_element, true);
2536 toElement.appendChild (to_element);
2537 }
2538 private void doOAIElement (Document to, Element from) {
2539 Element toElement = to.getDocumentElement ();
2540
2541 Node oai_element = XMLTools.getChildByTagName (from, StaticStrings.OAI_ELEMENT);
2542 if (oai_element == null) {
2543 return; // such an element not found
2544 }
2545
2546 Element to_element = XMLTools.duplicateElement (to, (Element)oai_element, true);
2547 toElement.appendChild (to_element);
2548 }
2549 private void convertOAIElement (Document from, Document to) {
2550 Element toElement = to.getDocumentElement ();
2551
2552 Node oai_element = XMLTools.getChildByTagName (from.getDocumentElement (), StaticStrings.OAI_ELEMENT);
2553 if (oai_element == null) {
2554 return; // such an element not found
2555 }
2556
2557 Element to_element = XMLTools.duplicateElement (to, (Element)oai_element, true);
2558 toElement.appendChild (to_element);
2559 }
2560 private void doDefaultIndexLanguage (Document to, Node searchNode) {
2561 Element toElement = to.getDocumentElement ();
2562 String elementNameFrom = StaticStrings.LANGUAGE_DEFAULT_INDEX_ELEMENT;
2563 String elementNameTo = StaticStrings.LANGUAGE_DEFAULT_ELEMENT;
2564 Node from_element = XMLTools.getChildByTagName (searchNode, elementNameFrom);
2565 if (from_element == null) {
2566 return; // such an element not found
2567 }
2568
2569 Element to_element = to.createElement (elementNameTo);
2570
2571 String name_str = ((Element)from_element).getAttribute (StaticStrings.NAME_ATTRIBUTE);
2572 to_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
2573 to_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2574
2575 toElement.appendChild (to_element);
2576 }
2577
2578 //Handle 'indexOption' (i.e. casefold, stem etc). In the internal structure, the element is called 'IndexOption'
2579 private void doIndexOption (Document to, Node searchNode) {
2580 Element toElement = to.getDocumentElement ();
2581 Node index_option_node = XMLTools.getChildByTagName (searchNode, StaticStrings.INDEXOPTION_STR);
2582 if (index_option_node == null) {
2583 return;
2584 }
2585 NodeList option_children = ((Element)index_option_node).getElementsByTagName (StaticStrings.OPTION_STR);
2586 int option_nodes = option_children.getLength ();
2587
2588 // for lucene, there is no 'indexOption'. We build a default 'indexOption' and 'assigned=false' in case the user switches to mg or mgpp
2589 if (option_nodes < 1) {
2590 Element index_option = to.createElement (StaticStrings.INDEXOPTIONS_ELEMENT);
2591 index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
2592 index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.INDEXOPTIONS_STR);
2593 String[] option_str =
2594 { StaticStrings.CASEFOLD_OPTION_STR, StaticStrings.STEM_OPTION_STR };
2595 for (int i=0; i<option_str.length; i++) {
2596 Element option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
2597 option_element.setAttribute (StaticStrings.NAME_STR, option_str[i]);
2598 index_option.appendChild (option_element);
2599 }
2600 appendProperly (toElement, index_option);
2601 return;
2602 }
2603
2604 Element index_option = to.createElement (StaticStrings.INDEXOPTIONS_ELEMENT);
2605 index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2606 index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.INDEXOPTIONS_STR);
2607
2608 for (int i=0; i<option_nodes; i++) {
2609 String option_str = ((Element)option_children.item (i)).getAttribute (StaticStrings.NAME_ATTRIBUTE);
2610 Element option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
2611 option_element.setAttribute (StaticStrings.NAME_STR, option_str);
2612 index_option.appendChild (option_element);
2613 }
2614 appendProperly (toElement, index_option);
2615 }
2616
2617 private Element doBuildType (Document to, String att_value) {
2618
2619 //construct 'BuildType' element
2620 Element element = to.createElement (StaticStrings.BUILDTYPE_ELEMENT);
2621 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.BUILDTYPE_STR);
2622 element.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
2623 element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2624 element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
2625
2626 XMLTools.setNodeText (element, att_value);
2627
2628 return element;
2629 }
2630
2631 // Convert 'description', 'smallicon' etc.
2632 private void convertDisplayItemList (Document from, Document to) {
2633 Element displayItemList = to.createElement (StaticStrings.DISPLAYITEMLIST_STR);
2634 Element destination = to.getDocumentElement ();
2635
2636 String []att_values =
2637 {StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR,
2638 StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR,
2639 StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR,
2640 StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR};
2641
2642 String []map_attrs =
2643 {StaticStrings.DESCRIPTION_STR,
2644 StaticStrings.SMALLICON_STR,
2645 StaticStrings.ICON_STR,
2646 StaticStrings.NAME_STR};
2647
2648 for (int i=0; i<att_values.length; i++) {
2649
2650 //dOc
2651 ArrayList e_list = XMLTools.getNamedElementList (from.getDocumentElement (),
2652 StaticStrings.COLLECTIONMETADATA_ELEMENT,
2653 StaticStrings.NAME_ATTRIBUTE, att_values[i]);
2654 // if such elements don't exist, don't bother
2655 if (e_list == null) {
2656 continue;
2657 }
2658 for (int j=0; j<e_list.size (); j++) {
2659 Element e = (Element)e_list.get (j);
2660 if (e.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
2661 continue;
2662 }
2663 String text = XMLTools.getNodeText (e);
2664 String lang = e.getAttribute (StaticStrings.LANGUAGE_ATTRIBUTE);
2665
2666 Element displayItem = constructElement (StaticStrings.DISPLAYITEM_STR, map_attrs[i],
2667 StaticStrings.LANG_STR, lang, text, to);
2668 displayItemList.appendChild (displayItem);
2669 }
2670
2671 }
2672 destination.appendChild (displayItemList);
2673 }
2674 // This method creates a DisplayItem element of the type of 'to' by using the ingredients from the element 'e'
2675 private Element constructDisplayItem (Element e, Document to) {
2676 String lang_string = e.getAttribute (StaticStrings.LANGUAGE_ATTRIBUTE);
2677 String text = XMLTools.getNodeText (e);
2678 Element displayItem = to.createElement (StaticStrings.DISPLAYITEM_STR);
2679 displayItem.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_ATTRIBUTE);
2680 displayItem.setAttribute (StaticStrings.LANG_STR, lang_string);
2681 XMLTools.setNodeText (displayItem, text);
2682 return displayItem;
2683 }
2684 private void convertMetadataList (Document from, Document to) {
2685 Element metadataList = to.createElement (StaticStrings.METADATALIST_STR);
2686 Element destination = to.getDocumentElement ();
2687
2688 String []ele_names =
2689 {StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT,
2690 StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT,
2691 StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT};
2692 String []att_names =
2693 {StaticStrings.COLLECTIONMETADATA_CREATOR_STR,
2694 StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR,
2695 StaticStrings.COLLECTIONMETADATA_PUBLIC_STR};
2696 for (int i=0; i<ele_names.length; i++) {
2697 Element e = XMLTools.getNamedElement (from.getDocumentElement (),
2698 ele_names[i], StaticStrings.NAME_ATTRIBUTE, att_names[i]);
2699 if (e == null) {
2700 continue;
2701 }
2702 String text = XMLTools.getNodeText (e);
2703 Element metadata = to.createElement (StaticStrings.METADATA_STR);
2704 metadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, att_names[i]);
2705 metadata.setAttribute (StaticStrings.LANG_STR, StaticStrings.ENGLISH_LANGUAGE_STR);
2706 XMLTools.setNodeText (metadata, text);
2707 metadataList.appendChild (metadata);
2708 }
2709
2710 destination.appendChild (metadataList);
2711 }
2712 // This method creates an element with the name 'element_name' of the type of 'to' by using the other three strings
2713 private Element constructElement (String element_name, String name_value, String lang_att, String lang_value, String text, Document to) {
2714 Element e = to.createElement (element_name);
2715 e.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2716 e.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_value);
2717 e.setAttribute (lang_att, lang_value);
2718 XMLTools.setNodeText (e, text);
2719
2720 return e;
2721 }
2722 // Convert classify in the internal(i.e. Document from) to collectionconfig.xml (i.e. Document to)
2723 private void convertClassifier (Document from, Document to) {
2724 Element browse_element = to.createElement (StaticStrings.BROWSE_STR);
2725 NodeList children = from.getDocumentElement ().getElementsByTagName (StaticStrings.CLASSIFY_ELEMENT);
2726
2727 int num_children = (children == null)? 0 : children.getLength ();
2728
2729 if (num_children == 0) {
2730 return ;
2731 }
2732
2733 for (int i=0; i<num_children; i++) {
2734
2735 Element child = (Element)children.item (i);
2736 if (child.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
2737 continue;
2738 }
2739 String str = child.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
2740 Element classifier_element = to.createElement (StaticStrings.CLASSIFIER_STR);
2741 classifier_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, str);
2742
2743 NodeList option_children = child.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
2744 for (int j=0; j<option_children.getLength (); j++) {
2745 Element el = (Element)option_children.item (j);
2746 if (el.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
2747 continue;
2748 }
2749 String name_str = el.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2750 String value_str = XMLTools.getNodeText (el);
2751
2752
2753 if (name_str == null && value_str == null) {
2754 continue;
2755 }
2756 Element option_element = to.createElement (StaticStrings.OPTION_STR);
2757 if (name_str != null && name_str.equals (StaticStrings.METADATA_STR)) {
2758
2759 // The metadata argument is the fully qualified name of a metadata element, so if it contains a namespace, remove the extracted metadata namespace as the build process doesn't know about it.
2760 String[] values = value_str.split (StaticStrings.COMMA_CHARACTER);
2761 value_str = "";
2762 for (int k=0; k<=values.length-1; k++) {
2763 if(values[k].startsWith (StaticStrings.EXTRACTED_NAMESPACE)) {
2764 values[k] = values[k].substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
2765 }
2766 else {
2767 MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName (values[k]);
2768 if (metadata_element != null) {
2769 values[k] = metadata_element.getFullName ();
2770 }
2771 }
2772 if (k < values.length-1) {
2773 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
2774 } else {
2775 value_str = value_str + values[k];
2776 }
2777 }
2778 }
2779
2780 if (!name_str.equals ("")) {
2781 if (!name_str.startsWith (StaticStrings.MINUS_CHARACTER)) {
2782 name_str = StaticStrings.MINUS_CHARACTER + name_str;
2783 }
2784 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
2785 }
2786
2787 if (!value_str.equals ("")) {
2788 option_element.setAttribute (StaticStrings.VALUE_ATTRIBUTE, value_str);
2789 }
2790
2791 classifier_element.appendChild (option_element);
2792 }
2793
2794 //format element for this classifier
2795 Element e = (Element)XMLTools.getChildByTagName (child, StaticStrings.FORMAT_STR);
2796
2797 if (e != null) {
2798 classifier_element.appendChild (convertFormat(to, e));
2799 }
2800 browse_element.appendChild (classifier_element);
2801 }
2802
2803 //convert default classifier format
2804 Element e = XMLTools.getNamedElement (from.getDocumentElement (), StaticStrings.FORMAT_STR,
2805 StaticStrings.NAME_ATTRIBUTE, StaticStrings.BROWSE_STR);
2806 browse_element.appendChild (convertFormat(to, e));
2807
2808 to.getDocumentElement ().appendChild (browse_element);
2809 }
2810 private Element convertFormat(Document to, Element e) {
2811 String format_str = XMLTools.getNodeText(e);
2812 Element format = to.createElement (StaticStrings.FORMAT_STR);
2813 //XMLTools.copyAllChildren (format, e);
2814 XMLTools.setNodeText(format, format_str);
2815 return format;
2816 }
2817 //convert format statement for search
2818 private void convertSearchFormat (Document from, Document to) {
2819 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
2820 Element e = XMLTools.getNamedElement (from.getDocumentElement (), StaticStrings.FORMAT_STR,
2821 StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCH_STR);
2822
2823 search.appendChild (convertFormat(to, e));
2824
2825 }
2826 //convert format statement for display of the documents
2827 private void convertDisplayFormat (Document from, Document to) {
2828 Element e = XMLTools.getNamedElement (from.getDocumentElement (), StaticStrings.FORMAT_STR,
2829 StaticStrings.NAME_ATTRIBUTE, StaticStrings.DISPLAY_STR);
2830 if (e == null) {
2831 return;
2832 }
2833 Element display = to.createElement (StaticStrings.DISPLAY_STR);
2834 display.appendChild (convertFormat(to, e));
2835 to.getDocumentElement ().appendChild (display);
2836 }
2837 // Convert plugins in the internal(i.e. Document from) to collectionconfig.xml (i.e. Document to)
2838 private void convertPlugin (Document from, Document to) {
2839 Element import_element = to.createElement (StaticStrings.IMPORT_STR);
2840 Element plugin_list_element = to.createElement (StaticStrings.PLUGINLIST_STR);
2841
2842 NodeList children = from.getDocumentElement ().getElementsByTagName (StaticStrings.PLUGIN_ELEMENT);
2843 int num_children = (children == null)? 0 : children.getLength ();
2844 if (num_children == 0) {
2845 return ;
2846 }
2847
2848 for (int i=0; i<num_children; i++) {
2849
2850 Element child = (Element)children.item (i);
2851 if (child.getAttribute (StaticStrings.SEPARATOR_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
2852 continue;
2853 }
2854 if (child.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
2855 continue;
2856 }
2857
2858 String str = child.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
2859 Element plugin_element = to.createElement (StaticStrings.PLUGIN_STR);
2860 plugin_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, str);
2861
2862 NodeList option_children = child.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
2863 for (int j=0; j<option_children.getLength (); j++) {
2864 Element el = (Element)option_children.item (j);
2865 if(!el.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
2866 continue;
2867 }
2868 String name_str = el.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2869 String value_str = XMLTools.getNodeText (el);
2870
2871
2872 if (name_str == null && value_str == null) {
2873 continue;
2874 }
2875 Element option_element = to.createElement (StaticStrings.OPTION_STR);
2876 if (name_str != null && name_str.equals (StaticStrings.METADATA_STR)) {
2877
2878 // The metadata argument is the fully qualified name of a metadata element, so if it contains a namespace, remove the extracted metadata namespace as the build process doesn't know about it.
2879 String[] values = value_str.split (StaticStrings.COMMA_CHARACTER);
2880 value_str = "";
2881 for (int k=0; k<=values.length-1; k++) {
2882 if(values[k].startsWith (StaticStrings.EXTRACTED_NAMESPACE)) {
2883 values[k] = values[k].substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
2884 }
2885
2886 if (k < values.length-1) {
2887 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
2888 } else {
2889 value_str = value_str + values[k];
2890 }
2891 }
2892 }
2893
2894 if (!name_str.equals ("")) {
2895 if (!name_str.startsWith (StaticStrings.MINUS_CHARACTER)) {
2896 name_str = StaticStrings.MINUS_CHARACTER + name_str;
2897 }
2898 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
2899 }
2900
2901 if (!value_str.equals ("")) {
2902 option_element.setAttribute (StaticStrings.VALUE_ATTRIBUTE, value_str);
2903 }
2904
2905 plugin_element.appendChild (option_element);
2906 }//for loop ends
2907
2908 plugin_list_element.appendChild (plugin_element);
2909 }//for loop ends
2910
2911 import_element.appendChild (plugin_list_element);
2912
2913 //do the plugout element (used by building flax collections)
2914 Node plugout = XMLTools.getChildByTagNameIndexed (from.getDocumentElement (), PLUGOUT_ELEMENT, 0);
2915 if (plugout != null) {
2916 Element to_element = XMLTools.duplicateElement (to, (Element)plugout, true);
2917 import_element.appendChild (to_element);
2918 }
2919
2920 to.getDocumentElement ().appendChild (import_element);
2921 }
2922
2923 //Handle 'searchType' of collectionConfig.xml. In the internal structure, its also called 'searchType', eg. plain, form
2924 private void convertSearchType (Document from, Document to) {
2925 Element e = XMLTools.getNamedElement (from.getDocumentElement (), StaticStrings.FORMAT_STR,
2926 StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCHTYPE_ELEMENT);//searchType
2927
2928 if (e == null) {
2929 return;
2930 }
2931 Element search_type_element = to.createElement (StaticStrings.FORMAT_STR);
2932
2933// String searchtype_str = e.getAttribute (StaticStrings.VALUE_ATTRIBUTE);
2934// search_type_element.setAttribute (StaticStrings.VALUE_ATTRIBUTE, searchtype_str);
2935 String searchtype_str = XMLTools.getNodeText(e);
2936 XMLTools.setNodeText(search_type_element, searchtype_str);
2937
2938 search_type_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCHTYPE_ELEMENT);
2939 //Get the 'search' element from 'to'
2940 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
2941 search.appendChild (search_type_element);
2942 }
2943
2944 private void convertBuildType (Document from, Document to) {
2945 Element e = XMLTools.getNamedElement (from.getDocumentElement (),
2946 StaticStrings.BUILDTYPE_ELEMENT,
2947 StaticStrings.NAME_ATTRIBUTE, StaticStrings.BUILDTYPE_STR);
2948 if (e == null) {
2949 return;
2950 }
2951 String indexer = XMLTools.getNodeText (e);
2952 Element search = to.createElement (StaticStrings.SEARCH_STR);
2953 search.setAttribute (StaticStrings.TYPE_ATTRIBUTE, indexer);
2954 to.getDocumentElement ().appendChild (search);
2955 }
2956 private void convertDefaultIndex (Document from, Document to, Element search) {
2957 Element source = from.getDocumentElement ();
2958
2959 Element default_index_element = (Element)XMLTools.getChildByTagName (source, StaticStrings.INDEX_DEFAULT_ELEMENT);
2960 if (default_index_element == null) {
2961 return;
2962 }
2963
2964 String indexer = search.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
2965 String level_str = default_index_element.getAttribute (StaticStrings.LEVEL_ATTRIBUTE);
2966 // Debugging purposes
2967 if (level_str.equals ("") && indexer.equals (StaticStrings.MG_STR) ) {
2968 System.out.println ("Bug: DefaultIndex should have its level attribute not empty.");
2969 }
2970
2971 NodeList content_elements = default_index_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
2972 int content_elements_length = content_elements.getLength ();
2973
2974 // Don't output anything if no indexes are set
2975 if(content_elements_length == 0) {
2976 return ;//
2977 }
2978
2979 String index_str = "";
2980
2981 if (indexer.equals (StaticStrings.MG_STR)) {
2982 //combine level with indexes
2983 index_str = level_str + StaticStrings.COLON_CHARACTER;
2984 }
2985 else { //for mgpp/lucene, just take index
2986 //do nothing
2987 }
2988
2989 for(int k = 0; k < content_elements_length; k++) {
2990 Element content_element = (Element) content_elements.item (k);
2991 if (content_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
2992 continue;
2993 }
2994 String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
2995
2996 if(name_str.startsWith (StaticStrings.EXTRACTED_NAMESPACE)) {
2997 name_str = name_str.substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
2998 }
2999
3000 index_str = index_str + name_str;
3001
3002 // Make it comma separated string
3003 if(k < content_elements_length - 1) {
3004 index_str = index_str + StaticStrings.COMMA_CHARACTER;
3005 }
3006 content_element = null;
3007 }//for loop ends
3008
3009
3010 Element default_index = to.createElement (StaticStrings.INDEX_DEFAULT_ELEMENT_LOWERCASE);
3011 default_index.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_str);
3012 search.appendChild (default_index);
3013
3014 }
3015 private void convertSubcollection (Document from, Document to) {
3016 Element source = from.getDocumentElement ();
3017 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
3018 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
3019
3020 // Get the Subcollection element from the internal structure
3021 NodeList subcollection_elements = source.getElementsByTagName (StaticStrings.SUBCOLLECTION_ELEMENT);
3022 if (subcollection_elements == null) {
3023 return;
3024 }
3025 int subcollection_elements_length = subcollection_elements.getLength ();
3026
3027 if (subcollection_elements_length == 0) { // no
3028 return ;
3029 }
3030
3031 for(int j = 0; j < subcollection_elements_length; j++) {
3032
3033 Element e = (Element) subcollection_elements.item (j);
3034 if (e.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3035 continue;
3036 }
3037 String content = e.getAttribute (StaticStrings.CONTENT_ATTRIBUTE);
3038 String name = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
3039 String options = e.getAttribute (StaticStrings.OPTIONS_ATTRIBUTE);
3040 String type = e.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
3041 String text = XMLTools.getNodeText (e);
3042
3043 String filter = "";
3044 if (type.equals (StaticStrings.EXCLUDE_STR)) {
3045 filter = StaticStrings.EXCLAMATION_CHARACTER;
3046 }
3047
3048 if(content.startsWith (StaticStrings.EXTRACTED_NAMESPACE)) {
3049 content = content.substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
3050 }
3051 filter = filter + content + StaticStrings.SEPARATOR_CHARACTER + text;
3052 if (options != null && options != "") {
3053 filter = filter + StaticStrings.SEPARATOR_CHARACTER + options;
3054 }
3055 Element subcollection = to.createElement (StaticStrings.SUBCOLLECTION_STR);
3056 subcollection.setAttribute (StaticStrings.FILTER_ATTRIBUTE, filter);
3057 subcollection.setAttribute (StaticStrings.NAME_ATTRIBUTE, name);
3058
3059 search.appendChild (subcollection);
3060 }
3061 }
3062 private void convertSubcollectionIndexes (Document from, Document to) {
3063 Element source = from.getDocumentElement ();
3064 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
3065 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
3066
3067 // Get the SubcollectionIndexes element from the internal structure
3068 Element subcollection_indexes = (Element)XMLTools.getChildByTagName (source, StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT);
3069 if (subcollection_indexes == null) {
3070 return;
3071 }
3072 NodeList index_elements = subcollection_indexes.getElementsByTagName (StaticStrings.INDEX_ELEMENT);
3073 int index_elements_length = index_elements.getLength ();
3074
3075 if (index_elements_length == 0) { // no indexes
3076 return ;
3077 }
3078
3079 for(int j = 0; j < index_elements_length; j++) {
3080 Element index_element = (Element) index_elements.item (j);
3081 if (index_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3082 continue;
3083 }
3084
3085 Element index = to.createElement (StaticStrings.SUBCOLLECTION_INDEX_ELEMENT);
3086
3087 String index_value = "";
3088
3089 NodeList content_elements = index_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
3090 int content_elements_length = content_elements.getLength ();
3091
3092 for(int k = 0; k < content_elements_length; k++) {
3093 Element content_element = (Element) content_elements.item (k);
3094 if (content_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3095 continue;
3096 }
3097 String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
3098 index_value += name_str;
3099 // Make it comma separated string
3100 if(k < content_elements_length - 1) {
3101 index_value += StaticStrings.COMMA_CHARACTER;
3102 }
3103 content_element = null;
3104 }//for loop ends
3105
3106 index.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_value);
3107
3108 // Now constructing 'displayItem' element for this 'indexSubcollection' element
3109 // from the collectionmetadata element
3110 ArrayList collectionmetadata_list = XMLTools.getNamedElementList (source,
3111 StaticStrings.COLLECTIONMETADATA_ELEMENT,
3112 StaticStrings.NAME_ATTRIBUTE, index_value);
3113
3114 if (collectionmetadata_list != null) {
3115
3116 for(int k = 0; k < collectionmetadata_list.size (); k++) {
3117 Element collectionmetadata = (Element)collectionmetadata_list.get (k);
3118 if (collectionmetadata.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3119 continue;
3120 }
3121 Element displayItem = constructDisplayItem (collectionmetadata, to);
3122 index.appendChild (displayItem);
3123 }
3124 }
3125
3126 search.appendChild (index);
3127
3128 } //for loop ends
3129 }
3130
3131 private void convertLanguages (Document from, Document to) {
3132 Element source = from.getDocumentElement ();
3133 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
3134 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
3135
3136 // Get the Languages element from the internal structure
3137 Element languages = (Element)XMLTools.getChildByTagName (source, StaticStrings.LANGUAGES_ELEMENT);
3138 if (languages == null) {
3139 return;
3140 }
3141 NodeList language_elements = languages.getElementsByTagName (StaticStrings.LANGUAGE_ELEMENT);
3142 int language_elements_length = language_elements.getLength ();
3143
3144 if (language_elements_length == 0) {
3145 return ;
3146 }
3147
3148 for(int j = 0; j < language_elements_length; j++) {
3149 Element element = (Element) language_elements.item (j);
3150 if (element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3151 continue;
3152 }
3153
3154 // Create indexLanguage element
3155 Element index_language = to.createElement (StaticStrings.LANGUAGE_INDEX_ELEMENT);
3156
3157 String name_str = element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
3158 index_language.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
3159
3160 // Now constructing 'displayItem' element for this 'indexLanguage' element
3161 // from the collectionmetadata element
3162 ArrayList collectionmetadata_list = XMLTools.getNamedElementList (source,
3163 StaticStrings.COLLECTIONMETADATA_ELEMENT,
3164 StaticStrings.NAME_ATTRIBUTE, name_str);
3165
3166 if (collectionmetadata_list != null) {
3167
3168 for(int k = 0; k < collectionmetadata_list.size (); k++) {
3169 Element collectionmetadata = (Element)collectionmetadata_list.get (k);
3170 if (collectionmetadata.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3171 continue;
3172 }
3173 Element displayItem = constructDisplayItem (collectionmetadata, to);
3174 index_language.appendChild (displayItem);
3175 }
3176 }
3177
3178 search.appendChild (index_language);
3179
3180 } //for loop ends
3181
3182 // Convert DefaultLanguage
3183 // Get the DefaultLanguage element from the internal structure
3184 Element default_language = (Element)XMLTools.getChildByTagName (source, StaticStrings.LANGUAGE_DEFAULT_ELEMENT);
3185 if(default_language != null) {
3186 String lang_name = default_language.getAttribute (StaticStrings.NAME_ATTRIBUTE);
3187 Element default_index_language = to.createElement (StaticStrings.LANGUAGE_DEFAULT_INDEX_ELEMENT);
3188 default_index_language.setAttribute (StaticStrings.NAME_ATTRIBUTE, lang_name);
3189 search.appendChild (default_index_language);
3190 }
3191 // Convert LanguageMetadata
3192 // Get the LanguageMetadata element from the internal structure
3193 Element language_metadata = (Element)XMLTools.getChildByTagName (source, StaticStrings.LANGUAGE_METADATA_ELEMENT);
3194 if(language_metadata != null) {
3195 String meta_name = language_metadata.getAttribute (StaticStrings.NAME_ATTRIBUTE);
3196 Element language_meta = to.createElement (StaticStrings.LANGUAGE_METADATA_ELEMENT_STR);
3197 if(meta_name.startsWith (StaticStrings.EXTRACTED_NAMESPACE)) {
3198 meta_name = meta_name.substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
3199 }
3200 language_meta.setAttribute (StaticStrings.NAME_ATTRIBUTE, meta_name);
3201 search.appendChild (language_meta);
3202 }
3203 }
3204
3205 //convert indexes and their displayItems, which go in 'search' element in collectionConfig.xml
3206 //parameter 'to' is the document to be saved as collectionConfig.xml
3207 //parameter 'from' is the internal xml structure
3208 private void convertIndex (Document from, Document to) {
3209 Element source = from.getDocumentElement ();
3210 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
3211 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
3212
3213 //THere are two sets of indexes elements, find the one which is assigned 'true'
3214 Element indexes = XMLTools.getNamedElement (source,
3215 StaticStrings.INDEXES_ELEMENT,
3216 StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
3217 if (indexes == null) {
3218 return;
3219 }
3220 NodeList index_elements = indexes.getElementsByTagName (StaticStrings.INDEX_ELEMENT);
3221 int index_elements_length = index_elements.getLength ();
3222
3223 if (index_elements_length == 0) { // no indexes
3224 return ;
3225 }
3226
3227 //find out it's mg or mgpp/lucene
3228 String mg = search.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
3229 boolean mg_indexer = false;
3230 if (mg.equals (StaticStrings.MG_STR)) {
3231 mg_indexer = true;//it's mg, then the level is set as attribute of
3232 }
3233 if (mg_indexer == false) {
3234 // It's mgpp. Construct 'level' and 'defaultLevel' elements separately.
3235 convertLevels (from, to, search);
3236 }
3237
3238 for(int j = 0; j < index_elements_length; j++) {
3239 Element index_element = (Element) index_elements.item (j);
3240 if (index_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3241 continue;
3242 }
3243
3244 Element index_ele = to.createElement (StaticStrings.INDEX_LOW_STR);//index
3245
3246 // Used for creating displayItem for this element 'index_ele' further below
3247 // full_index_names contain 'ex.'
3248 String full_index_name = "";
3249 String level_str = "";
3250
3251 StringBuffer index_value = new StringBuffer ();
3252 if (mg_indexer == true) {
3253 // For mg indexer, there is a 'level' attribute in the index element of the internal structure
3254 // But mgpp/lucene don't
3255 level_str = index_element.getAttribute (StaticStrings.LEVEL_ATTRIBUTE);
3256 if(level_str.length () > 0) {
3257 index_value.append (level_str).append (StaticStrings.COLON_CHARACTER);
3258 //index_value = index_value.StaticStrings.COLON_CHARACTER;
3259 }
3260 }
3261
3262 NodeList content_elements = index_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
3263 int content_elements_length = content_elements.getLength ();
3264
3265 for(int k = 0; k < content_elements_length; k++) {
3266 Element content_element = (Element) content_elements.item (k);
3267 if (content_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3268 continue;
3269 }
3270 String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
3271
3272 full_index_name = full_index_name + name_str;
3273 if (k < content_elements_length - 1) {
3274 full_index_name = full_index_name + StaticStrings.COMMA_CHARACTER;
3275 }
3276
3277 if(name_str.startsWith (StaticStrings.EXTRACTED_NAMESPACE)) {
3278 name_str = name_str.substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
3279 }
3280
3281 index_value.append (name_str);
3282 name_str = null;
3283 // Make it comma separated string
3284 if(k < content_elements_length - 1) {
3285 index_value.append (StaticStrings.COMMA_CHARACTER);
3286 }
3287 content_element = null;
3288 }//for loop ends
3289
3290 String temp_str = index_value.toString ();
3291 index_ele.setAttribute (StaticStrings.NAME_ATTRIBUTE, temp_str);
3292
3293 // Now constructing 'displayItem' element for this 'index_ele' element
3294 // The index names in the collectionmetadata elements in the internal structure are not the names that
3295 // are used in the content elements (i.e. ex.Source or dc.Subject and keywords), but the names that are
3296 // in the configuration files (i.e. Source or dc.Subject)
3297 ArrayList collectionmetadata_list = XMLTools.getNamedElementList (source,
3298 StaticStrings.COLLECTIONMETADATA_ELEMENT,
3299 StaticStrings.NAME_ATTRIBUTE, temp_str);
3300
3301 if (collectionmetadata_list == null) {
3302 //try the full name, i.e. with 'ex.'
3303 if (mg_indexer == true) {
3304 // but first append level info if we are mg
3305 full_index_name = level_str+StaticStrings.COLON_CHARACTER+full_index_name;
3306 }
3307 collectionmetadata_list = XMLTools.getNamedElementList (source,
3308 StaticStrings.COLLECTIONMETADATA_ELEMENT,
3309 StaticStrings.NAME_ATTRIBUTE, full_index_name);
3310 }
3311
3312 if (collectionmetadata_list != null) {
3313
3314 for(int k = 0; k < collectionmetadata_list.size (); k++) {
3315 Element collectionmetadata = (Element)collectionmetadata_list.get (k);
3316 if (collectionmetadata.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3317 continue;
3318 }
3319 Element displayItem = constructDisplayItem (collectionmetadata, to);
3320
3321 index_ele.appendChild (displayItem);
3322 }
3323 }
3324
3325 search.appendChild (index_ele);
3326
3327 } //for loop ends
3328
3329 //Convert default index
3330 convertDefaultIndex (from, to, search);
3331 convertIndexOptions(from, to, search);
3332 }
3333 // Convert levels for mgpp/lucene. This method is called by converIndex() when mgpp indexer is detected.
3334 private void convertLevels (Document from, Document to, Element search) {
3335 Element source = from.getDocumentElement ();
3336 Element index_option = XMLTools.getNamedElement (source,
3337 StaticStrings.INDEXOPTIONS_ELEMENT,
3338 StaticStrings.NAME_ATTRIBUTE, StaticStrings.LEVELS_STR);
3339 if (index_option == null) {
3340 return;
3341 }
3342 //Debugging purposes
3343 if (index_option.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3344 DebugStream.println ("For mgpp, there should be an IndexOption element for levels which is assigned 'true': possible bug.");
3345 }
3346
3347 NodeList option_elements = index_option.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
3348 int num_elements = option_elements.getLength ();
3349
3350 // Don't output anything if no indexes are set
3351 if(num_elements == 0) {
3352 return ;//
3353 }
3354
3355 for(int k = 0; k < num_elements; k++) {
3356 Element e = (Element) option_elements.item (k);
3357 String name_str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
3358 Element level_element = to.createElement (StaticStrings.LEVEL_ELEMENT);
3359 level_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
3360
3361 //Now construct displayItem for this level element from collectionmetadata
3362 ArrayList collectionmetadata_list = XMLTools.getNamedElementList (source,
3363 StaticStrings.COLLECTIONMETADATA_ELEMENT,
3364 StaticStrings.NAME_ATTRIBUTE, name_str);
3365
3366 if (collectionmetadata_list != null) {
3367
3368 for(int j = 0; j < collectionmetadata_list.size (); j++) {
3369 Element collectionmetadata = (Element)collectionmetadata_list.get (j);
3370
3371 Element displayItem = constructDisplayItem (collectionmetadata, to);
3372 level_element.appendChild (displayItem);
3373 }
3374 }
3375 search.appendChild (level_element);
3376 }
3377
3378 //Convert default level
3379 Element default_index_option = XMLTools.getNamedElement (source,
3380 StaticStrings.INDEXOPTION_DEFAULT_ELEMENT,
3381 StaticStrings.NAME_ATTRIBUTE, StaticStrings.LEVEL_DEFAULT_STR);
3382 if (default_index_option == null) {
3383 return;
3384 }
3385 Element default_level = to.createElement (StaticStrings.LEVEL_DEFAULT_ELEMENT);
3386 String default_level_str = default_index_option.getAttribute (StaticStrings.VALUE_ATTRIBUTE);
3387 default_level.setAttribute (StaticStrings.NAME_ATTRIBUTE, default_level_str);
3388 search.appendChild (default_level);
3389
3390 }
3391 // Convert indexoptions for mg/mgpp/lucene. This method is called by convertIndex().
3392 private void convertIndexOptions (Document from, Document to, Element search) {
3393 Element source = from.getDocumentElement ();
3394 Element index_option = XMLTools.getNamedElement (source,
3395 StaticStrings.INDEXOPTIONS_ELEMENT,
3396 StaticStrings.NAME_ATTRIBUTE, StaticStrings.INDEXOPTIONS_STR);
3397 if (index_option == null) {
3398 return;
3399 }
3400 //Debugging purposes
3401 if (index_option.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
3402 DebugStream.println ("There should be an IndexOption element which is assigned 'true': possible bug.");
3403 }
3404 Element indexOptionEl = to.createElement(StaticStrings.INDEXOPTION_STR);
3405 NodeList option_elements = index_option.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
3406 int num_elements = option_elements.getLength ();
3407 // Don't output anything if no index
3408 if(num_elements == 0) {
3409 return ;//
3410 }
3411 search.appendChild (indexOptionEl);
3412
3413 for(int k = 0; k < num_elements; k++) {
3414 Element e = (Element) option_elements.item (k);
3415 String name_att = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
3416 Element optionEl = to.createElement(StaticStrings.OPTION_STR);
3417 optionEl.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_att);
3418 indexOptionEl.appendChild(optionEl);
3419 }
3420
3421 }
3422 // Append the element son to the element mother in the appropriate position.
3423 static public void appendProperly (Element mother, Element son) {
3424 if (son == null)
3425 return;
3426
3427 Node reference_node = findInsertionPoint (mother, son);
3428 if(reference_node != null) {
3429 mother.insertBefore (son, reference_node);
3430 }
3431 else {
3432 mother.appendChild (son);
3433 }
3434 }
3435 /** Find the best insertion position for the given DOM Element 'target_element' in the DOM Element 'document_element'. This should try to match command tag, and if found should then try to group by name or type (eg CollectionMeta), or append to end is no such grouping exists (eg Plugins). Failing a command match it will check against the command order for the best insertion location.
3436 * @param target_element the command Element to be inserted
3437 * @return the Element which the given command should be inserted before, or null to append to end of list
3438 */
3439 static public Node findInsertionPoint (Element document_element, Element target_element) {
3440 ///ystem.err.println("Find insertion point: " + target_element.getNodeName());
3441 String target_element_name = target_element.getNodeName ();
3442
3443 // Try to find commands with the same tag.
3444 NodeList matching_elements = document_element.getElementsByTagName (target_element_name);
3445 // If we found matching elements, then we have our most likely insertion location, so check within for groupings
3446 if(matching_elements.getLength () != 0) {
3447 ///ystem.err.println("Found matching elements.");
3448 // Only CollectionMeta are grouped.
3449 if(target_element_name.equals (StaticStrings.COLLECTIONMETADATA_ELEMENT)) {
3450 ///ystem.err.println("Dealing with collection metadata");
3451 // Special case: CollectionMeta can be added at either the start or end of a collection configuration file. However the start position is reserved for special metadata, so if no non-special metadata can be found we must append to the end.
3452 // So if the command to be added is special add it immediately after any other special command
3453 if(target_element.getAttribute (StaticStrings.SPECIAL_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
3454 int index = 0;
3455 Element matched_element = (Element) matching_elements.item (index);
3456 Element sibling_element = (Element) matched_element.getNextSibling ();
3457 while(sibling_element.getAttribute (StaticStrings.SPECIAL_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
3458 index++;
3459 matched_element = (Element) matching_elements.item (index);
3460 sibling_element = (Element) matched_element.getNextSibling ();
3461 }
3462 if(sibling_element.getNodeName ().equals (NEWLINE_ELEMENT)) {
3463 Element newline_element = document.createElement (NEWLINE_ELEMENT);
3464 document_element.insertBefore (newline_element, sibling_element);
3465 }
3466 return sibling_element;
3467 }
3468 // Otherwise try to find a matching 'name' and add after the last one in that group.
3469 else {
3470 int index = 0;
3471 target_element_name = target_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
3472 boolean found = false;
3473 // Skip all of the special metadata
3474 Element matched_element = (Element) matching_elements.item (index);
3475 while(matched_element.getAttribute (StaticStrings.SPECIAL_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
3476 index++;
3477 matched_element = (Element) matching_elements.item (index);
3478 }
3479 // Begin search
3480 while(!found && matched_element != null) {
3481 if(matched_element.getAttribute (StaticStrings.NAME_ATTRIBUTE).equals (target_element_name)) {
3482 found = true;
3483 }
3484 else {
3485 index++;
3486 matched_element = (Element) matching_elements.item (index);
3487 }
3488 }
3489 // If we found a match, we need to continue checking until we find the last name match.
3490 if(found) {
3491 index++;
3492 Element previous_sibling = matched_element;
3493 Element sibling_element = (Element) matching_elements.item (index);
3494 while(sibling_element != null && sibling_element.getAttribute (StaticStrings.NAME_ATTRIBUTE).equals (target_element_name)) {
3495 previous_sibling = sibling_element;
3496 index++;
3497 sibling_element = (Element) matching_elements.item (index);
3498 }
3499 // Previous sibling now holds the command immediately before where we want to add, so find its next sibling and add to that. In this one case we can ignore new lines!
3500 return previous_sibling.getNextSibling ();
3501 }
3502 // If not found we just add after last metadata element
3503 else {
3504 Element last_element = (Element) matching_elements.item (matching_elements.getLength () - 1);
3505 return last_element.getNextSibling ();
3506 }
3507 }
3508
3509 }
3510 else {
3511 ///ystem.err.println("Not dealing with collection meta.");
3512 Element matched_element = (Element) matching_elements.item (matching_elements.getLength () - 1);
3513 // One final quick test. If the matched element is immediately followed by a NewLine command, then we insert another NewLine after the matched command, then return the NewLine instead (thus the about to be inserted command will be placed between the two NewLines)
3514 Node sibling_element = matched_element.getNextSibling ();
3515 if(sibling_element != null && sibling_element.getNodeName ().equals (NEWLINE_ELEMENT)) {
3516 Element newline_element = document.createElement (NEWLINE_ELEMENT);
3517 document_element.insertBefore (newline_element, sibling_element);
3518 }
3519 return sibling_element; // Note that this may be null
3520 }
3521 }
3522 ///ystem.err.println("No matching elements found.");
3523 // Locate where this command is in the ordering
3524 int command_index = -1;
3525 for(int i = 0; command_index == -1 && i < COMMAND_ORDER.length; i++) {
3526 if(COMMAND_ORDER[i].equals (target_element_name)) {
3527 command_index = i;
3528 }
3529 }
3530 ///ystem.err.println("Command index is: " + command_index);
3531 // Now move forward, checking for existing elements in each of the preceeding command orders.
3532 int preceeding_index = command_index - 1;
3533 ///ystem.err.println("Searching before the target command.");
3534 while(preceeding_index >= 0) {
3535 matching_elements = document_element.getElementsByTagName (COMMAND_ORDER[preceeding_index]);
3536 // If we've found a match
3537 if(matching_elements.getLength () > 0) {
3538 // We add after the last element
3539 Element matched_element = (Element) matching_elements.item (matching_elements.getLength () - 1);
3540 // One final quick test. If the matched element is immediately followed by a NewLine command, then we insert another NewLine after the matched command, then return the NewLine instead (thus the about to be inserted command will be placed between the two NewLines)
3541 Node sibling_element = matched_element.getNextSibling ();
3542 if(sibling_element != null && sibling_element.getNodeName ().equals (NEWLINE_ELEMENT)) {
3543 Element newline_element = document.createElement (NEWLINE_ELEMENT);
3544 document_element.insertBefore (newline_element, sibling_element);
3545 }
3546 return sibling_element; // Note that this may be null
3547 }
3548 preceeding_index--;
3549 }
3550 // If all that fails, we now move backwards through the commands
3551 int susceeding_index = command_index + 1;
3552 ///ystem.err.println("Searching after the target command.");
3553 while(susceeding_index < COMMAND_ORDER.length) {
3554 matching_elements = document_element.getElementsByTagName (COMMAND_ORDER[susceeding_index]);
3555 // If we've found a match
3556 if(matching_elements.getLength () > 0) {
3557 // We add before the first element
3558 Element matched_element = (Element) matching_elements.item (0);
3559 // One final quick test. If the matched element is immediately preceeded by a NewLine command, then we insert another NewLine before the matched command, then return this new NewLine instead (thus the about to be inserted command will be placed between the two NewLines)
3560 Node sibling_element = matched_element.getPreviousSibling ();
3561 if(sibling_element != null && sibling_element.getNodeName ().equals (NEWLINE_ELEMENT)) {
3562 Element newline_element = document.createElement (NEWLINE_ELEMENT);
3563 document_element.insertBefore (newline_element, sibling_element);
3564 }
3565 return sibling_element; // Note that this may be null
3566 }
3567 susceeding_index++;
3568 }
3569 // Well. Apparently there are no other commands in this collection configuration. So append away...
3570 return null;
3571 }
3572 // From collectionConfig.xml to internal structure:add 'ex.' namespace (if none).
3573 // From internal structure to collectionConfig.xml:always peel off 'ex.' namespace (if any), except for format statement
3574 //This method parses 'xml_file_doc' into 'dOc'
3575 public void parseCollectionConfigXML (File xml_file, Document dOc) {
3576
3577 Document xml_file_doc = XMLTools.parseXMLFile (xml_file);
3578 Element fromElement = xml_file_doc.getDocumentElement ();
3579 Element toElement = dOc.getDocumentElement ();
3580
3581 // It's deliberately set that 'creator', 'maintainer', and 'public' are only in English (as they are just names).
3582 // So the following ArrayList have only one element.
3583 Node metadataListNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.METADATALIST_STR, 0);
3584 if (metadataListNode != null) {
3585 ArrayList creator = doMetadataList (dOc, metadataListNode, StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT,
3586 StaticStrings.COLLECTIONMETADATA_CREATOR_STR);
3587 ArrayList maintainer = doMetadataList (dOc, metadataListNode,
3588 StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT,
3589 StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR);
3590 ArrayList is_public = doMetadataList (dOc, metadataListNode, StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT,
3591 StaticStrings.COLLECTIONMETADATA_PUBLIC_STR);
3592
3593 appendArrayList (toElement, creator);
3594 appendArrayList (toElement, maintainer);
3595 appendArrayList (toElement, is_public);
3596 }
3597
3598 Node searchNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.SEARCH_STR, 0);
3599 String buildtype_value = ((Element)searchNode).getAttribute (StaticStrings.TYPE_ATTRIBUTE);//might be mg|mgpp|lucene
3600 Element buildtype = doBuildType (dOc, buildtype_value);
3601 appendProperly (toElement, buildtype);
3602
3603
3604 Node importNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.IMPORT_STR, 0);
3605 if (importNode == null) {
3606 System.out.println ("There is no content in the 'import' block.");
3607 }
3608 if (importNode != null) {
3609 //do plugin list nodes
3610 Node pluginListNode = XMLTools.getChildByTagNameIndexed ((Element)importNode, StaticStrings.PLUGINLIST_STR, 0);
3611 if (pluginListNode == null) {
3612 System.out.println ("There is no pluginlist set.");
3613 }
3614 if (pluginListNode != null) {
3615
3616 doPlugin (dOc, pluginListNode);
3617 }
3618
3619 //do the plugout element (used by building flax collections)
3620 Node plugout = XMLTools.getChildByTagNameIndexed ((Element)importNode, PLUGOUT_ELEMENT, 0);
3621 if (plugout != null) {
3622 Element to_element = XMLTools.duplicateElement (dOc, (Element)plugout, true);
3623 toElement.appendChild (to_element);
3624 }
3625 }
3626
3627 Node browseNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.BROWSE_STR, 0);
3628 if (browseNode != null) {
3629 if (browseNode == null) {
3630 System.out.println ("There is no classifier.");
3631 }
3632 doClassifier (dOc, browseNode);
3633 }
3634
3635 Node displayItemListNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.DISPLAYITEMLIST_STR, 0);
3636 if (displayItemListNode != null) {
3637 ArrayList description = doDisplayItemList (dOc, displayItemListNode, StaticStrings.DESCRIPTION_STR,
3638 StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
3639 ArrayList smallicon = doDisplayItemList (dOc, displayItemListNode, StaticStrings.SMALLICON_STR,
3640 StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR);
3641 ArrayList icon = doDisplayItemList (dOc, displayItemListNode, StaticStrings.ICON_STR,
3642 StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR);
3643 ArrayList name = doDisplayItemList (dOc, displayItemListNode, StaticStrings.NAME_STR,
3644 StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
3645
3646 appendArrayList (toElement, description);
3647 appendArrayList (toElement, smallicon);
3648 appendArrayList (toElement, icon);
3649 appendArrayList (toElement, name);
3650 }
3651
3652 if (buildtype_value.equalsIgnoreCase ("mg")) {
3653 doMGIndexes (dOc, searchNode);
3654 }
3655 else {
3656 doMGPPIndexes (dOc, searchNode);
3657 }
3658
3659 doDefaultIndex (dOc, searchNode);
3660 //doSearchType (dOc, searchNode);
3661 doDefaultLevel (dOc, searchNode);
3662 doLevel (dOc, searchNode);
3663 doIndexOption (dOc, searchNode);
3664 doSubcollection (dOc, searchNode);
3665 doIndexSubcollection (dOc, searchNode);
3666 doIndexLanguage (dOc, searchNode);
3667 doDefaultIndexLanguage (dOc, searchNode);
3668 doLanguageMetadata (dOc, searchNode);
3669 doSearchFormat (dOc, searchNode);
3670 doDisplayFormat (dOc, fromElement);
3671 doReplaceListRef (dOc, fromElement);
3672 doOAIElement (dOc, fromElement);
3673
3674 }
3675
3676
3677 public void saveCollectionConfigXML (File collect_cfg_file, Document doc) {
3678 //In this method, the file collect_cfg_file must be 'collectionConfig.xml'
3679
3680
3681 //Compare the internal structure (doc) with the saved structure from collectionConfig.xml and see if it has changed
3682 StringBuffer collect_cfg_string_buffer = new StringBuffer (XMLTools.xmlNodeToString (doc));
3683 String collect_cfg_string = collect_cfg_string_buffer.toString ();
3684 if (saved_collect_cfg_string_buffer != null) {
3685 String saved_collect_cfg_string = saved_collect_cfg_string_buffer.toString ();
3686 if (collect_cfg_string.equals (saved_collect_cfg_string)) {
3687 DebugStream.println ("'collectionConfig.xml' file hasn't changed so no save necessary...");
3688 return;
3689 }
3690 }
3691
3692 DebugStream.println ("'collectionConfig.xml' file has changed, saving now...");
3693
3694 // If we're using the Local Library we must release the collection before writing to the collect.cfg file
3695 String collection_name = Gatherer.c_man.getCollection ().getName ();
3696 boolean collection_released = false;
3697 if (Gatherer.c_man.built () && LocalLibraryServer.isRunning () == true) {
3698 // Release the collection
3699 LocalLibraryServer.releaseCollection (collection_name);
3700 collection_released = true;
3701 }
3702
3703 // Make a backup of the collectionConfig.xml file so that the user can manully change back
3704 if (collect_cfg_file.exists ()) {
3705
3706 File original_file = new File (collect_cfg_file.getParentFile (), Utility.COLLECTION_CONFIG_XML);
3707 File backup_file = new File (collect_cfg_file.getParentFile (), Utility.COLLECTION_CONFIG_BAK);
3708 if (backup_file.exists ()) {
3709 backup_file.delete ();
3710 }
3711 if (!original_file.renameTo (backup_file)) {
3712 System.err.println ("Warning: can't rename collectionConfig.xml to collectionConfig.bak.");
3713 }
3714 }
3715
3716 Document collection_config_xml_document = convertInternalToCollectionConfig (doc);
3717 String[] nonEscapingTagNames = {StaticStrings.FORMAT_STR};
3718 XMLTools.writeXMLFile (collect_cfg_file, collection_config_xml_document, nonEscapingTagNames);
3719 saved_collect_cfg_string_buffer = collect_cfg_string_buffer;
3720
3721 // If we're using a remote Greenstone server, upload the new 'collectionConfig.xml' file
3722 if (Gatherer.isGsdlRemote) {
3723 Gatherer.remoteGreenstoneServer.uploadCollectionFile (collection_name, collect_cfg_file);
3724 }
3725
3726 // Now re-add the collection to the Local Library server
3727 if (collection_released) {
3728 LocalLibraryServer.addCollection (collection_name);
3729 }
3730
3731 }
3732 public void saveIfNecessary () {
3733 if (Gatherer.GS3 == true) {
3734 saveCollectionConfigXML (collect_cfg_file, document);
3735 return;
3736 }
3737 // Convert the collection configuration XML tree to the collect.cfg version
3738 StringBuffer collect_cfg_string_buffer = new StringBuffer ();
3739 NodeList command_elements = document.getDocumentElement ().getChildNodes ();
3740 boolean just_wrote_blank_line = false; // Prevent two or more blank lines in a row
3741 for (int i = 0; i < command_elements.getLength (); i++) {
3742 Node command_node = command_elements.item (i);
3743 if (!(command_node instanceof Element)) {
3744 // We're only interested in Elements
3745 continue;
3746 }
3747 Element command_element = (Element) command_node;
3748
3749 // Handle NewLine elements (blank lines)
3750 if (command_element.getNodeName ().equals (NEWLINE_ELEMENT) && !just_wrote_blank_line) {
3751 collect_cfg_string_buffer.append ("\n");
3752 just_wrote_blank_line = true;
3753 }
3754
3755 // Anything else we write to file, but only if it has been assigned, except for index and level commands
3756 // (which just get commented out if unassigned -- a side effect of MG & MGPP compatibility)
3757 else if (!command_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR) || command_element.getNodeName ().equals (StaticStrings.INDEXES_ELEMENT) || command_element.getNodeName ().equals (StaticStrings.INDEX_DEFAULT_ELEMENT) || command_element.getNodeName ().equals (StaticStrings.INDEXOPTIONS_ELEMENT) || command_element.getNodeName ().equals (StaticStrings.INDEXOPTION_DEFAULT_ELEMENT)) {
3758 String command;
3759 if (command_element.getNodeName ().equals (StaticStrings.FORMAT_ELEMENT)) {
3760 // Format statements we write out with ex. still present
3761 command = toString (command_element, true);
3762 }
3763 else {
3764 command = toString (command_element, false);
3765 }
3766
3767 if (command != null && command.length ()> 0 ) {
3768 collect_cfg_string_buffer.append (command + "\n");
3769 just_wrote_blank_line = false;
3770 }
3771 }
3772 }
3773
3774 String collect_cfg_string = collect_cfg_string_buffer.toString ();
3775 String saved_collect_cfg_string = saved_collect_cfg_string_buffer.toString ();
3776 if (collect_cfg_string.equals (saved_collect_cfg_string)) {
3777 DebugStream.println ("Collect.cfg file hasn't changed so no save necessary...");
3778 return;
3779 }
3780
3781 DebugStream.println ("Collect.cfg file has changed, saving now...");
3782
3783 // If we're using the Local Library we must release the collection before writing to the collect.cfg file
3784 String collection_name = CollectionManager.getLoadedCollectionName ();
3785 boolean collection_released = false;
3786 if (Gatherer.c_man.built () && LocalLibraryServer.isRunning () == true) {
3787 // Release the collection
3788 LocalLibraryServer.releaseCollection (collection_name);
3789 collection_released = true;
3790 }
3791
3792 // Make a backup of the collect.cfg file
3793 if (collect_cfg_file.exists ()) {
3794 File original_file = new File (collect_cfg_file.getParentFile (), StaticStrings.COLLECT_CFG);
3795 File backup_file = new File (collect_cfg_file.getParentFile (), Utility.COLLECT_BAK);
3796 if (backup_file.exists ()) {
3797 backup_file.delete ();
3798 }
3799 if (!original_file.renameTo (backup_file)) {
3800 System.err.println ("Warning: can't rename collect.cfg to collect.bak.");
3801 }
3802 }
3803
3804 try {
3805 OutputStream ostream = new FileOutputStream (collect_cfg_file);
3806 Writer file_writer = new OutputStreamWriter (ostream, ENCODING);
3807 BufferedWriter buffered_writer = new BufferedWriter (file_writer);
3808 buffered_writer.write (collect_cfg_string);
3809 buffered_writer.close ();
3810
3811 saved_collect_cfg_string_buffer = collect_cfg_string_buffer;
3812
3813 // If we're using a remote Greenstone server, upload the new collect.cfg file
3814 if (Gatherer.isGsdlRemote) {
3815 Gatherer.remoteGreenstoneServer.uploadCollectionFile (collection_name, collect_cfg_file);
3816 }
3817 }
3818 catch (Exception exception) {
3819 DebugStream.println ("Error in CollectionConfiguration.save(): " + exception);
3820 DebugStream.printStackTrace (exception);
3821 }
3822
3823 // Now re-add the collection to the Local Library server
3824 if (collection_released) {
3825 LocalLibraryServer.addCollection (collection_name);
3826 }
3827 }
3828 // This method is initilised in CollectionDesignManager.java constructor
3829 public CollectionConfiguration (File collect_cfg_file) {
3830 this.collect_cfg_file = collect_cfg_file;
3831
3832 // parse the XML template
3833 document = XMLTools.parseXMLFile ("xml/CollectionConfig.xml", true);
3834 String filename = collect_cfg_file.getName ().toLowerCase ();
3835
3836 if (filename.endsWith (".cfg")) {
3837 parse (collect_cfg_file);
3838 }
3839 if (filename.endsWith (".xml")) {
3840 parseCollectionConfigXML (collect_cfg_file, document);
3841 }
3842
3843 //XMLTools.printXMLNode(document.getDocumentElement());
3844 }
3845
3846 // Append the elements, which are of Element type, in 'list' to Element 'to'
3847 private void appendArrayList (Element to, ArrayList list) {
3848 if (list == null) return;
3849
3850 for (int i=0; i<list.size (); i++) {
3851 appendProperly (to, (Element)list.get (i));
3852 }
3853 }
3854 //Convert the internal XML DOM tree (dOc) into that of collectionConfig.xml (skeleton)
3855 public Document convertInternalToCollectionConfig (Document dOc) {
3856 //first parse an empty skeleton of xml config file
3857 //The aim is to convert the internal structure into this skeleton
3858 Document skeleton = XMLTools.parseXMLFile ("xml/CollectionConfig.xml", true);
3859 //Element internal = dOc.getDocumentElement();
3860 convertMetadataList (dOc, skeleton);
3861 convertDisplayItemList (dOc, skeleton);
3862 convertBuildType (dOc, skeleton);
3863 convertIndex (dOc, skeleton);
3864 convertPlugin (dOc, skeleton);//also do the plugout element
3865 convertClassifier (dOc, skeleton);
3866 convertSubcollectionIndexes (dOc, skeleton);
3867 convertLanguages (dOc, skeleton);
3868 convertSubcollection (dOc, skeleton);
3869 convertSearchType (dOc, skeleton);
3870 convertSearchFormat (dOc, skeleton);
3871 convertDisplayFormat (dOc, skeleton);
3872 convertReplaceListRef (dOc, skeleton);
3873 convertOAIElement(dOc, skeleton);
3874
3875 return skeleton;
3876 }
3877
3878 static private HashMap plugin_map = null;
3879
3880 private void setUpPluginNameMap() {
3881 plugin_map = new HashMap();
3882 plugin_map.put("GAPlug", "GreenstoneXMLPlugin");
3883 plugin_map.put("RecPlug", "DirectoryPlugin");
3884 plugin_map.put("ArcPlug","ArchivesInfPlugin");
3885 plugin_map.put("TEXTPlug","TextPlugin");
3886 plugin_map.put("XMLPlug","ReadXMLFile");
3887 plugin_map.put("EMAILPlug","EmailPlugin");
3888 plugin_map.put("SRCPlug","SourceCodePlugin");
3889 plugin_map.put("NULPlug","NulPlugin");
3890 plugin_map.put("W3ImgPlug","HTMLImagePlugin");
3891 plugin_map.put("PagedImgPlug","PagedImagePlugin");
3892 plugin_map.put("METSPlug", "GreenstoneMETSPlugin");
3893 plugin_map.put("DBPlug", "DatabasePlugin");
3894 plugin_map.put("PPTPlug", "PowerPointPlugin");
3895 plugin_map.put("PSPlug", "PostScriptPlugin");
3896 }
3897
3898 private String ensureNewPluginName(String plugin) {
3899 if (plugin.endsWith("Plugin")) return plugin;
3900 if (plugin_map == null) {
3901 setUpPluginNameMap();
3902 }
3903 String new_name = (String)plugin_map.get(plugin);
3904 if (new_name != null) return new_name;
3905 new_name = plugin.replaceAll("Plug", "Plugin");
3906 return new_name;
3907 }
3908
3909
3910 ///*********************************************************************************************************///
3911}
3912
Note: See TracBrowser for help on using the repository browser.