PRANG/Marshaller.pm

filter: Statementstmt 75.3% 104 / 138 Branchbran 40.7% 22 / 54 Conditioncond 42.8% 12 / 28 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 8 totaltotal 59.1% 155 / 262 CC 39 SCAR 30.6
CC
39
Cov
63%
generate_prefix
71.0
get
17.5
from_root_node
12.0
CRAP 21.3
« PRANG/Graph/Element.pm ↑ Summary PRANG/Graph/Choice.pm »
1  
2 package PRANG::Marshaller;
3 11 $PRANG::Marshaller::VERSION = '0.21';
4 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use XML::LibXML 1.65;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use PRANG::Util qw(types_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 class_type 'Moose::Meta::Class';
14 11 class_type "Moose::Meta::Role";
15 11 class_type "XML::LibXML::Element";
16 11 class_type "XML::LibXML::Node";
17 11 role_type "PRANG::Graph";
18 }
19  
20 has 'class' =>
21 isa => "Moose::Meta::Class|Moose::Meta::Role",
22 is => "ro",
23 required => 1,
24 handles => [qw(marshall_in_element to_libxml)],
25 39 ▶ trigger => sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
26 39 my $self = shift;
27 39 my $class = shift;
28  
29 39 ▶ if ( !$class->can("marshall_in_element") && ! $class->does_role('PRANG::Graph') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $class->can("marshall_in_element") || $class->does_role("PRANG::Graph")) 0 39 39
Condition66%2/3
$class->can('marshall_in_element') || $class->does_role('PRANG::Graph')
l!l&&r!l&&!r
3810
MC/DC0%0/2
$class->can('marshall_in_element') || $class->does_role('PRANG::Graph')
$class->can('marshall_in_element') $class->does_role('PRANG::Graph')
Truth table66%2/3
A || B
A= $class->can('marshall_in_element')B= $class->does_role('PRANG::Graph')
ABresult
1X1
011
000
30  
31 0 ▶ $class = $class->name if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
32 0 die "Can't marshall $class; didn't 'use PRANG::Graph' ?";
33 }
34 },
35 11 ;
36
37 11 has 'encoding' =>
38 isa => 'Str',
39 is => 'ro',
40 default => 'UTF-8';
41  
42 11 our %marshallers; # could use MooseX::NaturalKey?
43  
44 408 ▶sub get {
Subroutine get: called CC=9 SCAR=28.6
Pod undocumented
45 408 my $inv = shift;
46 408 my ( $class ) = pos_validated_list(
47 \@_,
48 { isa => 'Str' },
49 );
50
51 408 ▶ if ( ref $inv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inv) 288 120 408
52 288 $inv = ref $inv;
53 }
54 408 ▶ $class->can("meta") or do {
Branch: 1 branch
BranchTrueFalseTotal
$class->can("meta") or do { my $filename = $class; $filename =~ s[::][/]g; $filename .= '.pm'; unless ($INC{$filename}) { eval { do { require $filename } }; } ; $class->can("meta") or die "cannot marshall $class; no ->meta" } 408 0 408
55 0 my $filename = $class;
56 0 $filename =~ s{::}{/}g;
57 0 $filename .= ".pm";
58 0 ▶ if ( !$INC{$filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$filename}) 0 0 0
59 0 eval { require $filename };
60 }
61 0 ▶ $class->can("meta") or
Branch: 1 branch
BranchTrueFalseTotal
$class->can("meta") or die "cannot marshall $class; no ->meta" 0 0 0
62 die "cannot marshall $class; no ->meta";
63 };
64 408 my $meta = $class->meta;
65 408 ▶ if ($meta->does_role("PRANG::Graph")
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->does_role('PRANG::Graph') or $meta->meta->does_role('PRANG::Graph::Meta::Class')) { } 408 0 408
Condition66%2/3
$meta->does_role("PRANG::Graph") or $meta->meta->does_role("PRANG::Graph::Meta::Class")
l!l&&r!l&&!r
2361720
MC/DC0%0/2
$meta->does_role("PRANG::Graph") or $meta->meta->does_role("PRANG::Graph::Meta::Class")
$meta->does_role("PRANG::Graph") $meta->meta->does_role("PRANG::Graph::Meta::Class")
Truth table66%2/3
A or B
A= $meta->does_role("PRANG::Graph")B= $meta->meta->does_role("PRANG::Graph::Meta::Class")
ABresult
1X1
011
000
66 or
67 $meta->meta->does_role("PRANG::Graph::Meta::Class")
68 )
69 {
70
71 408 ▶ my $encoding = $class->can('encoding') ? $class->encoding : 'UTF-8';
Branch: 1 branch
BranchTrueFalseTotal
$class->can('encoding') ? : 0 408 408
72 408 ▶ $marshallers{$class} ||= do {
Condition66%2/3
$PRANG::Marshaller::marshallers{$class} ||= do { $inv->new("class", $class->meta, "encoding", $encoding) }
l!l&&r!l&&!r
369390
MC/DC0%0/2
$PRANG::Marshaller::marshallers{$class} ||= do { $inv->new("class", $class->meta, "encoding", $encoding) }
$PRANG::Marshaller::marshallers{$class} do { $inv->new("class", $class->meta, "encoding", $encoding) }
Truth table66%2/3
A ||= B
A= $PRANG::Marshaller::marshallers{$class}B= do { $inv->new("class", $class->meta, "encoding", $encoding) }
ABresult
1X1
011
000
73 39 $inv->new( class => $class->meta, encoding => $encoding );
74 }
75 }
76 else {
77 0 die "cannot marshall ".$meta->name
78 ."; not a PRANG Class/Node";
79 }
80 }
81  
82 69 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
Pod undocumented
83 69 my $self = shift;
84 69 my ( $xml, $filename, $fh, $lax ) = validated_list(
85 \@_,
86 xml => { isa => 'Str', optional => 1 },
87 filename => { isa => 'Str', optional => 1 },
88 fh => { isa => 'GlobRef', optional => 1 },
89 lax => { isa => 'Bool', optional => 1, default => 0 },
90 );
91  
92 69 my $parser = XML::LibXML->new;
93 69 ▶ my $dom = (
Branch: 3 branches
BranchTrueFalseTotal
defined $xml ? : 67 2 69
defined $filename ? : 1 1 2
defined $fh ? : 1 0 1
94 defined $xml ? $parser->parse_string($xml) :
95 defined $filename ? $parser->parse_file($filename) :
96 defined $fh ? $parser->parse_fh($fh) :
97 croak("no input passed to parse")
98 );
99  
100 69 return $self->from_dom(
101 dom => $dom,
102 lax => $lax
103 );
104 }
105  
106 72 ▶sub from_dom {
Subroutine from_dom: called CC=1 SCAR=0.0
Pod undocumented
107 72 my $self = shift;
108  
109 72 my ( $dom, $lax ) = validated_list(
110 \@_,
111 dom => { isa => 'XML::LibXML::Document', },
112 lax => { isa => 'Bool', optional => 1, default => 0 },
113 );
114  
115 72 my $rootNode = $dom->documentElement;
116
117 72 return $self->from_root_node(
118 root_node => $rootNode,
119 lax => $lax,
120 );
121 }
122  
123 72 ▶sub from_root_node {
Subroutine from_root_node: called CC=10 SCAR=24.9
Pod undocumented
124 72 my $self = shift;
125  
126 72 my ( $rootNode, $lax ) = validated_list(
127 \@_,
128 root_node => { isa => 'XML::LibXML::Node', },
129 lax => { isa => 'Bool', optional => 1, default => 0 },
130 );
131
132 72 my $rootNodeNS = $rootNode->namespaceURI;
133  
134 72 my $xsi = {};
135 72 ▶ if ( $self->class->isa("Moose::Meta::Role") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class->isa("Moose::Meta::Role")) 7 65 72
136 7 my @possible = types_of($self->class);
137 7 my $found;
138 7 my $root_localname = $rootNode->localname;
139 7 my @expected;
140 7 for my $class (@possible) {
141 12 ▶ if ($root_localname eq
Branch: 1 branch
BranchTrueFalseTotal
if ($root_localname eq $class->name->root_element) { } 7 5 12
142 $class->name->root_element
143 )
144 {
145  
146 # yeah, this is lazy ;-)
147 7 $self = (ref $self)->get($class->name);
148 7 $found = 1;
149 7 last;
150 }
151 else {
152 5 push @expected, $class->name->root_element;
153 }
154 }
155 7 ▶ if ( !$found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 7 7
156 0 die "No type of ".$self->class->name
157 ." that expects '$root_localname' as a root element (expected: @expected)";
158 }
159 }
160 72 my $expected_ns = $self->class->name->xmlns;
161 72 ▶ if ( $rootNodeNS and $expected_ns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rootNodeNS and $expected_ns) 0 72 72
Condition33%1/3
$rootNodeNS and $expected_ns
!ll&&!rl&&r
7200
MC/DC0%0/2
$rootNodeNS and $expected_ns
$rootNodeNS $expected_ns
Truth table33%1/3
A and B
A= $rootNodeNSB= $expected_ns
ABresult
0X0
100
111
162 0 ▶ if ( $rootNodeNS ne $expected_ns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rootNodeNS ne $expected_ns) 0 0 0
163 0 die
164 "Namespace mismatch: expected '$expected_ns', found '$rootNodeNS'";
165 }
166 }
167 72 ▶ if (!defined($rootNode->prefix)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rootNode->prefix or defined $rootNode->getAttribute("xmlns")) 72 0 72
Condition33%1/3
defined $rootNode->prefix or defined $rootNode->getAttribute("xmlns")
l!l&&r!l&&!r
0072
MC/DC0%0/2
defined $rootNode->prefix or defined $rootNode->getAttribute("xmlns")
defined $rootNode->prefix defined $rootNode->getAttribute("xmlns")
Truth table33%1/3
A or B
A= defined $rootNode->prefixB= defined $rootNode->getAttribute("xmlns")
ABresult
1X1
011
000
168 and
169 !defined($rootNode->getAttribute("xmlns"))
170 )
171 {
172  
173 # namespace free;
174 72 $xsi->{""}="";
175 }
176  
177 72 my $context = PRANG::Graph::Context->new(
178 base => $self,
179 xpath => "",
180 xsi => $xsi,
181 prefix => "",
182 );
183
184 72 my $rv = $self->class->marshall_in_element(
185 $rootNode,
186 $context,
187 $lax,
188 );
189 58 $rv;
190 }
191  
192 39 ▶sub xml_version { "1.0" }
Subroutine xml_version: called CC=1 SCAR=0.0
Pod undocumented
193  
194 # nothing to see here ... move along please ...
195 11 our $zok;
196 11 our %zok_seen;
197 11 our @zok_themes = (
198 qw( tmnt octothorpe quantum pokemon hhgg pasta
199 phonetic sins punctuation discworld lotr
200 loremipsum batman tld garbage python pooh
201 norse_mythology )
202 );
203 11 our $zok_theme;
204  
205 11 our $gen_prefix;
206  
207 28 ▶sub generate_prefix {
Subroutine generate_prefix: called CC=11 SCAR=42.6
Pod undocumented
208 28 my $self = shift;
209 28 my ( $xmlns ) = pos_validated_list(
210 \@_,
211 { isa => 'Str' },
212 );
213
214 0 ▶ if ( $zok or eval { require Acme::MetaSyntactic; 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PRANG::Marshaller::zok or eval { do { require Acme::MetaSyntactic; 1 } }) { } 0 28 28
Condition33%1/3
$PRANG::Marshaller::zok or eval { do { require Acme::MetaSyntactic; 1 } }
l!l&&r!l&&!r
0028
MC/DC0%0/2
$PRANG::Marshaller::zok or eval { do { require Acme::MetaSyntactic; 1 } }
$PRANG::Marshaller::zok eval { do { require Acme::MetaSyntactic; 1 } }
Truth table33%1/3
A or B
A= $PRANG::Marshaller::zokB= eval { do { require Acme::MetaSyntactic; 1 } }
ABresult
1X1
011
000
215 0 my $name;
216 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$name or do { do { $PRANG::Marshaller::zok ||= do { %PRANG::Marshaller::zok_seen = (); if (defined $PRANG::Marshaller::zok_theme) { ++$PRANG::Marshaller::zok_theme; if ($PRANG::Marshaller::zok_theme > $#PRANG::Marshaller::zok_themes) { $PRANG::Marshaller::zok_theme = 0; } ; } else { $PRANG::Marshaller::zok_theme = int(time / 86400) % scalar(@PRANG::Marshaller::zok_themes); } ; "Acme::MetaSyntactic"->new($PRANG::Marshaller::zok_themes[$PRANG::Marshaller::zok_theme]) }; do { $name = $PRANG::Marshaller::zok->name; if ($PRANG::Marshaller::zok_seen{$name}++) { undef $PRANG::Marshaller::zok; undef $name; goto next_theme; } } while length $name > 10 or not $name =~ /^[A-Za-z]\w+$/; next_theme: } } 0 0 0
217 0 ▶ $zok ||= do {
Condition0%0/3
$PRANG::Marshaller::zok ||= do { %PRANG::Marshaller::zok_seen = (); if (defined $PRANG::Marshaller::zok_theme) { ++$PRANG::Marshaller::zok_theme; if ($PRANG::Marshaller::zok_theme > $#PRANG::Marshaller::zok_themes) { $PRANG::Marshaller::zok_theme = 0; } ; } else { $PRANG::Marshaller::zok_theme = int(time / 86400) % scalar(@PRANG::Marshaller::zok_themes); } ; "Acme::MetaSyntactic"->new($PRANG::Marshaller::zok_themes[$PRANG::Marshaller::zok_theme]) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$PRANG::Marshaller::zok ||= do { %PRANG::Marshaller::zok_seen = (); if (defined $PRANG::Marshaller::zok_theme) { ++$PRANG::Marshaller::zok_theme; if ($PRANG::Marshaller::zok_theme > $#PRANG::Marshaller::zok_themes) { $PRANG::Marshaller::zok_theme = 0; } ; } else { $PRANG::Marshaller::zok_theme = int(time / 86400) % scalar(@PRANG::Marshaller::zok_themes); } ; "Acme::MetaSyntactic"->new($PRANG::Marshaller::zok_themes[$PRANG::Marshaller::zok_theme]) }
$PRANG::Marshaller::zok do { %PRANG::Marshaller::zok_seen = (); if (defined $PRANG::Marshaller::zok_theme) { ++$PRANG::Marshaller::zok_theme; if ($PRANG::Marshaller::zok_theme > $#PRANG::Marshaller::zok_themes) { $PRANG::Marshaller::zok_theme = 0; } ; } else { $PRANG::Marshaller::zok_theme = int(time / 86400) % scalar(@PRANG::Marshaller::zok_themes); } ; "Acme::MetaSyntactic"->new($PRANG::Marshaller::zok_themes[$PRANG::Marshaller::zok_theme]) }
Truth table0%0/3
A ||= B
A= $PRANG::Marshaller::zokB= do { %PRANG::Marshaller::zok_seen = (); if (defined $PRANG::Marshaller::zok_theme) { ++$PRANG::Marshaller::zok_theme; if ($PRANG::Marshaller::zok_theme > $#PRANG::Marshaller::zok_themes) { $PRANG::Marshaller::zok_theme = 0; } ; } else { $PRANG::Marshaller::zok_theme = int(time / 86400) % scalar(@PRANG::Marshaller::zok_themes); } ; "Acme::MetaSyntactic"->new($PRANG::Marshaller::zok_themes[$PRANG::Marshaller::zok_theme]) }
ABresult
1X1
011
000
218 0 %zok_seen=();
219 0 ▶ if ( defined $zok_theme ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PRANG::Marshaller::zok_theme) { } 0 0 0
220 0 $zok_theme++;
221 0 ▶ if ( $zok_theme > $#zok_themes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PRANG::Marshaller::zok_theme > $#PRANG::Marshaller::zok_themes) 0 0 0
222 0 $zok_theme = 0;
223 }
224 }
225 else {
226 0 $zok_theme = int(time() / 86400)
227 % scalar(@zok_themes);
228 }
229 0 Acme::MetaSyntactic->new(
230 $zok_themes[$zok_theme],
231 );
232 };
233 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
length $name > 10 || !($name =~ /^[A-Za-z]\w+$/) and do { do { $name = $PRANG::Marshaller::zok->name; if ($PRANG::Marshaller::zok_seen{$name}++) { undef $PRANG::Marshaller::zok; undef $name; goto next_theme; } } } 0 0 0
Condition0%0/3
length $name > 10 or not $name =~ /^[A-Za-z]\w+$/
l!l&&r!l&&!r
000
MC/DC0%0/2
length $name > 10 or not $name =~ /^[A-Za-z]\w+$/
length $name > 10 not $name =~ /^[A-Za-z]\w+$/
Truth table0%0/3
A or not(B)
A= length $name > 10B= not $name =~ /^[A-Za-z]\w+$/
ABresult
1X1
011
000
234 0 $name = $zok->name;
235 0 ▶ if ($zok_seen{$name}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PRANG::Marshaller::zok_seen{$name}++) 0 0 0
236 0 undef($zok);
237 0 undef($name);
238 0 goto next_theme;
239 }
240 } while (
241 length($name) > 10
242 or
243 $name !~ m{^[A-Za-z]\w+$}
244 );
245 next_theme:
246 0 }
247 until ($name);
248 0 return $name;
249 }
250 else {
251  
252 # revert to a more boring prefix :)
253 28 ▶ $gen_prefix ||= "a";
Condition100%2/2
$PRANG::Marshaller::gen_prefix ||= "a"
l!l
1414
MC/DC100%1/1
$PRANG::Marshaller::gen_prefix ||= "a"
$PRANG::Marshaller::gen_prefix
Truth table100%2/2
A
A= $PRANG::Marshaller::gen_prefix
Aresult
11
00
254 28 $gen_prefix++;
255 }
256 }
257  
258 39 ▶sub to_xml_doc {
Subroutine to_xml_doc: called CC=6 SCAR=19.4
Pod undocumented
259 39 my $self = shift;
260 39 my ( $item ) = pos_validated_list(
261 \@_,
262 { isa => 'PRANG::Graph' },
263 );
264
265 39 my $xmlns = $item->xmlns;
266 39 my $prefix = "";
267 39 ▶ if ( $item->can("preferred_prefix") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->can("preferred_prefix")) 0 39 39
268 0 $prefix = $item->preferred_prefix;
269 }
270 39 ▶ my $xsi = { $prefix => ($xmlns||"") };
Condition50%1/2
$xmlns || ''
l!l
039
MC/DC0%0/1
$xmlns || ''
$xmlns
Truth table50%1/2
A
A= $xmlns
Aresult
11
00
271  
272 # whoops, this is non-reentrant
273 39 %zok_seen=();
274 39 undef($gen_prefix);
275 39 my $doc = XML::LibXML::Document->new(
276 $self->xml_version, $self->encoding,
277 );
278 39 ▶ my $root = $doc->createElement(
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 39 39
279 ($prefix ? "$prefix:" : "" ) .$item->root_element,
280 );
281 39 ▶ if ($xmlns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlns) 0 39 39
282 0 ▶ $root->setAttribute(
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
283 "xmlns".($prefix?":$prefix":""),
284 $xmlns,
285 );
286 }
287 39 $doc->setDocumentElement($root);
288 39 my $ctx = PRANG::Graph::Context->new(
289 xpath => "/".$root->nodeName,
290 base => $self,
291 prefix => $prefix,
292 xsi => $xsi,
293 );
294 39 $item->meta->to_libxml( $item, $root, $ctx );
295 38 $doc;
296 }
297  
298 39 ▶sub to_xml {
Subroutine to_xml: called CC=1 SCAR=0.0
Pod undocumented
299 39 my $self = shift;
300 39 my ( $item, $format ) = pos_validated_list(
301 \@_,
302 { isa => 'PRANG::Graph' },
303 { isa => 'Int', default => 0 },
304 );
305
306 39 my $document = $self->to_xml_doc($item);
307 38 $document->toString($format);
308 }
309  
310 11 1;
311  
312 __END__
« PRANG/Graph/Element.pm ↑ Summary PRANG/Graph/Choice.pm »