XML parsers should accept already-parsed XML object too
Tara L Andrews [Fri, 16 Aug 2013 07:28:22 +0000 (09:28 +0200)]
base/lib/Text/Tradition/Parser/CTE.pm
base/lib/Text/Tradition/Parser/GraphML.pm
base/lib/Text/Tradition/Parser/TEI.pm

index 70248d5..bcde6e6 100644 (file)
@@ -121,14 +121,22 @@ sub parse {
     
     # Finally, add explicit witness paths, remove the base paths, and remove
     # the app/anchor tags.
-    _expand_all_paths( $c );
+    try {
+           _expand_all_paths( $c );
+       } catch( $err ) {
+               throw( $err );
+       }
 
     # Save the text for each witness so that we can ensure consistency
     # later on
     unless( $opts->{'nocalc'} ) {
-               $tradition->collation->text_from_paths();       
-               $tradition->collation->calculate_ranks();
-               $tradition->collation->flatten_ranks();
+       try {
+                       $tradition->collation->text_from_paths();       
+                       $tradition->collation->calculate_ranks();
+                       $tradition->collation->flatten_ranks();
+               } catch( $err ) {
+                       throw( $err );
+               }
        }
 }
 
@@ -157,6 +165,8 @@ sub _remove_formatting {
         $doc = $parser->parse_string( $opts->{'string'} );
     } elsif ( exists $opts->{'file'} ) {
         $doc = $parser->parse_file( $opts->{'file'} );
+    } elsif ( exists $opts->{'xmlobj'} ) {
+       $doc = $opts->{'xmlobj'};
     } else {
         warn "Could not find string or file option to parse";
         return;
@@ -402,7 +412,7 @@ sub _attach_transposition {
                        }
                $success = 1;
                } else {
-                       say STDERR "ERROR: lemma and transposed sequence different lengths?!"
+                       throw( "Lemma at $found and transposed sequence different lengths?!" );
                }
        } else {
                say STDERR "WARNING: Unable to find $reftxt in base text for transposition";
@@ -468,7 +478,7 @@ sub interpret {
                my @end = split( /\s+/, $2 );
                if( scalar( @begin ) + scalar ( @end ) > scalar( @words ) ) {
                        # Something is wrong and we can't do the splice.
-                       say STDERR "ERROR: $lemma is too short to accommodate $oldreading";
+                       throw( "$lemma is too short to accommodate $oldreading" );
                } else {
                        splice( @words, 0, scalar @begin, @begin );
                        splice( @words, -(scalar @end), scalar @end, @end );
index 63305ac..63f695d 100644 (file)
@@ -46,6 +46,8 @@ sub graphml_parse {
         $doc = $parser->parse_string( $opts->{'string'} );
     } elsif ( exists $opts->{'file'} ) {
         $doc = $parser->parse_file( $opts->{'file'} );
+    } elsif ( exists $opts->{'xmlobj'} ) {
+       $doc = $opts->{'xmlobj'};
     } else {
         warn "Could not find string or file option to parse";
         return;
index 3e3c47e..3b0649f 100644 (file)
@@ -108,6 +108,8 @@ sub parse {
         $doc = $parser->parse_string( $opts->{'string'} );
     } elsif ( exists $opts->{'file'} ) {
         $doc = $parser->parse_file( $opts->{'file'} );
+    } elsif ( exists $opts->{'xmlobj'} ) {
+       $doc = $opts->{'xmlobj'};
     } else {
         warn "Could not find string or file option to parse";
         return;