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

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

Kathy was making some important changes in GLI's processing of the collectionConfig file, but didn't get to finish yet. For now commenting out importoption and buildoption from the known list, to make things as before, so that these two toplevel elements are still preserved by GLI in the collconfig.xml file. For now, this solves the weird problem of a rebuilt solr demo collection's search result display showing the doc list after the footer.

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