File Coverage

lib/XML/Compile/Dumper.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             # Copyrights 2007-2014 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.01.
5              
6 1     1   2050 use warnings;
  1         1  
  1         29  
7 1     1   5 use strict;
  1         1  
  1         57  
8              
9             package XML::Compile::Dumper;
10             our $VERSION = '0.14';
11              
12              
13 1     1   788 use Log::Report 'xml-compile', syntax => 'SHORT';
  1         148153  
  1         8  
14 1     1   2144 use Data::Dump::Streamer;
  1         97665  
  1         14  
15 1     1   176 use POSIX qw/asctime/;
  1         2  
  1         11  
16 1     1   73 use IO::File;
  1         2  
  1         170  
17              
18             # I have no idea why the next is needed, but without it, the
19             # tests are failing.
20 1     1   1809 use XML::Compile::Schema;
  0            
  0            
21              
22              
23             sub new(@)
24             { my ($class, %opts) = @_;
25             (bless {}, $class)->init(\%opts);
26             }
27              
28             sub init($)
29             { my ($self, $opts) = @_;
30              
31             my $fh = $opts->{filehandle};
32             unless($fh)
33             { my $fn = $opts->{filename}
34             or error __x"either filename or filehandle required";
35              
36             $fh = IO::File->new($fn, '>:utf8')
37             or fault __x"cannot write to {filename}", filename => $fn;
38             }
39             $self->{XCD_fh} = $fh;
40              
41             my $package = $opts->{package}
42             or error __x"package name required";
43              
44             $self->header($fh, $package);
45             $self;
46             }
47              
48              
49             sub close()
50             { my $self = shift;
51             my $fh = $self->file or return 1;
52              
53             $self->footer($fh);
54             delete $self->{XCD_fh};
55             $fh->close;
56             }
57              
58             sub DESTROY()
59             { my $self = shift;
60             $self->close;
61             }
62              
63              
64             sub file() {shift->{XCD_fh}}
65              
66              
67             sub header($$)
68             { my ($self, $fh, $package) = @_;
69             my $date = asctime localtime;
70             $date =~ s/\n.*//;
71              
72             my $xc_version = $XML::Compile::VERSION || '(devel)';
73              
74             $fh->print( <<__HEADER );
75             #crash
76             # This module has been generated using
77             # XML::Compile $xc_version
78             # Data::Dump::Streamer $Data::Dump::Streamer::VERSION
79             # Created with a script
80             # named $0
81             # on $date
82              
83             use warnings;
84             no warnings 'once';
85             no strict; # sorry
86              
87             package $package;
88             use base 'Exporter';
89              
90             use XML::LibXML ();
91             use Log::Report;
92             use Data::Dump::Streamer ':undump';
93              
94             our \@EXPORT;
95              
96             # We will need these modules
97             { package XML::Compile::Translate::Reader;
98             use Log::Report;
99              
100             ; package XML::Compile::Translate::Writer;
101             use Log::Report;
102              
103             ; package XML::Compile::Transport::SOAPHTTP;
104             use Log::Report;
105              
106             ; package XML::Compile::Transport;
107             use Log::Report;
108             }
109              
110             __HEADER
111             }
112              
113              
114             sub freeze(@)
115             { my $self = shift;
116              
117             error "freeze needs PAIRS or a HASH"
118             if (@_==1 && ref $_[0] ne 'HASH') || @_ % 2;
119              
120             error "freeze can only be called once"
121             if $self->{XCD_freeze}++;
122              
123             my (@names, @data);
124             if(@_==1) # Hash
125             { my $h = shift;
126             @names = keys %$h;
127             @data = values %$h;
128             }
129             else # Pairs
130             { while(@_)
131             { push @names, shift;
132             push @data, shift;
133             }
134             }
135              
136             my $fh = $self->file;
137             my $export = join "\n ", sort @names;
138             $fh->print("push \@EXPORT, qw/\n $export/;\n\n");
139              
140             Data::Dump::Streamer->new->To($fh)->Data(@data)->Out;
141              
142             for(my $i = 0; $i < @names; $i++)
143             { ref $data[$i] eq 'CODE'
144             or error __x"value with '{label}' is not a code reference"
145             , label => $names[$i];
146              
147             my $code = '$CODE'.($i+1);
148             $fh->print("*${names[$i]} = $code;\n");
149             }
150             }
151              
152              
153             sub footer($)
154             { my ($self, $fh) = @_;
155             $fh->print( "\n1;\n" );
156             }
157              
158             1;