| 1 | # | ||||||||||
| 2 | # Graph::Writer::XML - write a directed graph out as XML | ||||||||||
| 3 | # | ||||||||||
| 4 | package Graph::Writer::XML; | ||||||||||
| 5 | 1 | $Graph::Writer::XML::VERSION = '2.10'; | |||||||||
| 6 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use parent 'Graph::Writer'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use XML::Writer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | #======================================================================= | ||||||||||
| 15 | # | ||||||||||
| 16 | # _write_graph() - perform the writing of the graph | ||||||||||
| 17 | # | ||||||||||
| 18 | # This is invoked from the public write_graph() method, | ||||||||||
| 19 | # and is where the actual writing of the graph happens. | ||||||||||
| 20 | # | ||||||||||
| 21 | # Basically we start a graph element then: | ||||||||||
| 22 | # [] dump out any attributes of the graph | ||||||||||
| 23 | # [] dump out all vertices, with any attributes of each vertex | ||||||||||
| 24 | # [] dump out all edges, with any attributes of each edge | ||||||||||
| 25 | # And then close the graph element. Ta da! | ||||||||||
| 26 | # | ||||||||||
| 27 | #======================================================================= | ||||||||||
| 28 | sub _write_graph | ||||||||||
| 29 | 1 | ▶ | { | ||||||||
|
Subroutine
_write_graph: called CC=8 SCAR=22.9
| |||||||||||
| 30 | 1 | my $self = shift; | |||||||||
| 31 | 1 | my $graph = shift; | |||||||||
| 32 | 1 | my $FILE = shift; | |||||||||
| 33 | |||||||||||
| 34 | 1 | my $v; | |||||||||
| 35 | my $from; | ||||||||||
| 36 | 1 | my $to; | |||||||||
| 37 | 1 | my $aref; | |||||||||
| 38 | 1 | my $xmlwriter; | |||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | 1 | $xmlwriter = XML::Writer->new(OUTPUT => $FILE, | |||||||||
| 42 | DATA_MODE => 1, | ||||||||||
| 43 | DATA_INDENT => 2); | ||||||||||
| 44 | 1 | $xmlwriter->setOutput($FILE); | |||||||||
| 45 | |||||||||||
| 46 | 1 | $xmlwriter->startTag('graph'); | |||||||||
| 47 | |||||||||||
| 48 | #------------------------------------------------------------------- | ||||||||||
| 49 | # dump out attributes of the graph, if it has any | ||||||||||
| 50 | #------------------------------------------------------------------- | ||||||||||
| 51 | 1 | $aref = $graph->get_graph_attributes(); | |||||||||
| 52 | 1 | foreach my $attr (keys %{ $aref }) | |||||||||
| 53 | { | ||||||||||
| 54 | $xmlwriter->emptyTag('attribute', | ||||||||||
| 55 | 'name' => $attr, | ||||||||||
| 56 | 0 | 'value' => $aref->{$attr}); | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | #------------------------------------------------------------------- | ||||||||||
| 60 | # dump out vertices of the graph, including any attributes | ||||||||||
| 61 | #------------------------------------------------------------------- | ||||||||||
| 62 | 1 | foreach $v (sort $graph->vertices) | |||||||||
| 63 | { | ||||||||||
| 64 | 5 | $aref = $graph->get_vertex_attributes($v); | |||||||||
| 65 | |||||||||||
| 66 | 5 | ▶ | if (keys(%{ $aref }) > 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | { | ||||||||||
| 68 | 0 | $xmlwriter->startTag('node', 'id' => $v); | |||||||||
| 69 | |||||||||||
| 70 | 0 | foreach my $attr (keys %{ $aref }) | |||||||||
| 71 | { | ||||||||||
| 72 | $xmlwriter->emptyTag('attribute', | ||||||||||
| 73 | 'name' => $attr, | ||||||||||
| 74 | 0 | 'value' => $aref->{$attr}); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | $xmlwriter->endTag('node'); | |||||||||
| 78 | } | ||||||||||
| 79 | else | ||||||||||
| 80 | { | ||||||||||
| 81 | 5 | $xmlwriter->emptyTag('node', 'id' => $v); | |||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | #------------------------------------------------------------------- | ||||||||||
| 86 | # dump out edges of the graph, including any attributes | ||||||||||
| 87 | #------------------------------------------------------------------- | ||||||||||
| 88 | 1 | foreach my $edge (sort _by_vertex $graph->edges) | |||||||||
| 89 | { | ||||||||||
| 90 | 7 | ($from, $to) = @$edge; | |||||||||
| 91 | 7 | $aref = $graph->get_edge_attributes($from, $to); | |||||||||
| 92 | 7 | ▶ | if (keys(%{ $aref }) > 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | { | ||||||||||
| 94 | 0 | $xmlwriter->startTag('edge', 'from' => $from, 'to' => $to); | |||||||||
| 95 | |||||||||||
| 96 | 0 | foreach my $attr (keys %{ $aref }) | |||||||||
| 97 | { | ||||||||||
| 98 | $xmlwriter->emptyTag('attribute', | ||||||||||
| 99 | 'name' => $attr, | ||||||||||
| 100 | 0 | 'value' => $aref->{$attr}); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 0 | $xmlwriter->endTag('edge'); | |||||||||
| 104 | } | ||||||||||
| 105 | else | ||||||||||
| 106 | { | ||||||||||
| 107 | 7 | $xmlwriter->emptyTag('edge', 'from' => $from, 'to' => $to); | |||||||||
| 108 | } | ||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 1 | $xmlwriter->endTag('graph'); | |||||||||
| 112 | 1 | $xmlwriter->end(); | |||||||||
| 113 | |||||||||||
| 114 | 1 | return 1; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | sub _by_vertex | ||||||||||
| 118 | 12 | ▶ | { | ||||||||
|
Subroutine
_by_vertex: called CC=1 SCAR=0.0
| |||||||||||
| 119 | 12 | return $a->[0].$a->[1] cmp $b->[0].$b->[1]; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 1 | 1; | |||||||||
| 123 | |||||||||||
| 124 | __END__ | ||||||||||