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

Last change on this file since 30678 was 30678, checked in by ak19, 8 years ago

Committing a working version of CollectionConfigXMLReadWrite that preserves Kathy's new key and dictionary elements in displayitems. Will tidy up in a subsequent commit as the code has become repetitive.

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