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

Last change on this file since 25744 was 25744, checked in by ak19, 12 years ago

Fixed bug of duplicate displayitems being written out to the collectionConfig.xml: some displayItems should be left to the functions handling indexer-specific displayItems.

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