1 package Text::Tradition::Parser::GraphML;
6 use XML::LibXML::XPathContext;
10 Text::Tradition::Parser::GraphML
14 Parser module for Text::Tradition, given a GraphML file that describes
15 a collation graph. Returns the information about the graph that has
16 been parsed out from the GraphML. This module is meant to be used
17 with a module (e.g. CollateX or Self) that interprets the specific
18 GraphML conventions of the source program.
26 parse( $graphml_string );
28 Takes a string containing the GraphML; returns a list of nodes, edges,
29 and their associated data.
33 use vars qw/ $xpc $graphattr $nodedata $witnesses /;
35 # Return graph -> nodeid -> { key1/val1, key2/val2, key3/val3 ... }
36 # -> edgeid -> { source, target, wit1/val1, wit2/val2 ...}
39 my( $graphml_str ) = @_;
41 my $graph_hash = { 'nodes' => [],
44 my $parser = XML::LibXML->new();
45 my $doc = $parser->parse_string( $graphml_str );
46 my $graphml = $doc->documentElement();
47 $xpc = XML::LibXML::XPathContext->new( $graphml );
48 $xpc->registerNs( 'g', 'http://graphml.graphdrawing.org/xmlns' );
50 # First get the ID keys, for witnesses and for collation data
51 foreach my $k ( $xpc->findnodes( '//g:key' ) ) {
52 # Each key has a 'for' attribute; the edge keys are witnesses, and
53 # the node keys contain an ID and string for each node.
54 my $keyid = $k->getAttribute( 'id' );
55 my $keyname = $k->getAttribute( 'attr.name' );
57 # Keep track of the XML identifiers for the data carried
58 # in each node element.
59 my $dtype = $k->getAttribute( 'for' );
60 if( $dtype eq 'graph' ) {
61 $graphattr->{$keyid} = $keyname;
62 } elsif( $dtype eq 'node' ) {
63 $nodedata->{$keyid} = $keyname;
65 $witnesses->{$keyid} = $keyname;
69 my $graph_el = $xpc->find( '/g:graphml/g:graph' )->[0];
73 # Read in graph globals (if any).
74 print STDERR "Reading graphml global data\n";
75 foreach my $dkey ( keys %$graphattr ) {
76 my $keyname = $graphattr->{$dkey};
77 my $keyvalue = _lookup_node_data( $graph_el, $dkey );
78 $graph_hash->{'global'}->{$keyname} = $keyvalue;
81 # Add the nodes to the graph hash.
82 print STDERR "Reading graphml nodes\n";
83 my @nodes = $xpc->findnodes( '//g:node' );
84 foreach my $n ( @nodes ) {
85 # Could use a better way of registering these
87 foreach my $dkey ( keys %$nodedata ) {
88 my $keyname = $nodedata->{$dkey};
89 my $keyvalue = _lookup_node_data( $n, $dkey );
90 $node_hash->{$keyname} = $keyvalue if defined $keyvalue;
92 $node_reg->{$n->getAttribute( 'id' )} = $node_hash;
93 push( @{$graph_hash->{'nodes'}}, $node_hash );
96 # Now add the edges, and cross-ref with the node objects.
97 print STDERR "Reading graphml edges\n";
98 my @edges = $xpc->findnodes( '//g:edge' );
99 foreach my $e ( @edges ) {
100 my $from = $e->getAttribute('source');
101 my $to = $e->getAttribute('target');
103 # We don't know whether the edge data is one per witness
104 # or one per witness type, or something else. So we just
105 # save it and let our calling parser decide.
107 'source' => $node_reg->{$from},
108 'target' => $node_reg->{$to},
110 foreach my $wkey( keys %$witnesses ) {
111 my $wname = $witnesses->{$wkey};
112 my $wlabel = _lookup_node_data( $e, $wkey );
113 $edge_hash->{$wname} = $wlabel if $wlabel;
115 push( @{$graph_hash->{'edges'}}, $edge_hash );
120 sub _lookup_node_data {
121 my( $xmlnode, $key ) = @_;
122 my $lookup_xpath = './g:data[@key="%s"]/child::text()';
123 my $data = $xpc->find( sprintf( $lookup_xpath, $key ), $xmlnode );
124 # If we get back an empty nodelist, we return undef.
126 return undef unless $data->size;
127 return $data->to_literal->value;
129 # Otherwise we got back a value. Return it.
137 This package is free software and is provided "as is" without express
138 or implied warranty. You can redistribute it and/or modify it under
139 the same terms as Perl itself.
143 Tara L Andrews, aurum@cpan.org