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

Last change on this file since 34185 was 34185, checked in by kjdon, 4 years ago

changed doClassifier to doClassifiers as it was misleading and bugged me. copy in and out displayItems inside a classifier, so that GLI doesn't delete them. Oh this needs an overhaul. But when does one have the time...

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