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

Last change on this file since 26016 was 26016, checked in by kjdon, 12 years ago

added code to look for and display a top level format element - called 'global' in GLI

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