Declare/LibXML.pm

filter: Statementstmt 98.3% 60 / 61 Branchbran 87.5% 14 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 7 totaltotal 87.1% 95 / 109 CC 11 SCAR 8.7
CC
11
Cov
94%
_get_ns
4.2
set_node_attributes
4.0
add_child_node
3.0
CRAP 2.4
« Declare.pm ↑ Summary Declare/DAG_Node.pm »
1 package Tree::Template::Declare::LibXML;
2 {
3 2 $Tree::Template::Declare::LibXML::DIST = 'Tree-Template-Declare';
4 }
5 2 $Tree::Template::Declare::LibXML::VERSION = '0.7';
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 2 my ($class)=@_;
13  
14 2 return bless {ns=>{':default'=>undef}},$class;
15 }
16  
17 2 ▶sub _munge_exports {
Subroutine _munge_exports: called CC=1 SCAR=0.0
18 2 my ($self,$exports,$current_node_aref)=@_;
19  
20 my %all_exports=(
21 2 %{$exports},
22 3 ▶ xmlns => sub($$) {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 3 $self->{ns}->{$_[0]}=$_[1];
24 3 return;
25 },
26 2 ▶ text_node => sub($) {
Subroutine __ANON__: called CC=2 SCAR=7.2
27 2 ▶ if ($current_node_aref->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_node_aref->[0]) 2 0 2
28 2 $current_node_aref->[0]->appendTextNode($_[0]);
29 }
30 },
31 2 );
32  
33 2 return \%all_exports;
34 }
35  
36 3 ▶sub new_tree {
Subroutine new_tree: called CC=1 SCAR=0.0
Pod undocumented
37 3 my ($self)=@_;
38  
39 3 return XML::LibXML::Document->new();
40 }
41  
42 3 ▶sub finalize_tree {
Subroutine finalize_tree: called CC=1 SCAR=0.0
Pod undocumented
43 3 my ($self,$tree)=@_;
44  
45 3 return $tree;
46 }
47  
48 21 ▶sub _get_ns {
Subroutine _get_ns: called CC=4 SCAR=14.2
49 21 my ($self,$name)=@_;
50  
51 21 my ($prefix)=($name=~m{\A (.*?) : }smx);
52  
53 21 ▶ if (!defined($prefix) || length($prefix)==0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $prefix or length $prefix == 0) 12 9 21
Condition66%2/3
not defined $prefix or length $prefix == 0
l!l&&r!l&&!r
1209
MC/DC50%1/2
not defined $prefix or length $prefix == 0
not defined $prefix length $prefix == 0
Truth table66%2/3
not(A) or B
A= not defined $prefixB= length $prefix == 0
ABresult
1X1
011
000
54 12 return '',$self->{ns}->{':default'};
55 }
56  
57 9 ▶ if (exists $self->{ns}->{$prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"ns"}{$prefix}) 9 0 9
58 9 return $prefix, $self->{ns}->{$prefix};
59 }
60 0 return;
61 }
62  
63 12 ▶sub new_node {
Subroutine new_node: called CC=1 SCAR=0.0
Pod undocumented
64 12 my ($self)=@_;
65  
66 12 return XML::LibXML::Element->new('');
67 }
68  
69 12 ▶sub add_child_node {
Subroutine add_child_node: called CC=3 SCAR=11.0
Pod undocumented
70 12 my ($self,$parent,$child)=@_;
71  
72 12 my $doc=$parent->ownerDocument;
73 12 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 3 9 12
74 3 $child=$doc->adoptNode($child);
75 }
76  
77 12 ▶ if ($parent->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa('XML::LibXML::Document')) { } 3 9 12
78 3 $parent->setDocumentElement($child);
79 }
80 else {
81 9 $parent->appendChild($child);
82 }
83 12 return $parent;
84 }
85  
86 12 ▶sub set_node_name {
Subroutine set_node_name: called CC=2 SCAR=6.9
Pod undocumented
87 12 my ($self,$node,$name)=@_;
88  
89 12 $node->setNodeName($name);
90 12 my ($prefix,$uri)=$self->_get_ns($name);
91 12 ▶ if ($uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri) 8 4 12
92 8 $node->setNamespace($uri,$prefix,1);
93 }
94  
95 12 return;
96 }
97  
98 8 ▶sub set_node_attributes {
Subroutine set_node_attributes: called CC=4 SCAR=13.9
Pod undocumented
99 8 my ($self,$node,$attrs)=@_;
100  
101 8 ▶ while (my ($name,$val)=each %{$attrs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$attrs;}) 9 8 17
102 9 my ($prefix,$uri)=$self->_get_ns($name);
103 9 ▶ if ($prefix and $uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix and $uri) { } 3 6 9
Condition66%2/3
$prefix and $uri
!ll&&!rl&&r
603
MC/DC50%1/2
$prefix and $uri
$prefix $uri
Truth table66%2/3
A and B
A= $prefixB= $uri
ABresult
0X0
100
111
104 3 $node->setAttributeNS($uri, $name, $val);
105 }
106 else {
107 6 $node->setAttribute($name, $val);
108 }
109 }
110  
111 8 return;
112 }
113  
114 2 1;
115  
116 =pod
117  
118 =encoding UTF-8
119  
120 =head1 NAME
121  
122 Tree::Template::Declare::LibXML - adaptor for LibXML
123  
124 =head1 VERSION
125  
126 version 0.7
127  
128 =head1 SYNOPSIS
129  
130 See L<Tree::Template::Declare>.
131  
132 =head1 SPECIFICITIES
133  
134 A function C<xmlns> is exported, so that you can declare XML namespaces:
135  
136 xmlns test => 'http://test/';
137  
138 node { name 'test:elem'; attribs id => 1, 'test:attr' => 5 };
139  
140 You I<can> create nodes with qualified names with undeclared prefixes,
141 but it's probably not a good idea.
142  
143 To add text nodes, you could do something like:
144  
145 my $el=node { name 'elem_with_text' };
146 $el->appendTextNode('some text content');
147  
148 This is ugly, so you can do:
149  
150 node {
151 name 'elem_with_text';
152 text_node 'some text content';
153 };
154  
155 =for Pod::Coverage add_child_node finalize_tree new new_node new_tree set_node_attributes set_node_name
156  
157 =head1 AUTHOR
158  
159 Gianni Ceccarelli <dakkar@thenautilus.net>
160  
161 =head1 COPYRIGHT AND LICENSE
162  
163 This software is copyright (c) 2015 by Gianni Ceccarelli <dakkar@thenautilus.net>.
164  
165 This is free software; you can redistribute it and/or modify it under
166 the same terms as the Perl 5 programming language system itself.
167  
168 =cut
169  
170 __END__
« Declare.pm ↑ Summary Declare/DAG_Node.pm »