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

Last change on this file since 36272 was 36263, checked in by kjdon, 2 years ago

rank and none are also special words (for sortfields), along with text and allfields (for indexes), so don't put ex. with them

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