PRANG/Marshaller.pm

filter: Statementstmt 40.4% 57 / 141 Branchbran 9.6% 5 / 52 Conditioncond 8.0% 2 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 72.7% 16 / 22 Podpod n/a totaltotal 31.2% 80 / 256 CC 37 SCAR 42.5
CC
37
Cov
29%
__ANON__
172.6
__ANON__
124.6
__ANON__
38.1
CRAP 69.8
« PRANG/Graph/Context.pm ↑ Summary PRANG/Graph/Quantity.pm »
1  
2 package PRANG::Marshaller;
3  
4 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use MooseX::Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use XML::LibXML 1.65;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use PRANG::Util qw(types_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 BEGIN {
13 9 ▶ class_type 'Moose::Meta::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 class_type "Moose::Meta::Role";
15 9 class_type "XML::LibXML::Element";
16 9 class_type "XML::LibXML::Node";
17 9 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 trigger => sub {
26 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.3
27 1 my $class = $self->class;
28 1 ▶ if ( !$class->can("marshall_in_element") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $class->can("marshall_in_element")) 1 0 1
29 1 ▶ $class = $class->name if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 0 1
30 1 die "Can't marshall $class; didn't 'use PRANG::Graph' ?";
31 }
32 },
33 9 ;
34  
35 9 our %marshallers; # could use MooseX::NaturalKey?
36 9 ▶method get($inv: Str $class) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=8 SCAR=31.0
37 1 ▶ if ( ref $inv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inv) 0 1 1
38 0 $inv = ref $inv;
39 }
40 1 ▶ $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" } 0 1 1
41 0 my $filename = $class;
42 0 $filename =~ s{::}{/}g;
43 0 $filename .= ".pm";
44 0 ▶ if ( !$INC{$filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$filename}) 0 0 0
45 0 eval { require $filename };
46 }
47 0 ▶ $class->can("meta") or
Branch: 1 branch
BranchTrueFalseTotal
$class->can("meta") or die "cannot marshall $class; no ->meta" 0 0 0
48 die "cannot marshall $class; no ->meta";
49 };
50 1 my $meta = $class->meta;
51 1 ▶ if ( $meta->does_role("PRANG::Graph") or
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->does_role('PRANG::Graph') or $meta->meta->does_role('PRANG::Graph::Meta::Class')) { } 1 0 1
Condition33%1/3
$meta->does_role("PRANG::Graph") or $meta->meta->does_role("PRANG::Graph::Meta::Class")
l!l&&r!l&&!r
100
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 table33%1/3
A or B
A= $meta->does_role("PRANG::Graph")B= $meta->meta->does_role("PRANG::Graph::Meta::Class")
ABresult
1X1
011
000
52 $meta->meta->does_role("PRANG::Graph::Meta::Class")
53 ) {
54 1 ▶ $marshallers{$class} ||= do {
Condition33%1/3
$PRANG::Marshaller::marshallers{$class} ||= do {
l!l&&r!l&&!r
001
MC/DC0%0/2
$PRANG::Marshaller::marshallers{$class} ||= do { $inv->new("class", $class->meta) }
$PRANG::Marshaller::marshallers{$class} do { $inv->new("class", $class->meta) }
Truth table33%1/3
A ||= B
A= $PRANG::Marshaller::marshallers{$class}B= do { $inv->new("class", $class->meta) }
ABresult
1X1
011
000
55 1 $inv->new( class => $class->meta );
56 }
57 }
58 else {
59 0 die "cannot marshall ".$meta->name
60 ."; not a PRANG Class/Node";
61 }
62 9 }
63  
64 9 ▶method parse( Str :$xml, Str :$filename, GlobRef :$fh ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=13 SCAR=51.5
65  
66 0 my $parser = XML::LibXML->new;
67 0 ▶ my $dom = (
Branch: 3 branches
BranchTrueFalseTotal
defined $xml ? : 0 0 0
defined $filename ? : 0 0 0
defined $fh ? : 0 0 0
68 defined $xml ? $parser->parse_string($xml) :
69 defined $filename ? $parser->parse_file($filename) :
70 defined $fh ? $parser->parse_fh($fh) :
71 croak("no input passed to parse")
72 );
73  
74 0 my $rootNode = $dom->documentElement;
75 0 my $rootNodeNS = $rootNode->namespaceURI;
76  
77 0 my $xsi = {};
78 0 ▶ if ( $self->class->isa("Moose::Meta::Role") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class->isa("Moose::Meta::Role")) 0 0 0
79 0 my @possible = types_of($self->class);
80 0 my $found;
81 0 my $root_localname = $rootNode->localname;
82 0 my @expected;
83 0 for my $class ( @possible ) {
84 0 ▶ if ( $root_localname eq
Branch: 1 branch
BranchTrueFalseTotal
if ($root_localname eq $class->name->root_element) { } 0 0 0
85 $class->name->root_element ) {
86 # yeah, this is lazy ;-)
87 0 $self = (ref $self)->get($class->name);
88 0 $found = 1;
89 0 last;
90 }
91 else {
92 0 push @expected, $class->name->root_element;
93 }
94 }
95 0 ▶ if ( !$found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
96 0 die "No type of ".$self->class->name
97 ." that expects '$root_localname' as a root element (expected: @expected)";
98 }
99 }
100 0 my $expected_ns = $self->class->name->xmlns;
101 0 ▶ if ( $rootNodeNS and $expected_ns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rootNodeNS and $expected_ns) 0 0 0
Condition0%0/3
$rootNodeNS and $expected_ns
!ll&&!rl&&r
000
MC/DC0%0/2
$rootNodeNS and $expected_ns
$rootNodeNS $expected_ns
Truth table0%0/3
A and B
A= $rootNodeNSB= $expected_ns
ABresult
0X0
100
111
102 0 ▶ if ( $rootNodeNS ne $expected_ns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rootNodeNS ne $expected_ns) 0 0 0
103 0 die "Namespace mismatch: expected '$expected_ns', found '$rootNodeNS'";
104 }
105 }
106 0 ▶ if ( !defined($rootNode->prefix) and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rootNode->prefix or defined $rootNode->getAttribute("xmlns")) 0 0 0
Condition0%0/3
defined $rootNode->prefix or defined $rootNode->getAttribute("xmlns")
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $rootNode->prefix or defined $rootNode->getAttribute("xmlns")
defined $rootNode->prefix defined $rootNode->getAttribute("xmlns")
Truth table0%0/3
A or B
A= defined $rootNode->prefixB= defined $rootNode->getAttribute("xmlns")
ABresult
1X1
011
000
107 !defined($rootNode->getAttribute("xmlns")) ) {
108 # namespace free;
109 0 $xsi->{""}="";
110 }
111  
112 0 my $context = PRANG::Graph::Context->new(
113 base => $self,
114 xpath => "",
115 xsi => $xsi,
116 prefix => "",
117 );
118  
119 0 my $rv = $self->class->marshall_in_element(
120 $rootNode,
121 $context,
122 );
123 0 $rv;
124 9 }
125  
126 9 ▶method xml_version { "1.0" };
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
127 9 ▶method encoding { "UTF-8" };
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
128  
129 # nothing to see here ... move along please ...
130 9 our $zok;
131 9 our %zok_seen;
132 9 our @zok_themes = (qw( tmnt octothorpe quantum pokemon hhgg pasta
133 phonetic sins punctuation discworld lotr
134 loremipsum batman tld garbage python pooh
135 norse_mythology ));
136 9 our $zok_theme;
137  
138 9 our $gen_prefix;
139  
140 9 ▶method generate_prefix( Str $xmlns ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=11 SCAR=48.3
141 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 0 0
Condition0%0/3
$PRANG::Marshaller::zok or eval {
l!l&&r!l&&!r
000
MC/DC0%0/2
$PRANG::Marshaller::zok or eval { do { require Acme::MetaSyntactic; 1 } }
$PRANG::Marshaller::zok eval { do { require Acme::MetaSyntactic; 1 } }
Truth table0%0/3
A or B
A= $PRANG::Marshaller::zokB= eval { do { require Acme::MetaSyntactic; 1 } }
ABresult
1X1
011
000
142 0 my $name;
143 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
144 0 ▶ $zok ||= do {
Condition0%0/3
$PRANG::Marshaller::zok ||= do {
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
145 0 %zok_seen=();
146 0 ▶ if ( defined $zok_theme ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PRANG::Marshaller::zok_theme) { } 0 0 0
147 0 $zok_theme++;
148 0 ▶ if ( $zok_theme > $#zok_themes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PRANG::Marshaller::zok_theme > $#PRANG::Marshaller::zok_themes) 0 0 0
149 0 $zok_theme = 0;
150 }
151 }
152 else {
153 0 $zok_theme = int(time / 86400)
154 % scalar(@zok_themes);
155 }
156 0 Acme::MetaSyntactic->new(
157 $zok_themes[$zok_theme],
158 );
159 };
160 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
161 0 $name = $zok->name;
162 0 ▶ if ($zok_seen{$name}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PRANG::Marshaller::zok_seen{$name}++) 0 0 0
163 0 undef($zok);
164 0 undef($name);
165 0 goto next_theme;
166 };
167 } while ( length($name) > 10 or
168 $name !~ m{^[A-Za-z]\w+$} );
169 next_theme:
170 0 }
171 until ($name);
172 0 return $name;
173 }
174 else {
175 # revert to a more boring prefix :)
176 0 ▶ $gen_prefix ||= "a";
Condition0%0/2
$PRANG::Marshaller::gen_prefix ||= "a"
l!l
00
MC/DC0%0/1
$PRANG::Marshaller::gen_prefix ||= "a"
$PRANG::Marshaller::gen_prefix
Truth table0%0/2
A
A= $PRANG::Marshaller::gen_prefix
Aresult
11
00
177 0 $gen_prefix++;
178 }
179 9 }
180  
181 9 ▶method to_xml_doc( PRANG::Graph $item ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=6 SCAR=36.4
182 0 my $xmlns = $item->xmlns;
183 0 my $prefix = "";
184 0 ▶ if ( $item->can("preferred_prefix") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->can("preferred_prefix")) 0 0 0
185 0 $prefix = $item->preferred_prefix;
186 }
187 0 ▶ my $xsi = { $prefix => ($xmlns||"") };
Condition0%0/2
$xmlns || ''
l!l
00
MC/DC0%0/1
$xmlns || ''
$xmlns
Truth table0%0/2
A
A= $xmlns
Aresult
11
00
188 # whoops, this is non-reentrant
189 0 %zok_seen=();
190 0 undef($gen_prefix);
191 0 my $doc = XML::LibXML::Document->new(
192 $self->xml_version, $self->encoding,
193 );
194 0 ▶ my $root = $doc->createElement(
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
195 ($prefix ? "$prefix:" : "" ) .$item->root_element,
196 );
197 0 ▶ if ( $xmlns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmlns) 0 0 0
198 0 ▶ $root->setAttribute(
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
199 "xmlns".($prefix?":$prefix":""),
200 $xmlns,
201 );
202 }
203 0 $doc->setDocumentElement( $root );
204 0 my $ctx = PRANG::Graph::Context->new(
205 xpath => "/".$root->nodeName,
206 base => $self,
207 prefix => $prefix,
208 xsi => $xsi,
209 );
210 0 $item->meta->to_libxml( $item, $root, $ctx );
211 0 $doc;
212 9 }
213  
214 9 ▶method to_xml( PRANG::Graph $item, Int $format = 0 ) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=3.5
215 0 my $document = $self->to_xml_doc($item);
216 0 $document->toString($format);
217 9 }
218  
219 9 1;
220  
221 __END__
222  
223 =head1 NAME
224  
225 PRANG::Marshaller - entry point for PRANG XML marshalling machinery
226  
227 =head1 SYNOPSIS
228  
229 my $marshaller = PRANG::Marshaller->get($class_or_role);
230  
231 my $object = $marshaller->parse($xml);
232  
233 my $xml = $marshaller->to_xml($object);
234  
235 =head1 DESCRIPTION
236  
237 The B<PRANG::Marshaller> currently serves two major functions;
238  
239 =over
240  
241 =item 1.
242  
243 A place-holder for role-based marshalling (ie, marshalling documents
244 with multiple root element types)
245  
246 =item 2.
247  
248 A place for document-scoped information on emitting to be held (ie,
249 mapping XML namespace prefixes to URIs and generating namespace
250 prefixes).
251  
252 =back
253  
254 This class is a bit of a stop-gap measure; it started out as the only
255 place where any XML marshalling happened, and gradually parts have
256 been moved into metaclass methods, in packages such as
257 L<PRANG::Graph::Meta::Class>, L<PRANG::Graph::Meta::Element> and
258 L<PRANG::Graph::Node> implementations.
259  
260 =head1 SEE ALSO
261  
262 L<PRANG>, L<PRANG::Graph::Meta::Class>,
263 L<PRANG::Graph::Meta::Element>, L<PRANG::Graph::Node>
264  
265 =head1 AUTHOR AND LICENCE
266  
267 Development commissioned by NZ Registry Services, and carried out by
268 Catalyst IT - L<http://www.catalyst.net.nz/>
269  
270 Copyright 2009, 2010, NZ Registry Services. This module is licensed
271 under the Artistic License v2.0, which permits relicensing under other
272 Free Software licenses.
273  
274 =cut
275  
« PRANG/Graph/Context.pm ↑ Summary PRANG/Graph/Quantity.pm »