File Coverage

lib/XML/Compile/XOP/Include.pm
Criterion Covered Total %
statement 53 67 79.1
branch 6 10 60.0
condition 4 10 40.0
subroutine 15 20 75.0
pod 10 10 100.0
total 88 117 75.2


line stmt bran cond sub pod time code
1             # Copyrights 2007-2021 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.02.
5             # This code is part of distribution XML-Compile-SOAP. Meta-POD processed
6             # with OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package XML::Compile::XOP::Include;
10 2     2   15 use vars '$VERSION';
  2         3  
  2         146  
11             $VERSION = '3.27';
12              
13              
14 2     2   14 use warnings;
  2         6  
  2         87  
15 2     2   13 use strict;
  2         14  
  2         68  
16              
17 2     2   12 use Log::Report 'xml-compile-soap';
  2         3  
  2         12  
18 2     2   522 use XML::Compile::SOAP::Util qw/:xop10/;
  2         3  
  2         211  
19 2     2   15 use HTTP::Message ();
  2         3  
  2         54  
20 2     2   1074 use File::Slurper qw/read_binary write_binary/;
  2         6603  
  2         126  
21 2     2   15 use Encode qw/decode FB_CROAK/;
  2         4  
  2         99  
22              
23              
24 2         16 use overload '""' => 'content'
25 2     2   12 , fallback => 1;
  2         4  
26              
27              
28             sub new(@)
29 4     4 1 647 { my ($class, %args) = @_;
30             $args{bytes} = \(delete $args{bytes})
31 4 100 66     27 if defined $args{bytes} && ref $args{bytes} ne 'SCALAR';
32 4         21 bless \%args, $class;
33             }
34              
35              
36             sub fromMime($)
37 2     2 1 6 { my ($class, $http) = @_;
38              
39 2   50     6 my $cid = $http->header('Content-ID') || '';
40 2 50       123 if($cid !~ s/^\s*\<(.*?)\>\s*$/$1/ )
41 0         0 { warning __x"part has illegal Content-ID: `{cid}'", cid => $cid;
42 0         0 return ();
43             }
44              
45 2   33     8 my $content = $http->decoded_content(ref => 1) || $http->content(ref => 1);
46 2         757 $class->new
47             ( bytes => $content
48             , cid => $cid
49             , type => scalar $http->content_type
50             , charset => scalar $http->content_type_charset
51             );
52             }
53              
54              
55 4     4 1 1598 sub cid { shift->{cid} }
56              
57              
58             sub content(;$)
59 15     15 1 1830 { my ($self, $byref) = @_;
60 15 50       81 unless($self->{bytes})
61 0         0 { my $f = $self->{file};
62 0     0   0 my $bytes = try { read_binary $f };
  0         0  
63 0 0       0 fault "failed reading XOP file {fn}", fn => $f if $@;
64 0         0 $self->{bytes} = \$bytes;
65             }
66 15 100       49 $byref ? $self->{bytes} : ${$self->{bytes}};
  12         48  
67             }
68              
69              
70             sub string() {
71 0     0 1 0 my $self = shift;
72 0   0     0 my $cs = $self->contentCharset || 'UTF-8';
73 0         0 decode $cs, $self->content, FB_CROAK;
74             }
75              
76              
77 0     0 1 0 sub contentType() { shift->{type} }
78 0     0 1 0 sub contentCharset() { shift->{charset} }
79              
80             #---------
81              
82             sub xmlNode($$$$)
83 1     1 1 4 { my ($self, $doc, $path, $tag) = @_;
84 1         7 my $node = $doc->createElement($tag);
85 1         5 $node->setNamespace($self->{xmime}, 'xmime', 0);
86 1         19 $node->setAttributeNS($self->{xmime}, contentType => $self->{type});
87              
88 1         34 my $include = $node->addChild($doc->createElement('Include'));
89 1         4 $include->setNamespace($self->{xop}, 'xop', 1);
90 1         38 $include->setAttribute(href => 'cid:'.$self->{cid});
91 1         13 $node;
92             }
93              
94              
95             sub mimePart(;$)
96 2     2 1 2216 { my ($self, $headers) = @_;
97 2         16 my $mime = HTTP::Message->new($headers);
98             $mime->header
99             ( Content_Type => $self->{type}
100             , Content_Transfer_Encoding => 'binary'
101 2         64 , Content_ID => '<'.$self->{cid}.'>'
102             );
103              
104 2         331 $mime->content_ref($self->content(1));
105 2         44 $mime;
106             }
107              
108              
109             sub write($)
110 0     0 1   { my ($self, $file) = @_;
111 0           write_binary $file, $self->content(1);
112             }
113              
114             1;