source: main/trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfigXMLReadWrite.java@ 24425

Last change on this file since 24425 was 24425, checked in by ak19, 13 years ago

Fixed the error whereby completely deleting a GS3 format statement from GLI (this issue was encountered in the format statement for Browse) caused a double up of format elements to be written out to the collectionConfig.xml (<format><format /></format>) which was both wrong and prevented GLI from reopening that collection.

File size: 96.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 * Methods to read collectionConfig.xml files into internal XML form, and write
9 * them back out again.
10 *
11 * Copyright (C) 1999 New Zealand Digital Library Project
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *########################################################################
27 */
28package org.greenstone.gatherer.cdm;
29
30import java.io.File;
31import java.util.ArrayList;
32import java.util.HashMap;
33import java.util.StringTokenizer;
34
35import org.greenstone.gatherer.DebugStream;
36import org.greenstone.gatherer.metadata.MetadataElement;
37import org.greenstone.gatherer.metadata.MetadataTools;
38import org.greenstone.gatherer.util.XMLTools;
39import org.greenstone.gatherer.util.StaticStrings;
40import org.greenstone.gatherer.util.Utility;
41
42import org.w3c.dom.*;
43
44public class CollectionConfigXMLReadWrite {
45
46 static final private String PLUGOUT_ELEMENT = "plugout";//used by building flax collections
47
48 /** ********************************************************************************************************
49 The code from this point below are used for greenstone 3 collection configuration, i.e., read ColletionConfig.xml
50 * into the internal DOM tree, and convert the internal DOM tree back to CollectionConfig.xml.
51 *
52 Methods named 'doXXXX' are for convert collectionConfig.xml into the internal configuration xml structure;
53 Methods named 'convertXXXX' are for convert the internal configuration xml structure back to collectionConfig.xml.
54 ************************************************************************************************************ */
55
56 /**Arguments: metadataListNode->the 'displayItemList' element in collectionConfig.xml
57 name_value->the value of the 'name' attribute of 'index' element;
58 att_value->the value of the 'name' attribute of 'displayItem' element
59 return: an ArrayList of the contructed 'CollectionMetadata' elements
60 */
61 static private ArrayList doDisplayItemList (Document to, Node displayListNode, String att_value, String name_value) {
62 Element toElement = to.getDocumentElement ();
63 ArrayList display_item_list = new ArrayList ();
64 ArrayList item_list = XMLTools.getNamedElementList ((Element)displayListNode,
65 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, att_value);
66 if (item_list == null) {
67 return null;
68 }
69
70 for (int i=0; i<item_list.size (); i++) {
71 Element item = (Element)item_list.get (i);
72 String text = XMLTools.getNodeText (item);
73
74 //If there is nothing to display, don't bother creating the element
75 if (text == "") {
76 continue;
77 }
78 //get the value in 'lang=value'
79 String lang = item.getAttribute (StaticStrings.LANG_STR);
80
81 Element e = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
82 e.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
83 e.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_value);
84 e.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
85 XMLTools.setNodeText (e, text);
86 display_item_list.add (e);
87 }
88 return display_item_list;
89 }
90
91 static private ArrayList doMetadataList (Document to, Node metadataListNode, String ele_name, String att_value) {
92 Element toElement = to.getDocumentElement ();
93 ArrayList metadata_list = new ArrayList ();
94
95 ArrayList item_list = XMLTools.getNamedElementList ((Element)metadataListNode,
96 StaticStrings.METADATA_STR, StaticStrings.NAME_ATTRIBUTE, att_value);
97 if (item_list == null) {
98 return null;
99 }
100
101 for (int i=0; i<item_list.size (); i++) {
102 Element item = (Element)item_list.get (i);
103 String text = XMLTools.getNodeText (item);
104
105 //If there is nothing to display, don't bother creating the element
106 if (text == "") {
107 continue;
108 }
109 //get the value in 'lang=value'
110 String lang = item.getAttribute (StaticStrings.LANG_STR);
111
112 Element element = to.createElement (ele_name);
113 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, att_value);
114 element.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
115 element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
116 element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
117 XMLTools.setNodeText (element, text);
118
119 metadata_list.add (element);
120 }
121 return metadata_list;
122 }
123 // 'to' is the internal structure
124 static private void doMGIndexes (Document to, Node searchNode) {
125 Element toElement = to.getDocumentElement ();
126 Element indexes_element = to.createElement (StaticStrings.INDEXES_ELEMENT);//<Indexes>
127 indexes_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
128 indexes_element.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.FALSE_STR);
129
130 NodeList index_children = ((Element)searchNode).getElementsByTagName (StaticStrings.INDEX_LOW_STR);//index
131 int num_nodes = index_children.getLength ();
132
133 for (int i=0; i<num_nodes; i++) {
134 Element e = (Element)index_children.item (i);
135 String index_str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
136 String index_str_display = index_str;//used for creating collectionmetadata for this index
137 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);//<Index>
138
139 // For mg, it's the 'Old G2.38 and earlier' that use level:source tuplets, but we double check it anyway
140 boolean old_index = true;
141 if(index_str.indexOf (StaticStrings.COLON_CHARACTER) == -1) {
142 // It doesn't contain ':' character
143 System.err.println ("Something is wrong! the index should be level:source tuplets.");
144 old_index = false;
145 }
146 else {
147 // Handling 'index' element
148 index_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE,
149 index_str.substring (0, index_str.indexOf (StaticStrings.COLON_CHARACTER)));
150 index_str = index_str.substring (index_str.indexOf (StaticStrings.COLON_CHARACTER) + 1);
151
152 //Each index may have a list of comma-separated strings.
153 //split them into 'content' elements in the internal structure
154 StringTokenizer content_tokenizer = new StringTokenizer (index_str, StaticStrings.COMMA_CHARACTER);
155 //index_str = "";
156 while(content_tokenizer.hasMoreTokens ()) {
157 // Replace index_str to be qualified name, eg. dc.Subject and keywords insread of dc.Subject.
158
159
160 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
161 String content_str = content_tokenizer.nextToken ();
162 // Since the contents of indexes have to be certain keywords, or metadata elements,
163 //if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
164 if(content_str.indexOf (StaticStrings.NS_SEP) == -1) {
165 if(content_str.equals (StaticStrings.TEXT_STR) ||
166 (!old_index && content_str.equals (StaticStrings.ALLFIELDS_STR))) {
167 // in this case, do nothing
168 }
169 else {
170 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
171 }
172 }
173
174 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_str);
175 index_element.appendChild (content_element);
176 content_element = null;
177 } // while ends
178
179 indexes_element.appendChild (index_element);
180
181 // Handling 'displayItem' elements and Constructing 'collectionmetadata' elements
182 // Use the fully qualified index names
183 ArrayList collectionmetadata_list = doDisplayItemList (to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
184 appendArrayList (toElement, collectionmetadata_list);
185 } //else ends
186 } //for loop ends
187 appendProperly (toElement, indexes_element);
188
189 //***//
190 // create another set of <indexes> which will be used when user switches to MGPP/LUCENE
191 // i.e. we build a default index set for a start
192
193 String []index_strs =
194 {StaticStrings.TEXT_STR,
195 StaticStrings.EXTRACTED_NAMESPACE + StaticStrings.TITLE_ELEMENT,
196 StaticStrings.EXTRACTED_NAMESPACE + StaticStrings.SOURCE_ELEMENT};
197
198 Element mgpp_indexes = to.createElement (StaticStrings.INDEXES_ELEMENT);//<Indexes>
199 mgpp_indexes.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
200 mgpp_indexes.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
201 for (int i=0; i<index_strs.length; i++) {
202 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);//<Index>
203 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
204 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_strs[i]);
205 index_element.appendChild (content_element);
206 mgpp_indexes.appendChild (index_element);
207
208 // Contructing 'collectionmetadata' elements for 'mgpp' indexes
209 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
210 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
211 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_strs[i]);
212 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
213 if (index_strs[i].indexOf (StaticStrings.NS_SEP) != -1) {
214 index_strs[i] = index_strs[i].substring (index_strs[i].indexOf (StaticStrings.NS_SEP) + 1);
215 }
216 XMLTools.setNodeText (collectionmetadata, index_strs[i]);
217
218 appendProperly (toElement, collectionmetadata);
219
220 }
221 appendProperly (toElement, mgpp_indexes);
222 }
223
224 //This is actually doing indexes for both mgpp and lucene
225 static private void doMGPPIndexes (Document to, Node searchNode) {
226 Element toElement = to.getDocumentElement ();
227 Element indexes_element = to.createElement (StaticStrings.INDEXES_ELEMENT);//<Indexes>
228 indexes_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
229 indexes_element.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.TRUE_STR);
230
231 NodeList index_children = ((Element)searchNode).getElementsByTagName (StaticStrings.INDEX_LOW_STR);//index
232 int num_nodes = index_children.getLength ();
233
234 for (int i=0; i<num_nodes; i++) {
235
236 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);//<Index>
237 Element e = (Element)index_children.item (i);
238 String index_str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
239 String index_str_display = index_str;//for creating collectionmetadata for this index
240
241 // Handling 'index' element
242 // Double check to make sure it's not colon separated style index.
243 boolean old_index = false;
244 if(index_str.indexOf (StaticStrings.COLON_CHARACTER) != -1) {
245 System.err.println ("Something is wrong! the index should NOT be level:source tuplets style.");
246 old_index = true;
247 }
248 //Each index may have a list of comma-separated strings.
249 //split them into 'content' elements in the internal structure
250 StringTokenizer content_tokenizer = new StringTokenizer (index_str, StaticStrings.COMMA_CHARACTER);
251 //index_str = "";
252 while(content_tokenizer.hasMoreTokens ()) {
253 // Replace index_str to be qualified name, eg. dc.Subject and keywords insread of dc.Subject.
254
255 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
256 String content_str = content_tokenizer.nextToken ();
257 // 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.
258 if(content_str.indexOf (StaticStrings.NS_SEP) == -1) {
259 if(content_str.equals (StaticStrings.TEXT_STR)) {
260 // in this case, do nothing
261 }
262 else {
263 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
264 }
265 }
266 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_str);
267 index_element.appendChild (content_element);
268 content_element = null;
269 } //while ends
270
271 indexes_element.appendChild (index_element);
272
273
274 index_element = null;
275
276 // Handling 'displayItem' element of this 'index' element
277 // 'e' is the parent element 'index' of 'displayItem' element
278 ArrayList collectionmetadata_list = doDisplayItemList (to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
279 appendArrayList (toElement, collectionmetadata_list);
280
281
282 } // for loop ends
283 toElement.appendChild (indexes_element);
284
285 // create another set of <indexes> which will be used when user switches to MG
286 // i.e. we build a default index set for a start
287 Element mg_indexes = to.createElement (StaticStrings.INDEXES_ELEMENT);//<Indexes>
288 mg_indexes.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
289 mg_indexes.setAttribute (StaticStrings.MGPP_ATTRIBUTE, StaticStrings.FALSE_STR);
290
291 //put the namespace '.ex' as prefix to the indexes
292 String []index_strs =
293 {StaticStrings.TEXT_STR,
294 StaticStrings.EXTRACTED_NAMESPACE + StaticStrings.TITLE_ELEMENT,
295 StaticStrings.EXTRACTED_NAMESPACE + StaticStrings.SOURCE_ELEMENT};
296 for (int i=0; i<index_strs.length; i++) {
297 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);//<Index>
298 index_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE, StaticStrings.DOCUMENT_STR);
299 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
300 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_strs[i]);
301 index_element.appendChild (content_element);
302
303 mg_indexes.appendChild (index_element);
304
305 // Contructing 'collectionmetadata' elements for 'mg' indexes
306 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
307 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
308 String temp = StaticStrings.DOCUMENT_STR.concat (StaticStrings.COLON_CHARACTER).concat (index_strs[i]);
309 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, temp);
310 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
311 if (index_strs[i].indexOf (StaticStrings.NS_SEP) != -1) {
312 index_strs[i] = index_strs[i].substring (index_strs[i].indexOf (StaticStrings.NS_SEP) + 1);
313 }
314 XMLTools.setNodeText (collectionmetadata, index_strs[i]);
315
316 appendProperly (toElement, collectionmetadata);
317
318 }
319 toElement.appendChild (mg_indexes);
320
321 }
322
323 static private void doDisplayFormat (Document to, Element from) {
324 //display element in the xml file
325 Element de = (Element)XMLTools.getChildByTagName (from, StaticStrings.DISPLAY_STR);
326 if (de == null) {
327 return;
328 }
329 //format element in the display element
330 Element fe = (Element)XMLTools.getChildByTagName (de, StaticStrings.FORMAT_STR);
331
332 to.getDocumentElement ().appendChild (doFormat(to, fe, StaticStrings.DISPLAY_STR));
333 }
334
335 //construct 'DefaultIndex' element in the internal structure from collectionConfig.xml
336 static private void doDefaultIndex (Document to, Node searchNode) {
337 Element toElement = to.getDocumentElement ();
338 Element default_index_element = to.createElement (StaticStrings.INDEX_DEFAULT_ELEMENT);
339 default_index_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
340
341 Element e = (Element)XMLTools.getChildByTagName (searchNode, StaticStrings.INDEX_DEFAULT_ELEMENT_LOWERCASE);//defaultIndex
342 if (e == null) {
343 return;
344 }
345 String index_str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
346
347 boolean old_index = false;
348 if(index_str.indexOf (StaticStrings.COLON_CHARACTER) != -1) {
349 //The index is 'level:source tuplets' which is for mg. Take out 'level'
350 old_index = true;
351 default_index_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE,
352 index_str.substring (0, index_str.indexOf (StaticStrings.COLON_CHARACTER)));
353 index_str = index_str.substring (index_str.indexOf (StaticStrings.COLON_CHARACTER) + 1);
354 } else {
355 default_index_element.setAttribute (StaticStrings.LEVEL_ATTRIBUTE, "");
356 }
357
358 //Each index may have a list of comma-separated strings.
359 //split them into 'content' elements in the internal structure
360 StringTokenizer content_tokenizer = new StringTokenizer (index_str, StaticStrings.COMMA_CHARACTER);
361 while(content_tokenizer.hasMoreTokens ()) {
362 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
363 String content_str = content_tokenizer.nextToken ();
364 // 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.
365 if(content_str.indexOf (StaticStrings.NS_SEP) == -1) {
366 if(content_str.equals (StaticStrings.TEXT_STR) ||
367 (!old_index && content_str.equals (StaticStrings.ALLFIELDS_STR))) {
368 // in this case, do nothing
369 }
370 else {
371 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
372 }
373 }
374
375 content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_str);
376 default_index_element.appendChild (content_element);
377 content_element = null;
378 }
379 appendProperly (toElement, default_index_element);
380 }
381 // For mg, this method is still called, but make it 'assigned=false'
382 static private void doDefaultLevel (Document to, Node searchNode) {
383 Element toElement = to.getDocumentElement ();
384 Element default_index_option = to.createElement (StaticStrings.INDEXOPTION_DEFAULT_ELEMENT);
385 default_index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.LEVEL_DEFAULT_STR);
386
387 Element e = (Element)XMLTools.getChildByTagName (searchNode, StaticStrings.LEVEL_DEFAULT_ELEMENT);
388 if (e != null) {
389 default_index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
390 String level = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
391 default_index_option.setAttribute (StaticStrings.VALUE_ATTRIBUTE, level);
392 } else {
393 //In the case of mg, there's no level! build a default one using 'assigned=false value=document'
394 default_index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
395 default_index_option.setAttribute (StaticStrings.VALUE_ATTRIBUTE, StaticStrings.DOCUMENT_STR);
396 }
397 appendProperly (toElement, default_index_option);
398 }
399
400 // Transform plugins (pluginListNode) of collectionConfig.xml into the internal structure (i.e. Document to)
401 static private void doPlugin (Document to, Node pluginListNode) {
402 Element toElement = to.getDocumentElement ();
403 NodeList plugin_children = ((Element)pluginListNode).getElementsByTagName (StaticStrings.PLUGIN_STR);
404 int plugin_nodes = plugin_children.getLength ();
405
406 if (plugin_nodes < 1) {
407 return;
408 }
409
410 for (int i=0; i<plugin_nodes; i++) {
411 Element e = (Element)plugin_children.item (i);
412 String str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
413 str = Utility.ensureNewPluginName(str);
414 Element plugin_element = to.createElement (StaticStrings.PLUGIN_ELEMENT);
415 plugin_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, str);
416
417
418 NodeList option_children = e.getElementsByTagName (StaticStrings.OPTION_STR);
419
420 for (int j=0; j<option_children.getLength (); j++) {
421 Element el = (Element)option_children.item (j);
422 String name_str = el.getAttribute (StaticStrings.NAME_ATTRIBUTE);
423 if (name_str.startsWith (StaticStrings.MINUS_CHARACTER)) {
424 name_str = name_str.substring (1);
425 }
426 String value_str = el.getAttribute (StaticStrings.VALUE_ATTRIBUTE);
427 Element option_element = null;
428
429 if (name_str.equals ("") && !value_str.equals ("")) {
430 continue;
431 }
432
433
434 option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
435 option_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
436 if(name_str.equals (StaticStrings.RECPLUG_STR) && value_str.equals (StaticStrings.USE_METADATA_FILES_ARGUMENT)) {
437 continue; // ignore this option
438 }
439
440 if(value_str != null) {
441 // Remove any speech marks appended in strings containing whitespace
442 if(value_str.startsWith (StaticStrings.SPEECH_CHARACTER) && value_str.endsWith (StaticStrings.SPEECH_CHARACTER)) {
443 value_str = value_str.substring (1, value_str.length () - 1);
444 }
445 if(name_str.equals (StaticStrings.METADATA_STR)) {
446 // 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.
447 String[] values = value_str.split (StaticStrings.COMMA_CHARACTER);
448 value_str = "";
449 for (int k=0; k<=values.length-1; k++) {
450 if (values[k].indexOf (StaticStrings.NS_SEP) == -1) {
451 values[k] = StaticStrings.EXTRACTED_NAMESPACE + values[k];
452 }
453
454 if (k < values.length-1) {
455 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
456
457 } else {
458 value_str = value_str + values[k];
459 }
460 }
461 }
462 }
463 if (!name_str.equals ("")) {
464 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
465 }
466 if (!value_str.equals ("")) {
467 XMLTools.setNodeText (option_element, value_str);
468 }
469 plugin_element.appendChild (option_element);
470
471 }
472
473 appendProperly (toElement, plugin_element);
474 }
475
476 }
477
478 //Handle classifiers
479 static private void doClassifier (Document to, Node browseNode) {
480 Element toElement = to.getDocumentElement ();
481 NodeList classifier_children = ((Element)browseNode).getElementsByTagName (StaticStrings.CLASSIFIER_STR);
482 int num_nodes = classifier_children.getLength ();
483
484 if (num_nodes < 1) {
485 return;
486 }
487
488 for (int i=0; i<num_nodes; i++) {
489 Element e = (Element)classifier_children.item (i);
490 String str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
491 Element classify_element = to.createElement (StaticStrings.CLASSIFY_ELEMENT);
492 classify_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, str);
493
494 String options_str = "";
495 NodeList option_children = e.getElementsByTagName (StaticStrings.OPTION_STR);
496 for (int j=0; j<option_children.getLength (); j++) {
497 Element el = (Element)option_children.item (j);
498 String name_str = el.getAttribute (StaticStrings.NAME_ATTRIBUTE);
499 options_str = options_str + ((name_str.equals(""))? "" : (" " + name_str));
500 if (name_str.startsWith (StaticStrings.MINUS_CHARACTER)) {
501 name_str = name_str.substring (1);
502 }
503 String value_str = el.getAttribute (StaticStrings.VALUE_ATTRIBUTE);
504 options_str = options_str + ((name_str.equals(""))? "" : (" " + value_str));
505 Element option_element = null;
506
507 if (name_str.equals ("") && !value_str.equals ("")) {
508 continue; //invalid condition
509 }
510
511 option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
512 option_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
513
514 if (!name_str.equals ("")) {
515 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
516 }
517
518 if (!value_str.equals ("") && name_str.equals (StaticStrings.METADATA_STR)) {
519 // 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.
520 String[] values = value_str.split (StaticStrings.COMMA_CHARACTER);
521 value_str = "";
522 for (int k=0; k<=values.length-1; k++) {
523 if (values[k].indexOf (StaticStrings.NS_SEP) == -1) {
524 values[k] = StaticStrings.EXTRACTED_NAMESPACE + values[k];
525 }
526 else {
527 MetadataElement metadata_element = MetadataTools.getMetadataElementWithName (values[k]);
528 if (metadata_element != null) {
529 values[k] = metadata_element.getDisplayName ();
530 }
531 }
532 if (k < values.length-1) {
533 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
534 } else {
535 value_str = value_str + values[k];
536 }
537 }
538 }
539
540 if (value_str != null && !value_str.equals ("")) {
541 XMLTools.setNodeText (option_element, value_str);
542 }
543 classify_element.appendChild (option_element);
544 }
545 //format element for this classifier
546 Element format = (Element)XMLTools.getChildByTagName (e, StaticStrings.FORMAT_STR);
547 if(format != null) {
548 classify_element.appendChild (doFormat(to, format, null));
549 }
550 appendProperly (toElement, classify_element);
551 }
552
553 // default format statement for all classifiers
554 Element default_classifier_format = (Element)XMLTools.getChildByTagName (browseNode, StaticStrings.FORMAT_STR);
555
556 to.getDocumentElement ().appendChild (doFormat(to, default_classifier_format, StaticStrings.BROWSE_STR));
557 }
558 static private Element doFormat(Document to, Element format, String name_str) {
559 Element format_element = to.createElement (StaticStrings.FORMAT_STR);
560 if (name_str != null) {
561 format_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
562 }
563
564 // Don't write out an empty format statement of <format/> (i.e. format has no child nodes)
565 // as this will end up embedded in another format statement as <format><format/><format />
566 // This doubling up of format stmts will then prevent GLI from opening the collection again.
567 if (format != null && format.hasChildNodes()) { // not an empty format statement
568 String gsf_text = XMLTools.xmlNodeToStringWithoutIndenting(format);
569
570 if (gsf_text.startsWith("<") && (gsf_text.indexOf("<") != gsf_text.lastIndexOf("<"))) {
571 gsf_text = gsf_text.substring(gsf_text.indexOf("<gsf"),
572 gsf_text.indexOf(StaticStrings.FORMAT_END_TAG));
573 }
574
575 XMLTools.setNodeText(format_element, gsf_text);
576 }
577 return format_element;
578 }
579 // Handling 'subcollection' elements in 'search' element of 'collectionConfig.xml'
580 static private void doSubcollection (Document to, Node searchNode) {
581 Element toElement = to.getDocumentElement ();
582 NodeList sub_children = ((Element)searchNode).getElementsByTagName (StaticStrings.SUBCOLLECTION_STR);
583 int sub_nodes = sub_children.getLength ();
584
585 // There is no subcollection
586 if (sub_nodes < 1) {
587 return;
588 }
589
590 for (int i=0; i<sub_nodes; i++) {
591 Element sub_child = (Element)sub_children.item (i);
592 String name_str = sub_child.getAttribute (StaticStrings.NAME_ATTRIBUTE);
593 String filter_str = sub_child.getAttribute (StaticStrings.FILTER_ATTRIBUTE);
594
595 // filter_str is in the form '<! (if set)><metadata>/<metadata value>/<flag (if any)>'
596
597 int pos = filter_str.indexOf (StaticStrings.SEPARATOR_CHARACTER);
598 String meta_str = "";
599 String meta_value_str = "";
600 String clude_str = "";
601 String flag_str = "";
602 if (pos == -1) {
603
604 meta_str = meta_value_str = filter_str;
605 clude_str = StaticStrings.INCLUDE_STR;
606 } else {
607 clude_str = StaticStrings.INCLUDE_STR;
608 if (filter_str.startsWith (StaticStrings.EXCLAMATION_CHARACTER)) {
609 clude_str = StaticStrings.EXCLUDE_STR;
610 // Peel off "!"
611 filter_str = filter_str.substring (StaticStrings.EXCLAMATION_CHARACTER.length ());
612 }
613
614 String[] strs = filter_str.split (StaticStrings.SEPARATOR_CHARACTER);
615 if (strs[0] != null && strs[0] != "") {
616 meta_str = strs[0];
617 }
618 if(!meta_str.equals (StaticStrings.FILENAME_STR) && meta_str.indexOf (StaticStrings.NS_SEP) == -1) {
619 meta_str = StaticStrings.EXTRACTED_NAMESPACE + meta_str;
620 }
621
622 if (strs[1] != null && strs[1] != "") {
623 meta_value_str = strs[1];
624 }
625 if (strs.length > 2) {
626 //This means there has been set a flag
627 if (strs[2] != null && strs[2] != "") {
628 flag_str = strs[2];
629 }
630 }
631 }
632 Element subcollection_element = to.createElement (StaticStrings.SUBCOLLECTION_ELEMENT);
633 subcollection_element.setAttribute (StaticStrings.NAME_STR, name_str);
634 subcollection_element.setAttribute (StaticStrings.CONTENT_ATTRIBUTE, meta_str);
635 subcollection_element.setAttribute (StaticStrings.TYPE_ATTRIBUTE, clude_str);
636 if (flag_str != "") {
637 subcollection_element.setAttribute (StaticStrings.OPTIONS_ATTRIBUTE, flag_str);
638 }
639 XMLTools.setNodeText (subcollection_element, meta_value_str);
640
641 toElement.appendChild (subcollection_element);
642 }
643 }
644 //Handle levels (document, section). In the internal structure, the element is called 'IndexOption'
645 static private void doLevel (Document to, Node searchNode) {
646 Element toElement = to.getDocumentElement ();
647 NodeList level_children = ((Element)searchNode).getElementsByTagName (StaticStrings.LEVEL_ATTRIBUTE);
648 int level_nodes = level_children.getLength ();
649
650 // it's mg, there's no level. So we construct a default 'indexOption' in the internal structure
651 if (level_nodes < 1) {
652 Element index_option = to.createElement (StaticStrings.INDEXOPTIONS_ELEMENT);
653 index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
654 index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.LEVELS_STR);
655
656 Element option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
657 option_element.setAttribute (StaticStrings.NAME_STR, StaticStrings.DOCUMENT_STR);
658 index_option.appendChild (option_element);
659
660 appendProperly (toElement, index_option);
661
662 return;
663 }
664
665 Element index_option = to.createElement (StaticStrings.INDEXOPTIONS_ELEMENT);
666 index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
667 index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.LEVELS_STR);
668
669 for (int i=0; i<level_nodes; i++) {
670 Element level_element = (Element)level_children.item (i);
671 String level_str = level_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
672 Element option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
673 option_element.setAttribute (StaticStrings.NAME_STR, level_str);
674 index_option.appendChild (option_element);
675
676
677 // Contructing 'collectionmetadata' elements from the 'displayItem' of this 'level' element
678 ArrayList displayItem_list = XMLTools.getNamedElementList (level_element,
679 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
680 if (displayItem_list == null) {
681 return ;
682 }
683 for (int j=0; j<displayItem_list.size (); j++) {
684 Element item = (Element)displayItem_list.get (j);
685 String text = XMLTools.getNodeText (item);
686 String lang = item.getAttribute (StaticStrings.LANG_ATTRIBUTE);
687
688 //If there is nothing to display, don't bother creating the element
689 if (text == "") {
690 continue;
691 }
692 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
693 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
694 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, level_str);
695 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
696 XMLTools.setNodeText (collectionmetadata, text);
697
698 appendProperly (toElement, collectionmetadata);
699 }
700 }
701 appendProperly (toElement, index_option);
702 }
703 //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.
704 static private void doIndexSubcollection (Document to, Node searchNode) {
705 Element toElement = to.getDocumentElement ();
706 NodeList index_sub_children = ((Element)searchNode).getElementsByTagName (StaticStrings.SUBCOLLECTION_INDEX_ELEMENT);
707 int num_nodes = index_sub_children.getLength ();
708
709 // there is no subcollection index
710 if (num_nodes < 1) {
711 return;
712 }
713
714 Element subcollection_indexes = to.createElement (StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT);
715
716 for (int i=0; i<num_nodes; i++) {
717 Element index_element = to.createElement (StaticStrings.INDEX_ELEMENT);
718 Element index_sub_child = (Element)index_sub_children.item (i);
719 String name_str = index_sub_child.getAttribute (StaticStrings.NAME_ATTRIBUTE);
720
721 // name_str is in the form of comma separated strings, each of which is a subcollection filter name
722 String[] filters = name_str.split (StaticStrings.COMMA_CHARACTER);
723 for (int j=0; j<filters.length; j++) {
724
725 Element content_element = to.createElement (StaticStrings.CONTENT_ELEMENT);
726 content_element.setAttribute (StaticStrings.NAME_STR, filters[j]);
727 index_element.appendChild (content_element);
728 }
729 subcollection_indexes.appendChild (index_element);
730
731 // Contructing 'collectionmetadata' elements from the 'displayItem' of this 'indexSubcollection' element
732 ArrayList displayItem_list = XMLTools.getNamedElementList (index_sub_child,
733 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
734 if (displayItem_list == null) {
735 // there is no display item for this element
736 continue;
737 }
738 for (int j=0; j<displayItem_list.size (); j++) {
739 Element item = (Element)displayItem_list.get (j);
740 String text = XMLTools.getNodeText (item);
741 String lang = item.getAttribute (StaticStrings.LANG_ATTRIBUTE);
742
743 //If there is nothing to display, don't bother creating the element
744 if (text == "") {
745 continue;
746 }
747 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
748 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
749 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
750 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
751 XMLTools.setNodeText (collectionmetadata, text);
752
753 appendProperly (toElement, collectionmetadata);
754 }
755 }
756 appendProperly (toElement, subcollection_indexes);
757 }
758 //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.
759 static private void doIndexLanguage (Document to, Node searchNode) {
760 Element toElement = to.getDocumentElement ();
761 NodeList index_sub_children = ((Element)searchNode).getElementsByTagName (StaticStrings.LANGUAGE_INDEX_ELEMENT);
762 int num_nodes = index_sub_children.getLength ();
763
764 // there is no subcollection index
765 if (num_nodes < 1) {
766 return;
767 }
768
769 Element language_indexes = to.createElement (StaticStrings.LANGUAGES_ELEMENT);
770
771 for (int i=0; i<num_nodes; i++) {
772 Element language_element = to.createElement (StaticStrings.LANGUAGE_ELEMENT);
773 Element index_sub_child = (Element)index_sub_children.item (i);
774 String name_str = index_sub_child.getAttribute (StaticStrings.NAME_ATTRIBUTE);
775 language_element.setAttribute (StaticStrings.NAME_STR, name_str);
776 language_indexes.appendChild (language_element);
777
778 // Contructing 'collectionmetadata' elements from the 'displayItem' of this 'indexLanguage' element
779 ArrayList displayItem_list = XMLTools.getNamedElementList (index_sub_child,
780 StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
781 if (displayItem_list == null) {
782 // there is no display item for this element
783 continue;
784 }
785 for (int j=0; j<displayItem_list.size (); j++) {
786 Element item = (Element)displayItem_list.get (j);
787 String text = XMLTools.getNodeText (item);
788 String lang = item.getAttribute (StaticStrings.LANG_ATTRIBUTE);
789
790 //If there is nothing to display, don't bother creating the element
791 if (text == "") {
792 continue;
793 }
794 Element collectionmetadata = to.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
795 collectionmetadata.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
796 collectionmetadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
797 collectionmetadata.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, lang);
798 XMLTools.setNodeText (collectionmetadata, text);
799
800 appendProperly (toElement, collectionmetadata);
801 }
802 }
803 toElement.appendChild (language_indexes);
804 }
805
806 // Handling search types
807 static private void doSearchType (Document to, Node searchNode) {
808 NodeList type_children = ((Element)searchNode).getElementsByTagName (StaticStrings.SEARCHTYPE_ELEMENT);
809 int num_types = type_children.getLength();
810 String searchtype_str = "";
811 if (num_types < 1) {
812 // not defined yet, add in default
813 searchtype_str = "plain,simpleform,advancedform";
814 }
815 else {
816 for(int i=0; i<num_types; i++) {
817 Node e = type_children.item (i);
818 String t = ((Element)e).getAttribute (StaticStrings.NAME_ATTRIBUTE);
819 if (i>0) {
820 searchtype_str += ",";
821 }
822 searchtype_str += t;
823 }
824 }
825
826 // pretend its a format statement
827 Element search_type_element = to.createElement (StaticStrings.FORMAT_STR);
828 search_type_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCHTYPE_ELEMENT);
829 XMLTools.setNodeText(search_type_element, searchtype_str);
830 appendProperly (to.getDocumentElement (), search_type_element);
831
832 }
833
834 // Handling search format statement
835 static private void doSearchFormat (Document to, Node searchNode) {
836 // THere is currently just one format element for search. HOwever, need to check for old config files which used to have <format name="searchTypes">
837 NodeList format_children = ((Element)searchNode).getElementsByTagName (StaticStrings.FORMAT_STR);
838 int format_nodes = format_children.getLength ();
839 if (format_nodes < 1) {
840 return;
841 }
842 Element format = null;
843 for(int i=0; i<format_nodes; i++) {
844 Node e = format_children.item (i);
845 if (e.hasAttributes () == false) {
846 //The format element for format statement has no attribute
847 format = (Element)e;
848 }
849 }
850 //format statement for search
851 if (format != null) {
852 (to.getDocumentElement ()).appendChild (doFormat(to, format, StaticStrings.SEARCH_STR));
853 }
854 }
855 // Handling defaultIndexLanguage and languageMetadata in collectionConfig.xml ('elementNameFrom'); in the internal structure, they are called 'DefaultLanguage' and 'LanguageMetadata' ('elementNameTo') respectively.
856 // Converting from collectionConfig.xml to the internal xml structure.
857 static private void doLanguageMetadata (Document to, Node searchNode) {
858 Element toElement = to.getDocumentElement ();
859 String elementNameFrom = StaticStrings.LANGUAGE_METADATA_ELEMENT_STR;
860 String elementNameTo = StaticStrings.LANGUAGE_METADATA_ELEMENT;
861 Node from_element = XMLTools.getChildByTagName (searchNode, elementNameFrom);
862 if (from_element == null) {
863 return; // such an element not found
864 }
865
866 Element to_element = to.createElement (elementNameTo);
867
868 String name_str = ((Element)from_element).getAttribute (StaticStrings.NAME_ATTRIBUTE);
869 if (name_str.indexOf (StaticStrings.NS_SEP) == -1) {
870 name_str = StaticStrings.EXTRACTED_NAMESPACE + name_str;
871 }
872 to_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
873 to_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
874
875 toElement.appendChild (to_element);
876 }
877 static private void doReplaceListRef (Document to, Element from) {
878 Element toElement = to.getDocumentElement ();
879
880 NodeList replace_elements = from.getElementsByTagName(StaticStrings.REPLACELISTREF_STR);
881 int num_elems = replace_elements.getLength();
882 if (num_elems < 1) {
883 return;
884 }
885 for (int i=0; i<num_elems; i++) {
886 Element to_element = XMLTools.duplicateElement (to, (Element)replace_elements.item(i), true);
887 toElement.appendChild (to_element);
888 }
889 }
890 static private void convertReplaceListRef (Document from, Document to) {
891 Element toElement = to.getDocumentElement ();
892
893 NodeList replace_elements = from.getDocumentElement().getElementsByTagName(StaticStrings.REPLACELISTREF_STR);
894 int num_elems = replace_elements.getLength();
895 if (num_elems < 1) {
896 return;
897 }
898 for (int i=0; i<num_elems; i++) {
899 Element to_element = XMLTools.duplicateElement (to, (Element)replace_elements.item(i), true);
900 toElement.appendChild (to_element);
901 }
902 }
903
904 /** serviceRackList is currently not editable in GLI - just copy it in from config file and write it out again. */
905 static private void doServiceRackList (Document to, Element from) {
906 Element toElement = to.getDocumentElement ();
907
908 Node srl_element = XMLTools.getChildByTagName (from, StaticStrings.SERVICE_RACK_LIST_ELEMENT);
909 if (srl_element == null) {
910 return; // such an element not found
911 }
912
913 Element to_element = XMLTools.duplicateElement (to, (Element)srl_element, true);
914 toElement.appendChild (to_element);
915 }
916 static private void convertServiceRackList (Document from, Document to) {
917 Element toElement = to.getDocumentElement ();
918
919 Node srl_element = XMLTools.getChildByTagName (from.getDocumentElement (), StaticStrings.SERVICE_RACK_LIST_ELEMENT);
920 if (srl_element == null) {
921 return; // such an element not found
922 }
923
924 Element to_element = XMLTools.duplicateElement (to, (Element)srl_element, true);
925 toElement.appendChild (to_element);
926 }
927 static private void doDefaultIndexLanguage (Document to, Node searchNode) {
928 Element toElement = to.getDocumentElement ();
929 String elementNameFrom = StaticStrings.LANGUAGE_DEFAULT_INDEX_ELEMENT;
930 String elementNameTo = StaticStrings.LANGUAGE_DEFAULT_ELEMENT;
931 Node from_element = XMLTools.getChildByTagName (searchNode, elementNameFrom);
932 if (from_element == null) {
933 return; // such an element not found
934 }
935
936 Element to_element = to.createElement (elementNameTo);
937
938 String name_str = ((Element)from_element).getAttribute (StaticStrings.NAME_ATTRIBUTE);
939 to_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
940 to_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
941
942 toElement.appendChild (to_element);
943 }
944
945 //Handle 'indexOption' (i.e. casefold, stem etc). In the internal structure, the element is called 'IndexOption'
946 static private void doIndexOption (Document to, Node searchNode) {
947 Element toElement = to.getDocumentElement ();
948 Node index_option_node = XMLTools.getChildByTagName (searchNode, StaticStrings.INDEXOPTION_STR);
949 if (index_option_node == null) {
950 return;
951 }
952 NodeList option_children = ((Element)index_option_node).getElementsByTagName (StaticStrings.OPTION_STR);
953 int option_nodes = option_children.getLength ();
954
955 // for lucene, there is no 'indexOption'. We build a default 'indexOption' and 'assigned=false' in case the user switches to mg or mgpp
956 if (option_nodes < 1) {
957 Element index_option = to.createElement (StaticStrings.INDEXOPTIONS_ELEMENT);
958 index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
959 index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.INDEXOPTIONS_STR);
960 String[] option_str =
961 { StaticStrings.CASEFOLD_OPTION_STR, StaticStrings.STEM_OPTION_STR };
962 for (int i=0; i<option_str.length; i++) {
963 Element option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
964 option_element.setAttribute (StaticStrings.NAME_STR, option_str[i]);
965 index_option.appendChild (option_element);
966 }
967 appendProperly (toElement, index_option);
968 return;
969 }
970
971 Element index_option = to.createElement (StaticStrings.INDEXOPTIONS_ELEMENT);
972 index_option.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
973 index_option.setAttribute (StaticStrings.NAME_STR, StaticStrings.INDEXOPTIONS_STR);
974
975 for (int i=0; i<option_nodes; i++) {
976 String option_str = ((Element)option_children.item (i)).getAttribute (StaticStrings.NAME_ATTRIBUTE);
977 Element option_element = to.createElement (StaticStrings.OPTION_ELEMENT);
978 option_element.setAttribute (StaticStrings.NAME_STR, option_str);
979 index_option.appendChild (option_element);
980 }
981 appendProperly (toElement, index_option);
982 }
983
984 static private Element doBuildType (Document to, String att_value) {
985
986 //construct 'BuildType' element
987 Element element = to.createElement (StaticStrings.BUILDTYPE_ELEMENT);
988 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.BUILDTYPE_STR);
989 element.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
990 element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
991 element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
992
993 XMLTools.setNodeText (element, att_value);
994
995 return element;
996 }
997
998 static private Element doDatabaseType (Document to, String att_value) {
999
1000 //construct 'DatabaseType' element
1001 Element element = to.createElement (StaticStrings.DATABASETYPE_ELEMENT);
1002 element.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.DATABASETYPE_STR);
1003 element.setAttribute (StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
1004 element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1005 element.setAttribute (StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
1006
1007 XMLTools.setNodeText (element, att_value);
1008
1009 return element;
1010 }
1011
1012 // Convert 'description', 'smallicon' etc.
1013 static private void convertDisplayItemList (Document from, Document to) {
1014 Element displayItemList = to.createElement (StaticStrings.DISPLAYITEMLIST_STR);
1015 Element destination = to.getDocumentElement ();
1016
1017 String []att_values =
1018 {StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR,
1019 StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR,
1020 StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR,
1021 StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR};
1022
1023 String []map_attrs =
1024 {StaticStrings.DESCRIPTION_STR,
1025 StaticStrings.SMALLICON_STR,
1026 StaticStrings.ICON_STR,
1027 StaticStrings.NAME_STR};
1028
1029 for (int i=0; i<att_values.length; i++) {
1030
1031 //dOc
1032 ArrayList e_list = XMLTools.getNamedElementList (from.getDocumentElement (),
1033 StaticStrings.COLLECTIONMETADATA_ELEMENT,
1034 StaticStrings.NAME_ATTRIBUTE, att_values[i]);
1035 // if such elements don't exist, don't bother
1036 if (e_list == null) {
1037 continue;
1038 }
1039 for (int j=0; j<e_list.size (); j++) {
1040 Element e = (Element)e_list.get (j);
1041 if (e.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1042 continue;
1043 }
1044 String text = XMLTools.getNodeText (e);
1045 String lang = e.getAttribute (StaticStrings.LANGUAGE_ATTRIBUTE);
1046
1047 Element displayItem = constructElement (StaticStrings.DISPLAYITEM_STR, map_attrs[i],
1048 StaticStrings.LANG_STR, lang, text, to);
1049 displayItemList.appendChild (displayItem);
1050 }
1051
1052 }
1053 destination.appendChild (displayItemList);
1054 }
1055 // This method creates a DisplayItem element of the type of 'to' by using the ingredients from the element 'e'
1056 static private Element constructDisplayItem (Element e, Document to) {
1057 String lang_string = e.getAttribute (StaticStrings.LANGUAGE_ATTRIBUTE);
1058 String text = XMLTools.getNodeText (e);
1059 Element displayItem = to.createElement (StaticStrings.DISPLAYITEM_STR);
1060 displayItem.setAttribute (StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_ATTRIBUTE);
1061 displayItem.setAttribute (StaticStrings.LANG_STR, lang_string);
1062 XMLTools.setNodeText (displayItem, text);
1063 return displayItem;
1064 }
1065 static private void convertMetadataList (Document from, Document to) {
1066 Element metadataList = to.createElement (StaticStrings.METADATALIST_STR);
1067 Element destination = to.getDocumentElement ();
1068
1069 String []ele_names =
1070 {StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT,
1071 StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT,
1072 StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT};
1073 String []att_names =
1074 {StaticStrings.COLLECTIONMETADATA_CREATOR_STR,
1075 StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR,
1076 StaticStrings.COLLECTIONMETADATA_PUBLIC_STR};
1077 for (int i=0; i<ele_names.length; i++) {
1078 Element e = XMLTools.getNamedElement (from.getDocumentElement (),
1079 ele_names[i], StaticStrings.NAME_ATTRIBUTE, att_names[i]);
1080 if (e == null) {
1081 continue;
1082 }
1083 String text = XMLTools.getNodeText (e);
1084 Element metadata = to.createElement (StaticStrings.METADATA_STR);
1085 metadata.setAttribute (StaticStrings.NAME_ATTRIBUTE, att_names[i]);
1086 metadata.setAttribute (StaticStrings.LANG_STR, StaticStrings.ENGLISH_LANGUAGE_STR);
1087 XMLTools.setNodeText (metadata, text);
1088 metadataList.appendChild (metadata);
1089 }
1090
1091 destination.appendChild (metadataList);
1092 }
1093 // This method creates an element with the name 'element_name' of the type of 'to' by using the other three strings
1094 static private Element constructElement (String element_name, String name_value, String lang_att, String lang_value, String text, Document to) {
1095 Element e = to.createElement (element_name);
1096 e.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1097 e.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_value);
1098 e.setAttribute (lang_att, lang_value);
1099 XMLTools.setNodeText (e, text);
1100
1101 return e;
1102 }
1103 // Convert classify in the internal(i.e. Document from) to collectionconfig.xml (i.e. Document to)
1104 static private void convertClassifier (Document from, Document to) {
1105 Element browse_element = to.createElement (StaticStrings.BROWSE_STR);
1106 NodeList children = from.getDocumentElement ().getElementsByTagName (StaticStrings.CLASSIFY_ELEMENT);
1107
1108 int num_children = (children == null)? 0 : children.getLength ();
1109
1110 if (num_children == 0) {
1111 return ;
1112 }
1113
1114 for (int i=0; i<num_children; i++) {
1115
1116 Element child = (Element)children.item (i);
1117 if (child.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1118 continue;
1119 }
1120 String str = child.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
1121 Element classifier_element = to.createElement (StaticStrings.CLASSIFIER_STR);
1122 classifier_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, str);
1123
1124 NodeList option_children = child.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
1125 for (int j=0; j<option_children.getLength (); j++) {
1126 Element el = (Element)option_children.item (j);
1127 if (el.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1128 continue;
1129 }
1130 String name_str = el.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1131 String value_str = XMLTools.getNodeText (el);
1132
1133
1134 if (name_str == null && value_str == null) {
1135 continue;
1136 }
1137 Element option_element = to.createElement (StaticStrings.OPTION_STR);
1138 if (name_str != null && name_str.equals (StaticStrings.METADATA_STR)) {
1139
1140 // 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.
1141 String[] values = value_str.split (StaticStrings.COMMA_CHARACTER);
1142 value_str = "";
1143 for (int k=0; k<=values.length-1; k++) {
1144 if(values[k].startsWith (StaticStrings.EXTRACTED_NAMESPACE) && values[k].indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
1145 values[k] = values[k].substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
1146 }
1147 else {
1148 MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName (values[k]);
1149 if (metadata_element != null) {
1150 values[k] = metadata_element.getFullName ();
1151 }
1152 }
1153 if (k < values.length-1) {
1154 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
1155 } else {
1156 value_str = value_str + values[k];
1157 }
1158 }
1159 }
1160
1161 if (!name_str.equals ("")) {
1162 if (!name_str.startsWith (StaticStrings.MINUS_CHARACTER)) {
1163 name_str = StaticStrings.MINUS_CHARACTER + name_str;
1164 }
1165 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
1166 }
1167
1168 if (!value_str.equals ("")) {
1169 option_element.setAttribute (StaticStrings.VALUE_ATTRIBUTE, value_str);
1170 }
1171
1172 classifier_element.appendChild (option_element);
1173 }
1174
1175 //format element for this classifier
1176 Element e = (Element)XMLTools.getChildByTagName (child, StaticStrings.FORMAT_STR);
1177
1178 if (e != null) {
1179 classifier_element.appendChild (convertFormat(to, e));
1180 }
1181 browse_element.appendChild (classifier_element);
1182 }
1183
1184 //convert default classifier format
1185 Element e = XMLTools.getNamedElement (from.getDocumentElement (), StaticStrings.FORMAT_STR,
1186 StaticStrings.NAME_ATTRIBUTE, StaticStrings.BROWSE_STR);
1187 browse_element.appendChild (convertFormat(to, e));
1188
1189 to.getDocumentElement ().appendChild (browse_element);
1190 }
1191 static private Element convertFormat(Document to, Element e) {
1192 String format_str = XMLTools.getNodeText(e);
1193 Element format = to.createElement (StaticStrings.FORMAT_STR);
1194 //XMLTools.copyAllChildren (format, e);
1195 XMLTools.setNodeText(format, format_str);
1196 return format;
1197 }
1198 //convert format statement for search
1199 static private void convertSearchFormat (Document from, Document to) {
1200 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
1201 Element e = XMLTools.getNamedElement (from.getDocumentElement (), StaticStrings.FORMAT_STR,
1202 StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCH_STR);
1203
1204 search.appendChild (convertFormat(to, e));
1205
1206 }
1207 //convert format statement for display of the documents
1208 static private void convertDisplayFormat (Document from, Document to) {
1209 Element e = XMLTools.getNamedElement (from.getDocumentElement (), StaticStrings.FORMAT_STR,
1210 StaticStrings.NAME_ATTRIBUTE, StaticStrings.DISPLAY_STR);
1211 if (e == null) {
1212 return;
1213 }
1214 Element display = to.createElement (StaticStrings.DISPLAY_STR);
1215 display.appendChild (convertFormat(to, e));
1216 to.getDocumentElement ().appendChild (display);
1217 }
1218 // Convert plugins in the internal(i.e. Document from) to collectionconfig.xml (i.e. Document to)
1219 static private void convertPlugin (Document from, Document to) {
1220 Element import_element = to.createElement (StaticStrings.IMPORT_STR);
1221 Element plugin_list_element = to.createElement (StaticStrings.PLUGINLIST_STR);
1222
1223 NodeList children = from.getDocumentElement ().getElementsByTagName (StaticStrings.PLUGIN_ELEMENT);
1224 int num_children = (children == null)? 0 : children.getLength ();
1225 if (num_children == 0) {
1226 return ;
1227 }
1228
1229 for (int i=0; i<num_children; i++) {
1230
1231 Element child = (Element)children.item (i);
1232 if (child.getAttribute (StaticStrings.SEPARATOR_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
1233 continue;
1234 }
1235 if (child.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1236 continue;
1237 }
1238
1239 String str = child.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
1240 Element plugin_element = to.createElement (StaticStrings.PLUGIN_STR);
1241 plugin_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, str);
1242
1243 NodeList option_children = child.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
1244 for (int j=0; j<option_children.getLength (); j++) {
1245 Element el = (Element)option_children.item (j);
1246 if(!el.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
1247 continue;
1248 }
1249 String name_str = el.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1250 String value_str = XMLTools.getNodeText (el);
1251
1252
1253 if (name_str == null && value_str == null) {
1254 continue;
1255 }
1256 Element option_element = to.createElement (StaticStrings.OPTION_STR);
1257 if (name_str != null && name_str.equals (StaticStrings.METADATA_STR)) {
1258
1259 // 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, but ONLY if it is not embedded metadata (e.g. ex.dc.*)
1260 String[] values = value_str.split (StaticStrings.COMMA_CHARACTER);
1261 value_str = "";
1262 for (int k=0; k<=values.length-1; k++) {
1263 if(values[k].startsWith (StaticStrings.EXTRACTED_NAMESPACE) && values[k].indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
1264 values[k] = values[k].substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
1265 }
1266
1267 if (k < values.length-1) {
1268 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
1269 } else {
1270 value_str = value_str + values[k];
1271 }
1272 }
1273 }
1274
1275 if (!name_str.equals ("")) {
1276 if (!name_str.startsWith (StaticStrings.MINUS_CHARACTER)) {
1277 name_str = StaticStrings.MINUS_CHARACTER + name_str;
1278 }
1279 option_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
1280 }
1281
1282 if (!value_str.equals ("")) {
1283 option_element.setAttribute (StaticStrings.VALUE_ATTRIBUTE, value_str);
1284 }
1285
1286 plugin_element.appendChild (option_element);
1287 }//for loop ends
1288
1289 plugin_list_element.appendChild (plugin_element);
1290 }//for loop ends
1291
1292 import_element.appendChild (plugin_list_element);
1293
1294 //do the plugout element (used by building flax collections)
1295 Node plugout = XMLTools.getChildByTagNameIndexed (from.getDocumentElement (), PLUGOUT_ELEMENT, 0);
1296 if (plugout != null) {
1297 Element to_element = XMLTools.duplicateElement (to, (Element)plugout, true);
1298 import_element.appendChild (to_element);
1299 }
1300
1301 to.getDocumentElement ().appendChild (import_element);
1302 }
1303
1304 //Handle 'searchType' of collectionConfig.xml. In the internal structure, its also called 'searchType', eg. plain, form
1305 static private void convertSearchType (Document from, Document to) {
1306 Element e = XMLTools.getNamedElement (from.getDocumentElement (), StaticStrings.FORMAT_STR,
1307 StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCHTYPE_ELEMENT);//searchType
1308
1309 if (e == null) {
1310 return;
1311 }
1312 String searchtype_str = XMLTools.getNodeText(e);
1313 //Get the 'search' element from 'to'
1314 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
1315
1316 String[] types = searchtype_str.split(",");
1317 for (int i=0; i<types.length; i++) {
1318 Element search_type_element = to.createElement (StaticStrings.SEARCHTYPE_ELEMENT);
1319 search_type_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, types[i]);
1320 search.appendChild (search_type_element);
1321 }
1322 }
1323
1324 static private void convertBuildType (Document from, Document to) {
1325 Element e = XMLTools.getNamedElement (from.getDocumentElement (),
1326 StaticStrings.BUILDTYPE_ELEMENT,
1327 StaticStrings.NAME_ATTRIBUTE, StaticStrings.BUILDTYPE_STR);
1328 if (e == null) {
1329 return;
1330 }
1331 String indexer = XMLTools.getNodeText (e);
1332 Element search = to.createElement (StaticStrings.SEARCH_STR);
1333 search.setAttribute (StaticStrings.TYPE_ATTRIBUTE, indexer);
1334 to.getDocumentElement ().appendChild (search);
1335 }
1336 static private void convertDatabaseType (Document from, Document to) {
1337 Element e = XMLTools.getNamedElement (from.getDocumentElement (),
1338 StaticStrings.DATABASETYPE_ELEMENT,
1339 StaticStrings.NAME_ATTRIBUTE,
1340 StaticStrings.DATABASETYPE_STR);
1341 if (e == null) {
1342 return;
1343 }
1344 String db = XMLTools.getNodeText (e);
1345 Element dbtype = to.createElement (StaticStrings.INFODB_STR);
1346 dbtype.setAttribute (StaticStrings.TYPE_ATTRIBUTE, db);
1347 to.getDocumentElement ().appendChild (dbtype);
1348 }
1349 static private void convertDefaultIndex (Document from, Document to, Element search) {
1350 Element source = from.getDocumentElement ();
1351
1352 Element default_index_element = (Element)XMLTools.getChildByTagName (source, StaticStrings.INDEX_DEFAULT_ELEMENT);
1353 if (default_index_element == null) {
1354 return;
1355 }
1356
1357 String indexer = search.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
1358 String level_str = default_index_element.getAttribute (StaticStrings.LEVEL_ATTRIBUTE);
1359 // Debugging purposes
1360 if (level_str.equals ("") && indexer.equals (StaticStrings.MG_STR) ) {
1361 System.out.println ("Bug: DefaultIndex should have its level attribute not empty.");
1362 }
1363
1364 NodeList content_elements = default_index_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
1365 int content_elements_length = content_elements.getLength ();
1366
1367 // Don't output anything if no indexes are set
1368 if(content_elements_length == 0) {
1369 return ;//
1370 }
1371
1372 String index_str = "";
1373
1374 if (indexer.equals (StaticStrings.MG_STR)) {
1375 //combine level with indexes
1376 index_str = level_str + StaticStrings.COLON_CHARACTER;
1377 }
1378 else { //for mgpp/lucene, just take index
1379 //do nothing
1380 }
1381
1382 for(int k = 0; k < content_elements_length; k++) {
1383 Element content_element = (Element) content_elements.item (k);
1384 if (content_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1385 continue;
1386 }
1387 String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1388
1389 if(name_str.startsWith (StaticStrings.EXTRACTED_NAMESPACE) && name_str.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
1390 name_str = name_str.substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
1391 }
1392
1393 index_str = index_str + name_str;
1394
1395 // Make it comma separated string
1396 if(k < content_elements_length - 1) {
1397 index_str = index_str + StaticStrings.COMMA_CHARACTER;
1398 }
1399 content_element = null;
1400 }//for loop ends
1401
1402
1403 Element default_index = to.createElement (StaticStrings.INDEX_DEFAULT_ELEMENT_LOWERCASE);
1404 default_index.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_str);
1405 search.appendChild (default_index);
1406
1407 }
1408 static private void convertSubcollection (Document from, Document to) {
1409 Element source = from.getDocumentElement ();
1410 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
1411 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
1412
1413 // Get the Subcollection element from the internal structure
1414 NodeList subcollection_elements = source.getElementsByTagName (StaticStrings.SUBCOLLECTION_ELEMENT);
1415 if (subcollection_elements == null) {
1416 return;
1417 }
1418 int subcollection_elements_length = subcollection_elements.getLength ();
1419
1420 if (subcollection_elements_length == 0) { // no
1421 return ;
1422 }
1423
1424 for(int j = 0; j < subcollection_elements_length; j++) {
1425
1426 Element e = (Element) subcollection_elements.item (j);
1427 if (e.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1428 continue;
1429 }
1430 String content = e.getAttribute (StaticStrings.CONTENT_ATTRIBUTE);
1431 String name = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1432 String options = e.getAttribute (StaticStrings.OPTIONS_ATTRIBUTE);
1433 String type = e.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
1434 String text = XMLTools.getNodeText (e);
1435
1436 String filter = "";
1437 if (type.equals (StaticStrings.EXCLUDE_STR)) {
1438 filter = StaticStrings.EXCLAMATION_CHARACTER;
1439 }
1440
1441 if(content.startsWith (StaticStrings.EXTRACTED_NAMESPACE) && content.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
1442 content = content.substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
1443 }
1444 filter = filter + content + StaticStrings.SEPARATOR_CHARACTER + text;
1445 if (options != null && options != "") {
1446 filter = filter + StaticStrings.SEPARATOR_CHARACTER + options;
1447 }
1448 Element subcollection = to.createElement (StaticStrings.SUBCOLLECTION_STR);
1449 subcollection.setAttribute (StaticStrings.FILTER_ATTRIBUTE, filter);
1450 subcollection.setAttribute (StaticStrings.NAME_ATTRIBUTE, name);
1451
1452 search.appendChild (subcollection);
1453 }
1454 }
1455 static private void convertSubcollectionIndexes (Document from, Document to) {
1456 Element source = from.getDocumentElement ();
1457 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
1458 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
1459
1460 // Get the SubcollectionIndexes element from the internal structure
1461 Element subcollection_indexes = (Element)XMLTools.getChildByTagName (source, StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT);
1462 if (subcollection_indexes == null) {
1463 return;
1464 }
1465 NodeList index_elements = subcollection_indexes.getElementsByTagName (StaticStrings.INDEX_ELEMENT);
1466 int index_elements_length = index_elements.getLength ();
1467
1468 if (index_elements_length == 0) { // no indexes
1469 return ;
1470 }
1471
1472 for(int j = 0; j < index_elements_length; j++) {
1473 Element index_element = (Element) index_elements.item (j);
1474 if (index_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1475 continue;
1476 }
1477
1478 Element index = to.createElement (StaticStrings.SUBCOLLECTION_INDEX_ELEMENT);
1479
1480 String index_value = "";
1481
1482 NodeList content_elements = index_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
1483 int content_elements_length = content_elements.getLength ();
1484
1485 for(int k = 0; k < content_elements_length; k++) {
1486 Element content_element = (Element) content_elements.item (k);
1487 if (content_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1488 continue;
1489 }
1490 String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1491 index_value += name_str;
1492 // Make it comma separated string
1493 if(k < content_elements_length - 1) {
1494 index_value += StaticStrings.COMMA_CHARACTER;
1495 }
1496 content_element = null;
1497 }//for loop ends
1498
1499 index.setAttribute (StaticStrings.NAME_ATTRIBUTE, index_value);
1500
1501 // Now constructing 'displayItem' element for this 'indexSubcollection' element
1502 // from the collectionmetadata element
1503 ArrayList collectionmetadata_list = XMLTools.getNamedElementList (source,
1504 StaticStrings.COLLECTIONMETADATA_ELEMENT,
1505 StaticStrings.NAME_ATTRIBUTE, index_value);
1506
1507 if (collectionmetadata_list != null) {
1508
1509 for(int k = 0; k < collectionmetadata_list.size (); k++) {
1510 Element collectionmetadata = (Element)collectionmetadata_list.get (k);
1511 if (collectionmetadata.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1512 continue;
1513 }
1514 Element displayItem = constructDisplayItem (collectionmetadata, to);
1515 index.appendChild (displayItem);
1516 }
1517 }
1518
1519 search.appendChild (index);
1520
1521 } //for loop ends
1522 }
1523
1524 static private void convertLanguages (Document from, Document to) {
1525 Element source = from.getDocumentElement ();
1526 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
1527 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
1528
1529 // Get the Languages element from the internal structure
1530 Element languages = (Element)XMLTools.getChildByTagName (source, StaticStrings.LANGUAGES_ELEMENT);
1531 if (languages == null) {
1532 return;
1533 }
1534 NodeList language_elements = languages.getElementsByTagName (StaticStrings.LANGUAGE_ELEMENT);
1535 int language_elements_length = language_elements.getLength ();
1536
1537 if (language_elements_length == 0) {
1538 return ;
1539 }
1540
1541 for(int j = 0; j < language_elements_length; j++) {
1542 Element element = (Element) language_elements.item (j);
1543 if (element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1544 continue;
1545 }
1546
1547 // Create indexLanguage element
1548 Element index_language = to.createElement (StaticStrings.LANGUAGE_INDEX_ELEMENT);
1549
1550 String name_str = element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1551 index_language.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
1552
1553 // Now constructing 'displayItem' element for this 'indexLanguage' element
1554 // from the collectionmetadata element
1555 ArrayList collectionmetadata_list = XMLTools.getNamedElementList (source,
1556 StaticStrings.COLLECTIONMETADATA_ELEMENT,
1557 StaticStrings.NAME_ATTRIBUTE, name_str);
1558
1559 if (collectionmetadata_list != null) {
1560
1561 for(int k = 0; k < collectionmetadata_list.size (); k++) {
1562 Element collectionmetadata = (Element)collectionmetadata_list.get (k);
1563 if (collectionmetadata.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1564 continue;
1565 }
1566 Element displayItem = constructDisplayItem (collectionmetadata, to);
1567 index_language.appendChild (displayItem);
1568 }
1569 }
1570
1571 search.appendChild (index_language);
1572
1573 } //for loop ends
1574
1575 // Convert DefaultLanguage
1576 // Get the DefaultLanguage element from the internal structure
1577 Element default_language = (Element)XMLTools.getChildByTagName (source, StaticStrings.LANGUAGE_DEFAULT_ELEMENT);
1578 if(default_language != null) {
1579 String lang_name = default_language.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1580 Element default_index_language = to.createElement (StaticStrings.LANGUAGE_DEFAULT_INDEX_ELEMENT);
1581 default_index_language.setAttribute (StaticStrings.NAME_ATTRIBUTE, lang_name);
1582 search.appendChild (default_index_language);
1583 }
1584 // Convert LanguageMetadata
1585 // Get the LanguageMetadata element from the internal structure
1586 Element language_metadata = (Element)XMLTools.getChildByTagName (source, StaticStrings.LANGUAGE_METADATA_ELEMENT);
1587 if(language_metadata != null) {
1588 String meta_name = language_metadata.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1589 Element language_meta = to.createElement (StaticStrings.LANGUAGE_METADATA_ELEMENT_STR);
1590 if(meta_name.startsWith (StaticStrings.EXTRACTED_NAMESPACE) && meta_name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
1591 meta_name = meta_name.substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
1592 }
1593 language_meta.setAttribute (StaticStrings.NAME_ATTRIBUTE, meta_name);
1594 search.appendChild (language_meta);
1595 }
1596 }
1597
1598 //convert indexes and their displayItems, which go in 'search' element in collectionConfig.xml
1599 //parameter 'to' is the document to be saved as collectionConfig.xml
1600 //parameter 'from' is the internal xml structure
1601 static private void convertIndex (Document from, Document to) {
1602 Element source = from.getDocumentElement ();
1603 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
1604 Element search = (Element)XMLTools.getChildByTagName (to.getDocumentElement (), StaticStrings.SEARCH_STR);
1605
1606 //THere are two sets of indexes elements, find the one which is assigned 'true'
1607 Element indexes = XMLTools.getNamedElement (source,
1608 StaticStrings.INDEXES_ELEMENT,
1609 StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1610 if (indexes == null) {
1611 return;
1612 }
1613 NodeList index_elements = indexes.getElementsByTagName (StaticStrings.INDEX_ELEMENT);
1614 int index_elements_length = index_elements.getLength ();
1615
1616 if (index_elements_length == 0) { // no indexes
1617 return ;
1618 }
1619
1620 //find out it's mg or mgpp/lucene
1621 String mg = search.getAttribute (StaticStrings.TYPE_ATTRIBUTE);
1622 boolean mg_indexer = false;
1623 if (mg.equals (StaticStrings.MG_STR)) {
1624 mg_indexer = true;//it's mg, then the level is set as attribute of
1625 }
1626 if (mg_indexer == false) {
1627 // It's mgpp. Construct 'level' and 'defaultLevel' elements separately.
1628 convertLevels (from, to, search);
1629 }
1630
1631 for(int j = 0; j < index_elements_length; j++) {
1632 Element index_element = (Element) index_elements.item (j);
1633 if (index_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1634 continue;
1635 }
1636
1637 Element index_ele = to.createElement (StaticStrings.INDEX_LOW_STR);//index
1638
1639 // Used for creating displayItem for this element 'index_ele' further below
1640 // full_index_names contain 'ex.'
1641 String full_index_name = "";
1642 String level_str = "";
1643
1644 StringBuffer index_value = new StringBuffer ();
1645 if (mg_indexer == true) {
1646 // For mg indexer, there is a 'level' attribute in the index element of the internal structure
1647 // But mgpp/lucene don't
1648 level_str = index_element.getAttribute (StaticStrings.LEVEL_ATTRIBUTE);
1649 if(level_str.length () > 0) {
1650 index_value.append (level_str).append (StaticStrings.COLON_CHARACTER);
1651 //index_value = index_value.StaticStrings.COLON_CHARACTER;
1652 }
1653 }
1654
1655 NodeList content_elements = index_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
1656 int content_elements_length = content_elements.getLength ();
1657
1658 for(int k = 0; k < content_elements_length; k++) {
1659 Element content_element = (Element) content_elements.item (k);
1660 if (content_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1661 continue;
1662 }
1663 String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1664
1665 full_index_name = full_index_name + name_str;
1666 if (k < content_elements_length - 1) {
1667 full_index_name = full_index_name + StaticStrings.COMMA_CHARACTER;
1668 }
1669
1670 if(name_str.startsWith (StaticStrings.EXTRACTED_NAMESPACE) && name_str.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
1671 name_str = name_str.substring (StaticStrings.EXTRACTED_NAMESPACE.length ());
1672 }
1673
1674 index_value.append (name_str);
1675 name_str = null;
1676 // Make it comma separated string
1677 if(k < content_elements_length - 1) {
1678 index_value.append (StaticStrings.COMMA_CHARACTER);
1679 }
1680 content_element = null;
1681 }//for loop ends
1682
1683 String temp_str = index_value.toString ();
1684 index_ele.setAttribute (StaticStrings.NAME_ATTRIBUTE, temp_str);
1685
1686 // Now constructing 'displayItem' element for this 'index_ele' element
1687 // The index names in the collectionmetadata elements in the internal structure are not the names that
1688 // are used in the content elements (i.e. ex.Source or dc.Subject and keywords), but the names that are
1689 // in the configuration files (i.e. Source or dc.Subject)
1690 ArrayList collectionmetadata_list = XMLTools.getNamedElementList (source,
1691 StaticStrings.COLLECTIONMETADATA_ELEMENT,
1692 StaticStrings.NAME_ATTRIBUTE, temp_str);
1693
1694 if (collectionmetadata_list == null) {
1695 //try the full name, i.e. with 'ex.'
1696 if (mg_indexer == true) {
1697 // but first append level info if we are mg
1698 full_index_name = level_str+StaticStrings.COLON_CHARACTER+full_index_name;
1699 }
1700 collectionmetadata_list = XMLTools.getNamedElementList (source,
1701 StaticStrings.COLLECTIONMETADATA_ELEMENT,
1702 StaticStrings.NAME_ATTRIBUTE, full_index_name);
1703 }
1704
1705 if (collectionmetadata_list != null) {
1706
1707 for(int k = 0; k < collectionmetadata_list.size (); k++) {
1708 Element collectionmetadata = (Element)collectionmetadata_list.get (k);
1709 if (collectionmetadata.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1710 continue;
1711 }
1712 Element displayItem = constructDisplayItem (collectionmetadata, to);
1713
1714 index_ele.appendChild (displayItem);
1715 }
1716 }
1717
1718 search.appendChild (index_ele);
1719
1720 } //for loop ends
1721
1722 //Convert default index
1723 convertDefaultIndex (from, to, search);
1724 convertIndexOptions(from, to, search);
1725 }
1726 // Convert levels for mgpp/lucene. This method is called by converIndex() when mgpp indexer is detected.
1727 static private void convertLevels (Document from, Document to, Element search) {
1728 Element source = from.getDocumentElement ();
1729 Element index_option = XMLTools.getNamedElement (source,
1730 StaticStrings.INDEXOPTIONS_ELEMENT,
1731 StaticStrings.NAME_ATTRIBUTE, StaticStrings.LEVELS_STR);
1732 if (index_option == null) {
1733 return;
1734 }
1735 //Debugging purposes
1736 if (index_option.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1737 DebugStream.println ("For mgpp, there should be an IndexOption element for levels which is assigned 'true': possible bug.");
1738 }
1739
1740 NodeList option_elements = index_option.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
1741 int num_elements = option_elements.getLength ();
1742
1743 // Don't output anything if no indexes are set
1744 if(num_elements == 0) {
1745 return ;//
1746 }
1747
1748 for(int k = 0; k < num_elements; k++) {
1749 Element e = (Element) option_elements.item (k);
1750 String name_str = e.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1751 Element level_element = to.createElement (StaticStrings.LEVEL_ELEMENT);
1752 level_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
1753
1754 //Now construct displayItem for this level element from collectionmetadata
1755 ArrayList collectionmetadata_list = XMLTools.getNamedElementList (source,
1756 StaticStrings.COLLECTIONMETADATA_ELEMENT,
1757 StaticStrings.NAME_ATTRIBUTE, name_str);
1758
1759 if (collectionmetadata_list != null) {
1760
1761 for(int j = 0; j < collectionmetadata_list.size (); j++) {
1762 Element collectionmetadata = (Element)collectionmetadata_list.get (j);
1763
1764 Element displayItem = constructDisplayItem (collectionmetadata, to);
1765 level_element.appendChild (displayItem);
1766 }
1767 }
1768 search.appendChild (level_element);
1769 }
1770
1771 //Convert default level
1772 Element default_index_option = XMLTools.getNamedElement (source,
1773 StaticStrings.INDEXOPTION_DEFAULT_ELEMENT,
1774 StaticStrings.NAME_ATTRIBUTE, StaticStrings.LEVEL_DEFAULT_STR);
1775 if (default_index_option == null) {
1776 return;
1777 }
1778 Element default_level = to.createElement (StaticStrings.LEVEL_DEFAULT_ELEMENT);
1779 String default_level_str = default_index_option.getAttribute (StaticStrings.VALUE_ATTRIBUTE);
1780 default_level.setAttribute (StaticStrings.NAME_ATTRIBUTE, default_level_str);
1781 search.appendChild (default_level);
1782
1783 }
1784 // Convert indexoptions for mg/mgpp/lucene. This method is called by convertIndex().
1785 static private void convertIndexOptions (Document from, Document to, Element search) {
1786 Element source = from.getDocumentElement ();
1787 Element index_option = XMLTools.getNamedElement (source,
1788 StaticStrings.INDEXOPTIONS_ELEMENT,
1789 StaticStrings.NAME_ATTRIBUTE, StaticStrings.INDEXOPTIONS_STR);
1790 if (index_option == null) {
1791 return;
1792 }
1793 //Debugging purposes
1794 if (index_option.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
1795 DebugStream.println ("There should be an IndexOption element which is assigned 'true': possible bug.");
1796 }
1797 Element indexOptionEl = to.createElement(StaticStrings.INDEXOPTION_STR);
1798 NodeList option_elements = index_option.getElementsByTagName (StaticStrings.OPTION_ELEMENT);
1799 int num_elements = option_elements.getLength ();
1800 // Don't output anything if no index
1801 if(num_elements == 0) {
1802 return ;//
1803 }
1804 search.appendChild (indexOptionEl);
1805
1806 for(int k = 0; k < num_elements; k++) {
1807 Element e = (Element) option_elements.item (k);
1808 String name_att = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1809 Element optionEl = to.createElement(StaticStrings.OPTION_STR);
1810 optionEl.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_att);
1811 indexOptionEl.appendChild(optionEl);
1812 }
1813
1814 }
1815 // Append the element son to the element mother in the appropriate position.
1816 static public void appendProperly (Element mother, Element son) {
1817 if (son == null)
1818 return;
1819
1820 Node reference_node = findInsertionPoint (mother, son);
1821 if(reference_node != null) {
1822 mother.insertBefore (son, reference_node);
1823 }
1824 else {
1825 mother.appendChild (son);
1826 }
1827 }
1828 /** 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.
1829 * @param target_element the command Element to be inserted
1830 * @return the Element which the given command should be inserted before, or null to append to end of list
1831 */
1832 static public Node findInsertionPoint (Element document_element, Element target_element) {
1833 ///ystem.err.println("Find insertion point: " + target_element.getNodeName());
1834 String target_element_name = target_element.getNodeName ();
1835
1836 // Try to find commands with the same tag.
1837 NodeList matching_elements = document_element.getElementsByTagName (target_element_name);
1838 // If we found matching elements, then we have our most likely insertion location, so check within for groupings
1839 if(matching_elements.getLength () != 0) {
1840 ///ystem.err.println("Found matching elements.");
1841 // Only CollectionMeta are grouped.
1842 if(target_element_name.equals (StaticStrings.COLLECTIONMETADATA_ELEMENT)) {
1843 ///ystem.err.println("Dealing with collection metadata");
1844 // 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.
1845 // So if the command to be added is special add it immediately after any other special command
1846 if(target_element.getAttribute (StaticStrings.SPECIAL_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
1847 int index = 0;
1848 Element matched_element = (Element) matching_elements.item (index);
1849 Element sibling_element = (Element) matched_element.getNextSibling ();
1850 while(sibling_element.getAttribute (StaticStrings.SPECIAL_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
1851 index++;
1852 matched_element = (Element) matching_elements.item (index);
1853 sibling_element = (Element) matched_element.getNextSibling ();
1854 }
1855 if(sibling_element.getNodeName ().equals (CollectionConfiguration.NEWLINE_ELEMENT)) {
1856 Element newline_element = document_element.getOwnerDocument().createElement (CollectionConfiguration.NEWLINE_ELEMENT);
1857 document_element.insertBefore (newline_element, sibling_element);
1858 }
1859 return sibling_element;
1860 }
1861 // Otherwise try to find a matching 'name' and add after the last one in that group.
1862 else {
1863 int index = 0;
1864 target_element_name = target_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
1865 boolean found = false;
1866 // Skip all of the special metadata
1867 Element matched_element = (Element) matching_elements.item (index);
1868 while(matched_element.getAttribute (StaticStrings.SPECIAL_ATTRIBUTE).equals (StaticStrings.TRUE_STR)) {
1869 index++;
1870 matched_element = (Element) matching_elements.item (index);
1871 }
1872 // Begin search
1873 while(!found && matched_element != null) {
1874 if(matched_element.getAttribute (StaticStrings.NAME_ATTRIBUTE).equals (target_element_name)) {
1875 found = true;
1876 }
1877 else {
1878 index++;
1879 matched_element = (Element) matching_elements.item (index);
1880 }
1881 }
1882 // If we found a match, we need to continue checking until we find the last name match.
1883 if(found) {
1884 index++;
1885 Element previous_sibling = matched_element;
1886 Element sibling_element = (Element) matching_elements.item (index);
1887 while(sibling_element != null && sibling_element.getAttribute (StaticStrings.NAME_ATTRIBUTE).equals (target_element_name)) {
1888 previous_sibling = sibling_element;
1889 index++;
1890 sibling_element = (Element) matching_elements.item (index);
1891 }
1892 // 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!
1893 return previous_sibling.getNextSibling ();
1894 }
1895 // If not found we just add after last metadata element
1896 else {
1897 Element last_element = (Element) matching_elements.item (matching_elements.getLength () - 1);
1898 return last_element.getNextSibling ();
1899 }
1900 }
1901
1902 }
1903 else {
1904 ///ystem.err.println("Not dealing with collection meta.");
1905 Element matched_element = (Element) matching_elements.item (matching_elements.getLength () - 1);
1906 // 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)
1907 Node sibling_element = matched_element.getNextSibling ();
1908 if(sibling_element != null && sibling_element.getNodeName ().equals (CollectionConfiguration.NEWLINE_ELEMENT)) {
1909 Element newline_element = document_element.getOwnerDocument().createElement (CollectionConfiguration.NEWLINE_ELEMENT);
1910 document_element.insertBefore (newline_element, sibling_element);
1911 }
1912 return sibling_element; // Note that this may be null
1913 }
1914 }
1915 ///ystem.err.println("No matching elements found.");
1916 // Locate where this command is in the ordering
1917 int command_index = -1;
1918 for(int i = 0; command_index == -1 && i < CollectionConfiguration.COMMAND_ORDER.length; i++) {
1919 if(CollectionConfiguration.COMMAND_ORDER[i].equals (target_element_name)) {
1920 command_index = i;
1921 }
1922 }
1923 ///ystem.err.println("Command index is: " + command_index);
1924 // Now move forward, checking for existing elements in each of the preceeding command orders.
1925 int preceeding_index = command_index - 1;
1926 ///ystem.err.println("Searching before the target command.");
1927 while(preceeding_index >= 0) {
1928 matching_elements = document_element.getElementsByTagName (CollectionConfiguration.COMMAND_ORDER[preceeding_index]);
1929 // If we've found a match
1930 if(matching_elements.getLength () > 0) {
1931 // We add after the last element
1932 Element matched_element = (Element) matching_elements.item (matching_elements.getLength () - 1);
1933 // 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)
1934 Node sibling_element = matched_element.getNextSibling ();
1935 if(sibling_element != null && sibling_element.getNodeName ().equals (CollectionConfiguration.NEWLINE_ELEMENT)) {
1936 Element newline_element = document_element.getOwnerDocument().createElement (CollectionConfiguration.NEWLINE_ELEMENT);
1937 document_element.insertBefore (newline_element, sibling_element);
1938 }
1939 return sibling_element; // Note that this may be null
1940 }
1941 preceeding_index--;
1942 }
1943 // If all that fails, we now move backwards through the commands
1944 int susceeding_index = command_index + 1;
1945 ///ystem.err.println("Searching after the target command.");
1946 while(susceeding_index < CollectionConfiguration.COMMAND_ORDER.length) {
1947 matching_elements = document_element.getElementsByTagName (CollectionConfiguration.COMMAND_ORDER[susceeding_index]);
1948 // If we've found a match
1949 if(matching_elements.getLength () > 0) {
1950 // We add before the first element
1951 Element matched_element = (Element) matching_elements.item (0);
1952 // 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)
1953 Node sibling_element = matched_element.getPreviousSibling ();
1954 if(sibling_element != null && sibling_element.getNodeName ().equals (CollectionConfiguration.NEWLINE_ELEMENT)) {
1955 Element newline_element = document_element.getOwnerDocument().createElement (CollectionConfiguration.NEWLINE_ELEMENT);
1956 document_element.insertBefore (newline_element, sibling_element);
1957 }
1958 return sibling_element; // Note that this may be null
1959 }
1960 susceeding_index++;
1961 }
1962 // Well. Apparently there are no other commands in this collection configuration. So append away...
1963 return null;
1964 }
1965 // From collectionConfig.xml to internal structure:add 'ex.' namespace (if none).
1966 // From internal structure to collectionConfig.xml:always peel off 'ex.' namespace (if any), except for format statement
1967 //This method parses 'xml_file_doc' into 'dOc'
1968 static public void parse(File xml_file, Document dOc) {
1969
1970 Document xml_file_doc = XMLTools.parseXMLFile (xml_file);
1971 Element fromElement = xml_file_doc.getDocumentElement ();
1972 Element toElement = dOc.getDocumentElement ();
1973
1974 // It's deliberately set that 'creator', 'maintainer', and 'public' are only in English (as they are just names).
1975 // So the following ArrayList have only one element.
1976 Node metadataListNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.METADATALIST_STR, 0);
1977 if (metadataListNode != null) {
1978 ArrayList creator = doMetadataList (dOc, metadataListNode, StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT,
1979 StaticStrings.COLLECTIONMETADATA_CREATOR_STR);
1980 ArrayList maintainer = doMetadataList (dOc, metadataListNode,
1981 StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT,
1982 StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR);
1983 ArrayList is_public = doMetadataList (dOc, metadataListNode, StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT,
1984 StaticStrings.COLLECTIONMETADATA_PUBLIC_STR);
1985
1986 appendArrayList (toElement, creator);
1987 appendArrayList (toElement, maintainer);
1988 appendArrayList (toElement, is_public);
1989 }
1990
1991 Node databaseNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.INFODB_STR, 0);
1992 String databasetype_value = "gdbm";
1993 if(databaseNode != null) {
1994 databasetype_value = ((Element)databaseNode).getAttribute (StaticStrings.TYPE_ATTRIBUTE);//might be gdbm|jdbm|sqlite OR not yet set (in which case it should default to gdbm)
1995 }
1996
1997 Element databasetype = doDatabaseType(dOc, databasetype_value);
1998 appendProperly (toElement, databasetype);
1999
2000 Node searchNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.SEARCH_STR, 0);
2001 String buildtype_value = ((Element)searchNode).getAttribute (StaticStrings.TYPE_ATTRIBUTE);//might be mg|mgpp|lucene
2002 Element buildtype = doBuildType (dOc, buildtype_value);
2003 appendProperly (toElement, buildtype);
2004
2005
2006 Node importNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.IMPORT_STR, 0);
2007 if (importNode == null) {
2008 System.out.println ("There is no content in the 'import' block.");
2009 }
2010 if (importNode != null) {
2011 //do plugin list nodes
2012 Node pluginListNode = XMLTools.getChildByTagNameIndexed ((Element)importNode, StaticStrings.PLUGINLIST_STR, 0);
2013 if (pluginListNode == null) {
2014 System.out.println ("There is no pluginlist set.");
2015 }
2016 if (pluginListNode != null) {
2017
2018 doPlugin (dOc, pluginListNode);
2019 }
2020
2021 //do the plugout element (used by building flax collections)
2022 Node plugout = XMLTools.getChildByTagNameIndexed ((Element)importNode, PLUGOUT_ELEMENT, 0);
2023 if (plugout != null) {
2024 Element to_element = XMLTools.duplicateElement (dOc, (Element)plugout, true);
2025 toElement.appendChild (to_element);
2026 }
2027 }
2028
2029 Node browseNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.BROWSE_STR, 0);
2030 if (browseNode != null) {
2031 if (browseNode == null) {
2032 System.out.println ("There is no classifier.");
2033 }
2034 doClassifier (dOc, browseNode);
2035 }
2036
2037 Node displayItemListNode = XMLTools.getChildByTagNameIndexed (fromElement, StaticStrings.DISPLAYITEMLIST_STR, 0);
2038 if (displayItemListNode != null) {
2039 ArrayList description = doDisplayItemList (dOc, displayItemListNode, StaticStrings.DESCRIPTION_STR,
2040 StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
2041 ArrayList smallicon = doDisplayItemList (dOc, displayItemListNode, StaticStrings.SMALLICON_STR,
2042 StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR);
2043 ArrayList icon = doDisplayItemList (dOc, displayItemListNode, StaticStrings.ICON_STR,
2044 StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR);
2045 ArrayList name = doDisplayItemList (dOc, displayItemListNode, StaticStrings.NAME_STR,
2046 StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
2047
2048 appendArrayList (toElement, description);
2049 appendArrayList (toElement, smallicon);
2050 appendArrayList (toElement, icon);
2051 appendArrayList (toElement, name);
2052 }
2053
2054 if (buildtype_value.equalsIgnoreCase ("mg")) {
2055 doMGIndexes (dOc, searchNode);
2056 }
2057 else {
2058 doMGPPIndexes (dOc, searchNode);
2059 }
2060
2061 doDefaultIndex (dOc, searchNode);
2062 doDefaultLevel (dOc, searchNode);
2063 doLevel (dOc, searchNode);
2064 doIndexOption (dOc, searchNode);
2065 doSubcollection (dOc, searchNode);
2066 doIndexSubcollection (dOc, searchNode);
2067 doIndexLanguage (dOc, searchNode);
2068 doDefaultIndexLanguage (dOc, searchNode);
2069 doLanguageMetadata (dOc, searchNode);
2070 doSearchType (dOc, searchNode);
2071 doSearchFormat (dOc, searchNode);
2072 doDisplayFormat (dOc, fromElement);
2073 doReplaceListRef (dOc, fromElement);
2074 doServiceRackList (dOc, fromElement);
2075
2076 }
2077
2078
2079 static public String generateStringVersion(Document doc) {
2080 return XMLTools.xmlNodeToString(doc);
2081 }
2082
2083 static public void save (File collect_config_xml_file, Document doc) {
2084
2085 Document collection_config_xml_document = convertInternalToCollectionConfig (doc);
2086 String[] nonEscapingTagNames = {StaticStrings.FORMAT_STR};
2087 XMLTools.writeXMLFile (collect_config_xml_file, collection_config_xml_document, nonEscapingTagNames);
2088
2089 }
2090
2091 //Convert the internal XML DOM tree (dOc) into that of collectionConfig.xml (skeleton)
2092 static private Document convertInternalToCollectionConfig (Document dOc) {
2093 //first parse an empty skeleton of xml config file
2094 //The aim is to convert the internal structure into this skeleton
2095 Document skeleton = XMLTools.parseXMLFile ("xml/CollectionConfig.xml", true);
2096 //Element internal = dOc.getDocumentElement();
2097 convertMetadataList (dOc, skeleton);
2098 convertDisplayItemList (dOc, skeleton);
2099 convertBuildType (dOc, skeleton);
2100 convertDatabaseType (dOc, skeleton);
2101 convertIndex (dOc, skeleton);
2102 convertPlugin (dOc, skeleton);//also do the plugout element
2103 convertClassifier (dOc, skeleton);
2104 convertSubcollectionIndexes (dOc, skeleton);
2105 convertLanguages (dOc, skeleton);
2106 convertSubcollection (dOc, skeleton);
2107 convertSearchType (dOc, skeleton);
2108 convertSearchFormat (dOc, skeleton);
2109 convertDisplayFormat (dOc, skeleton);
2110 convertReplaceListRef (dOc, skeleton);
2111 convertServiceRackList(dOc, skeleton);
2112
2113 return skeleton;
2114 }
2115
2116 // Append the elements, which are of Element type, in 'list' to Element 'to'
2117 static private void appendArrayList (Element to, ArrayList list) {
2118 if (list == null) return;
2119
2120 for (int i=0; i<list.size (); i++) {
2121 appendProperly (to, (Element)list.get (i));
2122 }
2123 }
2124
2125
2126
2127}
Note: See TracBrowser for help on using the repository browser.