Index: trunk/gsdl/perllib/plugins/XMLMARCPlug.pm
===================================================================
--- trunk/gsdl/perllib/plugins/XMLMARCPlug.pm	(revision 13193)
+++ trunk/gsdl/perllib/plugins/XMLMARCPlug.pm	(revision 13196)
@@ -66,7 +66,9 @@
     $self->{'encoding'} = "";
     $self->{'marc_mapping'} = {};
-    $self->{'current_attr'} = "";
+    $self->{'current_code'} = "";
+    $self->{'current_tag'} = "";
     $self->{'current_element'} = "";
     $self->{'metadata_mapping'} = undef;
+    $self->{'num_processed'} = 0;
     
     return bless $self, $class;
@@ -227,20 +229,25 @@
 	$self->set_OID($doc_obj, $doc_obj->get_OID() , $self->{'record_count'});
 
+	my $outhandle = $self->{'outhandle'};
+	print $outhandle "Record $self->{'record_count'} - XMLMARCPlug: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
+
         $self->{'record_count'}++;
         $self->{'doc_obj'} = $doc_obj;       
-	
-    }
-
-     ## get the marc code, for example 520
+	$self->{'num_processed'}++;
+
+    }
+    
+    ## get the marc code, for example 520
      if ($element eq "datafield") {
 	 if (defined $attr_map{'tag'} and $attr_map{'tag'} ne ""){
-	     $self->{'current_attr'} = $attr_map{tag};  
+	     $self->{'current_tag'} = $attr_map{tag};  
 	 }
      }
 
+
     ## append the subcode to the marc code for example 520a or 520b 
     if ($element eq "subfield"){
-	if (defined $attr_map{'code'} and $attr_map{'code'} ne "" and $self->{'current_attr'} ne ""){
-	    $self->{'current_attr'} = $self->{'current_attr'}.$attr_map{'code'};
+	if (defined $attr_map{'code'} and $attr_map{'code'} ne "" and $self->{'current_tag'} ne ""){
+	    $self->{'current_code'} = $attr_map{'code'};
 	}
     }
@@ -276,37 +283,42 @@
        ##map { print STDERR $_."=>".$metadata_mapping->{$_}."\n"; } keys %$metadata_mapping;
 
-	foreach my $code (keys %$marc_mapping){
+	foreach my $marc_field (keys %$metadata_mapping){
 	    my ($meta_name,$meta_value);
-	    if (defined $metadata_mapping->{$code} and $metadata_mapping->{$code} ne ""){
-	      	$meta_name = $metadata_mapping->{$code};
-		$meta_value = $marc_mapping->{$code};
-		$doc_obj->add_utf8_metadata($doc_obj->get_top_section(),$meta_name, $meta_value);
-	    }
-	    else{ ##strip the subcode and try lookup again
-		$code =~ /(\d{3})/;
-		## get the firt three digits
-		$code = $1 ;
-		if (defined $metadata_mapping->{$code} and $metadata_mapping->{$code} ne ""){
-		    $meta_name = $metadata_mapping->{$code};
-		    ## get the first entry starting with this code from the marc_mapping		    
-		    foreach my $key (keys %$marc_mapping){
-			my $old_key = $key;
-			$key =~ /(\d{3})/;
-			## get the firt three digits
-			$key = $1 ;
-			if ($code =~ /$key/){
-			    $meta_value = $marc_mapping->{$old_key};
-			    last;
+	    my $matched_field = $marc_mapping->{$marc_field}; 
+	    my $subfield = undef;
+	    if (defined $matched_field){
+		## test whether this field has subfield
+		if ($marc_field =~ /\d\d\d(\w)/){
+		    $subfield = $1;
+		}
+		$meta_name = $metadata_mapping->{$marc_field};
+
+		if (defined $subfield){
+		    my %mapped_subfield = {@$matched_field};
+		    $meta_value = $mapped_subfield{$subfield};
+		}
+		else{ ## get all values 
+		    my $i =0;
+		    foreach my $value (@$matched_field){
+			if ($i%2 != 0){
+			    $meta_value .= $value." ";
 			}
-		    }
-		    if (defined $meta_value and $meta_value ne ""){
-			$doc_obj->add_utf8_metadata($doc_obj->get_top_section(),$meta_name, $meta_value);
+			$i++;
 		    }
 		}
-	    }  
+		
+		## escape [ and ]
+		$meta_value =~ s/\[/\\\[/g;
+		$meta_value =~ s/\]/\\\]/g;
+		##print STDERR  "$meta_name=$meta_value\n";
+		$doc_obj->add_utf8_metadata($doc_obj->get_top_section(),$meta_name, $meta_value);
+		
+	    }		    
+			
 	}
 
 	##clean up
 	$self->{'marc_mapping'} = undef;
+	$self->{'current_tag'} = "";
     }
 }
@@ -324,10 +336,11 @@
     my ($expat) = @_;
 
+
     ## store the text of a marc code, for exapmle 520a=>A poem about....
-    if ($self->{'current_element'} eq "subfield" and $self->{'current_attr'} ne "" and $_ ne "" ){
+    if ($self->{'current_element'} eq "subfield" and $self->{'current_code'} ne "" and $_ ne "" ){
 	##stored it in the marc_mapping 
-	$self->{'marc_mapping'}->{$self->{'current_attr'}} = $_;
-	## clean up
-	$self->{'current_attr'} = "";
+     	push(@{$self->{'marc_mapping'}->{$self->{'current_tag'}}},$self->{'current_code'});
+	push(@{$self->{'marc_mapping'}->{$self->{'current_tag'}}},$_);
+	$self->{'current_code'} = "";
     }
     
