File Coverage

blib/lib/SVG/Convert.pm
Criterion Covered Total %
statement 25 27 92.5
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 34 36 94.4


line stmt bran cond sub pod time code
1             package SVG::Convert;
2              
3 1     1   5 use strict;
  1         1  
  1         29  
4 1     1   5 use warnings;
  1         2  
  1         36  
5              
6 1     1   4 use base qw(Class::Accessor::Fast);
  1         2  
  1         740  
7              
8             __PACKAGE__->mk_accessors(qw/drivers parser/);
9              
10 1     1   4027 use Carp::Clan;
  1         4045  
  1         6  
11 1     1   1020 use Module::Load;
  1         984  
  1         6  
12             use Module::Pluggable::Fast (
13 1         7 name => '_drivers',
14             search => ['SVG::Convert::Driver'],
15             require => 1
16 1     1   829 );
  1         5223  
17 1     1   1045 use Params::Validate qw(:all);
  1         4549  
  1         226  
18 1     1   9 use Scalar::Util qw(weaken);
  1         6  
  1         97  
19 1     1   387 use XML::LibXML;
  0            
  0            
20              
21             =head1 NAME
22              
23             SVG::Convert - The fantastic new SVG::Convert!
24              
25             =head1 VERSION
26              
27             version 0.02
28              
29             =cut
30              
31             our $VERSION = '0.02';
32              
33             =head1 SYNOPSIS
34              
35             use SVG::Convert;
36            
37             my $svgconv = SVG::Convert->new();
38             print $svgconv->convert(format => "xaml", src_file => "examples/01.svg", output => "string");
39              
40             =head1 METHODS
41              
42             =head2 new
43              
44             Constructor.
45             The "$args" arguments is HASHREF.
46             See below more details about $args.
47              
48             =over 4
49              
50             =item driver_opts
51              
52             The driver_opts parameter is HASHREF.
53             The keys of HASHREF are lower-cased suffix of driver module name.
54              
55             For example, If driver is L, then the key is "xaml".
56             The values of HASHREF are parameter needed by each of drivers.
57              
58             my $sconv = SVG::Convert->new({
59             driver_opts => {
60             xaml => {
61             ## for Driver::XAML
62             }
63             }
64             });
65              
66             =back
67              
68             =cut
69              
70             sub new {
71             my ($class, $args) = @_;
72              
73             $args ||= {};
74             $args = { driver_opts => {}, %$args, drivers => {} };
75              
76             my $self = $class->SUPER::new($args);
77             $self->parser(XML::LibXML->new);
78              
79             for my $driver ($self->_drivers) {
80             eval { load $driver; };
81             if ($@) {
82             croak($@);
83             }
84              
85             my ($suffix) = map { lc } $driver =~ m/SVG::Convert::Driver::(.+)/;
86              
87             my $driver_opts = (
88             exists $args->{driver_opts}->{$suffix} &&
89             ref $args->{driver_opts}->{$suffix} eq 'HASH'
90             ) ? $args->{driver_opts}->{$suffix} : {};
91              
92             $self->drivers->{$suffix} = $driver->new({
93             parser => $self->parser,
94             %$driver_opts
95             });
96             }
97              
98             return $self;
99             }
100              
101             =head2 convert(%args)
102              
103             See below about %args details.
104              
105             my $xaml_doc = $sconv->convert(
106             format => "xaml",
107             src_file => $src_file,
108             output => "doc"
109             );
110              
111             =over 4
112              
113             =item format
114              
115             The format parameter is string value represented format type for converting.
116             This value is lower-cased suffix of driver module name.
117              
118             For example, If the driver module is L, then this value is "xaml".
119              
120             =item src_file
121              
122             The src_file parameter is string value represented SVG source file name.
123              
124             =item src_string
125              
126             The src_file parameter is string value represented SVG source string.
127              
128             =item src_doc
129              
130             The src_doc parameter is L object value represented SVG source document.
131              
132             =item output
133              
134             The output parameter is "file" or "string" or "doc".
135              
136             =item output_file
137              
138             The output_file parameter is output filename.
139              
140             =item convert_opts
141              
142             The convert_opts parameter is extra params for driver.
143              
144             =back
145              
146             =cut
147              
148             sub convert {
149             my $self = shift;
150             my %args = validate_with(
151             params => \@_,
152             spec => {
153             format => {
154             type => SCALAR,
155             callbacks => {
156             'installed driver' => sub {
157             exists $self->drivers->{$_[0]};
158             }
159             }
160             },
161             src_file => {
162             type => SCALAR,
163             optional => 1,
164             callbacks => {
165             'exists file' => sub {
166             -e $_[0] && -f $_[0];
167             }
168             }
169             },
170             src_string => {
171             type => SCALAR,
172             optional => 1,
173             },
174             src_doc => {
175             type => OBJECT,
176             optional => 1,
177             isa => [qw/XML::LibXML::Document/]
178             },
179             output => {
180             type => SCALAR,
181             default => 'string',
182             callbacks => {
183             'enable parameters' => sub {
184             $_[0] eq 'file' || $_[0] eq 'string' || $_[0] eq 'doc'
185             }
186             }
187             },
188             output_file => {
189             type => SCALAR,
190             optional => 1,
191             depends => [qw/output/]
192             },
193             convert_opts => {
194             type => HASHREF,
195             optional => 1,
196             },
197             }
198             );
199              
200             my $driver = $self->drivers->{$args{format}};
201             my $src_doc = $args{src_doc} ||
202             ($args{src_file}) ?
203             $self->parser->parse_file($args{src_file}) :
204             $self->parser->parse_string($args{src_string});;
205              
206             my $convert_opts = ($args{convert_opts}) ? $args{convert_opts} : {};
207              
208             my $method = "convert_" . $args{output};
209             return $driver->$method($src_doc, $args{output_file}, $convert_opts);
210             }
211              
212             =head1 SEE ALSO
213              
214             =over 4
215              
216             =item L
217              
218             =item L
219              
220             =item L
221              
222             =item L
223              
224             =item L
225              
226             =item L
227              
228             =item L
229              
230             =item L
231              
232             =back
233              
234             =head1 AUTHOR
235              
236             Toru Yamaguchi, C<< >>
237              
238             =head1 BUGS
239              
240             Please report any bugs or feature requests to
241             C, or through the web interface at
242             L. I will be notified, and then you'll automatically be
243             notified of progress on your bug as I make changes.
244              
245             =head1 COPYRIGHT & LICENSE
246              
247             Copyright 2007 Toru Yamaguchi, All Rights Reserved.
248              
249             This program is free software; you can redistribute it and/or modify it
250             under the same terms as Perl itself.
251              
252             =cut
253              
254             1; # End of SVG::Convert