Index: trunk/gsdl/perllib/plugins/GMLPlug.pm
===================================================================
--- trunk/gsdl/perllib/plugins/GMLPlug.pm	(revision 861)
+++ trunk/gsdl/perllib/plugins/GMLPlug.pm	(revision 862)
@@ -86,7 +86,4 @@
 
     # read in the document
-    my $gml = "";
-    my $line = "";
-
     if ($gz) {
 	if (!open (INFILE, "zcat $fullname |")) {
@@ -102,19 +99,15 @@
 
     undef $/;
-    while (defined ($line = <INFILE>)) {
-	$gml .= $line;
-    }
+    my $gml = <INFILE>;
     $/ = "\n";
-
     close (INFILE);
-
+    
     my @gml_sections = split("</gsdlsection>",$gml);
     $gml = shift(@gml_sections);
 
     my $no_docs = 0;
-    my $src_filename = "";
-
-    while (1)
-    {
+#    my $src_filename = ""; #### don't appear to use this anymore - not sure if that's right
+
+    while (1) {
 	# create a new document
 	my $doc_obj = new doc ();
@@ -123,24 +116,17 @@
 	# process the document
 	my $firstsection = 1;
-	while (1) 
-	{
-	    my $tags = "";
-	    my $text = "";
-
-	    my @indenting_sections = split("<gsdlsection",$gml);
+	while (1) {
+	    my ($tags, $text) = ("", "");
+
+	    my @indenting_sections = split("<gsdlsection", $gml);
 	    shift(@indenting_sections); # first entry is trivially empty
-####	    print STDERR "**** no indenting sections = ", scalar(@indenting_sections), "\n";
-
-	    foreach $gml (@indenting_sections)
-	    {
-####		print STDERR "\n\n\n\n!!!!!!!!!!! gml = $gml\n\n\n";
-
-		if ($gml =~ m/^\s*([^>]*)>(.*)$/so)
-		{
-		    $tags = $1 if (defined $1);
+
+	    foreach $gml (@indenting_sections) {
+
+		if ($gml =~ /^\s*([^>]*)>(.*)$/so) {
+		    $tags = $1 if defined $1;
 		    $text = &GMLPlug::_unescape_text($2);
-		}
-		else
-		{
+
+		} else {
 		    print STDERR "GMLPlug::read - error in file $fullname\n";
 		    print STDERR "text: \"$gml\"\n";
@@ -149,35 +135,23 @@
 
 		# create the section (unless this is the first section)
-		if ($firstsection) 
-		{
+		if ($firstsection) {
 		    $firstsection = 0;
-		    
-		    print STDERR "  0 of $src_filename\n" if ($no_docs==1);
-		    $tags =~ m/gsdlsourcefilename\s*=\s*(\"([^\"]*)\")|(\w+)/o;
-		    $src_filename = $2 || $3;
-		    print STDERR "  $no_docs of $src_filename\n" if ($no_docs>=1);
-		}
-		else
-		{
-####		    print STDERR "*** tags = $tags\n";
-
-		    if ($tags =~ s/gsdlnum\s*=\s*\"?(\d+)\"?//o)
-		    {
+#		    $tags =~ /gsdlsourcefilename\s*=\s*(?:\"([^\"]*)\")/o;
+#		    $src_filename = $2 || $3;
+
+		} else {
+
+		    $tags =~ s/gsdlnum\s*=\s*\"?(\d+)\"?//o;
+		    if (defined $1) {
 			$section .= ".$1";
 			$doc_obj->create_named_section($section);
-		    } 
-		    else 
-		    {
+		    } else {
 			$section = $doc_obj->insert_section($doc_obj->get_end_child($section));
 		    }
 		}
 	    
-
 		# add the tags
-		while ((defined $tags) 
-		       && ($tags =~ s/^\s*(\w+)\s*=\s*\"([^\"]*)\"//o)) 
-		{
-		    $doc_obj->add_utf8_metadata($section, $1, 
-						&GMLPlug::_unescape_text($2)) 
+		while ((defined $tags) && ($tags =~ s/^\s*(\w+)=\"([^\"]*)\"//o)) {
+		    $doc_obj->add_utf8_metadata($section, $1, &GMLPlug::_unescape_text($2)) 
 			if (defined $1 and defined $2);
 		}
@@ -189,22 +163,20 @@
 
 	    $gml = shift(@gml_sections); # get next bit of data
-	    last if (!defined $gml);
-###	    print "####### before section = $section\n";
+	    last unless defined $gml;
 	    $section = $doc_obj->get_parent_section ($section);
-###	    print "####### after section = $section\n";
-	    #### last if ($section eq "");
-	}
-
-    # add the associated files
-    $assoc_files = $doc_obj->get_metadata($doc_obj->get_top_section(), "gsdlassocfile");
-    my ($assoc_file_info);
-    foreach $assoc_file_info (@$assoc_files) {
-	my ($assoc_file, $mime_type, $dir) = split (":", $assoc_file_info);
-	$dir = "" unless defined $dir;
-	$doc_obj->associate_file(&util::filename_cat($parent_dir, $assoc_file),
-				 &util::filename_cat($dir, $assoc_file), $mime_type);
+	    last if $section eq ""; # back to top level again (more than one document in gml file)
+	}
+
+	# add the associated files
+	$assoc_files = $doc_obj->get_metadata($doc_obj->get_top_section(), "gsdlassocfile");
+	my ($assoc_file_info);
+	foreach $assoc_file_info (@$assoc_files) {
+	    my ($assoc_file, $mime_type, $dir) = split (":", $assoc_file_info);
+	    $dir = "" unless defined $dir;
+	    $doc_obj->associate_file(&util::filename_cat($parent_dir, $assoc_file),
+				     &util::filename_cat($dir, $assoc_file), $mime_type);
 	}
 	$doc_obj->delete_metadata($doc_obj->get_top_section(), "gsdlassocfile");
-
+	
 	# add metadata
 	foreach $field (keys(%$metadata)) {
@@ -218,16 +190,13 @@
 	    }
 	}
-
-
+	
 	# assume the document has an OID
-
+	
 	# process the document
 	$processor->process($doc_obj, $file);
-
+	
 	$no_docs++;
-
-	last if (defined $maxdocs && $maxdocs =~ /\d/ && $no_docs >= $maxdocs);
-
-	last if (!defined $gml);
+	last if ($maxdocs > -1 && $no_docs >= $maxdocs);
+	last unless defined $gml;
     }
 
