Index: /trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java
===================================================================
--- /trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java	(revision 12095)
+++ /trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java	(revision 12096)
@@ -58,17 +58,4 @@
     static public Document document;
 
-    static public void main(String[] args) {
-	if(args.length >= 1) {
-	    File file = new File(args[0]);
-	    CollectionConfiguration collect_cfg = new CollectionConfiguration(file);
-	    collect_cfg.save(true);
-	    collect_cfg.save(false);
-	    collect_cfg = null;
-	}
-	else {
-	    System.out.println("Usage: CollectionConfiguration <filename>");
-	}
-    }
-
     /** Find the best insertion position for the given DOM Element. This should try to match command tag, and if found should then try to group by name or type (eg CollectionMeta), or append to end is no such grouping exists (eg Plugins). Failing a command match it will check against the command order for the best insertion location.
      * @param target_element the command Element to be inserted
@@ -232,4 +219,7 @@
 	    return levelsToString(command_element);
 	}
+	else if(command_element_name.equals(LEVEL_DEFAULT_ELEMENT)) {
+	    return levelDefaultToString(command_element);
+	}
 	else if(command_element_name.equals(COLLECTIONMETADATA_ELEMENT)) {
 	    return metadataToString(command_element, show_extracted_namespace);
@@ -250,7 +240,7 @@
 	    return pluginToString(command_element, show_extracted_namespace);
 	}
-	else if(command_element_name.equals(SEARCHTYPE_ELEMENT)) {
-	    return searchtypeToString(command_element);
-	}
+// 	else if(command_element_name.equals(SEARCHTYPE_ELEMENT)) {
+// 	    return searchtypeToString(command_element);
+// 	}
 	else if(command_element_name.equals(SUBCOLLECTION_ELEMENT)) {
 	    return subcollectionToString(command_element, show_extracted_namespace);
@@ -310,5 +300,5 @@
 
     /** Gives the preferred ordering of commands */
-    static final private String[] COMMAND_ORDER = {StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT, StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT, StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT,  StaticStrings.SEARCHTYPE_ELEMENT, StaticStrings.BUILDTYPE_ELEMENT, StaticStrings.PLUGIN_ELEMENT, StaticStrings.INDEXES_ELEMENT, StaticStrings.LEVELS_ELEMENT, StaticStrings.INDEX_DEFAULT_ELEMENT, StaticStrings.LANGUAGES_ELEMENT, StaticStrings.LANGUAGE_DEFAULT_ELEMENT, StaticStrings.SUBCOLLECTION_ELEMENT, StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT, StaticStrings.SUBCOLLECTION_DEFAULT_INDEX_ELEMENT, StaticStrings.SUPERCOLLECTION_ELEMENT, StaticStrings.CLASSIFY_ELEMENT, StaticStrings.FORMAT_ELEMENT, StaticStrings.COLLECTIONMETADATA_ELEMENT};
+    static final private String[] COMMAND_ORDER = {StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT, StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT, StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT,  /*StaticStrings.SEARCHTYPE_ELEMENT,*/ StaticStrings.BUILDTYPE_ELEMENT, StaticStrings.PLUGIN_ELEMENT, StaticStrings.INDEXES_ELEMENT, StaticStrings.INDEX_DEFAULT_ELEMENT, StaticStrings.LEVELS_ELEMENT, StaticStrings.LEVEL_DEFAULT_ELEMENT, StaticStrings.LANGUAGES_ELEMENT, StaticStrings.LANGUAGE_DEFAULT_ELEMENT, StaticStrings.SUBCOLLECTION_ELEMENT, StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT, StaticStrings.SUBCOLLECTION_DEFAULT_INDEX_ELEMENT, StaticStrings.SUPERCOLLECTION_ELEMENT, StaticStrings.CLASSIFY_ELEMENT, StaticStrings.FORMAT_ELEMENT, StaticStrings.COLLECTIONMETADATA_ELEMENT};
 
     /** ************************** Public Data Members ***************************/
@@ -323,14 +313,8 @@
     {
 	this.collect_cfg_file = collect_cfg_file;
-	// If collect_cfg is xml we can load it straight away
-	String collect_config_name = collect_cfg_file.getName();
-	if(collect_config_name.equals(COLLECTCONFIGURATION_XML)) {
-	    document = XMLTools.parseXMLFile(collect_cfg_file);
-	}
-	// Otherwise if this is a legacy collect.cfg file then read in the template and send to magic parser
-	else if(collect_config_name.equals(COLLECT_CFG)) {
-	    document = XMLTools.parseXMLFile("xml/CollectionConfig.xml", true);
-	    parse(collect_cfg_file);
-	}
+	// parse the XML template
+	document = XMLTools.parseXMLFile("xml/CollectionConfig.xml", true);
+	parse(collect_cfg_file);
+	
     }
 
@@ -410,30 +394,30 @@
 
     }
-    /** Retrieve or create the searchtype element. */
-    public Element getSearchType() {
-	///ystem.err.println("Get or create element by tag name: " + name);
-	Element document_element = document.getDocumentElement();
-	NodeList elements = document_element.getElementsByTagName(SEARCHTYPE_ELEMENT);
-	int elements_length = elements.getLength();
-	if(elements_length > 0) {
-	    document_element = null;
-	    return (Element) elements.item(0);
-	}
-	// Create the element
-	Element element = document.createElement(SEARCHTYPE_ELEMENT);
-	Node target_node = findInsertionPoint(element);
-	if(target_node != null) {
-	    document_element.insertBefore(element, target_node);
-	}
-	else {
-	    document_element.appendChild(element);
-	}
-	document_element = null;
-	// Append a default search type node - form
-	Element a_searchtype_element = CollectionDesignManager.collect_config.document.createElement(CollectionConfiguration.CONTENT_ELEMENT);
-	a_searchtype_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, SearchTypeManager.SEARCH_TYPES[0]);
-	element.appendChild(a_searchtype_element);
-	return element;
-    }
+//     /** Retrieve or create the searchtype element. */
+//     public Element getSearchType() {
+// 	///ystem.err.println("Get or create element by tag name: " + name);
+// 	Element document_element = document.getDocumentElement();
+// 	NodeList elements = document_element.getElementsByTagName(SEARCHTYPE_ELEMENT);
+// 	int elements_length = elements.getLength();
+// 	if(elements_length > 0) {
+// 	    document_element = null;
+// 	    return (Element) elements.item(0);
+// 	}
+// 	// Create the element
+// 	Element element = document.createElement(SEARCHTYPE_ELEMENT);
+// 	Node target_node = findInsertionPoint(element);
+// 	if(target_node != null) {
+// 	    document_element.insertBefore(element, target_node);
+// 	}
+// 	else {
+// 	    document_element.appendChild(element);
+// 	}
+// 	document_element = null;
+// 	// Append a default search type node - form
+// 	Element a_searchtype_element = CollectionDesignManager.collect_config.document.createElement(CollectionConfiguration.CONTENT_ELEMENT);
+// 	a_searchtype_element.setAttribute(CollectionConfiguration.NAME_ATTRIBUTE, SearchTypeManager.SEARCH_TYPES[0]);
+// 	element.appendChild(a_searchtype_element);
+// 	return element;
+//     }
 
     /** Retrieve or create the subindexes Element. */
@@ -452,8 +436,4 @@
 
     public void save() {
-	save(false);
-    }
-
-    public void save(boolean force_xml) {
 	if(collect_cfg_file.exists()) {
 	    File original_file = new File(collect_cfg_file.getParentFile(), COLLECT_CFG);
@@ -466,57 +446,52 @@
 	    }
 	}
-	if(force_xml || collect_cfg_file.getName().equals(COLLECTCONFIGURATION_XML)) {
-	    ///ystem.err.println("Writing XML");
-	    XMLTools.writeXMLFile(new File(collect_cfg_file.getParentFile(), COLLECTCONFIGURATION_XML), document);
-	}
-	else {
-	    ///ystem.err.println("Writing text");
-	    try {		
-		OutputStream ostream = new FileOutputStream(collect_cfg_file);
-		Writer file_writer = new OutputStreamWriter(ostream, ENCODING);
-		//FileWriter file_writer = new FileWriter(collect_cfg_file, false);
-		BufferedWriter buffered_writer = new BufferedWriter(file_writer);
-		Element collect_config_element = document.getDocumentElement();
-		NodeList command_elements = collect_config_element.getChildNodes();
-		boolean just_wrote_newline = false; // Prevent two or more newlines in a row
-		for(int i = 0; i < command_elements.getLength(); i++) {
-		    Node command_node = command_elements.item(i);
-		    if(command_node instanceof Element) {
-			Element command_element = (Element) command_node;
-			// The only thing left are NewLine elements
-			if(command_element.getNodeName().equals(NEWLINE_ELEMENT) && !just_wrote_newline) {
+	
+	try {		
+	    OutputStream ostream = new FileOutputStream(collect_cfg_file);
+	    Writer file_writer = new OutputStreamWriter(ostream, ENCODING);
+	    //FileWriter file_writer = new FileWriter(collect_cfg_file, false);
+	    BufferedWriter buffered_writer = new BufferedWriter(file_writer);
+	    Element collect_config_element = document.getDocumentElement();
+	    NodeList command_elements = collect_config_element.getChildNodes();
+	    boolean just_wrote_newline = false; // Prevent two or more newlines in a row
+	    for(int i = 0; i < command_elements.getLength(); i++) {
+		Node command_node = command_elements.item(i);
+		if(command_node instanceof Element) {
+		    Element command_element = (Element) command_node;
+		    // The only thing left are NewLine elements
+		    if(command_element.getNodeName().equals(NEWLINE_ELEMENT) && !just_wrote_newline) {
+			buffered_writer.newLine();
+			just_wrote_newline = true;
+		    }
+		    // Anything else we write to file, but only if it has been assigned, the exception being the Indexes element which just get commented if unassigned (a side effect of MG && MGPP compatibility)
+		    else if(!command_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(FALSE_STR) || command_element.getNodeName().equals(INDEXES_ELEMENT) || command_element.getNodeName().equals(INDEX_DEFAULT_ELEMENT) || command_element.getNodeName().equals(LEVELS_ELEMENT) || command_element.getNodeName().equals(LEVEL_DEFAULT_ELEMENT)){
+			String command;
+			// format statements we write out with ex. still present
+			if (command_element.getNodeName().equals(FORMAT_ELEMENT)) {
+			    command = toString(command_element, true);
+			} else {
+			    command = toString(command_element, false);
+			}
+			if(command != null && command.length()> 0 ) {
+			    write(buffered_writer, command);
 			    buffered_writer.newLine();
-			    just_wrote_newline = true;
-			}
-			// Anything else we write to file, but only if it has been assigned, the exception being the Indexes element which just get commented if unassigned (a side effect of MG && MGPP compatibility)
-			else if(!command_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(FALSE_STR) || command_element.getNodeName().equals(INDEXES_ELEMENT) || command_element.getNodeName().equals(INDEX_DEFAULT_ELEMENT) || command_element.getNodeName().equals(LEVELS_ELEMENT)){
-			    String command;
-			    // format statements we write out with ex. still present
-			    if (command_element.getNodeName().equals(FORMAT_ELEMENT)) {
-				command = toString(command_element, true);
-			    } else {
-				command = toString(command_element, false);
-			    }
-			    if(command != null && command.length()> 0 ) {
-				write(buffered_writer, command);
-				buffered_writer.newLine();
-				just_wrote_newline = false;
-			    }
-			}
-		    }
-		}
-		buffered_writer.close();
-
-		// If we're using a remote Greenstone server, upload the new collect.cfg file
-		if (Gatherer.isGsdlRemote) {
-		    String collection_name = Gatherer.c_man.getCollection().getName();
-		    RemoteGreenstoneServer.uploadCollectionFile(collection_name, collect_cfg_file);
-		}
-	    }
-	    catch (Exception exception) {
-		DebugStream.println("Error in CollectionConfiguration.save(boolean): " + exception);
-		DebugStream.printStackTrace(exception);
-	    }
-	}
+			    just_wrote_newline = false;
+			}
+		    }
+		}
+	    }
+	    buffered_writer.close();
+	    
+	    // If we're using a remote Greenstone server, upload the new collect.cfg file
+	    if (Gatherer.isGsdlRemote) {
+		String collection_name = Gatherer.c_man.getCollection().getName();
+		RemoteGreenstoneServer.uploadCollectionFile(collection_name, collect_cfg_file);
+	    }
+	}
+	catch (Exception exception) {
+	    DebugStream.println("Error in CollectionConfiguration.save(boolean): " + exception);
+	    DebugStream.printStackTrace(exception);
+	}
+    
     }
 
@@ -763,5 +738,5 @@
 	text.append(TAB_CHARACTER);
 	text.append(TAB_CHARACTER);
-	NodeList content_elements = command_element.getElementsByTagName(CONTENT_ELEMENT);
+	NodeList content_elements = command_element.getElementsByTagName(LEVEL_ELEMENT);
 	int content_elements_length = content_elements.getLength();
 	// Don't output anything if no levels are set.
@@ -776,4 +751,17 @@
 	return text.substring(0, text.length() - 1);
     }
+
+    static private String levelDefaultToString(Element command_element) {
+	StringBuffer text = new StringBuffer("");
+	if(command_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(FALSE_STR)) {
+	    text.append("#");
+	}
+	text.append(LEVEL_DEFAULT_STR);
+	text.append(TAB_CHARACTER);
+	text.append(command_element.getAttribute(NAME_ATTRIBUTE));
+	return text.toString();
+    }
+    
+    
 
     static private String metadataToString(Element command_element, boolean text_value) {
@@ -907,4 +895,9 @@
 			command_element = parseLevels(command_str);
 		    }
+		    if(command_element == null && (command_type.equals(LEVEL_DEFAULT_STR) || command_type.equals(COMMENTED_LEVEL_DEFAULT_STR))) {
+			
+			command_element = parseLevelDefault(command_str);
+		    }
+
 		    if(command_element == null && command_type.equals(COLLECTIONMETADATA_STR)) {
 			command_element = parseMetadata(tokenizer); // Revised to handle multiple lines
@@ -916,4 +909,5 @@
 			command_element = parsePlugin(command_str);
 		    }
+		    // leave here for backwards compatibility
 		    if(command_element == null && command_type.equals(SEARCHTYPE_STR)) {
 			command_element = parseSearchType(command_str);
@@ -1147,32 +1141,4 @@
 	return command_element;
     }
-    private Element parseBuildType(String command_str) {
-	Element command_element = null;
-	try {
-	    StringTokenizer tokenizer = new StringTokenizer(command_str);
-	    if(tokenizer.countTokens() >= 2) {
-		command_element = document.createElement(INDEX_DEFAULT_ELEMENT);
-		command_element.setAttribute(ASSIGNED_ATTRIBUTE, ((tokenizer.nextToken()).equals(INDEX_DEFAULT_STR) ? TRUE_STR : FALSE_STR));
-		String index_str = tokenizer.nextToken();
-		command_element.setAttribute(LEVEL_ATTRIBUTE, index_str.substring(0, index_str.indexOf(StaticStrings.COLON_CHARACTER)));
-		String content_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1);
-		StringTokenizer content_tokenizer = new StringTokenizer(content_str, StaticStrings.COMMA_CHARACTER);
-		while(content_tokenizer.hasMoreTokens()) {
-		    Element content_element = document.createElement(CONTENT_ELEMENT);
-		    content_element.setAttribute(NAME_ATTRIBUTE, content_tokenizer.nextToken());
-		    command_element.appendChild(content_element);
-		    content_element = null;
-		}
-		content_tokenizer = null;
-		content_str = null;
-		content_str = null;
-		index_str = null;
-	    }
-	    tokenizer = null;
-	}
-	catch (Exception exception) {
-	}
-	return command_element;
-    }
 
     private Element parseLanguage(String command_str) {
@@ -1226,5 +1192,5 @@
 		command_element.setAttribute(ASSIGNED_ATTRIBUTE, (command.equals(LEVELS_STR) ? TRUE_STR : FALSE_STR));
 		while(tokenizer.hasMoreTokens()) {
-		    Element level_element = document.createElement(CONTENT_ELEMENT);
+		    Element level_element = document.createElement(LEVEL_ELEMENT);
 		    level_element.setAttribute(NAME_ATTRIBUTE, tokenizer.nextToken());
 		    command_element.appendChild(level_element);
@@ -1235,4 +1201,21 @@
 	}
 	catch(Exception exception) {
+	}
+	return command_element;
+    }
+
+    private Element parseLevelDefault(String command_str) {
+	Element command_element = null;
+	try {
+	    StringTokenizer tokenizer = new StringTokenizer(command_str);
+	    if(tokenizer.countTokens() >= 2) {
+		command_element = document.createElement(LEVEL_DEFAULT_ELEMENT);
+		command_element.setAttribute(ASSIGNED_ATTRIBUTE, ((tokenizer.nextToken()).equals(LEVEL_DEFAULT_STR) ? TRUE_STR : FALSE_STR)); // is it commented out or not?
+		command_element.setAttribute(NAME_ATTRIBUTE, tokenizer.nextToken());
+	    }
+	    
+	    tokenizer = null;
+	}
+	catch (Exception exception) {
 	}
 	return command_element;
@@ -1385,19 +1368,21 @@
     }
 
+    /* search types are now handled as formats - leave this here to convert in case we have an old config file */
     private Element parseSearchType(String command_str) {
 	Element command_element = null;
 	try {
 	    StringTokenizer tokenizer = new StringTokenizer(command_str);
-	    // First token is command type (levels)
+	    // First token is command type (searchtype)
 	    tokenizer.nextToken();
 	    if(tokenizer.hasMoreTokens()) {
-		command_element = document.createElement(SEARCHTYPE_ELEMENT);
-		command_element.setAttribute(ASSIGNED_ATTRIBUTE, TRUE_STR);
+		command_element = document.createElement(FORMAT_ELEMENT);
+		command_element.setAttribute(NAME_ATTRIBUTE, "SearchTypes");
+		String value = tokenizer.nextToken();
 		while(tokenizer.hasMoreTokens()) {
-		    Element search_element = document.createElement(CONTENT_ELEMENT);
-		    search_element.setAttribute(NAME_ATTRIBUTE, tokenizer.nextToken());
-		    command_element.appendChild(search_element);
-		    search_element = null;
-		}
+		    value += ","+tokenizer.nextToken();
+		}
+		value = Codec.transform(value, Codec.GREENSTONE_TO_DOM);
+		XMLTools.setValue(command_element, value);
+		
 	    }
 	}
