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

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

All displayItems in collectionConfig.xml are written out. Especially necesary in legacy GS2 dec collections that have been converted into GS3 collections and which contain translated displayItem elements. These were previously not preserved by GLI.

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