use strict;
use warnings;
+use Exporter 'import';
+use vars qw/ @EXPORT_OK $xpc /;
+
use XML::LibXML;
use XML::LibXML::XPathContext;
+@EXPORT_OK = qw/ graphml_parse /;
+
=head1 NAME
Text::Tradition::Parser::GraphML
=head1 METHODS
-=over
+=head2 B<graphml_parse>( $init_opts )
-=item B<parse>
+parse( $init_opts );
-parse( $graphml_string );
-
-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 $graphattr $nodedata $witnesses /;
-
# Return graph -> nodeid -> { key1/val1, key2/val2, key3/val3 ... }
# -> edgeid -> { source, target, wit1/val1, wit2/val2 ...}
-sub parse {
- my( $graphml_str ) = @_;
+sub graphml_parse {
+ 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( $graphattr, $nodedata, $witnesses ) = ( {}, {}, {} );
my $graphml = $doc->documentElement();
$xpc = XML::LibXML::XPathContext->new( $graphml );
$xpc->registerNs( 'g', 'http://graphml.graphdrawing.org/xmlns' );
}
my $graph_el = $xpc->find( '/g:graphml/g:graph' )->[0];
+ $graph_hash->{'name'} = $graph_el->getAttribute( 'id' );
my $node_reg = {};
return $graph_hash;
}
+
sub _lookup_node_data {
my( $xmlnode, $key ) = @_;
my $lookup_xpath = './g:data[@key="%s"]/child::text()';