Index: trunk/gsdl/perllib/plugins/MARCPlug.pm
===================================================================
--- trunk/gsdl/perllib/plugins/MARCPlug.pm	(revision 12401)
+++ trunk/gsdl/perllib/plugins/MARCPlug.pm	(revision 13198)
@@ -80,4 +80,6 @@
     my $self = new SplitPlug($pluginlist, $inputargs, $hashArgOptLists);
 
+    $self->{'type'} = "";
+    
     return bless $self, $class;
 }
@@ -150,5 +152,5 @@
 sub get_default_split_exp {
     # \r\n for msdos eol, \n for unix
-    return q^\r?\n\s*\r?\n^;
+    return q^\r?\n\s*\r?\n|\[\w+\]Record type: USmarc^;
 }
 
@@ -181,6 +183,5 @@
     $self->{'readfile_encoding'}->{$filename} = $encoding;
 
-    my @marc_entries = ();
-
+       
     if (!-r $filename)
     {
@@ -190,8 +191,29 @@
     }
 
+    ##handle ascii marc
+    #test whether this is ascii marc file 
+    if (open (FILE, $filename)) {
+	while (defined (my $line = <FILE>)) {
+	    $$textref .= $line;
+	   if ($line =~ /\[\w+\]Record type:/){
+	       undef $/;
+	       $$textref .= <FILE>;
+	       $/ = "\n";
+	       $self->{'type'} = "ascii";
+	       close FILE;
+	       return;
+	   }
+	}
+	close FILE;	
+    }
+
+      
+    $$textref = "";
+    my @marc_entries = ();
+ 
     my $batch = new MARC::Batch( 'USMARC', $filename );
     while ( my $marc = $batch->next ) 
     {
-	push(@marc_entries,$marc);
+      	push(@marc_entries,$marc);
 	$$textref .= $marc->as_formatted();
 	$$textref .= "\n\n"; # for SplitPlug - see default_split_exp above...
@@ -231,5 +253,11 @@
 
     my $encoding = $self->{'readfile_encoding'}->{$filename};
-    $self->extract_metadata ($marc, $metadata, $encoding, $doc_obj, $cursection);
+
+    if ($self->{'type'} ne "ascii" ){   
+       	$self->extract_metadata ($marc, $metadata, $encoding, $doc_obj, $cursection);
+    }
+    else{
+	$self->extract_ascii_metadata ($$textref,$metadata,$doc_obj, $cursection);
+    }
 
     # add spaces after the sub-field markers, for word boundaries
@@ -290,9 +318,15 @@
     my $outhandle = $self->{'outhandle'};
 
+    if (!defined $marc){
+	return;
+    }
+
     my $metadata_mapping = $self->{'metadata_mapping'};
     my $mm;
+
     foreach $mm ( @$metadata_mapping )
     {
 	my $marc_field = $mm->{'marc'};
+
 	my @metavalues = $marc->field($marc_field);
 
@@ -309,3 +343,83 @@
     }
 }
+
+sub extract_ascii_metadata 
+{
+    my $self = shift (@_);
+    my ($text, $metadata,$doc_obj, $section) = @_;
+    my $outhandle = $self->{'outhandle'};
+    my $metadata_mapping = $self->{'metadata_mapping'};
+    ## get fields
+    my @fields = split(/[\n\r]+/,$text);
+    my $marc_mapping ={};
+
+    foreach my $field (@fields){
+	if ($field ne ""){
+	    $field =~ /^(\d\d\d)\s/;
+	    my $code = $1;
+	    $field = $';
+	    ##get subfields
+	    my @subfields = split(/\$/,$field);
+	    my $i=0;
+	    $marc_mapping->{$code} = [];  
+	    foreach my $subfield (@subfields){
+		if ($i == 0){
+		    ##print STDERR $subfield."\n";
+		    push(@{$marc_mapping->{$code}},"info");
+		    push(@{$marc_mapping->{$code}},$subfield);
+		    	 
+		    $i++;
+		} 
+		 else{
+		     $subfield =~ /(\w)\s/;
+		     ##print STDERR "$1=>$'\n";
+		     push(@{$marc_mapping->{$code}},$1);
+                     push(@{$marc_mapping->{$code}},$'); 
+		 }
+	    }
+	}
+    }
+
+
+     foreach my $mm ( @$metadata_mapping )
+    {
+	my $marc_field = $mm->{'marc'};
+		
+	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;
+	    }
+	    my $metaname = $mm->{'gsdl'};
+ 
+	    my $metavalue;
+	    if (defined $subfield){
+		my %mapped_subfield = {@$matched_field};
+		$metavalue = $mapped_subfield{$subfield};
+	    }
+	    else{ ## get all values except info
+		my $i =0;
+		foreach my $value (@$matched_field){
+		    if ($i%2 != 0 and $i != 1){
+			$metavalue .= $value." ";
+		    }
+		    $i++;
+		}
+	    }
+	    
+	    ## escape [ and ]
+	    $metavalue =~ s/\[/\\\[/g;
+	    $metavalue =~ s/\]/\\\]/g;
+	    ##print STDERR  "$metaname=$metavalue\n";
+   	    $doc_obj->add_metadata ($section, $metaname, $metavalue) ;		 	
+	}
+       
+    }
+
+}
+
+
 1;
