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

Last change on this file since 34242 was 34242, checked in by ak19, 4 years ago

Annoying spelling mistake.

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