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

Last change on this file since 36259 was 36259, checked in by kjdon, 23 months ago

got rid of some debug strings, and commented out code

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