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

Last change on this file since 24931 was 24931, checked in by sjm84, 12 years ago

Display items are no longer unnecessarily escaped

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