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

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

Better way of removing duplicate collectionMetadata/displayItem elements from the collectionConfig.xml. Needed to do more to prevent duplicate elements than in the previous commit since the image-e/backdrop collections tested last time did not have search indexes while the reports collection does and these are the elements that got duplicated this time.

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