source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/cdm/CollectionConfigXMLReadWrite.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 109.7 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 doClassifier(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 //format element for this classifier
712 Element format = (Element) XMLTools.getChildByTagName(e, StaticStrings.FORMAT_STR);
713 if (format != null)
714 {
715 classify_element.appendChild(doFormat(to, format, null));
716 }
717 appendProperly(toElement, classify_element);
718 }
719
720 // default format statement for all classifiers
721 Element default_classifier_format = (Element) XMLTools.getChildByTagName(browseNode, StaticStrings.FORMAT_STR);
722
723 to.getDocumentElement().appendChild(doFormat(to, default_classifier_format, StaticStrings.BROWSE_STR));
724 }
725
726 static private Element doFormat(Document to, Element format, String name_str)
727 {
728 Element format_element = to.createElement(StaticStrings.FORMAT_STR);
729 if (name_str != null)
730 {
731 format_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
732 }
733
734 // Don't write out an empty format statement of <format/> (i.e. format has no child nodes)
735 // as this will end up embedded in another format statement as <format><format/><format />
736 // This doubling up of format stmts will then prevent GLI from opening the collection again.
737 if (format != null && format.hasChildNodes())
738 { // not an empty format statement
739 String gsf_text = XMLTools.xmlNodeToString(format);
740
741 //We don't want the <format> elements in the string
742 int startFormatIndex = gsf_text.indexOf(StaticStrings.FORMAT_START_TAG) + StaticStrings.FORMAT_START_TAG.length();
743 int endFormatIndex = gsf_text.lastIndexOf(StaticStrings.FORMAT_END_TAG);
744 gsf_text = gsf_text.substring(startFormatIndex, endFormatIndex);
745
746 XMLTools.setNodeText(format_element, gsf_text);
747 }
748 return format_element;
749 }
750
751 // Handling 'subcollection' elements in 'search' element of 'collectionConfig.xml'
752 static private void doSubcollection(Document to, Node searchNode)
753 {
754 Element toElement = to.getDocumentElement();
755 NodeList sub_children = ((Element) searchNode).getElementsByTagName(StaticStrings.SUBCOLLECTION_STR);
756 int sub_nodes = sub_children.getLength();
757
758 // There is no subcollection
759 if (sub_nodes < 1)
760 {
761 return;
762 }
763
764 for (int i = 0; i < sub_nodes; i++)
765 {
766 Element sub_child = (Element) sub_children.item(i);
767 String name_str = sub_child.getAttribute(StaticStrings.NAME_ATTRIBUTE);
768 String filter_str = sub_child.getAttribute(StaticStrings.FILTER_ATTRIBUTE);
769
770 // filter_str is in the form '<! (if set)><metadata>/<metadata value>/<flag (if any)>'
771
772 int pos = filter_str.indexOf(StaticStrings.SEPARATOR_CHARACTER);
773 String meta_str = "";
774 String meta_value_str = "";
775 String clude_str = "";
776 String flag_str = "";
777 if (pos == -1)
778 {
779
780 meta_str = meta_value_str = filter_str;
781 clude_str = StaticStrings.INCLUDE_STR;
782 }
783 else
784 {
785 clude_str = StaticStrings.INCLUDE_STR;
786 if (filter_str.startsWith(StaticStrings.EXCLAMATION_CHARACTER))
787 {
788 clude_str = StaticStrings.EXCLUDE_STR;
789 // Peel off "!"
790 filter_str = filter_str.substring(StaticStrings.EXCLAMATION_CHARACTER.length());
791 }
792
793 String[] strs = filter_str.split(StaticStrings.SEPARATOR_CHARACTER);
794 if (strs[0] != null && strs[0] != "")
795 {
796 meta_str = strs[0];
797 }
798 if (!meta_str.equals(StaticStrings.FILENAME_STR) && meta_str.indexOf(StaticStrings.NS_SEP) == -1)
799 {
800 meta_str = StaticStrings.EXTRACTED_NAMESPACE + meta_str;
801 }
802
803 if (strs[1] != null && strs[1] != "")
804 {
805 meta_value_str = strs[1];
806 }
807 if (strs.length > 2)
808 {
809 //This means there has been set a flag
810 if (strs[2] != null && strs[2] != "")
811 {
812 flag_str = strs[2];
813 }
814 }
815 }
816 Element subcollection_element = to.createElement(StaticStrings.SUBCOLLECTION_ELEMENT);
817 subcollection_element.setAttribute(StaticStrings.NAME_STR, name_str);
818 subcollection_element.setAttribute(StaticStrings.CONTENT_ATTRIBUTE, meta_str);
819 subcollection_element.setAttribute(StaticStrings.TYPE_ATTRIBUTE, clude_str);
820 if (flag_str != "")
821 {
822 subcollection_element.setAttribute(StaticStrings.OPTIONS_ATTRIBUTE, flag_str);
823 }
824 XMLTools.setNodeText(subcollection_element, meta_value_str);
825
826 toElement.appendChild(subcollection_element);
827 }
828 }
829
830 //Handle levels (document, section). In the internal structure, the element is called 'IndexOption'
831 static private void doLevel(Document to, Node searchNode)
832 {
833 Element toElement = to.getDocumentElement();
834 NodeList level_children = ((Element) searchNode).getElementsByTagName(StaticStrings.LEVEL_ATTRIBUTE);
835 int level_nodes = level_children.getLength();
836
837 // it's mg, there's no level. So we construct a default 'indexOption' in the internal structure
838 if (level_nodes < 1)
839 {
840 Element index_option = to.createElement(StaticStrings.INDEXOPTIONS_ELEMENT);
841 index_option.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
842 index_option.setAttribute(StaticStrings.NAME_STR, StaticStrings.LEVELS_STR);
843
844 Element option_element = to.createElement(StaticStrings.OPTION_ELEMENT);
845 option_element.setAttribute(StaticStrings.NAME_STR, StaticStrings.DOCUMENT_STR);
846 index_option.appendChild(option_element);
847
848 appendProperly(toElement, index_option);
849
850 return;
851 }
852
853 Element index_option = to.createElement(StaticStrings.INDEXOPTIONS_ELEMENT);
854 index_option.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
855 index_option.setAttribute(StaticStrings.NAME_STR, StaticStrings.LEVELS_STR);
856
857 for (int i = 0; i < level_nodes; i++)
858 {
859 Element level_element = (Element) level_children.item(i);
860 String level_str = level_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
861 Element option_element = to.createElement(StaticStrings.OPTION_ELEMENT);
862 option_element.setAttribute(StaticStrings.NAME_STR, level_str);
863 index_option.appendChild(option_element);
864
865 // Contructing 'collectionmetadata' elements from the 'displayItem' of this 'level' element
866 ArrayList displayItem_list = XMLTools.getNamedElementList(level_element, StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
867 if (displayItem_list == null)
868 {
869 return;
870 }
871 for (int j = 0; j < displayItem_list.size(); j++)
872 {
873 Element item = (Element) displayItem_list.get(j);
874 String text = XMLTools.getNodeText(item);
875
876 //If there is nothing to display, don't bother creating the element
877 // Either lang or key should be set. If lang set, then should have textnode.
878 if (item.hasAttribute(StaticStrings.LANG_STR) && text.equals("")) {
879 continue;
880 }
881
882 String lang = item.getAttribute(StaticStrings.LANG_ATTRIBUTE);
883 String key = item.getAttribute(StaticStrings.KEY_ATTRIBUTE);
884
885 Element collectionmetadata = to.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
886 collectionmetadata.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
887 collectionmetadata.setAttribute(StaticStrings.NAME_ATTRIBUTE, level_str);
888
889 if(!lang.equals("")) {
890 collectionmetadata.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, lang);
891 XMLTools.setNodeText(collectionmetadata, text);
892 }
893
894 if(!key.equals("")) {
895 collectionmetadata.setAttribute(StaticStrings.KEY_ATTRIBUTE, key);
896 String dictionary = item.getAttribute(StaticStrings.DICTIONARY_ATTRIBUTE);
897 if(!dictionary.equals("")) {
898 collectionmetadata.setAttribute(StaticStrings.DICTIONARY_ATTRIBUTE, dictionary);
899 }
900 }
901
902 appendProperly(toElement, collectionmetadata);
903 }
904 }
905 appendProperly(toElement, index_option);
906 }
907
908 //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.
909 static private void doIndexSubcollection(Document to, Node searchNode)
910 {
911 Element toElement = to.getDocumentElement();
912 NodeList index_sub_children = ((Element) searchNode).getElementsByTagName(StaticStrings.SUBCOLLECTION_INDEX_ELEMENT);
913 int num_nodes = index_sub_children.getLength();
914
915 // there is no subcollection index
916 if (num_nodes < 1)
917 {
918 return;
919 }
920
921 Element subcollection_indexes = to.createElement(StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT);
922
923 for (int i = 0; i < num_nodes; i++)
924 {
925 Element index_element = to.createElement(StaticStrings.INDEX_ELEMENT);
926 Element index_sub_child = (Element) index_sub_children.item(i);
927 String name_str = index_sub_child.getAttribute(StaticStrings.NAME_ATTRIBUTE);
928
929 // name_str is in the form of comma separated strings, each of which is a subcollection filter name
930 String[] filters = name_str.split(StaticStrings.COMMA_CHARACTER);
931 for (int j = 0; j < filters.length; j++)
932 {
933
934 Element content_element = to.createElement(StaticStrings.CONTENT_ELEMENT);
935 content_element.setAttribute(StaticStrings.NAME_STR, filters[j]);
936 index_element.appendChild(content_element);
937 }
938 subcollection_indexes.appendChild(index_element);
939
940 // Contructing 'collectionmetadata' elements from the 'displayItem' of this 'indexSubcollection' element
941 ArrayList displayItem_list = XMLTools.getNamedElementList(index_sub_child, StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
942 if (displayItem_list == null)
943 {
944 // there is no display item for this element
945 continue;
946 }
947 for (int j = 0; j < displayItem_list.size(); j++)
948 {
949 Element item = (Element) displayItem_list.get(j);
950 String text = XMLTools.getNodeText(item);
951
952
953 //If there is nothing to display, don't bother creating the element
954 // Either lang or key should be set. If key set, no textnode.
955 // if lang set, should have textnode. Check if it's meaningful
956 if (item.hasAttribute(StaticStrings.LANG_STR) && text.equals(""))
957 {
958 continue;
959 }
960
961 String lang = item.getAttribute(StaticStrings.LANG_ATTRIBUTE);
962 String key = item.getAttribute(StaticStrings.KEY_ATTRIBUTE);
963
964 Element collectionmetadata = to.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
965 collectionmetadata.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
966 collectionmetadata.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
967 if(!lang.equals("")) {
968 collectionmetadata.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, lang);
969 XMLTools.setNodeText(collectionmetadata, text);
970 }
971 if(!key.equals("")) {
972 collectionmetadata.setAttribute(StaticStrings.KEY_ATTRIBUTE, key);
973 String dictionary = item.getAttribute(StaticStrings.DICTIONARY_ATTRIBUTE);
974 if(!dictionary.equals("")) {
975 collectionmetadata.setAttribute(StaticStrings.DICTIONARY_ATTRIBUTE, dictionary);
976 }
977 }
978
979 appendProperly(toElement, collectionmetadata);
980 }
981 }
982 appendProperly(toElement, subcollection_indexes);
983 }
984
985 //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.
986 static private void doIndexLanguage(Document to, Node searchNode)
987 {
988 Element toElement = to.getDocumentElement();
989 NodeList index_sub_children = ((Element) searchNode).getElementsByTagName(StaticStrings.LANGUAGE_INDEX_ELEMENT);
990 int num_nodes = index_sub_children.getLength();
991
992 // there is no subcollection index
993 if (num_nodes < 1)
994 {
995 return;
996 }
997
998 Element language_indexes = to.createElement(StaticStrings.LANGUAGES_ELEMENT);
999
1000 for (int i = 0; i < num_nodes; i++)
1001 {
1002 Element language_element = to.createElement(StaticStrings.LANGUAGE_ELEMENT);
1003 Element index_sub_child = (Element) index_sub_children.item(i);
1004 String name_str = index_sub_child.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1005 language_element.setAttribute(StaticStrings.NAME_STR, name_str);
1006 language_indexes.appendChild(language_element);
1007
1008 // Contructing 'collectionmetadata' elements from the 'displayItem' of this 'indexLanguage' element
1009 ArrayList displayItem_list = XMLTools.getNamedElementList(index_sub_child, StaticStrings.DISPLAYITEM_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.NAME_STR);
1010 if (displayItem_list == null)
1011 {
1012 // there is no display item for this element
1013 continue;
1014 }
1015 for (int j = 0; j < displayItem_list.size(); j++)
1016 {
1017 Element item = (Element) displayItem_list.get(j);
1018 String text = XMLTools.getNodeText(item);
1019 //If there is nothing to display, don't bother creating the element
1020 // Either lang or key should be set. If key set, no textnode.
1021 // if lang set, should have textnode. Check if it's meaningful
1022 if (item.hasAttribute(StaticStrings.LANG_STR) && text.equals(""))
1023 {
1024 continue;
1025 }
1026
1027 // get the value in 'lang=langcode' or 'key=keyname'
1028 // We can have either a key attribute (with optional dictionary attribute, else dictionary is implicit)
1029 // OR we can have a lang attr, with the nodetext containing the actual display strin.g
1030 String lang = item.getAttribute(StaticStrings.LANG_ATTRIBUTE);
1031 String key = item.getAttribute(StaticStrings.KEY_ATTRIBUTE);
1032
1033
1034 Element collectionmetadata = to.createElement(StaticStrings.COLLECTIONMETADATA_ELEMENT);
1035 collectionmetadata.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1036 collectionmetadata.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
1037
1038 if(!lang.equals("")) {
1039 collectionmetadata.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, lang);
1040 XMLTools.setNodeText(collectionmetadata, text);
1041 }
1042 if(!key.equals("")) {
1043 collectionmetadata.setAttribute(StaticStrings.KEY_ATTRIBUTE, key);
1044 String dictionary = item.getAttribute(StaticStrings.DICTIONARY_ATTRIBUTE);
1045 if(!dictionary.equals("")) {
1046 collectionmetadata.setAttribute(StaticStrings.DICTIONARY_ATTRIBUTE, dictionary);
1047 }
1048 }
1049
1050 appendProperly(toElement, collectionmetadata);
1051 }
1052 }
1053 toElement.appendChild(language_indexes);
1054 }
1055
1056 // Handling search types
1057 static private void doSearchType(Document to, Node searchNode)
1058 {
1059 NodeList type_children = ((Element) searchNode).getElementsByTagName(StaticStrings.SEARCHTYPE_ELEMENT);
1060 int num_types = type_children.getLength();
1061 String searchtype_str = "";
1062 if (num_types < 1)
1063 {
1064 // not defined yet, add in default
1065 searchtype_str = "plain,simpleform,advancedform";
1066 }
1067 else
1068 {
1069 for (int i = 0; i < num_types; i++)
1070 {
1071 Node e = type_children.item(i);
1072 String t = ((Element) e).getAttribute(StaticStrings.NAME_ATTRIBUTE);
1073 if (i > 0)
1074 {
1075 searchtype_str += ",";
1076 }
1077 searchtype_str += t;
1078 }
1079 }
1080 searchtype_str = searchtype_str.trim();
1081
1082 // pretend its a format statement
1083 Element search_type_element = to.createElement(StaticStrings.FORMAT_STR);
1084 search_type_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCHTYPE_ELEMENT);
1085 XMLTools.setNodeText(search_type_element, searchtype_str);
1086 appendProperly(to.getDocumentElement(), search_type_element);
1087
1088 }
1089
1090 // Handling search format statement
1091 static private void doSearchFormat(Document to, Node searchNode)
1092 {
1093 // THere is currently just one format element for search. HOwever, need to check for old config files which used to have <format name="searchTypes">
1094 NodeList format_children = ((Element) searchNode).getElementsByTagName(StaticStrings.FORMAT_STR);
1095 int format_nodes = format_children.getLength();
1096 if (format_nodes < 1)
1097 {
1098 return;
1099 }
1100 Element format = null;
1101 for (int i = 0; i < format_nodes; i++)
1102 {
1103 Node e = format_children.item(i);
1104 if (e.hasAttributes() == false)
1105 {
1106 //The format element for format statement has no attribute
1107 format = (Element) e;
1108 }
1109 }
1110 //format statement for search
1111 if (format != null)
1112 {
1113 (to.getDocumentElement()).appendChild(doFormat(to, format, StaticStrings.SEARCH_STR));
1114 }
1115 }
1116
1117 // Handling defaultIndexLanguage and languageMetadata in collectionConfig.xml ('elementNameFrom'); in the internal structure, they are called 'DefaultLanguage' and 'LanguageMetadata' ('elementNameTo') respectively.
1118 // Converting from collectionConfig.xml to the internal xml structure.
1119 static private void doLanguageMetadata(Document to, Node searchNode)
1120 {
1121 Element toElement = to.getDocumentElement();
1122 String elementNameFrom = StaticStrings.LANGUAGE_METADATA_ELEMENT_STR;
1123 String elementNameTo = StaticStrings.LANGUAGE_METADATA_ELEMENT;
1124 Node from_element = XMLTools.getChildByTagName(searchNode, elementNameFrom);
1125 if (from_element == null)
1126 {
1127 return; // such an element not found
1128 }
1129
1130 Element to_element = to.createElement(elementNameTo);
1131
1132 String name_str = ((Element) from_element).getAttribute(StaticStrings.NAME_ATTRIBUTE);
1133 if (name_str.indexOf(StaticStrings.NS_SEP) == -1)
1134 {
1135 name_str = StaticStrings.EXTRACTED_NAMESPACE + name_str;
1136 }
1137 to_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
1138 to_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1139
1140 toElement.appendChild(to_element);
1141 }
1142
1143 static private void doReplaceListRef(Document to, Element from)
1144 {
1145 Element toElement = to.getDocumentElement();
1146
1147 NodeList replace_elements = from.getElementsByTagName(StaticStrings.REPLACELISTREF_STR);
1148 XMLTools.duplicateElementList(to, toElement, replace_elements, true);
1149 }
1150
1151 // int num_elems = replace_elements.getLength();
1152 // if (num_elems < 1)
1153 // {
1154 // return;
1155 // }
1156 // for (int i = 0; i < num_elems; i++)
1157 // {
1158 // Element to_element = XMLTools.duplicateElement(to, (Element) replace_elements.item(i), true);
1159 // toElement.appendChild(to_element);
1160 // }
1161 // }
1162
1163 static private void convertReplaceListRef(Document from, Document to)
1164 {
1165 Element toElement = to.getDocumentElement();
1166
1167 NodeList replace_elements = from.getDocumentElement().getElementsByTagName(StaticStrings.REPLACELISTREF_STR);
1168 XMLTools.duplicateElementList(to, toElement, replace_elements, true);
1169 }
1170
1171 // int num_elems = replace_elements.getLength();
1172 // if (num_elems < 1)
1173 // {
1174 // return;
1175 // }
1176 // for (int i = 0; i < num_elems; i++)
1177 // {
1178 // Element to_element = XMLTools.duplicateElement(to, (Element) replace_elements.item(i), true);
1179 // toElement.appendChild(to_element);
1180 // }
1181 // }
1182
1183 /*
1184 * replacelist currently not editable in GLI, just copy it in and back out
1185 * again
1186 */
1187 static private void doReplaceList(Document to, Element from)
1188 {
1189 Element toElement = to.getDocumentElement();
1190
1191 Node rl_element = XMLTools.getChildByTagName(from, StaticStrings.REPLACELIST_STR);
1192 if (rl_element == null)
1193 {
1194 return; // such an element not found
1195 }
1196
1197 Element to_element = XMLTools.duplicateElement(to, (Element) rl_element, true);
1198 toElement.appendChild(to_element);
1199 }
1200
1201 static private void convertReplaceList(Document from, Document to)
1202 {
1203 Element toElement = to.getDocumentElement();
1204
1205 Node rl_element = XMLTools.getChildByTagName(from.getDocumentElement(), StaticStrings.REPLACELIST_STR);
1206 if (rl_element == null)
1207 {
1208 return; // such an element not found
1209 }
1210
1211 Element to_element = XMLTools.duplicateElement(to, (Element) rl_element, true);
1212 toElement.appendChild(to_element);
1213 }
1214
1215 /**
1216 * serviceRackList is currently not editable in GLI - just copy it in from
1217 * config file and write it out again.
1218 */
1219 static private void doServiceRackList(Document to, Element from)
1220 {
1221 Element toElement = to.getDocumentElement();
1222
1223 Node srl_element = XMLTools.getChildByTagName(from, StaticStrings.SERVICE_RACK_LIST_ELEMENT);
1224 if (srl_element == null)
1225 {
1226 return; // such an element not found
1227 }
1228
1229 Element to_element = XMLTools.duplicateElement(to, (Element) srl_element, true);
1230 toElement.appendChild(to_element);
1231 }
1232
1233 static private void convertServiceRackList(Document from, Document to)
1234 {
1235 Element toElement = to.getDocumentElement();
1236
1237 Node srl_element = XMLTools.getChildByTagName(from.getDocumentElement(), StaticStrings.SERVICE_RACK_LIST_ELEMENT);
1238 if (srl_element == null)
1239 {
1240 return; // such an element not found
1241 }
1242
1243 Element to_element = XMLTools.duplicateElement(to, (Element) srl_element, true);
1244 toElement.appendChild(to_element);
1245 }
1246
1247 static private void doDefaultIndexLanguage(Document to, Node searchNode)
1248 {
1249 Element toElement = to.getDocumentElement();
1250 String elementNameFrom = StaticStrings.LANGUAGE_DEFAULT_INDEX_ELEMENT;
1251 String elementNameTo = StaticStrings.LANGUAGE_DEFAULT_ELEMENT;
1252 Node from_element = XMLTools.getChildByTagName(searchNode, elementNameFrom);
1253 if (from_element == null)
1254 {
1255 return; // such an element not found
1256 }
1257
1258 Element to_element = to.createElement(elementNameTo);
1259
1260 String name_str = ((Element) from_element).getAttribute(StaticStrings.NAME_ATTRIBUTE);
1261 to_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
1262 to_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1263
1264 toElement.appendChild(to_element);
1265 }
1266
1267 //Handle 'indexOption' (i.e. casefold, stem etc). In the internal structure, the element is called 'IndexOption'
1268 static private void doIndexOption(Document to, Node searchNode)
1269 {
1270 Element toElement = to.getDocumentElement();
1271 //Node index_option_node = XMLTools.getChildByTagName(searchNode, StaticStrings.INDEXOPTION_STR);
1272 //if (index_option_node == null)
1273 //{
1274 // return;
1275 //}
1276 //NodeList option_children = ((Element) index_option_node).getElementsByTagName(StaticStrings.OPTION_STR);
1277 NodeList option_children = ((Element) searchNode).getElementsByTagName(StaticStrings.INDEXOPTION_STR);
1278 int num_options = option_children.getLength();
1279
1280 // for lucene, there is no 'indexOption'. We build a default 'indexOption' and 'assigned=false' in case the user switches to mg or mgpp
1281 if (num_options < 1)
1282 {
1283 Element index_option = to.createElement(StaticStrings.INDEXOPTIONS_ELEMENT);
1284 index_option.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
1285 index_option.setAttribute(StaticStrings.NAME_STR, StaticStrings.INDEXOPTIONS_STR);
1286 String[] option_str = { StaticStrings.CASEFOLD_OPTION_STR, StaticStrings.STEM_OPTION_STR };
1287 for (int i = 0; i < option_str.length; i++)
1288 {
1289 Element option_element = to.createElement(StaticStrings.OPTION_ELEMENT);
1290 option_element.setAttribute(StaticStrings.NAME_STR, option_str[i]);
1291 index_option.appendChild(option_element);
1292 }
1293 appendProperly(toElement, index_option);
1294 return;
1295 }
1296
1297 Element index_option = to.createElement(StaticStrings.INDEXOPTIONS_ELEMENT);
1298 index_option.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1299 index_option.setAttribute(StaticStrings.NAME_STR, StaticStrings.INDEXOPTIONS_STR);
1300
1301 for (int i = 0; i < num_options; i++)
1302 {
1303 String option_str = ((Element) option_children.item(i)).getAttribute(StaticStrings.NAME_ATTRIBUTE);
1304 Element option_element = to.createElement(StaticStrings.OPTION_ELEMENT);
1305 option_element.setAttribute(StaticStrings.NAME_STR, option_str);
1306 index_option.appendChild(option_element);
1307 }
1308 appendProperly(toElement, index_option);
1309 }
1310
1311 static private Element doBuildType(Document to, String att_value)
1312 {
1313
1314 //construct 'BuildType' element
1315 Element element = to.createElement(StaticStrings.BUILDTYPE_ELEMENT);
1316 element.setAttribute(StaticStrings.NAME_ATTRIBUTE, StaticStrings.BUILDTYPE_STR);
1317 element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
1318 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1319 element.setAttribute(StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
1320
1321 XMLTools.setNodeText(element, att_value);
1322
1323 return element;
1324 }
1325
1326 static private Element doDatabaseType(Document to, String att_value)
1327 {
1328
1329 //construct 'DatabaseType' element
1330 Element element = to.createElement(StaticStrings.DATABASETYPE_ELEMENT);
1331 element.setAttribute(StaticStrings.NAME_ATTRIBUTE, StaticStrings.DATABASETYPE_STR);
1332 element.setAttribute(StaticStrings.LANGUAGE_ATTRIBUTE, StaticStrings.ENGLISH_LANGUAGE_STR);
1333 element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1334 element.setAttribute(StaticStrings.SPECIAL_ATTRIBUTE, StaticStrings.TRUE_STR);
1335
1336 XMLTools.setNodeText(element, att_value);
1337
1338 return element;
1339 }
1340
1341 // Convert 'description', 'smallicon' etc.
1342 static private void convertDisplayItemList(Document from, Document to)
1343 {
1344 Element displayItemList = to.createElement(StaticStrings.DISPLAYITEMLIST_STR);
1345 Element destination = to.getDocumentElement();
1346
1347 // certain special collectionmeta elements should have different names
1348 // as displayItems in the collectionConfig.xml than they do in memory
1349 Map attributeMap = new HashMap(4);
1350 attributeMap.put(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR, StaticStrings.DESCRIPTION_STR);
1351 attributeMap.put(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR, StaticStrings.NAME_STR);
1352 attributeMap.put(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR, StaticStrings.SMALLICON_STR);
1353 attributeMap.put(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR, StaticStrings.ICON_STR);
1354
1355 NodeList e_list = from.getDocumentElement().getElementsByTagName(StaticStrings.COLLECTIONMETADATA_ELEMENT);
1356 // if such elements don't exist, don't bother
1357 if (e_list != null)
1358 {
1359
1360 for (int j = 0; j < e_list.getLength(); j++)
1361 {
1362 Element e = (Element) e_list.item(j);
1363 if (e.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1364 {
1365 continue;
1366 }
1367 String text = XMLTools.getNodeText(e);
1368 String lang = e.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE);
1369 String key = e.getAttribute(StaticStrings.KEY_ATTRIBUTE);
1370 String dictionary = e.getAttribute(StaticStrings.DICTIONARY_ATTRIBUTE);
1371
1372 String name_value = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1373 String name_mapping = (String) attributeMap.get(name_value);
1374 if (name_mapping != null)
1375 {
1376 name_value = name_mapping;
1377 }
1378
1379 Element displayItem = constructElement(StaticStrings.DISPLAYITEM_STR, name_value, StaticStrings.LANG_STR, lang, text, StaticStrings.KEY_ATTRIBUTE, key, StaticStrings.DICTIONARY_ATTRIBUTE, dictionary, to);
1380 //Element displayItem = constructDisplayItem(e, to, name_value);
1381 displayItemList.appendChild(displayItem);
1382 }
1383
1384 }
1385 destination.appendChild(displayItemList);
1386 }
1387
1388 // This method creates a DisplayItem element of the type of 'to' by using the ingredients from the element 'e'
1389 static private Element constructDisplayItem(Element e, Document to, String name)
1390 {
1391 String lang_string = e.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE);
1392 String key_string = e.getAttribute(StaticStrings.KEY_ATTRIBUTE);
1393 String dictionary_string = e.getAttribute(StaticStrings.DICTIONARY_ATTRIBUTE);
1394 String text = XMLTools.getNodeText(e);
1395
1396 Element displayItem = to.createElement(StaticStrings.DISPLAYITEM_STR);
1397 displayItem.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
1398
1399 if(!lang_string.equals("")) {
1400 displayItem.setAttribute(StaticStrings.LANG_STR, lang_string);
1401 XMLTools.setNodeText(displayItem, text);
1402 }
1403 if(!key_string.equals("")) {
1404 displayItem.setAttribute(StaticStrings.KEY_ATTRIBUTE, key_string);
1405 if(!dictionary_string.equals("")) {
1406 displayItem.setAttribute(StaticStrings.DICTIONARY_ATTRIBUTE, dictionary_string);
1407 }
1408 }
1409 return displayItem;
1410 }
1411
1412 static private Element constructDisplayItem(Element e, Document to)
1413 {
1414 return constructDisplayItem(e, to, StaticStrings.NAME_ATTRIBUTE);
1415 }
1416
1417 static private void convertSecurity(Document from, Document to)
1418 {
1419 Node security = XMLTools.getChildByTagNameIndexed(from.getDocumentElement(), StaticStrings.SECURITY_STR, 0);
1420 if (security != null)
1421 {
1422 Element to_element = XMLTools.duplicateElement(to, (Element) security, true);
1423 to.getDocumentElement().appendChild(to_element);
1424
1425 }
1426 }
1427 static private void convertMetadataList(Document from, Document to)
1428 {
1429 Element metadataList = to.createElement(StaticStrings.METADATALIST_STR);
1430 Element destination = to.getDocumentElement();
1431
1432 String[] ele_names = { StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT, StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT, StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT };
1433 String[] att_names = { StaticStrings.COLLECTIONMETADATA_CREATOR_STR, StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR, StaticStrings.COLLECTIONMETADATA_PUBLIC_STR };
1434 for (int i = 0; i < ele_names.length; i++)
1435 {
1436 Element e = XMLTools.getNamedElement(from.getDocumentElement(), ele_names[i], StaticStrings.NAME_ATTRIBUTE, att_names[i]);
1437 if (e == null)
1438 {
1439 continue;
1440 }
1441 String text = XMLTools.getNodeText(e);
1442 Element metadata = to.createElement(StaticStrings.METADATA_STR);
1443 metadata.setAttribute(StaticStrings.NAME_ATTRIBUTE, att_names[i]);
1444 metadata.setAttribute(StaticStrings.LANG_STR, StaticStrings.ENGLISH_LANGUAGE_STR);
1445 XMLTools.setNodeText(metadata, text);
1446 metadataList.appendChild(metadata);
1447 }
1448
1449 destination.appendChild(metadataList);
1450 }
1451
1452 // This method creates an element with the name 'element_name' of the type of 'to' by using the other three strings
1453 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)
1454 {
1455 Element e = to.createElement(element_name);
1456 e.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
1457 e.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_value);
1458
1459 if(!lang_value.equals("")) {
1460 e.setAttribute(lang_att, lang_value);
1461 XMLTools.setNodeText(e, text);
1462 }
1463 if(!key.equals("")) {
1464 e.setAttribute(key_att, key);
1465 if(!dictionary.equals("")) {
1466 e.setAttribute(dict_att, dictionary);
1467 }
1468 }
1469
1470 return e;
1471 }
1472
1473 // Convert classify in the internal(i.e. Document from) to collectionconfig.xml (i.e. Document to)
1474 static private void convertClassifier(Document from, Document to)
1475 {
1476 Element browse_element = to.createElement(StaticStrings.BROWSE_STR);
1477 NodeList children = from.getDocumentElement().getElementsByTagName(StaticStrings.CLASSIFY_ELEMENT);
1478
1479 int num_children = (children == null) ? 0 : children.getLength();
1480
1481 if (num_children == 0)
1482 {
1483 return;
1484 }
1485
1486 for (int i = 0; i < num_children; i++)
1487 {
1488
1489 Element child = (Element) children.item(i);
1490 if (child.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1491 {
1492 continue;
1493 }
1494 String str = child.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
1495 Element classifier_element = to.createElement(StaticStrings.CLASSIFIER_STR);
1496 classifier_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, str);
1497
1498 NodeList option_children = child.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
1499 for (int j = 0; j < option_children.getLength(); j++)
1500 {
1501 Element el = (Element) option_children.item(j);
1502 if (el.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1503 {
1504 continue;
1505 }
1506 String name_str = el.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1507 String value_str = XMLTools.getNodeText(el);
1508
1509 if (name_str == null && value_str == null)
1510 {
1511 continue;
1512 }
1513 Element option_element = to.createElement(StaticStrings.OPTION_STR);
1514 if (name_str != null && name_str.equals(StaticStrings.METADATA_STR))
1515 {
1516
1517 // 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.
1518 String[] values = value_str.split(StaticStrings.COMMA_CHARACTER);
1519 value_str = "";
1520 for (int k = 0; k <= values.length - 1; k++)
1521 {
1522 if (values[k].startsWith(StaticStrings.EXTRACTED_NAMESPACE) && values[k].indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1)
1523 {
1524 values[k] = values[k].substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1525 }
1526 else
1527 {
1528 MetadataElement metadata_element = MetadataTools.getMetadataElementWithDisplayName(values[k]);
1529 if (metadata_element != null)
1530 {
1531 values[k] = metadata_element.getFullName();
1532 }
1533 }
1534 if (k < values.length - 1)
1535 {
1536 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
1537 }
1538 else
1539 {
1540 value_str = value_str + values[k];
1541 }
1542 }
1543 }
1544
1545 if (!name_str.equals(""))
1546 {
1547 if (!name_str.startsWith(StaticStrings.MINUS_CHARACTER))
1548 {
1549 name_str = StaticStrings.MINUS_CHARACTER + name_str;
1550 }
1551 option_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
1552 }
1553
1554 if (!value_str.equals(""))
1555 {
1556 option_element.setAttribute(StaticStrings.VALUE_ATTRIBUTE, value_str);
1557 }
1558
1559 classifier_element.appendChild(option_element);
1560 }
1561
1562 //format element for this classifier
1563 Element e = (Element) XMLTools.getChildByTagName(child, StaticStrings.FORMAT_STR);
1564
1565 if (e != null)
1566 {
1567 classifier_element.appendChild(convertFormat(to, e));
1568 }
1569 browse_element.appendChild(classifier_element);
1570 }
1571
1572 //convert default classifier format
1573 Element e = XMLTools.getNamedElement(from.getDocumentElement(), StaticStrings.FORMAT_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.BROWSE_STR);
1574 browse_element.appendChild(convertFormat(to, e));
1575
1576 to.getDocumentElement().appendChild(browse_element);
1577 }
1578
1579 static private Element convertFormat(Document to, Element e)
1580 {
1581 String format_str = XMLTools.getNodeText(e);
1582 Element format = to.createElement(StaticStrings.FORMAT_STR);
1583 //XMLTools.copyAllChildren (format, e);
1584 XMLTools.setNodeText(format, format_str);
1585 return format;
1586 }
1587
1588 //convert format statement for search
1589 static private void convertSearchFormat(Document from, Document to)
1590 {
1591 Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
1592 Element e = XMLTools.getNamedElement(from.getDocumentElement(), StaticStrings.FORMAT_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCH_STR);
1593
1594 search.appendChild(convertFormat(to, e));
1595
1596 }
1597
1598 //convert format statement for display of the documents
1599 static private void convertDisplayFormat(Document from, Document to)
1600 {
1601 Element e = XMLTools.getNamedElement(from.getDocumentElement(), StaticStrings.FORMAT_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.DISPLAY_STR);
1602 if (e == null)
1603 {
1604 return;
1605 }
1606 Element display = to.createElement(StaticStrings.DISPLAY_STR);
1607 display.appendChild(convertFormat(to, e));
1608 to.getDocumentElement().appendChild(display);
1609 }
1610
1611 // convert global format statement
1612 static private void convertGlobalFormat(Document from, Document to)
1613 {
1614 Element e = XMLTools.getNamedElement(from.getDocumentElement(), StaticStrings.FORMAT_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.GLOBAL_STR);
1615
1616 to.getDocumentElement().appendChild(convertFormat(to, e));
1617
1618 }
1619
1620 // Convert plugins in the internal(i.e. Document from) to collectionconfig.xml (i.e. Document to)
1621 static private void convertPlugins(Document from, Document to)
1622 {
1623 Element import_element = to.createElement(StaticStrings.IMPORT_STR);
1624 Element plugin_list_element = to.createElement(StaticStrings.PLUGINLIST_STR);
1625
1626 NodeList children = from.getDocumentElement().getElementsByTagName(StaticStrings.PLUGIN_ELEMENT);
1627 int num_children = (children == null) ? 0 : children.getLength();
1628 if (num_children == 0)
1629 {
1630 return;
1631 }
1632
1633 for (int i = 0; i < num_children; i++)
1634 {
1635
1636 Element child = (Element) children.item(i);
1637 if (child.getAttribute(StaticStrings.SEPARATOR_ATTRIBUTE).equals(StaticStrings.TRUE_STR))
1638 {
1639 continue;
1640 }
1641 if (child.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1642 {
1643 continue;
1644 }
1645
1646 String str = child.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
1647 Element plugin_element = to.createElement(StaticStrings.PLUGIN_STR);
1648 plugin_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, str);
1649
1650 NodeList option_children = child.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
1651 for (int j = 0; j < option_children.getLength(); j++)
1652 {
1653 Element el = (Element) option_children.item(j);
1654 if (!el.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR))
1655 {
1656 continue;
1657 }
1658 String name_str = el.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1659 String value_str = XMLTools.getNodeText(el);
1660
1661 if (name_str == null && value_str == null)
1662 {
1663 continue;
1664 }
1665 Element option_element = to.createElement(StaticStrings.OPTION_STR);
1666 if (name_str != null && name_str.equals(StaticStrings.METADATA_STR))
1667 {
1668
1669 // 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.*)
1670 String[] values = value_str.split(StaticStrings.COMMA_CHARACTER);
1671 value_str = "";
1672 for (int k = 0; k <= values.length - 1; k++)
1673 {
1674 if (values[k].startsWith(StaticStrings.EXTRACTED_NAMESPACE) && values[k].indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1)
1675 {
1676 values[k] = values[k].substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1677 }
1678
1679 if (k < values.length - 1)
1680 {
1681 value_str = value_str + values[k] + StaticStrings.COMMA_CHARACTER;
1682 }
1683 else
1684 {
1685 value_str = value_str + values[k];
1686 }
1687 }
1688 }
1689
1690 if (!name_str.equals(""))
1691 {
1692 if (!name_str.startsWith(StaticStrings.MINUS_CHARACTER))
1693 {
1694 name_str = StaticStrings.MINUS_CHARACTER + name_str;
1695 }
1696 option_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
1697 }
1698
1699 if (!value_str.equals(""))
1700 {
1701 option_element.setAttribute(StaticStrings.VALUE_ATTRIBUTE, value_str);
1702 }
1703
1704 plugin_element.appendChild(option_element);
1705 }//for loop ends
1706
1707 plugin_list_element.appendChild(plugin_element);
1708 }//for loop ends
1709
1710 import_element.appendChild(plugin_list_element);
1711
1712 //do the plugout element (used by building flax collections)
1713 Node plugout = XMLTools.getChildByTagNameIndexed(from.getDocumentElement(), PLUGOUT_ELEMENT, 0);
1714 if (plugout != null)
1715 {
1716 Element to_element = XMLTools.duplicateElement(to, (Element) plugout, true);
1717 import_element.appendChild(to_element);
1718 }
1719
1720 to.getDocumentElement().appendChild(import_element);
1721 }
1722
1723 //Handle 'searchType' of collectionConfig.xml. In the internal structure, its also called 'searchType', eg. plain, form
1724 static private void convertSearchType(Document from, Document to)
1725 {
1726 Element e = XMLTools.getNamedElement(from.getDocumentElement(), StaticStrings.FORMAT_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.SEARCHTYPE_ELEMENT);//searchType
1727
1728 if (e == null)
1729 {
1730 return;
1731 }
1732 String searchtype_str = XMLTools.getNodeText(e).trim();
1733 //Get the 'search' element from 'to'
1734 Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
1735
1736 String[] types = searchtype_str.split(",");
1737 for (int i = 0; i < types.length; i++)
1738 {
1739 Element search_type_element = to.createElement(StaticStrings.SEARCHTYPE_ELEMENT);
1740 search_type_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, types[i]);
1741 search.appendChild(search_type_element);
1742 }
1743 }
1744
1745 static private void convertBuildType(Document from, Document to)
1746 {
1747 Element e = XMLTools.getNamedElement(from.getDocumentElement(), StaticStrings.BUILDTYPE_ELEMENT, StaticStrings.NAME_ATTRIBUTE, StaticStrings.BUILDTYPE_STR);
1748 if (e == null)
1749 {
1750 return;
1751 }
1752 String indexer = XMLTools.getNodeText(e);
1753 Element search = to.createElement(StaticStrings.SEARCH_STR);
1754 search.setAttribute(StaticStrings.TYPE_ATTRIBUTE, indexer);
1755 to.getDocumentElement().appendChild(search);
1756 }
1757
1758 static private void convertDatabaseType(Document from, Document to)
1759 {
1760 Element e = XMLTools.getNamedElement(from.getDocumentElement(), StaticStrings.DATABASETYPE_ELEMENT, StaticStrings.NAME_ATTRIBUTE, StaticStrings.DATABASETYPE_STR);
1761 if (e == null)
1762 {
1763 return;
1764 }
1765 String db = XMLTools.getNodeText(e);
1766 Element dbtype = to.createElement(StaticStrings.INFODB_STR);
1767 dbtype.setAttribute(StaticStrings.TYPE_ATTRIBUTE, db);
1768 to.getDocumentElement().appendChild(dbtype);
1769 }
1770
1771 static private void convertDefaultIndex(Document from, Document to, Element search)
1772 {
1773 Element source = from.getDocumentElement();
1774
1775 Element default_index_element = (Element) XMLTools.getChildByTagName(source, StaticStrings.INDEX_DEFAULT_ELEMENT);
1776 if (default_index_element == null)
1777 {
1778 return;
1779 }
1780
1781 String indexer = search.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
1782 String level_str = default_index_element.getAttribute(StaticStrings.LEVEL_ATTRIBUTE);
1783 // Debugging purposes
1784 if (level_str.equals("") && indexer.equals(StaticStrings.MG_STR))
1785 {
1786 System.out.println("Bug: DefaultIndex should have its level attribute not empty.");
1787 }
1788
1789 NodeList content_elements = default_index_element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
1790 int content_elements_length = content_elements.getLength();
1791
1792 // Don't output anything if no indexes are set
1793 if (content_elements_length == 0)
1794 {
1795 return;//
1796 }
1797
1798 String index_str = "";
1799
1800 if (indexer.equals(StaticStrings.MG_STR))
1801 {
1802 //combine level with indexes
1803 index_str = level_str + StaticStrings.COLON_CHARACTER;
1804 }
1805 else
1806 { //for mgpp/lucene, just take index
1807 //do nothing
1808 }
1809
1810 for (int k = 0; k < content_elements_length; k++)
1811 {
1812 Element content_element = (Element) content_elements.item(k);
1813 if (content_element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1814 {
1815 continue;
1816 }
1817 String name_str = content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1818
1819 if (name_str.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && name_str.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1)
1820 {
1821 name_str = name_str.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1822 }
1823
1824 index_str = index_str + name_str;
1825
1826 // Make it comma separated string
1827 if (k < content_elements_length - 1)
1828 {
1829 index_str = index_str + StaticStrings.COMMA_CHARACTER;
1830 }
1831 content_element = null;
1832 }//for loop ends
1833
1834 Element default_index = to.createElement(StaticStrings.INDEX_DEFAULT_ELEMENT_LOWERCASE);
1835 default_index.setAttribute(StaticStrings.NAME_ATTRIBUTE, index_str);
1836 search.appendChild(default_index);
1837
1838 }
1839
1840 static private void convertSubcollection(Document from, Document to)
1841 {
1842 Element source = from.getDocumentElement();
1843 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
1844 Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
1845
1846 // Get the Subcollection element from the internal structure
1847 NodeList subcollection_elements = source.getElementsByTagName(StaticStrings.SUBCOLLECTION_ELEMENT);
1848 if (subcollection_elements == null)
1849 {
1850 return;
1851 }
1852 int subcollection_elements_length = subcollection_elements.getLength();
1853
1854 if (subcollection_elements_length == 0)
1855 { // no
1856 return;
1857 }
1858
1859 for (int j = 0; j < subcollection_elements_length; j++)
1860 {
1861
1862 Element e = (Element) subcollection_elements.item(j);
1863 if (e.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1864 {
1865 continue;
1866 }
1867 String content = e.getAttribute(StaticStrings.CONTENT_ATTRIBUTE);
1868 String name = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1869 String options = e.getAttribute(StaticStrings.OPTIONS_ATTRIBUTE);
1870 String type = e.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
1871 String text = XMLTools.getNodeText(e);
1872
1873 String filter = "";
1874 if (type.equals(StaticStrings.EXCLUDE_STR))
1875 {
1876 filter = StaticStrings.EXCLAMATION_CHARACTER;
1877 }
1878
1879 if (content.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && content.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1)
1880 {
1881 content = content.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
1882 }
1883 filter = filter + content + StaticStrings.SEPARATOR_CHARACTER + text;
1884 if (options != null && options != "")
1885 {
1886 filter = filter + StaticStrings.SEPARATOR_CHARACTER + options;
1887 }
1888 Element subcollection = to.createElement(StaticStrings.SUBCOLLECTION_STR);
1889 subcollection.setAttribute(StaticStrings.FILTER_ATTRIBUTE, filter);
1890 subcollection.setAttribute(StaticStrings.NAME_ATTRIBUTE, name);
1891
1892 search.appendChild(subcollection);
1893 }
1894 }
1895
1896 static private void convertSubcollectionIndexes(Document from, Document to)
1897 {
1898 Element source = from.getDocumentElement();
1899 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
1900 Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
1901
1902 // Get the SubcollectionIndexes element from the internal structure
1903 Element subcollection_indexes = (Element) XMLTools.getChildByTagName(source, StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT);
1904 if (subcollection_indexes == null)
1905 {
1906 return;
1907 }
1908 NodeList index_elements = subcollection_indexes.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
1909 int index_elements_length = index_elements.getLength();
1910
1911 if (index_elements_length == 0)
1912 { // no indexes
1913 return;
1914 }
1915
1916 for (int j = 0; j < index_elements_length; j++)
1917 {
1918 Element index_element = (Element) index_elements.item(j);
1919 if (index_element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1920 {
1921 continue;
1922 }
1923
1924 Element index = to.createElement(StaticStrings.SUBCOLLECTION_INDEX_ELEMENT);
1925
1926 String index_value = "";
1927
1928 NodeList content_elements = index_element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
1929 int content_elements_length = content_elements.getLength();
1930
1931 for (int k = 0; k < content_elements_length; k++)
1932 {
1933 Element content_element = (Element) content_elements.item(k);
1934 if (content_element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1935 {
1936 continue;
1937 }
1938 String name_str = content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
1939 index_value += name_str;
1940 // Make it comma separated string
1941 if (k < content_elements_length - 1)
1942 {
1943 index_value += StaticStrings.COMMA_CHARACTER;
1944 }
1945 content_element = null;
1946 }//for loop ends
1947
1948 index.setAttribute(StaticStrings.NAME_ATTRIBUTE, index_value);
1949
1950 // Now constructing 'displayItem' element for this 'indexSubcollection' element
1951 // from the collectionmetadata element
1952 ArrayList collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, index_value);
1953
1954 if (collectionmetadata_list != null)
1955 {
1956
1957 for (int k = 0; k < collectionmetadata_list.size(); k++)
1958 {
1959 Element collectionmetadata = (Element) collectionmetadata_list.get(k);
1960 if (collectionmetadata.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1961 {
1962 continue;
1963 }
1964 Element displayItem = constructDisplayItem(collectionmetadata, to);
1965 index.appendChild(displayItem);
1966 }
1967 }
1968
1969 search.appendChild(index);
1970
1971 } //for loop ends
1972 }
1973
1974 static private void convertLanguages(Document from, Document to)
1975 {
1976 Element source = from.getDocumentElement();
1977 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
1978 Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
1979
1980 // Get the Languages element from the internal structure
1981 Element languages = (Element) XMLTools.getChildByTagName(source, StaticStrings.LANGUAGES_ELEMENT);
1982 if (languages == null)
1983 {
1984 return;
1985 }
1986 NodeList language_elements = languages.getElementsByTagName(StaticStrings.LANGUAGE_ELEMENT);
1987 int language_elements_length = language_elements.getLength();
1988
1989 if (language_elements_length == 0)
1990 {
1991 return;
1992 }
1993
1994 for (int j = 0; j < language_elements_length; j++)
1995 {
1996 Element element = (Element) language_elements.item(j);
1997 if (element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
1998 {
1999 continue;
2000 }
2001
2002 // Create indexLanguage element
2003 Element index_language = to.createElement(StaticStrings.LANGUAGE_INDEX_ELEMENT);
2004
2005 String name_str = element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2006 index_language.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
2007
2008 // Now constructing 'displayItem' element for this 'indexLanguage' element
2009 // from the collectionmetadata element
2010 ArrayList collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, name_str);
2011
2012 if (collectionmetadata_list != null)
2013 {
2014
2015 for (int k = 0; k < collectionmetadata_list.size(); k++)
2016 {
2017 Element collectionmetadata = (Element) collectionmetadata_list.get(k);
2018 if (collectionmetadata.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
2019 {
2020 continue;
2021 }
2022 Element displayItem = constructDisplayItem(collectionmetadata, to);
2023 index_language.appendChild(displayItem);
2024 }
2025 }
2026
2027 search.appendChild(index_language);
2028
2029 } //for loop ends
2030
2031 // Convert DefaultLanguage
2032 // Get the DefaultLanguage element from the internal structure
2033 Element default_language = (Element) XMLTools.getChildByTagName(source, StaticStrings.LANGUAGE_DEFAULT_ELEMENT);
2034 if (default_language != null)
2035 {
2036 String lang_name = default_language.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2037 Element default_index_language = to.createElement(StaticStrings.LANGUAGE_DEFAULT_INDEX_ELEMENT);
2038 default_index_language.setAttribute(StaticStrings.NAME_ATTRIBUTE, lang_name);
2039 search.appendChild(default_index_language);
2040 }
2041 // Convert LanguageMetadata
2042 // Get the LanguageMetadata element from the internal structure
2043 Element language_metadata = (Element) XMLTools.getChildByTagName(source, StaticStrings.LANGUAGE_METADATA_ELEMENT);
2044 if (language_metadata != null)
2045 {
2046 String meta_name = language_metadata.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2047 Element language_meta = to.createElement(StaticStrings.LANGUAGE_METADATA_ELEMENT_STR);
2048 if (meta_name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && meta_name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1)
2049 {
2050 meta_name = meta_name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
2051 }
2052 language_meta.setAttribute(StaticStrings.NAME_ATTRIBUTE, meta_name);
2053 search.appendChild(language_meta);
2054 }
2055 }
2056
2057 //convert indexes and their displayItems, which go in 'search' element in collectionConfig.xml
2058 //parameter 'to' is the document to be saved as collectionConfig.xml
2059 //parameter 'from' is the internal xml structure
2060 static private void convertIndex(Document from, Document to)
2061 {
2062 Element source = from.getDocumentElement();
2063 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
2064 Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
2065
2066 //THere are two sets of indexes elements, find the one which is assigned 'true'
2067 Element indexes = XMLTools.getNamedElement(source, StaticStrings.INDEXES_ELEMENT, StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2068 if (indexes == null)
2069 {
2070 return;
2071 }
2072 NodeList index_elements = indexes.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
2073 int index_elements_length = index_elements.getLength();
2074
2075 if (index_elements_length == 0)
2076 { // no indexes
2077 return;
2078 }
2079
2080 //find out it's mg or mgpp/lucene
2081 String mg = search.getAttribute(StaticStrings.TYPE_ATTRIBUTE);
2082 boolean mg_indexer = false;
2083 if (mg.equals(StaticStrings.MG_STR))
2084 {
2085 mg_indexer = true;//it's mg, then the level is set as attribute of
2086 }
2087 if (mg_indexer == false)
2088 {
2089 // It's mgpp. Construct 'level' and 'defaultLevel' elements separately.
2090 convertLevels(from, to, search);
2091 }
2092
2093 for (int j = 0; j < index_elements_length; j++)
2094 {
2095 Element index_element = (Element) index_elements.item(j);
2096 if (index_element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
2097 {
2098 continue;
2099 }
2100
2101 Element index_ele = to.createElement(StaticStrings.INDEX_LOW_STR);//index
2102
2103 // Used for creating displayItem for this element 'index_ele' further below
2104 // full_index_names contain 'ex.'
2105 String full_index_name = "";
2106 String level_str = "";
2107
2108 StringBuffer index_value = new StringBuffer();
2109 if (mg_indexer == true)
2110 {
2111 // For mg indexer, there is a 'level' attribute in the index element of the internal structure
2112 // But mgpp/lucene don't
2113 level_str = index_element.getAttribute(StaticStrings.LEVEL_ATTRIBUTE);
2114 if (level_str.length() > 0)
2115 {
2116 index_value.append(level_str).append(StaticStrings.COLON_CHARACTER);
2117 //index_value = index_value.StaticStrings.COLON_CHARACTER;
2118 }
2119 }
2120
2121 NodeList content_elements = index_element.getElementsByTagName(StaticStrings.CONTENT_ELEMENT);
2122 int content_elements_length = content_elements.getLength();
2123
2124 for (int k = 0; k < content_elements_length; k++)
2125 {
2126 Element content_element = (Element) content_elements.item(k);
2127 if (content_element.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
2128 {
2129 continue;
2130 }
2131 String name_str = content_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2132
2133 full_index_name = full_index_name + name_str;
2134 if (k < content_elements_length - 1)
2135 {
2136 full_index_name = full_index_name + StaticStrings.COMMA_CHARACTER;
2137 }
2138
2139 if (name_str.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && name_str.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1)
2140 {
2141 name_str = name_str.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
2142 }
2143
2144 index_value.append(name_str);
2145 name_str = null;
2146 // Make it comma separated string
2147 if (k < content_elements_length - 1)
2148 {
2149 index_value.append(StaticStrings.COMMA_CHARACTER);
2150 }
2151 content_element = null;
2152 }//for loop ends
2153
2154 String temp_str = index_value.toString();
2155 index_ele.setAttribute(StaticStrings.NAME_ATTRIBUTE, temp_str);
2156
2157 // Now constructing 'displayItem' element for this 'index_ele' element
2158 // The index names in the collectionmetadata elements in the internal structure are not the names that
2159 // are used in the content elements (i.e. ex.Source or dc.Subject and keywords), but the names that are
2160 // in the configuration files (i.e. Source or dc.Subject)
2161 ArrayList collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, temp_str);
2162
2163 if (collectionmetadata_list == null)
2164 {
2165 //try the full name, i.e. with 'ex.'
2166 if (mg_indexer == true)
2167 {
2168 // but first append level info if we are mg
2169 full_index_name = level_str + StaticStrings.COLON_CHARACTER + full_index_name;
2170 }
2171 collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, full_index_name);
2172 }
2173
2174 if (collectionmetadata_list != null)
2175 {
2176
2177 for (int k = 0; k < collectionmetadata_list.size(); k++)
2178 {
2179 Element collectionmetadata = (Element) collectionmetadata_list.get(k);
2180 if (collectionmetadata.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
2181 {
2182 continue;
2183 }
2184 Element displayItem = constructDisplayItem(collectionmetadata, to);
2185
2186 index_ele.appendChild(displayItem);
2187 }
2188 }
2189
2190 // deal with any <option name='' value=''> children of this index
2191 // e.g. <option name='solrfieldtype' value='text_es'>
2192 NodeList option_children = index_element.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
2193 for (int k = 0; k < option_children.getLength(); k++)
2194 {
2195 Element el = (Element) option_children.item(k);
2196 if (el.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
2197 {
2198 continue;
2199 }
2200 String name_str = el.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2201 String value_str = XMLTools.getNodeText(el);
2202
2203 if (name_str == null && value_str == null)
2204 {
2205 continue;
2206 }
2207 Element option_element = to.createElement(StaticStrings.OPTION_STR);
2208 if (!name_str.equals("")) {
2209 option_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
2210 }
2211 if (!value_str.equals("")) { // or no value attribute without name attribute?
2212 option_element.setAttribute(StaticStrings.VALUE_ATTRIBUTE, value_str);
2213 }
2214 index_ele.appendChild(option_element);
2215 }
2216
2217 search.appendChild(index_ele);
2218
2219 } //for loop ends
2220
2221 //Convert default index
2222 convertDefaultIndex(from, to, search);
2223 convertIndexOptions(from, to, search);
2224 }
2225
2226 //convert <facet> and <sort> and <defaultSort> elements
2227 //which go in 'search' element in collectionConfig.xml
2228 //parameter 'to' is the document to be saved as collectionConfig.xml
2229 //parameter 'from' is the internal xml structure
2230 // for lucene and solr collections
2231 // just a straight copy at present
2232 // Note that GLI at present does not allow the user to set or modify the solr-specific elements
2233 // <sort> and <facet>. This function, and its inverse doSolrFacetsAndSorts, is only here to preserve such
2234 // elements if they already exist in the collectionConfig.xml. At present they need to be manually added there.
2235 static private void convertSolrFacetsAndSorts(Document from, Document to)
2236 {
2237 Element source = from.getDocumentElement();
2238 //Get the 'search' element from 'to' which has already been created in 'convertBuildType'
2239 Element search = (Element) XMLTools.getChildByTagName(to.getDocumentElement(), StaticStrings.SEARCH_STR);
2240
2241 //There are two sets of <Solr> elements, find the one which is assigned 'true' --- is this true???
2242 Element solr = XMLTools.getNamedElement(source, StaticStrings.SOLR_ELEMENT, StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
2243 if (solr == null)
2244 {
2245 return;
2246 }
2247 NodeList sort_elements = solr.getElementsByTagName(StaticStrings.SORT_LOW_STR);
2248 int sort_elements_length = sort_elements.getLength();
2249
2250 for (int j = 0; j < sort_elements_length; j++)
2251 {
2252 Element sort_ele = (Element) sort_elements.item(j);
2253 search.appendChild(to.importNode(sort_ele, true));
2254 }
2255 Element sort_def = (Element)XMLTools.getChildByTagName(solr, StaticStrings.SORT_DEFAULT_ELEMENT);
2256 if (sort_def != null) {
2257 search.appendChild(to.importNode(sort_def, true));
2258 }
2259 NodeList facet_elements = solr.getElementsByTagName(StaticStrings.FACET_LOW_STR);
2260 int facet_elements_length = facet_elements.getLength();
2261 for (int j = 0; j < facet_elements_length; j++)
2262 {
2263 Element facet_ele = (Element) facet_elements.item(j);
2264 search.appendChild(to.importNode(facet_ele, true));
2265 }
2266
2267
2268 }
2269
2270
2271 // Convert levels for mgpp/lucene. This method is called by converIndex() when mgpp indexer is detected.
2272 static private void convertLevels(Document from, Document to, Element search)
2273 {
2274 Element source = from.getDocumentElement();
2275 Element index_option = XMLTools.getNamedElement(source, StaticStrings.INDEXOPTIONS_ELEMENT, StaticStrings.NAME_ATTRIBUTE, StaticStrings.LEVELS_STR);
2276 if (index_option == null)
2277 {
2278 return;
2279 }
2280 //Debugging purposes
2281 if (index_option.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
2282 {
2283 DebugStream.println("For mgpp, there should be an IndexOption element for levels which is assigned 'true': possible bug.");
2284 }
2285
2286 NodeList option_elements = index_option.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
2287 int num_elements = option_elements.getLength();
2288
2289 // Don't output anything if no indexes are set
2290 if (num_elements == 0)
2291 {
2292 return;//
2293 }
2294
2295 for (int k = 0; k < num_elements; k++)
2296 {
2297 Element e = (Element) option_elements.item(k);
2298 String name_str = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2299 Element level_element = to.createElement(StaticStrings.LEVEL_ELEMENT);
2300 level_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_str);
2301
2302 //Now construct displayItem for this level element from collectionmetadata
2303 ArrayList collectionmetadata_list = XMLTools.getNamedElementList(source, StaticStrings.COLLECTIONMETADATA_ELEMENT, StaticStrings.NAME_ATTRIBUTE, name_str);
2304
2305 if (collectionmetadata_list != null)
2306 {
2307
2308 for (int j = 0; j < collectionmetadata_list.size(); j++)
2309 {
2310 Element collectionmetadata = (Element) collectionmetadata_list.get(j);
2311
2312 Element displayItem = constructDisplayItem(collectionmetadata, to);
2313 level_element.appendChild(displayItem);
2314 }
2315 }
2316 search.appendChild(level_element);
2317 }
2318
2319 //Convert default level
2320 Element default_index_option = XMLTools.getNamedElement(source, StaticStrings.INDEXOPTION_DEFAULT_ELEMENT, StaticStrings.NAME_ATTRIBUTE, StaticStrings.LEVEL_DEFAULT_STR);
2321 if (default_index_option == null)
2322 {
2323 return;
2324 }
2325 Element default_level = to.createElement(StaticStrings.LEVEL_DEFAULT_ELEMENT);
2326 String default_level_str = default_index_option.getAttribute(StaticStrings.VALUE_ATTRIBUTE);
2327 default_level.setAttribute(StaticStrings.NAME_ATTRIBUTE, default_level_str);
2328 search.appendChild(default_level);
2329
2330 }
2331
2332 // Convert indexoptions for mg/mgpp/lucene. This method is called by convertIndex().
2333 static private void convertIndexOptions(Document from, Document to, Element search)
2334 {
2335 Element source = from.getDocumentElement();
2336 Element index_option = XMLTools.getNamedElement(source, StaticStrings.INDEXOPTIONS_ELEMENT, StaticStrings.NAME_ATTRIBUTE, StaticStrings.INDEXOPTIONS_STR);
2337 if (index_option == null)
2338 {
2339 return;
2340 }
2341 //Debugging purposes
2342 if (index_option.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
2343 {
2344 DebugStream.println("There should be an IndexOption element which is assigned 'true': possible bug.");
2345
2346 // for lucene and solr collections, don't write out stemming, casefolding and accentfolding indexOptions
2347 // since they are not meant to be editable: stemming and casefolding are always on in lucene
2348 String buildtype_value = search.getAttribute(StaticStrings.TYPE_ATTRIBUTE); //might be mg|mgpp|lucene|solr
2349 if(buildtype_value.equalsIgnoreCase("solr") || buildtype_value.equalsIgnoreCase("lucene")) {
2350 return;
2351 }
2352 }
2353 //Element indexOptionEl = to.createElement(StaticStrings.INDEXOPTION_STR);
2354 NodeList option_elements = index_option.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
2355 int num_elements = option_elements.getLength();
2356 // Don't output anything if no index
2357 if (num_elements == 0)
2358 {
2359 return;//
2360 }
2361 //search.appendChild(indexOptionEl);
2362
2363 for (int k = 0; k < num_elements; k++)
2364 {
2365 Element e = (Element) option_elements.item(k);
2366 String name_att = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2367 Element optionEl = to.createElement(StaticStrings.INDEXOPTION_STR);
2368 optionEl.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_att);
2369 // default value?? on/off
2370 //indexOptionEl.appendChild(optionEl);
2371 search.appendChild(optionEl);
2372 }
2373
2374 }
2375
2376 // handle top level elements which GLI knows nothing about
2377 // we store them internally in a Unknown element for easy access when
2378 // we write them out.
2379 static private void doUnknownElements(Document to, Element from)
2380 {
2381 Element toElement = to.getDocumentElement();
2382 Element unknownElement = to.createElement(StaticStrings.UNKNOWN_ELEMENT);
2383 toElement.appendChild(unknownElement);
2384
2385 Node child = from.getFirstChild();
2386 while (child != null)
2387 {
2388 if (child.getNodeType() == Node.ELEMENT_NODE && !known_element_names.contains(child.getNodeName()))
2389 {
2390 unknownElement.appendChild(XMLTools.duplicateElement(to, (Element) child, true));
2391 }
2392 child = child.getNextSibling();
2393 }
2394
2395 }
2396
2397 // just copy all children of Unknown element into output doc.
2398 static private void convertUnknownElements(Document from, Document to)
2399 {
2400
2401 Element toElement = to.getDocumentElement();
2402 Node unknown_element = XMLTools.getChildByTagName(from.getDocumentElement(), StaticStrings.UNKNOWN_ELEMENT);
2403
2404 Node child = unknown_element.getFirstChild();
2405 while (child != null)
2406 {
2407 Element to_element = XMLTools.duplicateElement(to, (Element) child, true);
2408 toElement.appendChild(to_element);
2409
2410 child = child.getNextSibling();
2411 }
2412
2413 }
2414
2415 // Append the element son to the element mother in the appropriate position.
2416 static public void appendProperly(Element mother, Element son)
2417 {
2418 if (son == null)
2419 return;
2420
2421 Node reference_node = findInsertionPoint(mother, son);
2422 if (reference_node != null)
2423 {
2424 mother.insertBefore(son, reference_node);
2425 }
2426 else
2427 {
2428 mother.appendChild(son);
2429 }
2430 }
2431
2432 /**
2433 * Find the best insertion position for the given DOM Element
2434 * 'target_element' in the DOM Element 'document_element'. This should try
2435 * to match command tag, and if found should then try to group by name or
2436 * type (eg CollectionMeta), or append to end is no such grouping exists (eg
2437 * Plugins). Failing a command match it will check against the command order
2438 * for the best insertion location.
2439 *
2440 * @param target_element
2441 * the command Element to be inserted
2442 * @return the Element which the given command should be inserted before, or
2443 * null to append to end of list
2444 */
2445 static public Node findInsertionPoint(Element document_element, Element target_element)
2446 {
2447 ///ystem.err.println("Find insertion point: " + target_element.getNodeName());
2448 String target_element_name = target_element.getNodeName();
2449
2450 // Try to find commands with the same tag.
2451 NodeList matching_elements = document_element.getElementsByTagName(target_element_name);
2452 // If we found matching elements, then we have our most likely insertion location, so check within for groupings
2453 if (matching_elements.getLength() != 0)
2454 {
2455 ///ystem.err.println("Found matching elements.");
2456 // Only CollectionMeta are grouped.
2457 if (target_element_name.equals(StaticStrings.COLLECTIONMETADATA_ELEMENT))
2458 {
2459 ///ystem.err.println("Dealing with collection metadata");
2460 // 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.
2461 // So if the command to be added is special add it immediately after any other special command
2462 if (target_element.getAttribute(StaticStrings.SPECIAL_ATTRIBUTE).equals(StaticStrings.TRUE_STR))
2463 {
2464 int index = 0;
2465 Element matched_element = (Element) matching_elements.item(index);
2466 Element sibling_element = (Element) matched_element.getNextSibling();
2467 while (sibling_element.getAttribute(StaticStrings.SPECIAL_ATTRIBUTE).equals(StaticStrings.TRUE_STR))
2468 {
2469 index++;
2470 matched_element = (Element) matching_elements.item(index);
2471 sibling_element = (Element) matched_element.getNextSibling();
2472 }
2473 if (sibling_element.getNodeName().equals(CollectionConfiguration.NEWLINE_ELEMENT))
2474 {
2475 Element newline_element = document_element.getOwnerDocument().createElement(CollectionConfiguration.NEWLINE_ELEMENT);
2476 document_element.insertBefore(newline_element, sibling_element);
2477 }
2478 return sibling_element;
2479 }
2480 // Otherwise try to find a matching 'name' and add after the last one in that group.
2481 else
2482 {
2483 int index = 0;
2484 target_element_name = target_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2485 boolean found = false;
2486 // Skip all of the special metadata
2487 Element matched_element = (Element) matching_elements.item(index);
2488 while (matched_element.getAttribute(StaticStrings.SPECIAL_ATTRIBUTE).equals(StaticStrings.TRUE_STR))
2489 {
2490 index++;
2491 matched_element = (Element) matching_elements.item(index);
2492 }
2493 // Begin search
2494 while (!found && matched_element != null)
2495 {
2496 if (matched_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equals(target_element_name))
2497 {
2498 found = true;
2499 }
2500 else
2501 {
2502 index++;
2503 matched_element = (Element) matching_elements.item(index);
2504 }
2505 }
2506 // If we found a match, we need to continue checking until we find the last name match.
2507 if (found)
2508 {
2509 index++;
2510 Element previous_sibling = matched_element;
2511 Element sibling_element = (Element) matching_elements.item(index);
2512 while (sibling_element != null && sibling_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equals(target_element_name))
2513 {
2514 previous_sibling = sibling_element;
2515 index++;
2516 sibling_element = (Element) matching_elements.item(index);
2517 }
2518 // 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!
2519 return previous_sibling.getNextSibling();
2520 }
2521 // If not found we just add after last metadata element
2522 else
2523 {
2524 Element last_element = (Element) matching_elements.item(matching_elements.getLength() - 1);
2525 return last_element.getNextSibling();
2526 }
2527 }
2528
2529 }
2530 else
2531 {
2532 ///ystem.err.println("Not dealing with collection meta.");
2533 Element matched_element = (Element) matching_elements.item(matching_elements.getLength() - 1);
2534 // 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)
2535 Node sibling_element = matched_element.getNextSibling();
2536 if (sibling_element != null && sibling_element.getNodeName().equals(CollectionConfiguration.NEWLINE_ELEMENT))
2537 {
2538 Element newline_element = document_element.getOwnerDocument().createElement(CollectionConfiguration.NEWLINE_ELEMENT);
2539 document_element.insertBefore(newline_element, sibling_element);
2540 }
2541 return sibling_element; // Note that this may be null
2542 }
2543 }
2544 ///ystem.err.println("No matching elements found.");
2545 // Locate where this command is in the ordering
2546 int command_index = -1;
2547 for (int i = 0; command_index == -1 && i < CollectionConfiguration.COMMAND_ORDER.length; i++)
2548 {
2549 if (CollectionConfiguration.COMMAND_ORDER[i].equals(target_element_name))
2550 {
2551 command_index = i;
2552 }
2553 }
2554 ///ystem.err.println("Command index is: " + command_index);
2555 // Now move forward, checking for existing elements in each of the preceeding command orders.
2556 int preceeding_index = command_index - 1;
2557 ///ystem.err.println("Searching before the target command.");
2558 while (preceeding_index >= 0)
2559 {
2560 matching_elements = document_element.getElementsByTagName(CollectionConfiguration.COMMAND_ORDER[preceeding_index]);
2561 // If we've found a match
2562 if (matching_elements.getLength() > 0)
2563 {
2564 // We add after the last element
2565 Element matched_element = (Element) matching_elements.item(matching_elements.getLength() - 1);
2566 // 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)
2567 Node sibling_element = matched_element.getNextSibling();
2568 if (sibling_element != null && sibling_element.getNodeName().equals(CollectionConfiguration.NEWLINE_ELEMENT))
2569 {
2570 Element newline_element = document_element.getOwnerDocument().createElement(CollectionConfiguration.NEWLINE_ELEMENT);
2571 document_element.insertBefore(newline_element, sibling_element);
2572 }
2573 return sibling_element; // Note that this may be null
2574 }
2575 preceeding_index--;
2576 }
2577 // If all that fails, we now move backwards through the commands
2578 int susceeding_index = command_index + 1;
2579 ///ystem.err.println("Searching after the target command.");
2580 while (susceeding_index < CollectionConfiguration.COMMAND_ORDER.length)
2581 {
2582 matching_elements = document_element.getElementsByTagName(CollectionConfiguration.COMMAND_ORDER[susceeding_index]);
2583 // If we've found a match
2584 if (matching_elements.getLength() > 0)
2585 {
2586 // We add before the first element
2587 Element matched_element = (Element) matching_elements.item(0);
2588 // 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)
2589 Node sibling_element = matched_element.getPreviousSibling();
2590 if (sibling_element != null && sibling_element.getNodeName().equals(CollectionConfiguration.NEWLINE_ELEMENT))
2591 {
2592 Element newline_element = document_element.getOwnerDocument().createElement(CollectionConfiguration.NEWLINE_ELEMENT);
2593 document_element.insertBefore(newline_element, sibling_element);
2594 }
2595 return sibling_element; // Note that this may be null
2596 }
2597 susceeding_index++;
2598 }
2599 // Well. Apparently there are no other commands in this collection configuration. So append away...
2600 return null;
2601 }
2602
2603 // From collectionConfig.xml to internal structure:add 'ex.' namespace (if none).
2604 // From internal structure to collectionConfig.xml:always peel off 'ex.' namespace (if any), except for format statement
2605 //This method parses 'xml_file_doc' into 'dOc'
2606 static public void parse(File xml_file, Document dOc)
2607 {
2608
2609 Document xml_file_doc = XMLTools.parseXMLFile(xml_file);
2610 Element fromElement = xml_file_doc.getDocumentElement();
2611 Element toElement = dOc.getDocumentElement();
2612
2613 // security element. For now, we just save as is, as you can't edit this in GLI.
2614 Node securityNode = XMLTools.getChildByTagNameIndexed(fromElement, StaticStrings.SECURITY_STR,0);
2615 // just save for now
2616 if (securityNode != null) {
2617 Element to_element = XMLTools.duplicateElement(dOc, (Element) securityNode, true);
2618 toElement.appendChild(to_element);
2619 }
2620 // It's deliberately set that 'creator', 'maintainer', and 'public' are only in English (as they are just names).
2621 // So the following ArrayList have only one element.
2622 Node metadataListNode = XMLTools.getChildByTagNameIndexed(fromElement, StaticStrings.METADATALIST_STR, 0);
2623 if (metadataListNode != null)
2624 {
2625 ArrayList creator = doMetadataList(dOc, metadataListNode, StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT, StaticStrings.COLLECTIONMETADATA_CREATOR_STR);
2626 ArrayList maintainer = doMetadataList(dOc, metadataListNode, StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT, StaticStrings.COLLECTIONMETADATA_MAINTAINER_STR);
2627 ArrayList is_public = doMetadataList(dOc, metadataListNode, StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT, StaticStrings.COLLECTIONMETADATA_PUBLIC_STR);
2628
2629 appendArrayList(toElement, creator);
2630 appendArrayList(toElement, maintainer);
2631 appendArrayList(toElement, is_public);
2632 }
2633
2634 Node databaseNode = XMLTools.getChildByTagNameIndexed(fromElement, StaticStrings.INFODB_STR, 0);
2635 String databasetype_value = "gdbm";
2636 if (databaseNode != null)
2637 {
2638 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)
2639 }
2640
2641 Element databasetype = doDatabaseType(dOc, databasetype_value);
2642 appendProperly(toElement, databasetype);
2643
2644 Node searchNode = XMLTools.getChildByTagNameIndexed(fromElement, StaticStrings.SEARCH_STR, 0);
2645 String buildtype_value = ((Element) searchNode).getAttribute(StaticStrings.TYPE_ATTRIBUTE);//might be mg|mgpp|lucene
2646 Element buildtype = doBuildType(dOc, buildtype_value);
2647 appendProperly(toElement, buildtype);
2648
2649 Node importNode = XMLTools.getChildByTagNameIndexed(fromElement, StaticStrings.IMPORT_STR, 0);
2650 if (importNode == null)
2651 {
2652 System.out.println("There is no content in the 'import' block.");
2653 }
2654 if (importNode != null)
2655 {
2656 //do plugin list nodes
2657 Node pluginListNode = XMLTools.getChildByTagNameIndexed((Element) importNode, StaticStrings.PLUGINLIST_STR, 0);
2658 if (pluginListNode == null)
2659 {
2660 System.out.println("There is no pluginlist set.");
2661 }
2662 if (pluginListNode != null)
2663 {
2664
2665 doPlugins(dOc, pluginListNode);
2666 }
2667
2668 //do the plugout element (used by building flax collections)
2669 Node plugout = XMLTools.getChildByTagNameIndexed((Element) importNode, PLUGOUT_ELEMENT, 0);
2670 if (plugout != null)
2671 {
2672 Element to_element = XMLTools.duplicateElement(dOc, (Element) plugout, true);
2673 toElement.appendChild(to_element);
2674 }
2675 }
2676
2677 Node browseNode = XMLTools.getChildByTagNameIndexed(fromElement, StaticStrings.BROWSE_STR, 0);
2678 if (browseNode != null)
2679 {
2680 if (browseNode == null)
2681 {
2682 System.out.println("There is no classifier.");
2683 }
2684 doClassifier(dOc, browseNode);
2685 }
2686
2687 Node displayItemListNode = XMLTools.getChildByTagNameIndexed(fromElement, StaticStrings.DISPLAYITEMLIST_STR, 0);
2688 if (displayItemListNode != null)
2689 {
2690 ArrayList description = doDisplayItemList(dOc, displayItemListNode, StaticStrings.DESCRIPTION_STR, StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
2691 ArrayList smallicon = doDisplayItemList(dOc, displayItemListNode, StaticStrings.SMALLICON_STR, StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR);
2692 ArrayList icon = doDisplayItemList(dOc, displayItemListNode, StaticStrings.ICON_STR, StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR);
2693 ArrayList name = doDisplayItemList(dOc, displayItemListNode, StaticStrings.NAME_STR, StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
2694
2695 appendArrayList(toElement, description);
2696 appendArrayList(toElement, smallicon);
2697 appendArrayList(toElement, icon);
2698 appendArrayList(toElement, name);
2699 }
2700
2701 if (buildtype_value.equalsIgnoreCase("mg"))
2702 {
2703 doMGIndexes(dOc, searchNode);
2704 }
2705 else
2706 {
2707 doMGPPIndexes(dOc, searchNode);
2708 }
2709
2710 if(buildtype_value.equalsIgnoreCase("solr") || buildtype_value.equalsIgnoreCase("lucene")) {
2711 doSolrFacetsAndSorts(dOc, searchNode); // <facet><displayItem /></facet> and <sort><displayItem /></sort>
2712 // lucene will only have sort elements
2713 }
2714
2715 doDefaultIndex(dOc, searchNode);
2716 doDefaultLevel(dOc, searchNode);
2717 doLevel(dOc, searchNode);
2718 doIndexOption(dOc, searchNode);
2719 doSubcollection(dOc, searchNode);
2720 doIndexSubcollection(dOc, searchNode);
2721 doIndexLanguage(dOc, searchNode);
2722 doDefaultIndexLanguage(dOc, searchNode);
2723 doLanguageMetadata(dOc, searchNode);
2724 doSearchType(dOc, searchNode);
2725 doSearchFormat(dOc, searchNode);
2726 doGlobalFormat(dOc, fromElement);
2727 doDisplayFormat(dOc, fromElement);
2728 doReplaceListRef(dOc, fromElement);
2729 doReplaceList(dOc, fromElement);
2730 doServiceRackList(dOc, fromElement);
2731 doUnknownElements(dOc, fromElement);
2732 // the official displayItems in the displayItemList element have already been handled above
2733 // and created as collectionmetadata elements in the dOc object
2734 // Now we add in all the *other* (remaining) displayItems as collectionmeta elements
2735 NodeList collectionMetadataList = dOc.getDocumentElement().getElementsByTagName(StaticStrings.COLLECTIONMETADATA_ELEMENT);
2736 Set setOfUniqueColMetaNames = new HashSet();
2737 for (int i = 0; i < collectionMetadataList.getLength(); i++)
2738 {
2739 Element colMeta = (Element) collectionMetadataList.item(i);
2740 String name = colMeta.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2741 setOfUniqueColMetaNames.add(name);
2742 }
2743
2744 if (displayItemListNode != null)
2745 {
2746 NodeList nl = ((Element) displayItemListNode).getElementsByTagName(StaticStrings.DISPLAYITEM_STR);
2747
2748 // make a list of all unique attribute names that are specifically not
2749 // description, smallicon, icon and name, since these are already processed above
2750 Set setOfUniqueDisplayItemNames = new LinkedHashSet();
2751 for (int i = 0; i < nl.getLength(); i++)
2752 {
2753 Element displayItem = (Element) nl.item(i);
2754 String name = displayItem.getAttribute(StaticStrings.NAME_ATTRIBUTE);
2755
2756 if (name.equals(""))
2757 continue; // no name attribute
2758 if (setOfUniqueColMetaNames.contains(name))
2759 continue;
2760
2761 if (name.equals(StaticStrings.DESCRIPTION_STR))
2762 continue;
2763 if (name.equals(StaticStrings.SMALLICON_STR))
2764 continue;
2765 if (name.equals(StaticStrings.ICON_STR))
2766 continue;
2767 if (name.equals(StaticStrings.NAME_STR))
2768 continue;
2769 // don't add displayItems that are handled by the indexers, etc. E.g. document:ex.Title
2770 if (name.indexOf(":") != -1)
2771 continue;
2772
2773 // otherwise
2774 setOfUniqueDisplayItemNames.add(name); // Set will ensure no duplicate names
2775 }
2776
2777 Iterator i = setOfUniqueDisplayItemNames.iterator();
2778 while (i.hasNext())
2779 {
2780 String displayItemName = (String) i.next();
2781
2782 ArrayList custom_displayItem = doDisplayItemList(dOc, displayItemListNode, displayItemName, displayItemName);
2783 appendArrayList(toElement, custom_displayItem);
2784 }
2785 }
2786
2787 }
2788
2789 static public String generateStringVersion(Document doc)
2790 {
2791 return XMLTools.xmlNodeToString(doc);
2792 }
2793
2794 static public void save(File collect_config_xml_file, Document doc)
2795 {
2796 Document collection_config_xml_document = convertInternalToCollectionConfig(doc);
2797 String[] nonEscapingTagNames = { StaticStrings.FORMAT_STR, StaticStrings.DISPLAYITEM_STR };
2798 XMLTools.writeXMLFile(collect_config_xml_file, collection_config_xml_document, nonEscapingTagNames);
2799 }
2800
2801 //Convert the internal XML DOM tree (dOc) into that of collectionConfig.xml (skeleton)
2802 static private Document convertInternalToCollectionConfig(Document dOc)
2803 {
2804 //first parse an empty skeleton of xml config file
2805 //The aim is to convert the internal structure into this skeleton
2806 // This skeleton just has the CollectionConfig element and nothing else
2807 Document skeleton = XMLTools.parseXMLFile("xml/CollectionConfig.xml", true);
2808 //Element internal = dOc.getDocumentElement();
2809 convertSecurity(dOc, skeleton);
2810 convertMetadataList(dOc, skeleton);
2811 convertDisplayItemList(dOc, skeleton);
2812 convertGlobalFormat(dOc, skeleton);
2813 convertBuildType(dOc, skeleton);
2814 convertDatabaseType(dOc, skeleton);
2815 convertIndex(dOc, skeleton);
2816 convertSolrFacetsAndSorts(dOc, skeleton);
2817 convertPlugins(dOc, skeleton);//also do the plugout element
2818 convertClassifier(dOc, skeleton);
2819 convertSubcollectionIndexes(dOc, skeleton);
2820 convertLanguages(dOc, skeleton);
2821 convertSubcollection(dOc, skeleton);
2822 convertSearchType(dOc, skeleton);
2823 convertSearchFormat(dOc, skeleton);
2824 convertDisplayFormat(dOc, skeleton);
2825 convertReplaceListRef(dOc, skeleton);
2826 convertReplaceList(dOc, skeleton);
2827 convertServiceRackList(dOc, skeleton);
2828 convertUnknownElements(dOc, skeleton); // try to catch everything GLI doesn't know about
2829
2830 return skeleton;
2831 }
2832
2833 // Append the elements, which are of Element type, in 'list' to Element 'to'
2834 static private void appendArrayList(Element to, ArrayList list)
2835 {
2836 if (list == null)
2837 return;
2838
2839 for (int i = 0; i < list.size(); i++)
2840 {
2841 appendProperly(to, (Element) list.get(i));
2842 }
2843 }
2844
2845}
Note: See TracBrowser for help on using the repository browser.