blib/lib/XML/Chain.pm

filter: Statementstmt 97.1% 101 / 104 Branchbran 79.4% 27 / 34 Conditioncond 84.6% 11 / 13 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 92.0% 23 / 25 Podpod 75.0% 3 / 4 totaltotal 90.3% 168 / 186 CC 24 SCAR 19.9
CC
24
Cov
92%
_create_element
17.3
_xc_el_data
4.0
store
3.0
CRAP 7.3
↑ Summary _build/lib/XML/Chain/MB_Builder.pm »
1 package XML::Chain;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.06';
9  
10 4 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use XML::Chain::Selector;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use XML::Chain::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use IO::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 Moose::Exporter->setup_import_methods(as_is => ['xc'],);
20  
21 4 has 'dom' => (is => 'rw', isa => 'XML::LibXML::Document', lazy_build => 1);
22 has '_xml_libxml' => (
23 is => 'rw',
24 isa => 'XML::LibXML',
25 lazy => 1,
26 60 ▶ default => sub {XML::LibXML->new}
Subroutine __ANON__: called CC=1 SCAR=0.0
27 4 );
28  
29 sub _build_dom {
30 60 ▶ my ($self) = @_;
Subroutine _build_dom: called CC=1 SCAR=0.0
31 60 return $self->_xml_libxml->createDocument("1.0", "UTF-8");
32 }
33  
34 sub xc {
35 60 ▶ my ($el_name_object, @attrs) = @_;
Subroutine xc: called CC=2 SCAR=7.0
Pod documented
36  
37 60 my $self = __PACKAGE__->new();
38  
39 60 my $initial_el = $self->_create_element($el_name_object, undef, @attrs);
40 60 ▶ confess 'document creation must be from single element'
Branch: 1 branch
BranchTrueFalseTotal
unless @$initial_el == 1 0 60 60
41 unless @$initial_el == 1;
42  
43 60 $self->dom->setDocumentElement($initial_el->[0]->{lxml});
44 60 return $self->document_element;
45 }
46  
47 sub _create_element {
48 117 ▶ my ($self, $el_name_object, $ns, @attrs) = @_;
Subroutine _create_element: called CC=17 SCAR=28.5
49  
50 117 ▶ if (@attrs == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attrs == 1) 1 116 117
51 1 my $hash_attrs = $attrs[0];
52 1 ▶ croak 'with two argument second argument must be hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash_attrs eq "HASH" 0 1 1
53 unless ref($hash_attrs) eq 'HASH';
54 1 @attrs = map { $_ => $hash_attrs->{$_} } sort keys %$hash_attrs;
55 }
56  
57 117 ▶ $ns //= {@attrs}->{xmlns} // '';
Condition80%4/5
$ns //= {@attrs}->{"xmlns"} // ""
l!l&&r!l&&!r
57600
{@attrs}->{"xmlns"} // ""
l!l
357
MC/DC0%0/2
$ns //= {@attrs}->{"xmlns"} // ""
$ns {@attrs}->{"xmlns"}
Truth table66%2/3
A //= B
A= $nsB= {@attrs}->{"xmlns"}
ABresult
1X1
011
000
58  
59 117 my @create_elements;
60 117 ▶ if (@attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attrs) 37 80 117
61 # lxml create
62 }
63 117 ▶ if (ref($el_name_object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $el_name_object) { } 28 89 117
64 28 ▶ if (blessed($el_name_object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($el_name_object)) 23 5 28
65 23 ▶ if ($el_name_object->isa('XML::Chain::Selector')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($el_name_object->isa('XML::Chain::Selector')) { } 20 3 23
elsif ($el_name_object->isa('XML::LibXML::Document')) { } 1 2 3
elsif ($el_name_object->isa('XML::LibXML::Node')) { } 0 2 2
66 20 @create_elements = @{$el_name_object->current_elements};
67 }
68 elsif ($el_name_object->isa('XML::LibXML::Document')) {
69 1 @create_elements = $self->_xc_el_data($el_name_object->documentElement);
70 }
71 elsif ($el_name_object->isa('XML::LibXML::Node')) {
72 0 @create_elements = $self->_xc_el_data($el_name_object);
73 }
74 }
75  
76 28 ▶ unless (@create_elements) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@create_elements) 7 21 28
77 7 $self->{io_any} = $el_name_object;
78 7 my $dom = XML::LibXML->load_xml(
79 string => IO::Any->slurp($el_name_object),
80 );
81 7 @create_elements = $self->_xc_el_data($dom->documentElement);
82 }
83 }
84 else {
85 # lxml create
86 }
87  
88 117 ▶ unless (@create_elements) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@create_elements) 89 28 117
89 89 my $new_element = $self->dom->createElementNS($ns, $el_name_object);
90 89 ▶ while (@attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attrs) 39 89 128
91 39 my $attr_name = shift(@attrs);
92 39 my $attr_value = shift(@attrs);
93 39 ▶ next unless defined($attr_name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr_name 0 39 39
94 39 ▶ next unless defined($attr_value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr_value 0 39 39
95 39 ▶ if ($attr_name eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_name eq '-') { } 17 22 39
96 17 $new_element->appendText($attr_value);
97 }
98 else {
99 try {
100 22 ▶ $new_element->setAttribute($attr_name => $attr_value);
Subroutine __ANON__: called CC=1 SCAR=0.0
101 }
102 catch {
103 0 ▶ confess 'failed to set attribute "'.$attr_name.'" on "'.$el_name_object.'" - '.$_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 22 };
105 }
106 }
107 89 @create_elements = $self->_xc_el_data($new_element);
108 }
109  
110 117 return \@create_elements;
111 }
112  
113 sub _xc_el_data {
114 266 ▶ my ($self, $el) = @_;
Subroutine _xc_el_data: called CC=4 SCAR=13.9
115 266 ▶ croak 'need element as argument' unless defined($el);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $el 0 266 266
116  
117 266 my $eid = $el->unique_key;
118 266 ▶ return $self->{_xc_el_data}->{$eid} //= {
Condition100%4/4
$self->{"_xc_el_data"}{$eid} //= {"eid", $eid, "lxml", $el, "ns", $el->namespaceURI // ""}
l!l
148118
$el->namespaceURI // ""
l!l
2593
MC/DC100%1/1
$self->{"_xc_el_data"}{$eid} //= {"eid", $eid, "lxml", $el, "ns", $el->namespaceURI // ""}
$self->{"_xc_el_data"}{$eid}
MC/DC100%1/1
$el->namespaceURI // ""
$el->namespaceURI
Truth table100%2/2
A
A= $self->{"_xc_el_data"}{$eid}
Aresult
11
00
Truth table100%2/2
A
A= $el->namespaceURI
Aresult
11
00
119 eid => $eid,
120 lxml => $el,
121 ns => ($el->namespaceURI // ''),
122 };
123 }
124  
125 sub _lxml_document_element {
126 78 ▶ return $_[0]->dom->documentElement;
Subroutine _lxml_document_element: called CC=1 SCAR=0.0
127 }
128  
129 0 ▶sub _xc {return $_[0];}
Subroutine _xc: not called CC=1 SCAR=6.9
130  
131 sub document_element {
132 78 ▶ my ($self) = @_;
Subroutine document_element: called CC=1 SCAR=0.0
Pod undocumented
133 78 return XML::Chain::Element->new(
134 _xc_el_data => $self->_xc_el_data($self->_lxml_document_element),
135 _xc => $self,
136 );
137 }
138  
139 sub store {
140 2 ▶ my ($self) = @_;
Subroutine store: called CC=3 SCAR=11.0
Pod documented
141 2 my $io_any = $self->{io_any};
142 2 ▶ my $io_any_opts = $self->{io_any_opts} // {};
Condition100%2/2
$self->{"io_any_opts"} // {}
l!l
11
MC/DC100%1/1
$self->{"io_any_opts"} // {}
$self->{"io_any_opts"}
Truth table100%2/2
A
A= $self->{"io_any_opts"}
Aresult
11
00
143 2 ▶ croak 'io_any was not set' unless $io_any;
Branch: 1 branch
BranchTrueFalseTotal
unless $io_any 0 2 2
144 2 IO::Any->spew($io_any, $self->document_element->as_string, {atomic => 1, %$io_any_opts});
145 2 return $self->document_element;
146 }
147  
148 sub set_io_any {
149 1 ▶ my ($self, $to_set, $opts) = @_;
Subroutine set_io_any: called CC=2 SCAR=7.0
Pod documented
150 1 $self->{io_any} = $to_set;
151 1 ▶ $self->{io_any_opts} = $opts // {};
Condition50%1/2
$opts // {}
l!l
01
MC/DC0%0/1
$opts // {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
152 1 return $self->document_element;
153 }
154  
155 4 1;
156  
157 __END__
158  
159 =encoding utf8
160  
161 =head1 NAME
162  
163 XML::Chain - chained way of manipulating and inspecting XML documents
164  
165 =head1 SYNOPSIS
166  
167 use XML::Chain qw(xc);
168  
169 # basics
170 my $div = xc('div', class => 'pretty')
171 ->c('h1')->t('hello')
172 ->up
173 ->c('p', class => 'intro')->t('world')
174 ->root
175 ->a( xc('p')->t('of chained XML.') );
176 say $div->as_string;
177 # <div class="pretty"><h1>hello</h1><p class="intro">world</p><p>of chained XML.</p></div>
178  
179 my $sitemap =
180 xc('urlset', xmlns => 'http://www.sitemaps.org/schemas/sitemap/0.9')
181 ->t("\n")
182 ->c('url')
183 ->a('loc', '-' => 'https://metacpan.org/pod/XML::Chain::Selector')
184 ->a('lastmod', '-' => DateTime->from_epoch(epoch => 1507451828)->strftime('%Y-%m-%d'))
185 ->a('changefreq', '-' => 'monthly')
186 ->a('priority', '-' => '0.6')
187 ->up->t("\n")
188 ->c('url')
189 ->a('loc', '-' => 'https://metacpan.org/pod/XML::Chain::Element')
190 ->a('lastmod', '-' => DateTime->from_epoch(epoch => 1507279028)->strftime('%Y-%m-%d'))
191 ->a('changefreq', '-' => 'monthly')
192 ->a('priority', '-' => '0.5')
193 ->up->t("\n");
194 say $sitemap->as_string;
195 # <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
196 # <url><loc>https://metacpan.org/pod/XML::Chain::Selector</loc><lastmod>2017-10-08</lastmod><changefreq>monthly</changefreq><priority>0.6</priority></url>
197 # <url><loc>https://metacpan.org/pod/XML::Chain::Element</loc><lastmod>2017-10-06</lastmod><changefreq>monthly</changefreq><priority>0.5</priority></url>
198 # </urlset>
199  
200 =head1 DESCRIPTION
201  
202 This module provides fast and easy way to create and manipulate XML elements
203 via set of chained method calls.
204  
205 =head1 EXPORTS
206  
207 =head2 xc
208  
209 Exported factory method creating new L<XML::Chain::Element> object with
210 a document element as provided in parameters. For example:
211  
212 my $icon = xc('i', class => 'icon-download icon-white');
213 # <i class="icon-download icon-white"/>
214  
215 See also L<XML::Chain::Selector/c, append_and_current>, from which
216 L<XML::Chain::Element> inherits all methods, for the element parameter
217 description and L<XML::Chain::Selector/CHAINED METHODS> for methods of
218 returned object.
219  
220 =head3 xc($el_name, @attrs) scalar with 1+ arguments
221  
222 Element with C<$el_name> will be create as document element and C< @attrs >
223 will be added to it in the same order.
224  
225 In case of hash reference passed as argument, key + values will be set
226 as attributes, in alphabetical sorted key name order.
227  
228 Attribute name "-" is a special case and the value will used for text
229 content inside the element.
230  
231 =head3 xc($xml_libxml_ref)
232  
233 In case of XML::LibXML, it will be set as document element.
234  
235 =head3 xc($what_ref)
236  
237 Any other reference will be passed to L<IO::Any/slurp($what)> which will
238 be then parsed by L<XML::LibXML/load_xml> and result set as document element.
239  
240 say xc([$tmp_dir, 't01.xml'])->as_string
241 say xc(\'<body><h1>and</h1><h1>head</h1></body>')
242 ->find('//h1')->count
243  
244 =head3 xc($scalar)
245  
246 Element with C<$scalar> will be create as document element.
247  
248 say xc('body');
249  
250 =head1 CHAINED METHODS, METHODS and ELEMENT METHODS
251  
252 See L<XML::Chain::Selector> and L<XML::Chain::Element>.
253  
254 =head1 CHAINED DOCUMENT METHODS
255  
256 xc('body')->t('save me')->set_io_any([$tmp_dir, 't01.xml'])->store;
257 # $tmp_dir/t01.xml file now consists of:
258 <body>save me</body>
259 xc([$tmp_dir, 't01.xml'])->empty->c('div')->t('updated')->store;
260 # $tmp_dir/t01.xml file now consists of:
261 <body><div>updated</div></body>
262  
263 =head2 set_io_any
264  
265 Store C< $what , $options > of L<IO::Any> for future use with C< -E<gt>store() >
266  
267 =head2 store
268  
269 Calls C< IO::Any->spew($io_any, $self->as_string, {atomic => 1}) > to
270 save XML back it it's original file of the the target set via
271 C<set_io_any>.
272  
273 =head1 TODO
274  
275 - partial/special tidy (on elements inside xml)
276 - per ->data() storage
277 - ->each(sub {...}) / ->map(sub {}) / ->grep(sub {})
278 - setting and handling namespaces and elements with ns prefixes
279 - ~ton of selectors and manipulators to be added
280  
281 =head1 CONTRIBUTORS & CREDITS
282  
283 Initially inspired by Strophe.Builder, then also by jQuery.
284  
285 The following people have contributed to the XML::Chain by committing their
286 code, sending patches, reporting bugs, asking questions, suggesting useful
287 advice, nitpicking, chatting on IRC or commenting on my blog (in no particular
288 order):
289  
290 Slaven Rezic
291 Vienna.pm (for listening to my talk and providing valuable feedback)
292 Mohammad S Anwar
293 you?
294  
295 Also thanks to my current day-job-employer L<http://geizhals.at/>.
296  
297 =head1 BUGS
298  
299 Please report any bugs or feature requests via L<https://github.com/meon/XML-Chain/issues>.
300  
301 =head1 AUTHOR
302  
303 Jozef Kutej
304  
305 =head1 COPYRIGHT & LICENSE
306  
307 Copyright 2017 Jozef Kutej, all rights reserved.
308  
309 This program is free software; you can redistribute it and/or modify it
310 under the same terms as Perl itself.
311  
312 =cut
↑ Summary _build/lib/XML/Chain/MB_Builder.pm »