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

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

Another small fix for Greenstone 3 GLI

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