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

Last change on this file since 26062 was 26056, checked in by ak19, 12 years ago

Need to trim the search types that get written out to the collectionConfig.xml file, else there can be extraneous whitespace in the form of a newline before the terminating quote, which causes the FieldQuery to not turn up in the ServiceList XML being read in by the Greenstone server code when the TextQuery is not also present.

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