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

Last change on this file since 26572 was 26572, checked in by sjm84, 11 years ago

Removed an unnecessary print statement

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