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

Last change on this file since 29177 was 29177, checked in by ak19, 10 years ago

GLI now preserves the newly added (optional) option subelements of collectionConfig.xml's index element. This is only used for solr collections at present when the user hand-edits collectionConfig.xml and specifies the solr field type (option-name solrfieldtype) for an index other than the default text_en_splitting. E.g. type text_es for index allfields.

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