allow either file or string to be passed for parsing
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / GraphML.pm
index 7807189..c29f78a 100644 (file)
@@ -23,26 +23,36 @@ GraphML conventions of the source program.
 
 =item B<parse>
 
-parse( $graphml_string );
+parse( $init_opts );
 
-Takes a string containing the GraphML; returns a list of nodes, edges,
+Takes a set of Tradition initialization options, among which should be either
+'file' or 'string'; parses that file or string and returns a list of nodes, edges,
 and their associated data.
 
 =cut
 
-use vars qw/ $xpc $nodedata $witnesses /;
+use vars qw/ $xpc $graphattr $nodedata $witnesses /;
 
 # Return graph -> nodeid -> { key1/val1, key2/val2, key3/val3 ... }
 #              -> edgeid -> { source, target, wit1/val1, wit2/val2 ...}
 
 sub parse {
-    my( $graphml_str ) = @_;
+    my( $opts ) = @_;
 
     my $graph_hash = { 'nodes' => [],
                        'edges' => [] };
 
     my $parser = XML::LibXML->new();
-    my $doc = $parser->parse_string( $graphml_str );
+    my $doc;
+    if( exists $opts->{'string'} ) {
+        $doc = $parser->parse_string( $opts->{'string'} );
+    } elsif ( exists $opts->{'file'} ) {
+        $doc = $parser->parse_file( $opts->{'file'} );
+    } else {
+        warn "Could not find string or file option to parse";
+        return;
+    }
+    
     my $graphml = $doc->documentElement();
     $xpc = XML::LibXML::XPathContext->new( $graphml );
     $xpc->registerNs( 'g', 'http://graphml.graphdrawing.org/xmlns' );
@@ -54,10 +64,13 @@ sub parse {
         my $keyid = $k->getAttribute( 'id' );
         my $keyname = $k->getAttribute( 'attr.name' );
 
-        if( $k->getAttribute( 'for' ) eq 'node' ) {
-            # Keep track of the XML identifiers for the data carried
-            # in each node element.
-            $nodedata->{$keyid} = $keyname
+               # Keep track of the XML identifiers for the data carried
+               # in each node element.
+               my $dtype = $k->getAttribute( 'for' );
+               if( $dtype eq 'graph' ) {
+                       $graphattr->{$keyid} = $keyname;
+        } elsif( $dtype eq 'node' ) {
+            $nodedata->{$keyid} = $keyname;
         } else {
             $witnesses->{$keyid} = $keyname;
         }
@@ -66,8 +79,17 @@ sub parse {
     my $graph_el = $xpc->find( '/g:graphml/g:graph' )->[0];
 
     my $node_reg = {};
+    
+    # Read in graph globals (if any).
+    print STDERR "Reading graphml global data\n";
+    foreach my $dkey ( keys %$graphattr ) {
+       my $keyname = $graphattr->{$dkey};
+       my $keyvalue = _lookup_node_data( $graph_el, $dkey );
+       $graph_hash->{'global'}->{$keyname} = $keyvalue;
+    }
 
-    # Add the nodes to the graph hash. 
+    # Add the nodes to the graph hash.
+    print STDERR "Reading graphml nodes\n"; 
     my @nodes = $xpc->findnodes( '//g:node' );
     foreach my $n ( @nodes ) {
         # Could use a better way of registering these
@@ -82,6 +104,7 @@ sub parse {
     }
         
     # Now add the edges, and cross-ref with the node objects.
+    print STDERR "Reading graphml edges\n";
     my @edges = $xpc->findnodes( '//g:edge' );
     foreach my $e ( @edges ) {
         my $from = $e->getAttribute('source');
@@ -107,7 +130,13 @@ sub parse {
 sub _lookup_node_data {
     my( $xmlnode, $key ) = @_;
     my $lookup_xpath = './g:data[@key="%s"]/child::text()';
-    my $data = $xpc->findvalue( sprintf( $lookup_xpath, $key ), $xmlnode );
+    my $data = $xpc->find( sprintf( $lookup_xpath, $key ), $xmlnode );
+    # If we get back an empty nodelist, we return undef.
+    if( ref( $data ) ) {
+       return undef unless $data->size;
+       return $data->to_literal->value;
+    }
+    # Otherwise we got back a value. Return it.
     return $data;
 }