File Coverage

blib/lib/Autodia/Handler/dia.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Autodia::Handler::dia;
2             require Exporter;
3 1     1   1940 use strict;
  1         7  
  1         39  
4              
5             =head1 NAME
6              
7             Autodia::Handler::dia - AutoDia handler for dia
8              
9             =head1 DESCRIPTION
10              
11             This provides Autodia with the ability to read dia files, allowing you to convert them via the Diagram Export methods to images (using GraphViz and VCG) or html/xml using custom templates.
12              
13             The dia handler will parse dia xml files using XML::Simple and populating the diagram object with class, superclass and package objects.
14              
15             the dia handler is registered in the Autodia.pm module, which contains a hash of language names and the name of their respective language - in this case:
16              
17             =head1 SYNOPSIS
18              
19             use Autodia::Handler::dia;
20              
21             my $handler = Autodia::Handler::dia->New(\%Config);
22              
23             $handler->Parse(filename); # where filename includes full or relative path.
24              
25             =head2 CONSTRUCTION METHOD
26              
27             my $handler = Autodia::Handler::dia->New(\%Config);
28             This creates a new handler using the Configuration hash to provide rules selected at the command line.
29              
30             =head2 ACCESS METHODS
31              
32             $handler->Parse(filename); # where filename includes full or relative path.
33              
34             This parses the named file and returns 1 if successful or 0 if the file could not be opened.
35              
36             =cut
37              
38 1     1   5 use vars qw($VERSION @ISA @EXPORT);
  1         3  
  1         55  
39 1     1   5 use Autodia::Handler;
  1         2  
  1         42  
40              
41             @ISA = qw(Autodia::Handler Exporter);
42              
43 1     1   5 use Autodia::Diagram;
  1         1  
  1         17  
44 1     1   4 use Data::Dumper;
  1         2  
  1         37  
45              
46 1     1   398 use XML::Simple;
  0            
  0            
47              
48             #---------------------------------------------------------------
49              
50             #####################
51             # Constructor Methods
52              
53             # new inherited from Autodia::Handler
54              
55             #------------------------------------------------------------------------
56             # Access Methods
57              
58             # parse_file inherited from Autodia::Handler
59              
60             #-----------------------------------------------------------------------------
61             # Internal Methods
62              
63             # _initialise inherited from Autodia::Handler
64              
65             sub _parse {
66             my $self = shift;
67             my $fh = shift;
68             my $filename = shift;
69              
70             my $Diagram = $self->{Diagram};
71             my $xml = XMLin(join('',<$fh>));
72              
73             my %entity;
74             my @relationships;
75              
76             # Walk the data structure based on the XML created by XML Simple
77             foreach my $dia_object_id ( keys %{$xml->{'dia:layer'}->{'dia:object'}} ) {
78             my $object = $xml->{'dia:layer'}{'dia:object'}{$dia_object_id};
79             my $type = $object->{type};
80             if (is_entity($type)) {
81             warn "handling entity type : $type\n";
82             my $name = $object->{'dia:attribute'}{name}{'dia:string'};
83             $name =~ s/#(.*)#/$1/;
84             if ($type eq 'UML - Class') {
85             my $Class = Autodia::Diagram::Class->new($name);
86             $Diagram->add_class($Class);
87             $entity{$dia_object_id} = $Class;
88             foreach my $method ( @{get_methods($object->{'dia:attribute'}{operations}{'dia:composite'})} ) {
89             $Class->add_operation($method);
90             }
91             foreach my $attribute (@{get_attributes($object->{'dia:attribute'}{attributes}{'dia:composite'})}){
92             $Class->add_attribute( $attribute );
93             }
94             } else {
95             my $Component = Autodia::Diagram::Component->new($name);
96             $Diagram->add_component($Component);
97             $entity{$dia_object_id} = $Component;
98             }
99             } else {
100             my $connection = $object->{'dia:connections'}{'dia:connection'};
101             warn "handling connection type : $type\n";
102              
103             push (@relationships , {
104             from=>$connection->[0]{to},
105             to=> $connection->[1]{to},
106             type=> $type,
107             });
108             }
109             }
110              
111             foreach my $connection ( @relationships ) {
112             if ($connection->{type} eq 'UML - Generalization') {
113             my $Inheritance = Autodia::Diagram::Inheritance->new(
114             $entity{$connection->{from}},
115             $entity{$connection->{to}},
116             );
117             $entity{$connection->{from}}->add_inheritance($Inheritance);
118             $entity{$connection->{to}}->add_inheritance($Inheritance);
119             $Diagram->add_inheritance($Inheritance);
120             } else {
121             # create new dependancy
122             my $Dependancy = Autodia::Diagram::Dependancy->new(
123             $entity{$connection->{from}},
124             $entity{$connection->{to}},
125             );
126             # add dependancy to diagram
127             $Diagram->add_dependancy($Dependancy);
128             # add dependancy to class
129             $entity{$connection->{from}}->add_dependancy($Dependancy);
130             # add dependancy to component
131             $entity{$connection->{to}}->add_dependancy($Dependancy);
132             }
133             }
134             }
135              
136              
137             ####-----
138              
139             sub is_entity {
140             my $object_type = shift;
141             my $IsEntity = 0;
142             $IsEntity = 1 if ($object_type =~ /(class|package)/i);
143             return $IsEntity;
144             }
145              
146             sub get_methods {
147             my $methods = shift;
148             my $return = [];
149             my $ref = ref $methods;
150             if ($ref eq 'ARRAY' ) {
151             foreach my $method (@$methods) {
152             my $name = $method->{'dia:attribute'}{name}{'dia:string'};
153             my $type = $method->{'dia:attribute'}{type}{'dia:string'};
154             $name =~ s/#(.*)#/$1/g;
155             $type = 'void' if (ref $type);
156             $type =~ s/#//g;
157             my $arguments = get_parameters($method->{'dia:attribute'}{parameters}{'dia:composite'});
158             push(@$return,{name=>$name,type=>$type,Params=>$arguments, visibility=>0});
159             }
160             } elsif ($ref eq "HASH") {
161             my $name = $methods->{'dia:attribute'}{name}{'dia:string'};
162             my $type = $methods->{'dia:attribute'}{type}{'dia:string'};
163             $name =~ s/#(.*)#/$1/g;
164             $type = 'void' if (ref $type);
165             $type =~ s/#//g;
166             my $arguments = get_parameters($methods->{'dia:attribute'}{parameters}{'dia:composite'});
167             push(@$return,{name=>$name,type=>$type,Params=>$arguments, visibility=>0});
168             }
169             return $return;
170             }
171              
172             sub get_parameters {
173             my $arguments = shift;
174             my $return = [];
175             if (ref $arguments) {
176             if (ref $arguments eq 'ARRAY') {
177             my @arguments = map (
178             {
179             Type=> $_->{'dia:attribute'}{type}{'dia:string'},
180             Name=> $_->{'dia:attribute'}{name}{'dia:string'},
181             }, @$arguments
182             );
183             foreach my $argument (@arguments) {
184             $argument->{Type} =~ s/#//g;
185             $argument->{Name} =~ s/#//g;
186             }
187             $return = \@arguments;
188             } else {
189             my $argument = { Type=>$arguments->{'dia:attribute'}{type}{'dia:string'},
190             Name=>$arguments->{'dia:attribute'}{name}{'dia:string'}, };
191             $argument->{Type} =~ s/#//g;
192             $argument->{Name} =~ s/#//g;
193             push(@$return,$argument);
194             }
195             }
196             return $return;
197             }
198              
199             sub get_attributes {
200             my $attributes = shift;
201             my $ref = ref $attributes;
202             my $return = [];
203             if ($ref eq 'ARRAY') {
204             foreach my $attribute (@$attributes) {
205             my $name = $attribute->{'dia:attribute'}{name}{'dia:string'};
206             my $type = $attribute->{'dia:attribute'}{type}{'dia:string'};
207             $name =~ s/#//g;
208             $type =~ s/#//g;
209             push (@$return, {name => $name, type=> $type, visibility=>0});
210             }
211             } elsif ($ref eq 'HASH') {
212             my $name = $attributes->{'dia:attribute'}{name}{'dia:string'};
213             my $type = $attributes->{'dia:attribute'}{type}{'dia:string'};
214             $name =~ s/#//g;
215             $type =~ s/#//g;
216             push (@$return, {name => $name, type=> $type, visibility=>0});
217             }
218             return $return;
219             }
220              
221              
222             ###############################################################################
223              
224             =head1 SEE ALSO
225              
226             Autodia::Handler
227              
228             Autodia::Diagram
229              
230             =head1 AUTHOR
231              
232             Aaron Trevena, Eaaron.trevena@gmail.comE
233              
234             =head1 COPYRIGHT AND LICENSE
235              
236             Copyright (C) 2001-2007 by Aaron Trevena
237              
238             This library is free software; you can redistribute it and/or modify
239             it under the same terms as Perl itself, either Perl version 5.8.1 or,
240             at your option, any later version of Perl 5 you may have available.
241              
242             =cut
243              
244              
245             1;
246              
247              
248