- Timestamp:
- 2012-10-18T15:44:38+13:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfigXMLReadWrite.java
r26056 r26355 30 30 import java.io.File; 31 31 import java.util.ArrayList; 32 import java.util.Arrays; 32 33 import java.util.HashMap; 33 34 import java.util.HashSet; … … 52 53 static final private String PLUGOUT_ELEMENT = "plugout";//used by building flax collections 53 54 55 // a list of all known top level elements 56 static final private String known_element_names_array[] = {StaticStrings.METADATALIST_STR, StaticStrings.DISPLAYITEMLIST_STR, StaticStrings.FORMAT_STR, StaticStrings.SEARCH_STR, StaticStrings.INFODB_STR, StaticStrings.BROWSE_STR, StaticStrings.IMPORT_STR, StaticStrings.DISPLAY_STR, StaticStrings.REPLACELISTREF_STR, StaticStrings.REPLACELIST_STR, StaticStrings.SERVICE_RACK_LIST_ELEMENT}; 57 static final private Set known_element_names = new HashSet(Arrays.asList(known_element_names_array)); 54 58 /** 55 59 * ************************************************************************* 56 * ******************************* The code from this point below areused60 * ******************************* The code in this file is used 57 61 * for greenstone 3 collection configuration, i.e., read ColletionConfig.xml 58 62 * into the internal DOM tree, and convert the internal DOM tree back to … … 160 164 Element index_element = to.createElement(StaticStrings.INDEX_ELEMENT);//<Index> 161 165 162 // For mg, it's the 'Old G2.38 and earlier' that use level:source tuplets, but we double check it anyway163 boolean old_index = true;164 166 if (index_str.indexOf(StaticStrings.COLON_CHARACTER) == -1) 165 167 { 166 168 // It doesn't contain ':' character 167 169 System.err.println("Something is wrong! the index should be level:source tuplets."); 168 old_index = false; 170 // assume document level 171 index_element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, StaticStrings.DOCUMENT_STR); 169 172 } 170 173 else … … 172 175 // Handling 'index' element 173 176 index_element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, index_str.substring(0, index_str.indexOf(StaticStrings.COLON_CHARACTER))); 177 174 178 index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1); 175 179 } 176 180 //Each index may have a list of comma-separated strings. 177 181 //split them into 'content' elements in the internal structure … … 186 190 // Since the contents of indexes have to be certain keywords, or metadata elements, 187 191 //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 )192 if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR))) 189 193 { 190 if (content_str.equals(StaticStrings.TEXT_STR) || (!old_index && content_str.equals(StaticStrings.ALLFIELDS_STR)))191 {192 // in this case, do nothing193 }194 else195 {196 194 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str; 197 }195 198 196 } 199 197 … … 209 207 ArrayList collectionmetadata_list = doDisplayItemList(to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display); 210 208 appendArrayList(toElement, collectionmetadata_list); 211 } //else ends212 209 } //for loop ends 213 210 appendProperly(toElement, indexes_element); … … 268 265 // Handling 'index' element 269 266 // Double check to make sure it's not colon separated style index. 270 boolean old_index = false;271 267 if (index_str.indexOf(StaticStrings.COLON_CHARACTER) != -1) 272 268 { 273 269 System.err.println("Something is wrong! the index should NOT be level:source tuplets style."); 274 old_index = true; 270 index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1); 271 275 272 } 276 273 //Each index may have a list of comma-separated strings. 277 274 //split them into 'content' elements in the internal structure 278 275 StringTokenizer content_tokenizer = new StringTokenizer(index_str, StaticStrings.COMMA_CHARACTER); 279 //index_str = "";280 276 while (content_tokenizer.hasMoreTokens()) 281 277 { … … 285 281 String content_str = content_tokenizer.nextToken(); 286 282 // 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 { 283 if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR)|| content_str.equals(StaticStrings.ALLFIELDS_STR))) 284 { 295 285 content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str; 296 }297 286 } 298 287 content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, content_str); … … 1012 1001 1013 1002 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 } 1003 XMLTools.duplicateElementList(to, toElement, replace_elements, true); 1004 } 1005 // int num_elems = replace_elements.getLength(); 1006 // if (num_elems < 1) 1007 // { 1008 // return; 1009 // } 1010 // for (int i = 0; i < num_elems; i++) 1011 // { 1012 // Element to_element = XMLTools.duplicateElement(to, (Element) replace_elements.item(i), true); 1013 // toElement.appendChild(to_element); 1014 // } 1015 // } 1025 1016 1026 1017 static private void convertReplaceListRef(Document from, Document to) … … 1029 1020 1030 1021 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 1022 XMLTools.duplicateElementList(to, toElement, replace_elements, true); 1023 } 1024 // int num_elems = replace_elements.getLength(); 1025 // if (num_elems < 1) 1026 // { 1027 // return; 1028 // } 1029 // for (int i = 0; i < num_elems; i++) 1030 // { 1031 // Element to_element = XMLTools.duplicateElement(to, (Element) replace_elements.item(i), true); 1032 // toElement.appendChild(to_element); 1033 // } 1034 // } 1035 1036 /* replacelist currently not editable in GLI, just copy it in and back out again */ 1037 static private void doReplaceList(Document to, Element from) 1038 { 1039 Element toElement = to.getDocumentElement(); 1040 1041 Node rl_element = XMLTools.getChildByTagName(from, StaticStrings.REPLACELIST_STR); 1042 if (rl_element == null) 1043 { 1044 return; // such an element not found 1045 } 1046 1047 Element to_element = XMLTools.duplicateElement(to, (Element) rl_element, true); 1048 toElement.appendChild(to_element); 1049 } 1050 1051 static private void convertReplaceList(Document from, Document to) 1052 { 1053 Element toElement = to.getDocumentElement(); 1054 1055 Node rl_element = XMLTools.getChildByTagName(from.getDocumentElement(), StaticStrings.REPLACELIST_STR); 1056 if (rl_element == null) 1057 { 1058 return; // such an element not found 1059 } 1060 1061 Element to_element = XMLTools.duplicateElement(to, (Element) rl_element, true); 1062 toElement.appendChild(to_element); 1063 } 1064 1043 1065 /** 1044 1066 * serviceRackList is currently not editable in GLI - just copy it in from … … 1097 1119 { 1098 1120 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(); 1121 //Node index_option_node = XMLTools.getChildByTagName(searchNode, StaticStrings.INDEXOPTION_STR); 1122 //if (index_option_node == null) 1123 //{ 1124 // return; 1125 //} 1126 //NodeList option_children = ((Element) index_option_node).getElementsByTagName(StaticStrings.OPTION_STR); 1127 NodeList option_children = ((Element) searchNode).getElementsByTagName(StaticStrings.INDEXOPTION_STR); 1128 int num_options = option_children.getLength(); 1106 1129 1107 1130 // 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)1131 if (num_options < 1) 1109 1132 { 1110 1133 Element index_option = to.createElement(StaticStrings.INDEXOPTIONS_ELEMENT); … … 1126 1149 index_option.setAttribute(StaticStrings.NAME_STR, StaticStrings.INDEXOPTIONS_STR); 1127 1150 1128 for (int i = 0; i < option_nodes; i++)1151 for (int i = 0; i < num_options; i++) 1129 1152 { 1130 1153 String option_str = ((Element) option_children.item(i)).getAttribute(StaticStrings.NAME_ATTRIBUTE); … … 2056 2079 DebugStream.println("There should be an IndexOption element which is assigned 'true': possible bug."); 2057 2080 } 2058 Element indexOptionEl = to.createElement(StaticStrings.INDEXOPTION_STR);2081 //Element indexOptionEl = to.createElement(StaticStrings.INDEXOPTION_STR); 2059 2082 NodeList option_elements = index_option.getElementsByTagName(StaticStrings.OPTION_ELEMENT); 2060 2083 int num_elements = option_elements.getLength(); … … 2064 2087 return;// 2065 2088 } 2066 search.appendChild(indexOptionEl);2089 //search.appendChild(indexOptionEl); 2067 2090 2068 2091 for (int k = 0; k < num_elements; k++) … … 2070 2093 Element e = (Element) option_elements.item(k); 2071 2094 String name_att = e.getAttribute(StaticStrings.NAME_ATTRIBUTE); 2072 Element optionEl = to.createElement(StaticStrings. OPTION_STR);2095 Element optionEl = to.createElement(StaticStrings.INDEXOPTION_STR); 2073 2096 optionEl.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_att); 2074 indexOptionEl.appendChild(optionEl); 2075 } 2076 2077 } 2097 // default value?? on/off 2098 //indexOptionEl.appendChild(optionEl); 2099 search.appendChild(optionEl); 2100 } 2101 2102 } 2103 2104 // handle top level elements which GLI knows nothing about 2105 // we store them internally in a Unknown element for easy access when 2106 // we write them out. 2107 static private void doUnknownElements(Document to, Element from) { 2108 Element toElement = to.getDocumentElement(); 2109 Element unknownElement = to.createElement(StaticStrings.UNKNOWN_ELEMENT); 2110 toElement.appendChild(unknownElement); 2111 2112 Node child = from.getFirstChild(); 2113 while (child != null) 2114 { 2115 System.err.println("child name=" +child.getNodeName()); 2116 if (child.getNodeType() == Node.ELEMENT_NODE && ! known_element_names.contains(child.getNodeName())) { 2117 unknownElement.appendChild(XMLTools.duplicateElement(to, (Element) child, true)); 2118 } 2119 child = child.getNextSibling(); 2120 } 2121 2122 } 2123 2124 // just copy all children of Unknown element into output doc. 2125 static private void convertUnknownElements(Document from, Document to) { 2126 2127 Element toElement = to.getDocumentElement(); 2128 Node unknown_element = XMLTools.getChildByTagName(from.getDocumentElement(), StaticStrings.UNKNOWN_ELEMENT); 2129 2130 Node child = unknown_element.getFirstChild(); 2131 while (child != null) 2132 { 2133 Element to_element = XMLTools.duplicateElement(to, (Element) child, true); 2134 toElement.appendChild(to_element); 2135 2136 child = child.getNextSibling(); 2137 } 2138 2139 } 2078 2140 2079 2141 // Append the element son to the element mother in the appropriate position. … … 2379 2441 doDisplayFormat(dOc, fromElement); 2380 2442 doReplaceListRef(dOc, fromElement); 2443 doReplaceList(dOc, fromElement); 2381 2444 doServiceRackList(dOc, fromElement); 2382 2445 doUnknownElements(dOc, fromElement); 2383 2446 // the official displayItems in the displayItemList element have already been handled above 2384 2447 // and created as collectionmetadata elements in the dOc object … … 2445 2508 //first parse an empty skeleton of xml config file 2446 2509 //The aim is to convert the internal structure into this skeleton 2510 // This skeleton just has the CollectionConfig element and nothing else 2447 2511 Document skeleton = XMLTools.parseXMLFile("xml/CollectionConfig.xml", true); 2448 2512 //Element internal = dOc.getDocumentElement(); … … 2462 2526 convertDisplayFormat(dOc, skeleton); 2463 2527 convertReplaceListRef(dOc, skeleton); 2528 convertReplaceList(dOc, skeleton); 2464 2529 convertServiceRackList(dOc, skeleton); 2530 convertUnknownElements(dOc, skeleton); // try to catch everything GLI doesn't know about 2465 2531 2466 2532 return skeleton;
Note:
See TracChangeset
for help on using the changeset viewer.