Index: /gsdl/trunk/perllib/buildConfigxml.pm
===================================================================
--- /gsdl/trunk/perllib/buildConfigxml.pm	(revision 20104)
+++ /gsdl/trunk/perllib/buildConfigxml.pm	(revision 20105)
@@ -24,7 +24,11 @@
 ###########################################################################
 
-# reads in configuration files of xml form
+# reads in buildConfig.xml 
+# Note, only implemented the bits that are currently used, eg by incremental 
+# build code.
+# The resulting data is not a full representation on buildConfig.xml.
 
 package buildConfigxml;
+
 use strict;
 no strict 'refs';
@@ -60,326 +64,20 @@
 use XML::Parser;
 
+
 # A mapping hash to resolve name discrepancy between gs2 and gs3.
-my $nameMap = {"key" => "value",
-	       "creator" => "creator",
-	       "maintainer" => "maintainer",
-	       "public" => "public",
-	       "infodb" => "infodbtype",
-	       "defaultIndex" => "defaultindex",
-	       "defaultLevel" => "defaultlevel",
-	       "name" => "collectionname",
-	       "description" => "collectionextra",
-	       "smallicon" => "iconcollectionsmall",
-	       "icon" => "iconcollection",
-	       "level" => "levels",
-	       "classifier" => "classify",
-	       "indexSubcollection" => "indexsubcollections",
-	       "indexLanguage" => "languages",
-	       "defaultIndexLanguage" => "defaultlanguage",
-	       "index" => "indexes",
-	       "plugin" => "plugin",
-	       "plugout" => "plugout",
-	       "indexOption" => "indexoptions",
-	       "searchType" => "searchtype",
-	       "languageMetadata" => "languagemetadata",
+my $nameMap = {"numDocs" => "numdocs",
 	       "buildType" => "buildtype"
 	       };
+
+
 # A hash structure which is returned by sub read_cfg_file.
 my $data = {};
 
-my $repeatedBlock = q/^(browse|pluginList)$/;
-
 # use those unique attribute values to locate the text within the elements
-# creator, public, maintainer and within a displayItem.
 my $currentLocation = "";
-my $stringexp = q/^(creator|maintainer|public|buildType)$/;
-my $displayItemNames = q/^(name|description)$/;
+my $stringexp = q/^(buildType|numDocs)$/;
   
-# For storing the attributes during the StartTag subroutine, so that 
-# we can use it later in Text (or EndTag) subroutines
-my $currentAttrRef = undef; 
-
-my $currentLevel = "";
-
-# Count the elements with same name within the same block
-# ("plugin", "option")
-my $currentIndex = 0;
-my $arrayexp = q/^(index|level|indexSubcollection|indexLanguage)$/;
-my $arrayarrayexp= q/^(plugin|classifier)$/;
-my $hashexp = q/^(subcollection)$/; # add other element names that should be represented by hash expressions here
-my $hashhashexp = q/^(displayItem)$/; # add other (collectionmeta) element names that should be represented by hashes of hashes here.
-
-my $defaults = q/^(defaultIndex|defaultLevel|defaultIndexLanguage|languageMetadata)$/;
-
-sub StartTag {
-# Those marked with #@ will not be executed at the same time when this sub is being called
-# so that if/elsif is used to avoid unnecessary tests
-    my ($expat, $element) = @_;
-    
-    # See http://search.cpan.org/~msergeant/XML-Parser-2.36/Parser.pm#Stream
-    # %_ is a hash of all the attributes of this element, we want to store them so we can use the attributes
-    # when the textnode contents of the element are parsed in the subroutine Text (that's the handler for Text). 
-    $currentAttrRef = \%_; 
-
-    my $name = $_{'name'};
-    my $value = $_{'value'};
-    my $type = $_{'type'};
-
-    # for subcollections
-    my $filter = $_{'filter'};
-	
-    #@ Marking repeated block
-    if ($element =~ /$repeatedBlock/) {
-	$currentIndex = 0;
-    }
-
-    #@ handling block metadataList
-    elsif (defined $name and $name =~ /$stringexp/){
-      $currentLocation = $name;
-    }
-    #@ handling default search index/level/indexLanguage and languageMetadata
-    elsif ($element =~ /$defaults/) {
-      if (defined $name and $name =~ /\w/) {
-	$data->{$nameMap->{$element}} = $name;
-      }
-    }
-
-    #@ handling the displayItems name and description (known as collectionname and collectionextra in GS2)
-    elsif($element eq "displayItemList") {
-	$currentLevel = "displayItemList"; # storing the parent if it is displayItemList
-    } 
-    elsif($element =~ /$hashhashexp/) { # can expand on this to check for other collectionmeta elements
-	if((!defined $assigned) || (defined $assigned and $assigned =~ /\w/ and $assigned eq "true")) {
-	    # either when there is no "assigned" attribute, or when assigned=true (for displayItems):
-	    $currentLocation = $name;
-	}
-    }
-
-    #@ Handling database type: gdbm or gdbm-txtgz, later jdbm.
-    elsif ($element eq "infodb") {
-      $data->{'infodbtype'} = $type;
-    }
-    
-    #@ Handling indexer: mgpp/mg/lucene; stringexp
-    elsif ($element eq "search") {
-      $data->{'buildtype'} = $type;
-    }
-
-    #@ Handling searchtype: plain,form; arrayexp
-    #elsif ($element eq "format" and defined $name and $name =~ /searchType/) {
-	#@ Handling searchtype: plain, form
-	#$currentLocation = $name;	
-    #}
- 
-    #@ Handle index|level|indexSubcollection|indexLanguage
-    elsif ($element =~ /$arrayexp/) {
-      my $key = $nameMap->{$element};	
-      if (!defined $data->{$key}) {
-	$data->{$key} = [];
-      }
-
-      push (@{$data->{$key}},$name);
-    }
-
-    #@ indexoptions: accentfold/casefold/stem; arrayexp
-    elsif ($element eq "indexOption") {
-      $currentLevel = "indexOption";
-    }
-    if ($currentLevel eq "indexOption" and $element eq "option") {
-      my $key = $nameMap->{$currentLevel};	
-      if (!defined $data->{$key}) {
-	$data->{$key} = [];
-      }
-      push (@{$data->{$key}},$name);
-    }
-    #@ plugout options
-    elsif ($element eq "plugout") {
-	$currentLevel = "plugout";
-	my $key = $nameMap->{$currentLevel};	
-	if (!defined $data->{$key}) {
-	    $data->{$key} = [];
-	}
-	if(defined $name and $name ne ""){
-	    push (@{$data->{$key}},$name);
-	}
-	else{
-	   push (@{$data->{$key}},"GreenstoneXMLPlugout"); 
-	}
-    }
-    if ($currentLevel eq "plugout" and $element eq "option") {     
-	my $key = $nameMap->{$currentLevel};
-	if (defined $name and $name ne ""){
-	    push (@{$data->{$key}},$name);
-	}
-	if (defined $value and $value ne  ""){
-	    push (@{$data->{$key}},$value);
-	}
-    }
-
-    #@ use hash of hash of strings: hashexp
-    elsif ($element =~ /$hashexp/) {
-      if (!defined $data->{$element}) {
-	$data->{$element} = {};
-      }
-      if (defined $name and $name =~ /\w/) {
-	if (defined $filter and $filter =~ /\w/) {
-	  $data->{$element}->{$name} = $filter;
-
-	}
-      }
-    }
-
-    #@ Handling each classifier/plugin element
-    elsif ($element =~ /$arrayarrayexp/) {
-	# find the gs2 mapping name
-        $currentLevel = $element;
-        my $key = $nameMap->{$element};	
-
-	# define an array of array of strings	foreach $k (@{$data->{$key}}) {
-	if (!defined $data->{$key}) {
-	  $data->{$key} = [];
-	}
-	# Push classifier/plugin name (e.g. AZList) into $data as the first string
- 	push (@{$data->{$key}->[$currentIndex]},$name);
-	#print $currentIndex."indexup\n";
-      }
-    
-    #@ Handling the option elements in each classifier/plugin element (as the following strings)
-    elsif ($currentLevel =~ /$arrayarrayexp/ and $element eq "option") {
-	# find the gs2 mapping name for classifier and plugin
-        my $key = $nameMap->{$currentLevel};	
-
-	if (defined $name and $name =~ /\w/) {
-	    push (@{$data->{$key}->[$currentIndex]}, $name);
-	}
-	if (defined $value and $value =~ /\w/) {
-            push (@{$data->{$key}->[$currentIndex]}, $value);
-	}
-
-    }
-}
-
-sub EndTag {
-    my ($expat, $element) = @_;
-    my $endTags = q/^(browse|pluginList|displayItemList)$/;   
-    if ($element =~ /$endTags/) {
-		$currentIndex = 0;
-		$currentLevel = "";
-    }
-    # $arrayarrayexp contains classifier|plugin
-    elsif($element =~ /$arrayarrayexp/ ){
-     	$currentIndex = $currentIndex + 1;
-    }
-}
-
-sub Text {
-    if (defined $currentLocation) { 
-	#@ Handling block metadataList(creator, maintainer, public)
-	if($currentLocation =~ /$stringexp/){
-	    #print $currentLocation;
-	    my $key = $nameMap->{$currentLocation};	
-	    $data->{$key} = $_;
-	    undef $currentLocation;
-	}
-	
-	#@ Handling displayItem metadata that are children of displayItemList
-	# that means we will be getting the collection's name and possibly description ('collectionextra' in GS2).
-	elsif($currentLevel eq "displayItemList" && $currentLocation =~ /$displayItemNames/) {
-	    my $lang = $currentAttrRef->{'lang'};
-	    my $name = $currentAttrRef->{'name'};
-	    
-	    # this is how data->collectionmeta's language is set in Greenstone 2. 
-	    # Need to be consistent, since export.pl accesses these values all in the same way
-	    if(!defined $lang) {
-		$lang = 'default';
-	    } else {
-		$lang = "[l=$lang]"; 
-	    }
-	    
-	    if(defined $name and $name =~ /$displayItemNames/) { # attribute name = 'name' || 'description'
-		# using $nameMap->$name resolves to 'collectionname' if $name='name' and 'collectionextra' if $name='description'
-		$data->{'collectionmeta'}->{$nameMap->{$name}}->{$lang} = $_; # the value is the Text parsed
-		#print STDERR "***Found: $nameMap->{$name} collectionmeta, lang is $lang. Value: $data->{'collectionmeta'}->{$nameMap->{$name}}->{$lang}\n";
-	    }
-	    undef $currentLocation;
-	}
-  
-	#@ Handling searchtype: plain,form; arrayexp
-	elsif (defined $currentLocation and $currentLocation =~ /searchType/) {
-	    # map 'searchType' into 'searchtype'
-	    my $key = $nameMap->{$currentLocation};
-	    # split it by ','
-	    my ($plain, $form) = split (",", $_);
-	    
-	    if (!defined $data->{$key}) {
-		$data->{$key} = [];
-	    }
-	    if (defined $plain and $plain =~ /\w/) {
-		push @{ $data->{$key} }, $plain;
-	    }
-	    if (defined $form and $form =~ /\w/) {
-		push @{ $data->{$key} }, $form;
-	    }
-	}
-    }	
-}
-
-# This sub is for debugging purposes
-sub Display {
-    # metadataList
-    foreach my $k (keys %{$data}) {
-	print STDERR "*** metadatalist key $k\n"; 
-    }
-  
-    print $data->{'creator'}."\n" if (defined $data->{'creator'});
-    print $data->{"maintainer"}."\n" if (defined $data->{"maintainer"});
-    print $data->{"public"}."\n" if (defined $data->{"public"});
-    print $data->{"defaultindex"}."\n" if (defined $data->{"defaultindex"});
-    print $data->{"defaultlevel"}."\n" if (defined $data->{"defaultlevel"});
-    print $data->{"buildtype"}."\n" if (defined $data->{"buildtype"});
-    print  join(",",@{$data->{"searchtype"}})."\n" if (defined $data->{"searchtype"});
-    print  join(",",@{$data->{'levels'}})."\n" if (defined $data->{'levels'});
-    print  join(",",@{$data->{'indexsubcollections'}})."\n" if (defined $data->{'indexsubcollections'});
-    print  join(",",@{$data->{'indexes'}})."\n" if (defined $data->{'indexes'});
-    print  join(",",@{$data->{'indexoptions'}})."\n" if (defined $data->{'indexoptions'});
-    print  join(",",@{$data->{'languages'}})."\n" if (defined $data->{'languages'});
-    print  join(",",@{$data->{'languagemetadata'}})."\n" if (defined $data->{'languagemetadata'});
- 
-    if (defined $data->{'plugin'}) {
-	foreach $a (@{$data->{'plugin'}}) {
-	    print join(",",@$a);
-	    print "\n";
-	}
-    }
-    if (defined $data->{'classify'}) {
-	print "Classifiers: \n";
-	map { print join(",",@$_)."\n"; } @{$data->{'classify'}};
-    }
-    
-    if (defined $data->{'subcollection'}) {
-	foreach my $key (keys %{$data->{'subcollection'}}) {
-	    print "subcollection ".$key." ".$data->{'subcollection'}->{$key}."\n";
-	}
-    }
-}
-sub Doctype {
-    my ($expat, $name, $sysid, $pubid, $internal) = @_;
-
-    # allow the short-lived and badly named "GreenstoneDirectoryMetadata" files 
-    # to be processed as well as the "DirectoryMetadata" files which should now
-    # be created by import.pl
-    die if ($name !~ /^(Greenstone)?DirectoryMetadata$/);
-}
-
-# This Char function overrides the one in XML::Parser::Stream to overcome a
-# problem where $expat->{Text} is treated as the return value, slowing
-# things down significantly in some cases.
-sub Char {
-    if ($]<5.008) {
-	use bytes;  # Necessary to prevent encoding issues with XML::Parser 2.31+ and Perl 5.6
-    }
-    $_[0]->{'Text'} .= $_[1];
-    return undef;
-}
+my $indexmap_name = "";
+my $haveindexfields = 0;
 
 # Reads in the model collection configuration file, collectionConfig.xml,
@@ -389,5 +87,5 @@
     my ($filename) = @_;
     $data = {};
-    if (($filename !~ /collectionConfig\.xml$/ && $filename !~ /buildConfig\.xml$/) || !-f $filename) {
+    if ($filename !~ /buildConfig\.xml$/ || !-f $filename) {
         return undef;
     }
@@ -412,5 +110,5 @@
 
     if (!open (COLCFG, $filename)) {
-	print STDERR "cfgread::read_cfg_file couldn't read the cfg file $filename\n";
+	print STDERR "buildConfigxml::read_cfg_file couldn't read the cfg file $filename\n";
     } else {
 
@@ -422,4 +120,103 @@
     return $data;
 }
+
+sub StartTag {
+# Those marked with #@ will not be executed at the same time when this sub is being called
+# so that if/elsif is used to avoid unnecessary tests
+    my ($expat, $element) = @_;
+    
+    my $name = $_{'name'};
+    my $shortname = $_{'shortname'};
+
+    
+    #@ handling block metadataList
+    if (defined $name and $name =~ /$stringexp/){
+      $currentLocation = $name;
+      # the value will be retrieved later in Text sub
+    }
+
+    #@ handle indexes - store indexmap (mg) or indexfields and indexfieldmap (mgpp/lucene)
+    elsif ($element =~ /^indexList$/) {
+	# set up the data arrays
+	# this assumes that the build type has been read already, which is
+	# currently the order we save the file in.
+	if ($data->{'buildtype'} eq "mg") {
+	    $indexmap_name = "indexmap";
+	    if (!defined $data->{"indexmap"}) {
+		$data->{"indexmap"} = [];
+	    }
+	}
+	else {
+	    $indexmap_name = "indexfieldmap";
+	    $haveindexfields = 1;
+	    if (!defined $data->{"indexfieldmap"}) {
+		$data->{"indexfieldmap"} = [];
+	    }
+	    if (!defined $data->{"indexfields"}) {
+		$data->{"indexfields"} = [];
+	    }
+
+	}
+	
+    }
+    
+    elsif ($element =~ /index/) {
+	# store each index in the map
+	if (defined $name && defined $shortname) {
+	    push @{$data->{$indexmap_name}}, "$name->$shortname";
+	    if ($haveindexfields) {
+		push @{$data->{'indexfields'}}, $name;
+	    }
+	}
+    }
+
+
+}
+
+sub EndTag {
+    my ($expat, $element) = @_;
+}
+
+sub Text {
+    if (defined $currentLocation) { 
+	#@ Handling block metadataList(numDocs, buildType)
+	if($currentLocation =~ /$stringexp/){
+	    #print $currentLocation;
+	    my $key = $nameMap->{$currentLocation};	
+	    $data->{$key} = $_;
+	    undef $currentLocation;
+	}	
+    }	
+}
+
+# This sub is for debugging purposes
+sub Display {
+
+    print "NumDocs = ".$data->{'numdocs'}."\n" if (defined $data->{'numdocs'});
+    print "BuildType = ".$data->{'buildtype'}."\n" if (defined $data->{'buildtype'});
+    print  "IndexMap = ". join(" ",@{$data->{'indexmap'}})."\n" if (defined $data->{'indexmap'});
+    print  "IndexFieldMap = ". join(" ",@{$data->{'indexfieldmap'}})."\n" if (defined $data->{'indexfieldmap'});
+    print  "IndexFields = ". join(" ",@{$data->{'indexfields'}})."\n" if (defined $data->{'indexfields'});
+
+}
+
+# is this actually used??
+sub Doctype {
+    my ($expat, $name, $sysid, $pubid, $internal) = @_;
+
+    die if ($name !~ /^buildConfig$/);
+}
+
+# This Char function overrides the one in XML::Parser::Stream to overcome a
+# problem where $expat->{Text} is treated as the return value, slowing
+# things down significantly in some cases.
+sub Char {
+    if ($]<5.008) {
+	use bytes;  # Necessary to prevent encoding issues with XML::Parser 2.31+ and Perl 5.6
+    }
+    $_[0]->{'Text'} .= $_[1];
+    return undef;
+}
+
 
 
