blib/lib/XML/Handler/Machine2GraphViz.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package XML::Handler::Machine2GraphViz;
2  
3 =head1 NAME
4  
5 XML::Handler::Machine2GraphViz;
6  
7 =head1 SYNOPSIS
8  
9 use XML::Filter::Machine2GraphViz;
10  
11 my $m = Machine( .... );
12 binmode STDOUT;
13  
14 ## Short form:
15  
16 print machine_2_graphviz( $m )->as_png;
17
18 ## Long form:
19  
20 my $h = XML::Handler::Machine2GraphViz->new;
21  
22 $m->generate_description( $h );
23  
24 print $h->graphviz->as_png;
25  
26 =head1 DESCRIPTION
27  
28 A SAX2 processor that turns SAX machine descriptions in to a GraphViz object.
29  
30 =cut
31  
32 $VERSION = 0.2;
33  
34 1 ▶use XML::Filter::Dispatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 @ISA = qw( XML::Filter::Dispatcher Exporter );
38  
39 @EXPORT = qw( machine2graphviz );
40 %EXPORT_TAGS = ( all => \@EXPORT );
41  
42 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 0 ▶use GraphViz;
Subroutine BEGIN: called CC=1 SCAR=2.6
44  
45 sub _empty($) { !defined $_[0] || ! length $_[0] }
46  
47 sub new {
48 my $proto = shift;
49  
50 my $self = $proto->SUPER::new(
51 Rules => {
52 "//part" => sub {
53 my ( $self, $elt ) = @_;
54  
55 my $attrs = $elt->{Attributes};
56 my $id = $attrs->{id}->{Value};
57 my $desc = $attrs->{description}->{Value};
58 my $type = $attrs->{type}->{Value};
59 my $name = $attrs->{name}->{Value};
60  
61 $self->push( {
62 id => $id,
63 } );
64  
65 my $label = $desc;
66  
67 if ( _empty $label ) {
68 $label = $name || "";
69 $label .= "($type)" if ! _empty $type;
70 $label = "<<Unnamed, id= $id>>" if _empty $label;
71 }
72  
73 $label =~ s/ *\(/\\n(/;
74  
75 $self->{GraphViz}->add_node(
76 $id,
77 label => $label,
78 fontsize => 12,
79 fontname => "Helvetica",
80 );
81  
82 },
83  
84 "//sax-machine" => \&XML::Filter::Dispatcher::push,
85  
86 ## studiously avoid machine handlers, they're redundant and
87 ## we can't properly graph machines until GraphViz.pm
88 ## supports nested graphs :)
89 "//part/handler" => sub {
90 my ( $self, $elt ) = @_;
91  
92 my $attrs = $elt->{Attributes};
93 my $handler_id = $attrs->{"handler-id"}->{Value};
94 my $handler_name = $attrs->{"name"}->{Value};
95  
96 ## This usually happens only for Exhaust nodes.
97 return if _empty $handler_id;
98  
99 $self->{GraphViz}->add_edge(
100 $self->peek->{id},
101 $handler_id
102 );
103 },
104 },
105 );
106  
107 return $self;
108 }
109  
110  
111 sub start_document {
112 my $self = shift;
113  
114 $self->{GraphViz} = GraphViz->new(
115 rankdir => "LR",
116 );
117  
118 $self->SUPER::start_document( @_ );
119 }
120  
121  
122 sub graphviz {
123 return shift()->{GraphViz};
124 }
125  
126  
127 sub machine2graphviz {
128 my $machine = shift;
129  
130 my $h = __PACKAGE__->new;
131 $machine->generate_description( $h );
132 $h->graphviz || GraphViz->new;
133 }
134  
135  
136 =head1 LIMITATIONS
137  
138 Exports 1 functio by default, do
139  
140 use XML::Filter::Machine2GraphViz ();
141  
142 to prevent this.
143  
144 Does not show the machines yet, GraphViz.pm needs to be upgraded to deal with
145 nested graphs to do this. It does clustering, but not nested graphs at the
146 moment (v1.4).
147  
148 =head1 AUTHOR
149  
150 Barrie Slaymaker <barries@slaysys.com>
151  
152 =head1 COPYRIGHT
153  
154 Copyright 2002, Barrie Slaymaker, All Rights Reserved.
155  
156 You may use this module under the terms of the Artistic, GNU Public, or
157 BSD licenses, your choise.
158  
159 =cut
160  
161 1;
↑ Summary