XSH2/Functions.pm

filter: Statementstmt 52.9% 1997 / 3771 Branchbran 34.7% 831 / 2394 Conditioncond 29.5% 255 / 862 MC/DCmcdc 10.2% 51 / 497 Subroutinesub 67.9% 301 / 443 Podpod 0.0% 0 / 280 totaltotal 41.6% 3435 / 8247 CC 1588 SCAR 60.3
CC
1588
Cov
44%
save_doc
5070.9
edit
2652.0
insert_node
2542.3
CRAP 415.5
« XSH2/Completion.pm ↑ Summary XSH2/XPathToXML.pm »
1 # -*- cperl -*-
2 # $Id: Functions.pm,v 2.49 2008-01-27 10:48:39 pajas Exp $
3  
4 package XML::XSH2::Functions;
5  
6 #eval "no encoding";
7 #undef $@;
8 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use XML::XSH2::Help;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use XML::XSH2::Iterators;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use File::Temp qw(tempfile tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 17 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 15 ▶use vars qw/@ISA @EXPORT_OK %EXPORT_TAGS $VERSION $REVISION $OUT
Subroutine BEGIN: called CC=1 SCAR=0.0
23 @PARAM_VARS
24 $_xml_module $_sigint
25 $_xsh $_xpc $_parser @stored_variables
26 $lexical_variables $_newdoc
27 $TRAP_SIGINT $TRAP_SIGPIPE $_die_on_err $_on_exit
28 $_want_returns
29 %_files %_defs %_includes %_ns %_func %COMMANDS
30 $ENCODING $QUERY_ENCODING
31 $INDENT $BACKUPS $SWITCH_TO_NEW_DOCUMENTS $EMPTY_TAGS $SKIP_DTD
32 $QUIET $DEBUG $TEST_MODE $WARNINGS $ERRORS
33 $VALIDATION $RECOVERING $PARSER_EXPANDS_ENTITIES $KEEP_BLANKS
34 $PEDANTIC_PARSER $LOAD_EXT_DTD $PARSER_COMPLETES_ATTRIBUTES
35 $PARSER_EXPANDS_XINCLUDE $MAXPRINTLENGTH
36 $XPATH_AXIS_COMPLETION $STRICT_PWD
37 $XPATH_COMPLETION $DEFAULT_FORMAT $LINE_NUMBERS
38 $RT_LINE $RT_COLUMN $RT_OFFSET $RT_SCRIPT $SCRIPT
39 $BENCHMARK $DUMP $DUMP_APPEND $Xinclude_prefix $HISTFILE
40 $PROMPT
41 15 /;
42  
43 15 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
44 15 $VERSION='2.2.10'; # VERSION TEMPLATE
45 15 $REVISION=q($Revision: 2.49 $);
46 15 @ISA=qw(Exporter);
47 14 @PARAM_VARS=qw/$ENCODING
48 $QUERY_ENCODING
49 $INDENT
50 $EMPTY_TAGS
51 $SKIP_DTD
52 $BACKUPS
53 $SWITCH_TO_NEW_DOCUMENTS
54 $QUIET
55 $DEBUG
56 $TEST_MODE
57 $VALIDATION
58 $RECOVERING
59 $PARSER_EXPANDS_ENTITIES
60 $XPATH_AXIS_COMPLETION
61 $XPATH_COMPLETION
62 $KEEP_BLANKS
63 $PEDANTIC_PARSER
64 $LOAD_EXT_DTD
65 $PARSER_COMPLETES_ATTRIBUTES
66 $PARSER_EXPANDS_XINCLUDE
67 $DEFAULT_FORMAT
68 $LINE_NUMBERS
69 $WARNINGS
70 $MAXPRINTLENGTH
71 $HISTFILE
72 $STRICT_PWD
73 $PROMPT
74 /;
75 14 *XSH_NS=*XML::XSH2::xshNS;
76 14 *XML::XSH2::Map::XSH_NS=*XML::XSH2::xshNS;
77 14 *XML::XSH2::Map::OUT=\$OUT;
78 14 *EMPTY_TAGS=*XML::LibXML::setTagCompression;
79 14 *SKIP_DTD=*XML::LibXML::skipDTD;
80 14 *XML::XSH2::Map::PROGRAM_NAME=\$RT_SCRIPT;
81 14 @EXPORT_OK=(qw(&xsh_init &xsh &xsh_get_output
82 &xsh_set_output &xsh_set_parser
83 &set_quiet &set_debug &set_compile_only_mode
84 &create_doc &open_doc &set_doc
85 &xsh_pwd &out
86 &toUTF8 &fromUTF8 &xsh_set_script
87 &xsh_context_node &xsh_context_var
88 &xsh_xml_parser &xsh_parse_string
89 ),@PARAM_VARS);
90 14 %EXPORT_TAGS = (
91 default => [@EXPORT_OK],
92 param_vars => [@PARAM_VARS]
93 );
94  
95 14 $TRAP_SIGINT=0;
96 14 $_xml_module='XML::XSH2::LibXMLCompat';
97 14 $INDENT=1;
98 14 $EMPTY_TAGS=1; # no effect (reseted by XML::LibXML)
99 14 $SKIP_DTD=0; # no effect (reseted by XML::LibXML)
100 14 $BACKUPS=1;
101 14 $SWITCH_TO_NEW_DOCUMENTS=1;
102 14 $ENCODING='utf-8';
103 14 $QUERY_ENCODING='utf-8';
104 14 $QUIET=0;
105 14 $DEBUG=0;
106 14 $TEST_MODE=0;
107 14 $VALIDATION=0;
108 14 $RECOVERING=0;
109 14 $PARSER_EXPANDS_ENTITIES=1;
110 14 $KEEP_BLANKS=1;
111 14 $PEDANTIC_PARSER=0;
112 14 $LOAD_EXT_DTD=0;
113 14 $PARSER_COMPLETES_ATTRIBUTES=1;
114 14 $PARSER_EXPANDS_XINCLUDE=0;
115 14 $XPATH_COMPLETION=1;
116 14 $XPATH_AXIS_COMPLETION='always'; # never / when-empty
117 14 $DEFAULT_FORMAT='xml';
118 14 $LINE_NUMBERS=1;
119 14 $WARNINGS=1;
120 14 $ERRORS=1;
121 14 $BENCHMARK=0;
122 13 $MAXPRINTLENGTH=256;
123 13 $HISTFILE="$ENV{HOME}/.xsh2_history";
124 13 $STRICT_PWD=1;
125 13 $PROMPT='%p> ';
126 13 *XML::XSH2::Map::CURRENT_SCRIPT=\$RT_SCRIPT;
127  
128 13 $_newdoc=1;
129 13 $_die_on_err=1;
130 13 $_want_returns=0;
131  
132 13 autoflush STDOUT;
133 13 autoflush STDERR;
134 13 $lexical_variables = [];
135 13 $Xinclude_prefix = 'http://www.w3.org/2001/XInclude';
136 13 require XML::XSH2::Commands;
137 }
138  
139 3 ▶sub VERSION {
Subroutine VERSION: called CC=5 SCAR=19.4
Pod undocumented
140 1 ▶ shift if $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 0 0 0
141 1 my $ver = shift;
142 1 ▶ if (defined($ver)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ver) 0 0 0
143 1 my @V = split /\./,$VERSION;
144 1 my @v = split /\./,$ver;
145 1 for my $component (@v) {
146 1 ▶ croak __PACKAGE__." version $ver required--this is only version $VERSION"
Branch: 1 branch
BranchTrueFalseTotal
if $component > shift @V 0 0 0
147 if $component > shift @V;
148 }
149 }
150 1 return $VERSION;
151 }
152  
153  
154 1 ▶sub min { $_[0] > $_[1] ? $_[1] : $_[0] }
Subroutine min: called CC=2 SCAR=11.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $_[1] ? : 0 0 0
155  
156 143 ▶sub out {
Subroutine out: called CC=4 SCAR=13.9
Pod undocumented
157 141 ▶ if (ref($OUT) eq 'IO::Scalar') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $XML::XSH2::Functions::OUT eq 'IO::Scalar') { } 5 135 140
158 6 $OUT->print(@_);
159 } else {
160 136 foreach (map(fromUTF8($ENCODING,$_), @_)) {
161 198 my $l = length;
162 198 my $i = 1;
163 198 ▶ while ($l > $i*$MAXPRINTLENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $i * $XML::XSH2::Functions::MAXPRINTLENGTH) 5 197 202
164 6 print $OUT (substr($_,($i-1)*$MAXPRINTLENGTH,$MAXPRINTLENGTH));
165 6 $i++;
166 }
167 198 print $OUT (substr($_,($i-1)*$MAXPRINTLENGTH)); # the rest
168 }
169 }
170 }
171  
172 3 ▶sub __debug {
Subroutine __debug: called CC=1 SCAR=0.0
173 1 _err(@_);
174 }
175  
176 3 ▶sub __bug {
Subroutine __bug: called CC=1 SCAR=0.0
177 1 _err("BUG: ",@_);
178 }
179  
180 2 ▶sub _tilde_expand {
Subroutine _tilde_expand: called CC=1 SCAR=0.0
181 2 my ($filename)=@_;
182 13 ▶ $filename=~s{^(\~[^\/]*)}{(glob($1))[0]}eg;
Subroutine BEGIN: called CC=1 SCAR=0.0
183  
184 # $filename =~ s{ ^ ~ ( [^/]* ) }
185 # { $1
186 # ? (getpwnam($1))[7]
187 # : ( $ENV{HOME} || $ENV{LOGDIR}
188 # || (getpwuid($>))[7]
189 # )
190 # }ex;
191 2 return $filename;
192 }
193  
194 266 ▶sub _ev_opts {
Subroutine _ev_opts: called CC=8 SCAR=21.0
195 266 my ($opts)=@_;
196 266 ▶ return {} unless ref($opts);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts 0 265 265
197 266 ▶ return $opts if ref($opts) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts eq "HASH" 0 265 265
198 266 my %o;
199 266 my @opts = @$opts;
200 266 ▶ while (@opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opts) 26 265 291
201 27 my ($t,$n)=split /\//,shift(@opts);
202 27 my $v=shift @opts;
203 26 ▶ if ($t eq '' or $t eq 'exp') {
Branch: 3 branches
BranchTrueFalseTotal
if ($t eq '' or $t eq 'exp') { } 19 7 26
elsif ($t eq 'var') { } 0 7 7
elsif ($t eq 'xpath') { } 6 1 7
Condition100%3/3
$t eq "" or $t eq "exp"
l!l&&r!l&&!r
1457
MC/DC100%2/2
$t eq "" or $t eq "exp"
$t eq "" $t eq "exp"
Truth table100%3/3
A or B
A= $t eq ""B= $t eq "exp"
ABresult
1X1
011
000
204 19 $o { $n } = _ev($v);
205 } elsif ($t eq 'var') {
206 0 $o { $n } = $v;
207 } elsif ($t eq 'xpath') {
208 6 utf8::upgrade($v);
209 6 $o { $n } = _expand($v);
210 } else { # string
211 1 $o { $n } = _ev_string($v);
212 }
213 }
214 265 return \%o;
215 }
216  
217 12 ▶sub _hash_opts {
Subroutine _hash_opts: called CC=3 SCAR=11.0
218 11 my ($opts)=@_;
219 11 ▶ return {} unless ref($opts);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts 0 11 11
220 11 my %o;
221 11 my @opts = @$opts;
222 11 ▶ while (@opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opts) 16 11 27
223 16 my ($t,$n)=split /\//,shift(@opts);
224 16 my $v=shift @opts;
225 16 $o { $n } = $v;
226 }
227 11 return \%o;
228 }
229  
230 365 ▶sub alias_sr {
Subroutine alias_sr: called CC=1 SCAR=0.0
Pod undocumented
231 364 my($src, $dest)=@_;
232 364 tie($$dest, 'XML::XSH2::VarAlias', $src);
233 }
234  
235 343 ▶sub lexicalize {
Subroutine lexicalize: called CC=4 SCAR=13.9
Pod undocumented
236 342 my $p="package XML::XSH2::Map; no strict qw(vars); \$Lexical::Alias::SWAP=0; use utf8;";
237 342 my %seen;
238 342 ▶ for (my $i=$#$lexical_variables; $i>=0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 904 342 1246
239 904 foreach my $v (keys %{$lexical_variables->[$i]}) {
240 364 ▶ next if $seen{$v};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$v} 0 364 364
241 364 $seen{$v}=1;
242 364 $p.="my \$$v; \&XML::XSH2::Functions::alias_sr(\\\$XML::XSH2::Functions::lexical_variables->[$i]{q#$v#}, \\\$$v);"
243 }
244 }
245 # $p.="\n# line $RT_LINE \"$RT_SCRIPT\"\n";
246 342 return $p.$_[0];
247 }
248  
249 # initialize XSH and XML parsers
250 16 ▶sub xsh_init {
Subroutine xsh_init: called CC=8 SCAR=23.1
Pod undocumented
251 15 my $module=shift;
252 15 ▶ shift unless ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 15 0 15
253 15 ▶ if (ref($_[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 0 15 15
elsif (not $XML::XSH2::Functions::OUT) { } 8 7 15
254 0 $OUT=$_[0];
255 } elsif (! $OUT) {
256 8 ▶ if (open $OUT, '>&', \*STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $XML::XSH2::Functions::OUT, '>&', \*STDOUT) { } 8 0 8
257 8 binmode $OUT;
258 8 binmode $OUT, ':bytes';
259 } else {
260 0 $OUT = \*STDOUT;
261 }
262 }
263 15 set_encoding({},$ENCODING);
264 15 ▶ $_xml_module=$module if $module;
Branch: 1 branch
BranchTrueFalseTotal
if $module 0 15 15
265 15 eval("require $_xml_module;");
266 15 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 15 15
267 0 _err(
268 "\n------------------------------------------------------------\n",
269 $@,
270 "\n------------------------------------------------------------.\n",
271 "I suspect you have not installed XML::LibXML properly.\n",
272 "Please install and try again. If you are 100% sure you have, send\n",
273 "a full bug report to <pajas".'@'."users.sourceforge.net>\n");
274 0 exit 1;
275 }
276 15 my $mod=$_xml_module->module();
277 15 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.008) { } 15 0 15
278 15 require Encode;
279 15 *encodeToUTF8=*Encode::decode;
280 15 *decodeFromUTF8=*Encode::encode;
281 } else {
282 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
283 0 *encodeToUTF8=*{"$mod"."::encodeToUTF8"};
284 0 *decodeFromUTF8=*{"$mod"."::decodeFromUTF8"};
285 }
286 15 $_parser = $_xml_module->new_parser();
287  
288 15 xpc_init();
289 # xsh_rd_parser_init();
290  
291 # create a first document so that we always have non-empty context
292 15 create_doc('$scratch',"scratch",'xml','scratch.xml');
293 15 set_local_xpath({},'/');
294 }
295  
296 9 ▶sub xsh_rd_parser_init {
Subroutine xsh_rd_parser_init: called CC=4 SCAR=19.8
Pod undocumented
297 8 ▶ unless ($_xsh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($XML::XSH2::Functions::_xsh) 8 0 8
298 8 ▶ if (eval { require XML::XSH2::Parser; }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require XML::XSH2::Parser } }) { } 8 0 8
299 8 $_xsh=XML::XSH2::Parser->new();
300 } else {
301 0 print STDERR "Parsing raw grammar...\n";
302 0 require XML::XSH2::Grammar;
303 0 $_xsh=XML::XSH2::Grammar->new();
304 0 print STDERR "... done.\n";
305 0 ▶ unless ($QUIET) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($XML::XSH2::Functions::QUIET) 0 0 0
306 0 print STDERR << 'EOF';
307 NOTE: To avoid this, you should regenerate the XML::XSH2::Parser.pm
308 module from XML::XSH2::Grammar.pm module by changing to XML/XSH/
309 directory in your load-path and running the following command:
310  
311 perl -MGrammar -e XML::XSH2::Grammar::compile
312  
313 EOF
314 }
315 }
316 }
317 8 return $_xsh;
318 }
319  
320 0 ▶sub set_validation { shift if @_>1; $VALIDATION=$_[0]; 1; }
Subroutine set_validation: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
321 0 ▶sub set_recovering { shift if @_>1; $RECOVERING=$_[0]; 1; }
Subroutine set_recovering: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
322 0 ▶sub set_expand_entities { shift if @_>1; $PARSER_EXPANDS_ENTITIES=$_[0]; 1; }
Subroutine set_expand_entities: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
323 0 ▶sub set_keep_blanks { shift if @_>1; $KEEP_BLANKS=$_[0]; 1; }
Subroutine set_keep_blanks: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
324 0 ▶sub set_pedantic_parser { shift if @_>1; $PEDANTIC_PARSER=$_[0]; 1; }
Subroutine set_pedantic_parser: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
325 0 ▶sub set_load_ext_dtd { shift if @_>1; $LOAD_EXT_DTD=$_[0]; 1; }
Subroutine set_load_ext_dtd: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
326 0 ▶sub set_complete_attributes { shift if @_>1; $PARSER_COMPLETES_ATTRIBUTES=$_[0]; 1; }
Subroutine set_complete_attributes: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
327 0 ▶sub set_expand_xinclude { shift if @_>1; $PARSER_EXPANDS_XINCLUDE=$_[0]; 1; }
Subroutine set_expand_xinclude: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
328 2 ▶sub set_indent { shift if @_>1; $INDENT=$_[0]; 1; }
Subroutine set_indent: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 2 0 2
329 0 ▶sub set_empty_tags { shift if @_>1; $EMPTY_TAGS=$_[0]; 1; }
Subroutine set_empty_tags: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
330 0 ▶sub set_skip_dtd { shift if @_>1; $SKIP_DTD=$_[0]; 1; }
Subroutine set_skip_dtd: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
331 0 ▶sub set_backups { shift if @_>1; $BACKUPS=$_[0]; 1; }
Subroutine set_backups: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
332 0 ▶sub set_cdonopen { shift if @_>1; $SWITCH_TO_NEW_DOCUMENTS=$_[0]; 1; }
Subroutine set_cdonopen: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
333 0 ▶sub set_xpath_completion { shift if @_>1; $XPATH_COMPLETION=$_[0]; 1; }
Subroutine set_xpath_completion: called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
334 0 ▶sub set_xpath_axis_completion { shift if @_>1; $XPATH_AXIS_COMPLETION=$_[0];
Subroutine set_xpath_axis_completion: called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
335 0 ▶ if ($XPATH_AXIS_COMPLETION!~/^always|when-empty|never$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($XML::XSH2::Functions::XPATH_AXIS_COMPLETION =~ /^always|when-empty|never$/) 0 0 0
336 0 $XPATH_AXIS_COMPLETION='never';
337 }
338 0 1; }
339 0 ▶sub set_line_numbers { shift if @_>1; $LINE_NUMBERS=$_[0]; 1; }
Subroutine set_line_numbers: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
340  
341 0 ▶sub get_validation { $VALIDATION }
Subroutine get_validation: not called CC=1 SCAR=6.9
Pod undocumented
342 0 ▶sub get_recovering { $RECOVERING }
Subroutine get_recovering: not called CC=1 SCAR=6.9
Pod undocumented
343 0 ▶sub get_expand_entities { $PARSER_EXPANDS_ENTITIES }
Subroutine get_expand_entities: not called CC=1 SCAR=6.9
Pod undocumented
344 8 ▶sub get_keep_blanks { $KEEP_BLANKS }
Subroutine get_keep_blanks: called CC=1 SCAR=0.0
Pod undocumented
345 0 ▶sub get_pedantic_parser { $PEDANTIC_PARSER }
Subroutine get_pedantic_parser: not called CC=1 SCAR=6.9
Pod undocumented
346 0 ▶sub get_load_ext_dtd { $LOAD_EXT_DTD }
Subroutine get_load_ext_dtd: not called CC=1 SCAR=6.9
Pod undocumented
347 0 ▶sub get_complete_attributes { $PARSER_COMPLETES_ATTRIBUTES }
Subroutine get_complete_attributes: not called CC=1 SCAR=6.9
Pod undocumented
348 0 ▶sub get_expand_xinclude { $PARSER_EXPANDS_XINCLUDE }
Subroutine get_expand_xinclude: not called CC=1 SCAR=6.9
Pod undocumented
349 0 ▶sub get_indent { $INDENT }
Subroutine get_indent: not called CC=1 SCAR=6.9
Pod undocumented
350 0 ▶sub get_empty_tags { $EMPTY_TAGS }
Subroutine get_empty_tags: not called CC=1 SCAR=6.9
Pod undocumented
351 0 ▶sub get_skip_dtd { $SKIP_DTD }
Subroutine get_skip_dtd: not called CC=1 SCAR=6.9
Pod undocumented
352 0 ▶sub get_backups { $BACKUPS }
Subroutine get_backups: not called CC=1 SCAR=6.9
Pod undocumented
353 0 ▶sub get_cdonopen { $SWITCH_TO_NEW_DOCUMENTS }
Subroutine get_cdonopen: not called CC=1 SCAR=6.9
Pod undocumented
354 0 ▶sub get_xpath_completion { $XPATH_COMPLETION }
Subroutine get_xpath_completion: not called CC=1 SCAR=6.9
Pod undocumented
355 0 ▶sub get_xpath_axis_completion { $XPATH_AXIS_COMPLETION }
Subroutine get_xpath_axis_completion: not called CC=1 SCAR=6.9
Pod undocumented
356 0 ▶sub get_line_numbers { $LINE_NUMBERS }
Subroutine get_line_numbers: not called CC=1 SCAR=6.9
Pod undocumented
357  
358 # initialize global XPathContext
359 15 ▶sub xpc_init {
Subroutine xpc_init: called CC=1 SCAR=0.0
Pod undocumented
360 15 $_xpc=new_xpath_context();
361 15 $_ns{xsh}=$XML::XSH2::xshNS;
362 }
363  
364 16 ▶sub init_XPATH_funcs {
Subroutine init_XPATH_funcs: called CC=2 SCAR=6.9
Pod undocumented
365 16 my ($xpc,$ns)=@_;
366 16 foreach my $name (get_XPATH_extensions()) {
367 784 my $func=$name; $func =~ s/-/_/g;
368 784 $xpc->registerFunctionNS($name,$ns,\&{"XPATH_$func"});
369 }
370 }
371  
372 15 ▶sub new_xpath_context {
Subroutine new_xpath_context: called CC=2 SCAR=7.4
Pod undocumented
373 15 my $xpc;
374 15 ▶ unless (eval { require XML::LibXML::XPathContext;
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require XML::LibXML::XPathContext; $xpc = "XML::LibXML::XPathContext"->new } }) 0 15 15
375 15 $xpc=XML::LibXML::XPathContext->new();
376 }) {
377 0 require XML::XSH2::DummyXPathContext;
378 0 print STDERR ("Warning: XML::LibXML::XPathContext not found!\n".
379 "XSH will lack namespace and function registering functionality!\n\n");
380 0 return XML::XSH2::DummyXPathContext->new();
381 }
382 15 $xpc = XML::LibXML::XPathContext->new();
383 15 $xpc->registerVarLookupFunc(\&xpath_var_lookup,undef);
384 15 $xpc->registerNs('xsh',$XML::XSH2::xshNS);
385 15 init_XPATH_funcs($xpc,$XML::XSH2::xshNS);
386 15 return $xpc;
387 }
388  
389 0 ▶sub clone_xpc {
Subroutine clone_xpc: not called CC=4 SCAR=30.0
Pod undocumented
390 0 my $xpc = new_xpath_context();
391 0 foreach (keys(%_ns)) {
392 0 $xpc->registerNs($_,$_ns{$_});
393 }
394 0 foreach (keys(%_func)) {
395 0 ▶ if (/\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\n/) { } 0 0 0
396 0 my ($name,$ns)=/^(.*)\n((?:.|\n)*)$/;
397 0 $xpc->registerFunctionNS($name, $ns, $_func{$_});
398 } else {
399 0 $xpc->registerFunction($_, $_func{$_});
400 }
401 }
402 0 $xpc->setContextNode($_xpc->getContextNode());
403 0 return $xpc;
404 }
405  
406 1 ▶sub xpath_extensions {
Subroutine xpath_extensions: called CC=1 SCAR=0.0
Pod undocumented
407 1 my $opts = shift;
408 1 init_XPATH_funcs($_xpc,shift);
409 1 return 1;
410 }
411  
412 # ===================== XPATH EXT FUNC ================
413  
414 16 ▶sub get_XPATH_extensions {
Subroutine get_XPATH_extensions: called CC=1 SCAR=0.0
Pod undocumented
415 16 qw( current doc filename grep id2 if join lc uc ucfirst lcfirst
416 lineno evaluate map matches match max min new-attribute
417 new-cdata new-chunk new-comment new-element new-element-ns new-pi
418 new-text node-type parse path reverse same serialize split sprintf
419 strmax strmin subst substr sum times var document documents lookup span context
420 resolve-uri base-uri document-uri
421 )
422 }
423  
424 0 ▶sub XPATH_doc {
Subroutine XPATH_doc: not called CC=4 SCAR=25.3
Pod undocumented
425 0 ▶ die "Wrong number of arguments for function xsh:doc(nodeset)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
426 0 my ($nodelist)=@_;
427 0 ▶ die "1st argument must be a nodeset in xsh:doc(nodeset)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nodelist and UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList") 0 0 0
Condition0%0/3
ref $nodelist and UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $nodelist and UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList")
ref $nodelist UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList")
Truth table0%0/3
A and B
A= ref $nodelistB= UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList")
ABresult
0X0
100
111
428 unless (ref($nodelist) and UNIVERSAL::isa($nodelist,'XML::LibXML::NodeList'));
429 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
430 0 return XML::LibXML::NodeList->new(grep { ref($_) } map { $_->ownerDocument } @$nodelist);
431 }
432  
433 0 ▶sub XPATH_filename {
Subroutine XPATH_filename: not called CC=7 SCAR=37.0
Pod undocumented
434 0 ▶ die "Wrong number of arguments for function xsh:filename(nodeset?) or xsh:document-uri(nodeset?)!\n" if (@_>1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
435 0 my $doc;
436 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
437 0 ▶ die "1st argument must be a node in xsh:filename(nodeset?) or xsh:document-uri(nodeset?)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList") 0 0 0
Condition0%0/3
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ref $_[0] UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
Truth table0%0/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ABresult
0X0
100
111
438 unless (ref($_[0]) and UNIVERSAL::isa($_[0],'XML::LibXML::NodeList'));
439 }
440 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
441 0 ▶ return XML::LibXML::Literal->new('') unless $_[0][0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][0] 0 0 0
442 0 $doc = $_[0][0]->ownerDocument;
443 } else {
444 0 $doc = $XML::XSH2::Functions::_xpc->getContextNode()->ownerDocument;
445 }
446 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
447 0 return XML::LibXML::Literal->new($doc->URI());
448 }
449  
450 0 ▶sub XPATH_resolve_uri {
Subroutine XPATH_resolve_uri: not called CC=3 SCAR=24.8
Pod undocumented
451 0 ▶ die "Wrong number of arguments for function xsh:resolve-uri(relative-URI,base-URI?)!\n" if (@_>2 or @_==0);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 or @_ == 0 0 0 0
Condition0%0/3
@_ > 2 or @_ == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ > 2 or @_ == 0
@_ > 2 @_ == 0
Truth table0%0/3
A or B
A= @_ > 2B= @_ == 0
ABresult
1X1
011
000
452 0 my ($rel,$base)=map literal_value($_), @_;
453 0 return XML::LibXML::Literal->new(XML::XSH2::Map::resolve_uri($rel,$base)->as_string);
454 }
455  
456 0 ▶sub XPATH_document_uri {
Subroutine XPATH_document_uri: not called CC=1 SCAR=6.9
Pod undocumented
457 0 &XPATH_filename;
458 }
459  
460 0 ▶sub XPATH_base_uri {
Subroutine XPATH_base_uri: not called CC=8 SCAR=39.7
Pod undocumented
461 0 ▶ die "Wrong number of arguments for function xsh:base_uri(node?)!\n" if (@_>1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
462 0 my $node;
463 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
464 0 ▶ die "1st argument must be a node in xsh:base_uri(node?)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList") 0 0 0
Condition0%0/3
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ref $_[0] UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
Truth table0%0/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ABresult
0X0
100
111
465 unless (ref($_[0]) and UNIVERSAL::isa($_[0],'XML::LibXML::NodeList'));
466 }
467 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
468 0 ▶ return XML::LibXML::Literal->new('') unless $_[0][0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][0] 0 0 0
469 0 $node = $_[0][0];
470 } else {
471 0 $node = $XML::XSH2::Functions::_xpc->getContextNode();
472 }
473 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
474 0 ▶ return XML::LibXML::Literal->new($node->baseURI() || '');
Condition0%0/2
$node->baseURI || ''
l!l
00
MC/DC0%0/1
$node->baseURI || ''
$node->baseURI
Truth table0%0/2
A
A= $node->baseURI
Aresult
11
00
475 }
476  
477  
478 4 ▶sub XPATH_var {
Subroutine XPATH_var: called CC=2 SCAR=7.1
Pod undocumented
479 4 ▶ die "Wrong number of arguments for function xsh:var(id)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 4 4
480 4 my ($id)=literal_value($_[0]);
481 4 return var_value($id);
482 }
483  
484 6 ▶sub XPATH_matches {
Subroutine XPATH_matches: called CC=5 SCAR=16.2
Pod undocumented
485 6 ▶ die "Wrong number of arguments for function xsh:matches(string,regexp)!\n" if (@_!=2 and @_!=3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 and @_ != 3 0 6 6
Condition66%2/3
@_ != 2 and @_ != 3
!ll&&!rl&&r
240
MC/DC0%0/2
@_ != 2 and @_ != 3
@_ != 2 @_ != 3
Truth table66%2/3
A and B
A= @_ != 2B= @_ != 3
ABresult
0X0
100
111
486 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
487 6 my ($string,$regexp,$ignore_case)=@_;
488 6 $string=literal_value($string);
489 6 $regexp=literal_value($regexp);
490 6 $ignore_case=literal_value($ignore_case);
491 6 ▶ return ($ignore_case ?
Branch: 2 branches
BranchTrueFalseTotal
($ignore_case ? $string =~ /$regexp/i : $string =~ /$regexp/) ? : 3 3 6
$ignore_case ? : 3 3 6
492 $string=~m{$regexp}i :
493 $string=~m{$regexp}) ?
494 XML::LibXML::Boolean->True :
495 XML::LibXML::Boolean->False;
496 }
497  
498 7 ▶sub XPATH_substr {
Subroutine XPATH_substr: called CC=5 SCAR=16.7
Pod undocumented
499 7 ▶ die "Wrong number of arguments for function xsh:substr(string,position,[length])!\n" if (@_<2 or @_>3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 or @_ > 3 0 7 7
Condition33%1/3
@_ < 2 or @_ > 3
l!l&&r!l&&!r
007
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2 @_ > 3
Truth table33%1/3
A or B
A= @_ < 2B= @_ > 3
ABresult
1X1
011
000
500 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
501 7 my ($str,$pos,$len)=@_;
502 7 ▶ my $result = (@_ == 2) ?
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 5 2 7
503 substr(literal_value($str),
504 literal_value($pos)) :
505 substr(literal_value($str),
506 literal_value($pos),
507 literal_value($len));
508 7 ▶ $result = "" unless defined ($result);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 7 7
509 7 return $result;
510 }
511  
512 3 ▶sub XPATH_reverse {
Subroutine XPATH_reverse: called CC=2 SCAR=7.0
Pod undocumented
513 3 ▶ die "Wrong number of arguments for function xsh:reverse(string)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 3 3
514 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
515 3 return scalar reverse(literal_value($_[0]));
516 }
517  
518 0 ▶sub XPATH_lc {
Subroutine XPATH_lc: not called CC=2 SCAR=10.1
Pod undocumented
519 0 ▶ die "Wrong number of arguments for function xsh:lc(string)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
520 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
521 0 return lc(literal_value($_[0]));
522 }
523  
524 0 ▶sub XPATH_uc {
Subroutine XPATH_uc: not called CC=2 SCAR=10.1
Pod undocumented
525 0 ▶ die "Wrong number of arguments for function xsh:uc(string)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
526 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
527 0 return uc(literal_value($_[0]));
528 }
529  
530 0 ▶sub XPATH_lcfirst {
Subroutine XPATH_lcfirst: not called CC=2 SCAR=10.1
Pod undocumented
531 0 ▶ die "Wrong number of arguments for function xsh:lcfirst(string)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
532 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
533 0 return lcfirst(literal_value($_[0]));
534 }
535  
536 0 ▶sub XPATH_ucfirst {
Subroutine XPATH_ucfirst: not called CC=2 SCAR=10.1
Pod undocumented
537 0 ▶ die "Wrong number of arguments for function xsh:ucfirst(string)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
538 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
539 0 return ucfirst(literal_value($_[0]));
540 }
541  
542 13 ▶sub XPATH_grep {
Subroutine XPATH_grep: called CC=4 SCAR=14.6
Pod undocumented
543 13 ▶ die "Wrong number of arguments for function xsh:grep(list,regexp)!\n" if (@_!=2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 13 13
544 13 my ($nodelist,$regexp)=@_;
545 13 ▶ die "1st argument must be a nodeset in grep(list,regexp)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nodelist and UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList") 0 13 13
Condition33%1/3
ref $nodelist and UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList")
!ll&&!rl&&r
0013
MC/DC0%0/2
ref $nodelist and UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList")
ref $nodelist UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList")
Truth table33%1/3
A and B
A= ref $nodelistB= UNIVERSAL::isa($nodelist, "XML::LibXML::NodeList")
ABresult
0X0
100
111
546 unless (ref($nodelist) and UNIVERSAL::isa($nodelist,'XML::LibXML::NodeList'));
547 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
548 13 [grep { $_->to_literal=~m{$regexp} } @$nodelist];
549 }
550  
551 13 ▶sub XPATH_same {
Subroutine XPATH_same: called CC=8 SCAR=22.8
Pod undocumented
552 13 ▶ die "Wrong number of arguments for function xsh:same(node,node)!\n" if (@_!=2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 13 13
553 13 my ($nodea,$nodeb)=@_;
554 13 ▶ die "1st argument must be a node in grep(list,regexp)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nodea and UNIVERSAL::isa($nodea, "XML::LibXML::NodeList") 0 13 13
Condition33%1/3
ref $nodea and UNIVERSAL::isa($nodea, "XML::LibXML::NodeList")
!ll&&!rl&&r
0013
MC/DC0%0/2
ref $nodea and UNIVERSAL::isa($nodea, "XML::LibXML::NodeList")
ref $nodea UNIVERSAL::isa($nodea, "XML::LibXML::NodeList")
Truth table33%1/3
A and B
A= ref $nodeaB= UNIVERSAL::isa($nodea, "XML::LibXML::NodeList")
ABresult
0X0
100
111
555 unless (ref($nodea) and UNIVERSAL::isa($nodea,'XML::LibXML::NodeList'));
556 13 ▶ die "2nd argument must be a node in grep(list,regexp)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nodeb and UNIVERSAL::isa($nodeb, "XML::LibXML::NodeList") 0 13 13
Condition33%1/3
ref $nodeb and UNIVERSAL::isa($nodeb, "XML::LibXML::NodeList")
!ll&&!rl&&r
0013
MC/DC0%0/2
ref $nodeb and UNIVERSAL::isa($nodeb, "XML::LibXML::NodeList")
ref $nodeb UNIVERSAL::isa($nodeb, "XML::LibXML::NodeList")
Truth table33%1/3
A and B
A= ref $nodebB= UNIVERSAL::isa($nodeb, "XML::LibXML::NodeList")
ABresult
0X0
100
111
557 unless (ref($nodeb) and UNIVERSAL::isa($nodeb,'XML::LibXML::NodeList'));
558 13 ▶ return XML::LibXML::Boolean->new($nodea->size() && $nodeb->size() &&
Condition100%6/6
$nodea->size && $nodeb->size && $nodea->[0]->isSameNode($nodeb->[0])
!ll&&!rl&&r
2110
$nodea->size and $nodeb->size
!ll&&!rl&&r
1111
MC/DC100%3/3
$nodea->size && $nodeb->size && $nodea->[0]->isSameNode($nodeb->[0])
$nodea->size $nodeb->size $nodea->[0]->isSameNode($nodeb->[0])
Truth table100%4/4
A and B && C
A= $nodea->sizeB= $nodeb->sizeC= $nodea->[0]->isSameNode($nodeb->[0])
ABCresult
0XX0
10X0
1100
1111
559 $nodea->[0]->isSameNode($nodeb->[0]));
560 }
561  
562 7 ▶sub XPATH_max {
Subroutine XPATH_max: called CC=5 SCAR=16.1
Pod undocumented
563 7 my $r;
564 7 foreach (cast_objects_to_values(@_)) {
565 17 ▶ next unless /^\s*(-\s*)?(\d+(\.\d*)?|\.\d+)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s*(-\s*)?(\d+(\.\d*)?|\.\d+)\s*$/ 0 17 17
566 17 ▶ $r = $_ unless defined($r);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 6 11 17
567 17 ▶ $r = $_>$r ? $_ : $r;
Branch: 1 branch
BranchTrueFalseTotal
$_ > $r ? : 4 13 17
568 }
569 7 ; 0+$r;
570 }
571  
572 4 ▶sub XPATH_strmax {
Subroutine XPATH_strmax: called CC=5 SCAR=16.1
Pod undocumented
573 4 my $r;
574 4 foreach (cast_objects_to_values(@_)) {
575 9 ▶ $r = $_ unless defined($r);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 4 5 9
576 9 ▶ $r = $_ ge $r ? $_ : $r;
Branch: 1 branch
BranchTrueFalseTotal
$_ ge $r ? : 7 2 9
577 }
578 4 ▶ ; defined($r) ? $r : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $r ? : 4 0 4
579 }
580  
581 9 ▶sub XPATH_min {
Subroutine XPATH_min: called CC=5 SCAR=16.1
Pod undocumented
582 9 my $r;
583 9 foreach (cast_objects_to_values(@_)) {
584 21 ▶ next unless /^\s*(-\s*)?(\d+(\.\d*)?|\.\d+)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s*(-\s*)?(\d+(\.\d*)?|\.\d+)\s*$/ 0 21 21
585 21 ▶ $r = $_ unless defined($r);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 8 13 21
586 21 ▶ $r = $_ < $r ? $_ : $r;
Branch: 1 branch
BranchTrueFalseTotal
$_ < $r ? : 6 15 21
587 }
588 ;
589 9 return 0+$r;
590 }
591  
592 4 ▶sub XPATH_strmin {
Subroutine XPATH_strmin: called CC=5 SCAR=16.1
Pod undocumented
593 4 my $r;
594 4 foreach (cast_objects_to_values(@_)) {
595 9 ▶ $r = $_ unless defined($r);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 4 5 9
596 9 ▶ $r = $_ le $r ? $_ : $r;
Branch: 1 branch
BranchTrueFalseTotal
$_ le $r ? : 5 4 9
597 }
598 4 ▶ ; defined($r) ? $r : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $r ? : 4 0 4
599 }
600  
601 9 ▶sub XPATH_sum {
Subroutine XPATH_sum: called CC=2 SCAR=6.9
Pod undocumented
602 9 my $r=0;
603 9 foreach (cast_objects_to_values(@_)) {
604 29 $r += $_;
605 }
606 9 ; $r;
607 }
608  
609 4 ▶sub XPATH_join {
Subroutine XPATH_join: called CC=1 SCAR=0.0
Pod undocumented
610 4 my $j=literal_value(shift @_);
611 4 join $j,cast_objects_to_values(@_);
612 }
613  
614 21 ▶sub XPATH_serialize {
Subroutine XPATH_serialize: called CC=5 SCAR=17.8
Pod undocumented
615 21 my $result="";
616 21 foreach my $obj (@_) {
617 23 ▶ if (ref($obj) and
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj and UNIVERSAL::isa($obj, 'XML::LibXML::NodeList')) { } 23 0 23
Condition33%1/3
ref $obj and UNIVERSAL::isa($obj, "XML::LibXML::NodeList")
!ll&&!rl&&r
0023
MC/DC0%0/2
ref $obj and UNIVERSAL::isa($obj, "XML::LibXML::NodeList")
ref $obj UNIVERSAL::isa($obj, "XML::LibXML::NodeList")
Truth table33%1/3
A and B
A= ref $objB= UNIVERSAL::isa($obj, "XML::LibXML::NodeList")
ABresult
0X0
100
111
618 UNIVERSAL::isa($obj,'XML::LibXML::NodeList')) {
619 23 foreach my $node (@$obj) {
620 41 $result.=$node->toString();
621 }
622 } else {
623 0 $result.=literal_value($obj);
624 }
625 }
626 21 $result;
627 }
628  
629 11 ▶sub XPATH_subst {
Subroutine XPATH_subst: called CC=5 SCAR=16.4
Pod undocumented
630 11 ▶ die "Wrong number of arguments for function xsh:subst(string,regexp,replacement,[options])!\n" if (@_!=3 and @_!=4);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 and @_ != 4 0 11 11
Condition66%2/3
@_ != 3 and @_ != 4
!ll&&!rl&&r
560
MC/DC0%0/2
@_ != 3 and @_ != 4
@_ != 3 @_ != 4
Truth table66%2/3
A and B
A= @_ != 3B= @_ != 4
ABresult
0X0
100
111
631 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
632 11 my ($string,$regexp,$replace,$options)=@_;
633 11 $string=literal_value($string);
634 11 $regexp=literal_value($regexp);
635 11 ▶ return $string unless $regexp ne "";
Branch: 1 branch
BranchTrueFalseTotal
unless $regexp ne "" 0 11 11
636 11 $replace=literal_value($replace);
637 11 $options=literal_value($options);
638 11 ▶ die "Invalid options: $options (should only consist of 'egimsx')!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $options =~ /^[egimsx]*$/ 0 11 11
639 unless ($options =~ /^[egimsx]*$/);
640 11 $replace =~ s{\\(.)|(/)|(\\)$}{\\$1$2$3}gs;
641 11 eval "\$string=~s/\$regexp/$replace/$options";
642 11 return $string;
643 }
644  
645 0 ▶sub XPATH_parse {
Subroutine XPATH_parse: not called CC=2 SCAR=12.6
Pod undocumented
646 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
647 0 my $string=join '',map {literal_value($_)} @_;
648 0 my $dom=xsh_parse_string($string,'xml');
649 0 ▶ if ($dom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dom) { } 0 0 0
650 0 return XML::LibXML::NodeList->new($dom->childNodes());
651 } else {
652 0 return XML::LibXML::NodeList->new();
653 }
654 }
655  
656 7 ▶sub XPATH_sprintf {
Subroutine XPATH_sprintf: called CC=2 SCAR=7.0
Pod undocumented
657 7 ▶ die "Wrong number of arguments for function xsh:sprintf(format-string,...)!\n" if (@_<1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 7 7
658 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
659 7 my @args=map { literal_value($_) } @_;
660 7 return sprintf(shift(@args),@args);
661 }
662  
663 6 ▶sub XPATH_current {
Subroutine XPATH_current: called CC=3 SCAR=11.7
Pod undocumented
664 6 ▶ die "Wrong number of arguments for function xsh:current()!\n" if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6 6
665 6 my $ln = xsh_context_node();
666 6 ▶ return XML::LibXML::NodeList->new($ln ? $ln : ());
Branch: 1 branch
BranchTrueFalseTotal
$ln ? : 6 0 6
667 }
668  
669 5 ▶sub XPATH_path {
Subroutine XPATH_path: called CC=5 SCAR=18.6
Pod undocumented
670 5 ▶ die "Wrong number of arguments for function xsh:path(nodeset)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 5 5
671 5 ▶ die "Wrong type of argument 1 for xsh:path(nodeset)!\n" unless (ref($_[0]) and UNIVERSAL::isa($_[0],'XML::LibXML::NodeList'));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList") 0 5 5
Condition33%1/3
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
!ll&&!rl&&r
005
MC/DC0%0/2
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ref $_[0] UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
Truth table33%1/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ABresult
0X0
100
111
672 5 ▶ return "" unless $_[0][0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][0] 0 5 5
673 return
674 5 XML::LibXML::Literal->new(pwd($_[0][0]));
675 }
676  
677 1 ▶sub XPATH_node_type {
Subroutine XPATH_node_type: called CC=5 SCAR=18.6
Pod undocumented
678 1 ▶ die "Wrong number of arguments for function xsh:node-type(node-set)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1 1
679 1 ▶ die "Wrong type of argument 1 for xsh:node-type(node-set)!\n" unless (ref($_[0]) and UNIVERSAL::isa($_[0],'XML::LibXML::NodeList'));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList") 0 1 1
Condition33%1/3
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
!ll&&!rl&&r
001
MC/DC0%0/2
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ref $_[0] UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
Truth table33%1/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ABresult
0X0
100
111
680 1 ▶ return "" unless $_[0][0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][0] 0 1 1
681 return
682 1 XML::LibXML::Literal->new(node_type($_[0][0]));
683 }
684  
685 0 ▶sub XPATH_object_type {
Subroutine XPATH_object_type: not called CC=7 SCAR=40.3
Pod undocumented
686 0 ▶ die "Wrong number of arguments for function xsh:object-type(object)!\n" if (@_!=1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
687 0 my ($obj)=@_;
688 0 my $ret;
689 0 ▶ if (!ref($obj)) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $obj) { } 0 0 0
elsif (UNIVERSAL::isa($obj, 'XML::LibXML::NodeList')) { } 0 0 0
elsif (UNIVERSAL::isa($obj, 'XML::LibXML::Literal')) { } 0 0 0
elsif (UNIVERSAL::isa($obj, 'XML::LibXML::Boolean')) { } 0 0 0
elsif (UNIVERSAL::isa($obj, 'XML::LibXML::Number')) { } 0 0 0
690 0 $ret = "string"
691 } elsif (UNIVERSAL::isa($obj,"XML::LibXML::NodeList")) {
692 0 $ret = "nodeset"
693 } elsif (UNIVERSAL::isa($obj,"XML::LibXML::Literal")) {
694 0 $ret = "string"
695 } elsif (UNIVERSAL::isa($obj,"XML::LibXML::Boolean")) {
696 0 $ret = "boolean"
697 } elsif (UNIVERSAL::isa($obj,"XML::LibXML::Number")) {
698 0 $ret = "float"
699 } else {
700 0 $ret = "unknown"
701 }
702 0 return XML::LibXML::Literal->new($ret);
703 }
704  
705 0 ▶sub XPATH_evaluate {
Subroutine XPATH_evaluate: not called CC=10 SCAR=47.0
Pod undocumented
706 0 ▶ die "Wrong number of arguments for function xsh:evaluate(string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 or @_ > 4 0 0 0
Condition0%0/3
@_ == 0 or @_ > 4
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 0 or @_ > 4
@_ == 0 @_ > 4
Truth table0%0/3
A or B
A= @_ == 0B= @_ > 4
ABresult
1X1
011
000
707 if ((@_==0) or (@_>4));
708 0 my ($xpath,$node,$size,$pos)=@_;
709 0 my $old_context;
710 0 ▶ if (@_>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
711 0 $old_context = _save_context();
712 0 ▶ die "Wrong type of argument 1 for xsh:evaluate(string,node?,size?,position?)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node and UNIVERSAL::isa($node, "XML::LibXML::NodeList") 0 0 0
Condition0%0/3
ref $node and UNIVERSAL::isa($node, "XML::LibXML::NodeList")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $node and UNIVERSAL::isa($node, "XML::LibXML::NodeList")
ref $node UNIVERSAL::isa($node, "XML::LibXML::NodeList")
Truth table0%0/3
A and B
A= ref $nodeB= UNIVERSAL::isa($node, "XML::LibXML::NodeList")
ABresult
0X0
100
111
713 unless (ref($node) and UNIVERSAL::isa($node,'XML::LibXML::NodeList'));
714 0 ▶ if (@$node) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$node) { } 0 0 0
715 0 _set_context([$node->[0],$size,$pos]);
716 } else {
717 0 return XML::LibXML::NodeList->new();
718 }
719 }
720 0 ▶ if ($xpath eq "") { return XML::LibXML::NodeList->new() }
Branch: 1 branch
BranchTrueFalseTotal
if ($xpath eq "") 0 0 0
721 0 my $val;
722 0 eval { $val = $_xpc->find($xpath) };
723 0 ▶ _set_context($old_context) if $old_context;
Branch: 1 branch
BranchTrueFalseTotal
if $old_context 0 0 0
724 0 ▶ return defined($val) ? $val : XML::LibXML::NodeList->new();
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
725 }
726  
727 7 ▶sub XPATH_map {
Subroutine XPATH_map: called CC=12 SCAR=27.5
Pod undocumented
728 7 ▶ die "Wrong number of arguments for function xsh:map(nodeset,string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 7 7
729 if (@_!=2);
730 7 ▶ die "Wrong type of argument 1 for xsh:map(nodeset,string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList") 0 7 7
Condition33%1/3
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
!ll&&!rl&&r
007
MC/DC0%0/2
ref $_[0] and UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ref $_[0] UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
Truth table33%1/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], "XML::LibXML::NodeList")
ABresult
0X0
100
111
731 unless (ref($_[0]) and UNIVERSAL::isa($_[0],'XML::LibXML::NodeList'));
732 7 my ($nl,$xpath)=@_;
733 7 my $res = XML::LibXML::NodeList->new();
734 0 ▶ unless (@{$nl} and $xpath ne "") { return $res; }
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$nl;} and $xpath ne "") 0 7 7
Condition33%1/3
@{$nl;} and $xpath ne ""
!ll&&!rl&&r
007
MC/DC0%0/2
@{$nl;} and $xpath ne ""
@{$nl;} $xpath ne ""
Truth table33%1/3
A and B
A= @{$nl;}B= $xpath ne ""
ABresult
0X0
100
111
735 7 ▶ return $res if $xpath eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $xpath eq "" 0 7 7
736 # my $xpc = clone_xpc();
737 7 my $res_el;
738 7 ▶ $_xpc->setContextSize(0+@{$nl}) if $_xpc->can('setContextSize');
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::_xpc->can("setContextSize") 7 0 7
739 7 my $pos=1;
740 7 foreach my $node (@{$nl}) {
741 9 ▶ $_xpc->setContextPosition($pos++) if $_xpc->can('setContextSize');
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::_xpc->can("setContextSize") 9 0 9
742 9 my $val;
743 9 eval { $val = $_xpc->find($xpath,$node); };
744 9 ▶ return XML::LibXML::NodeList->new() if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
745 9 ▶ next unless (ref($val));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 0 9 9
746 9 push @$res,cast_value_to_objects($val,undef,1);
747 }
748 7 return $res;
749 }
750  
751 0 ▶sub XPATH_match {
Subroutine XPATH_match: not called CC=6 SCAR=34.8
Pod undocumented
752 0 ▶ die "Wrong number of arguments for function xsh:match(string,regexp,options?)!\n" if (@_!=2 and @_!=3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 and @_ != 3 0 0 0
Condition0%0/3
@_ != 2 and @_ != 3
!ll&&!rl&&r
000
MC/DC0%0/2
@_ != 2 and @_ != 3
@_ != 2 @_ != 3
Truth table0%0/3
A and B
A= @_ != 2B= @_ != 3
ABresult
0X0
100
111
753 13 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
754 0 my ($string,$regexp,$options)=@_;
755 0 $string=literal_value($string);
756 0 $regexp=literal_value($regexp);
757 0 $options=literal_value($options);
758  
759 0 ▶ die "Invalid options: $options (should only consist of 'cgimosx')!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $options =~ /^[cgimosx]*$/ 0 0 0
760 unless ($options =~ /^[cgimosx]*$/);
761 0 my @result = eval "\$string=~/\$regexp/$options";
762 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
763 0 my $res = XML::LibXML::NodeList->new();
764 0 my $res_doc=XML::LibXML::Document->new();
765 0 my $res_el=$res_doc->createElementNS($XML::XSH2::xshNS,'xsh:result');
766 0 $res_doc->setDocumentElement($res_el);
767 0 my $el;
768 0 foreach my $str (@result) {
769 0 $el = $res_doc->createElementNS($XML::XSH2::xshNS,'xsh:string');
770 0 $el->appendText($str);
771 0 $res_el->appendChild($el);
772 0 push @$res,$el;
773 }
774 0 return $res;
775 }
776  
777 0 ▶sub XPATH_split {
Subroutine XPATH_split: not called CC=3 SCAR=24.8
Pod undocumented
778 0 ▶ die "Wrong number of arguments for function xsh:split(regexp,string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
779 if (@_!=2);
780 0 my ($regexp,$string)=@_;
781 0 $regexp=literal_value($regexp);
782 0 $string=literal_value($string);
783 0 my $res = XML::LibXML::NodeList->new();
784 0 my $res_doc=XML::LibXML::Document->new();
785 0 my $res_el=$res_doc->createElementNS($XML::XSH2::xshNS,'xsh:result');
786 0 $res_doc->setDocumentElement($res_el);
787 0 my $el;
788 0 foreach my $str (split $regexp,$string) {
789 0 $el = $res_doc->createElementNS($XML::XSH2::xshNS,'xsh:string');
790 0 $el->appendText($str);
791 0 $res_el->appendChild($el);
792 0 push @$res,$el;
793 }
794 0 return $res;
795 }
796  
797 3 ▶sub XPATH_new_attribute {
Subroutine XPATH_new_attribute: called CC=5 SCAR=18.7
Pod undocumented
798 3 ▶ die "Wrong number of arguments for function xsh:new-attributes(string, string, [string, string,...])!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and scalar(@_) % 2 == 0 0 3 3
Condition33%1/3
@_ and scalar(@_) % 2 == 0
!ll&&!rl&&r
003
MC/DC0%0/2
@_ and scalar(@_) % 2 == 0
@_ scalar(@_) % 2 == 0
Truth table33%1/3
A and B
A= @_B= scalar(@_) % 2 == 0
ABresult
0X0
100
111
799 unless (@_ and (scalar(@_) % 2 == 0));
800 3 my %attr=map { literal_value($_) } @_;
801 3 my $doc = $_xpc->getContextNode;
802 3 ▶ unless (ref($doc) and ref($doc = $doc->ownerDocument())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $doc and ref($doc = $doc->ownerDocument)) 0 3 3
Condition33%1/3
ref $doc and ref($doc = $doc->ownerDocument)
!ll&&!rl&&r
003
MC/DC0%0/2
ref $doc and ref($doc = $doc->ownerDocument)
ref $doc ref($doc = $doc->ownerDocument)
Truth table33%1/3
A and B
A= ref $docB= ref($doc = $doc->ownerDocument)
ABresult
0X0
100
111
803 0 die "No context document\n";
804 }
805 3 return XML::LibXML::NodeList->new(map {$doc->createAttribute($_,$attr{$_})} keys %attr);
806 }
807  
808 2 ▶sub XPATH_new_element {
Subroutine XPATH_new_element: called CC=5 SCAR=17.3
Pod undocumented
809 2 ▶ die "Wrong number of arguments for function xsh:new-element(string, [string,string,...])!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar(@_) % 2 0 2 2
810 unless (scalar(@_)%2);
811 2 my ($name,%attrs)=map {literal_value($_)} @_;
812 2 my $doc = $_xpc->getContextNode;
813 2 ▶ unless (ref($doc) and ref($doc = $doc->ownerDocument())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $doc and ref($doc = $doc->ownerDocument)) 0 2 2
Condition33%1/3
ref $doc and ref($doc = $doc->ownerDocument)
!ll&&!rl&&r
002
MC/DC0%0/2
ref $doc and ref($doc = $doc->ownerDocument)
ref $doc ref($doc = $doc->ownerDocument)
Truth table33%1/3
A and B
A= ref $docB= ref($doc = $doc->ownerDocument)
ABresult
0X0
100
111
814 0 die "No context document\n";
815 }
816 2 my $e = $doc->createElement($name);
817 2 foreach my $aname (keys %attrs) {
818 2 $e->setAttribute($aname,$attrs{$aname});
819 }
820 2 return XML::LibXML::NodeList->new($e);
821 }
822  
823 1 ▶sub XPATH_new_element_ns {
Subroutine XPATH_new_element_ns: called CC=6 SCAR=20.2
Pod undocumented
824 1 ▶ die "Wrong number of arguments for function xsh:new-element-ns(string, string, [string,string])!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and (scalar(@_) + 1) % 2 0 1 1
Condition33%1/3
@_ and (scalar(@_) + 1) % 2
!ll&&!rl&&r
001
MC/DC0%0/2
@_ and (scalar(@_) + 1) % 2
@_ (scalar(@_) + 1) % 2
Truth table33%1/3
A and B
A= @_B= (scalar(@_) + 1) % 2
ABresult
0X0
100
111
825 unless (@_ and (scalar(@_)+1)%2);
826 1 my ($name,$ns,%attrs)=map {literal_value($_)} @_;
827 1 my $doc = $_xpc->getContextNode;
828 1 ▶ unless (ref($doc) and ref($doc = $doc->ownerDocument())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $doc and ref($doc = $doc->ownerDocument)) 0 1 1
Condition33%1/3
ref $doc and ref($doc = $doc->ownerDocument)
!ll&&!rl&&r
001
MC/DC0%0/2
ref $doc and ref($doc = $doc->ownerDocument)
ref $doc ref($doc = $doc->ownerDocument)
Truth table33%1/3
A and B
A= ref $docB= ref($doc = $doc->ownerDocument)
ABresult
0X0
100
111
829 0 die "No context document\n";
830 }
831 # __debug("ns: $ns");
832 1 my $e=$doc->createElementNS("$ns",$name);
833 # my ($prefix,$name) = split ':',$name;
834 # my $e=XML::LibXML::Element->new($name);
835 # $e->setNamespace("$ns","$prefix",1);
836 1 foreach my $aname (keys %attrs) {
837 2 $e->setAttribute($aname,$attrs{$aname});
838 }
839 1 return XML::LibXML::NodeList->new($e);
840 }
841  
842  
843 1 ▶sub XPATH_new_text {
Subroutine XPATH_new_text: called CC=4 SCAR=15.5
Pod undocumented
844 1 ▶ die "Wrong number of arguments for function xsh:new-text(string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1 1
845 if (@_!=1);
846 1 my $text=literal_value(shift);
847 1 my $doc = $_xpc->getContextNode;
848 1 ▶ unless (ref($doc) and ref($doc = $doc->ownerDocument())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $doc and ref($doc = $doc->ownerDocument)) 0 1 1
Condition33%1/3
ref $doc and ref($doc = $doc->ownerDocument)
!ll&&!rl&&r
001
MC/DC0%0/2
ref $doc and ref($doc = $doc->ownerDocument)
ref $doc ref($doc = $doc->ownerDocument)
Truth table33%1/3
A and B
A= ref $docB= ref($doc = $doc->ownerDocument)
ABresult
0X0
100
111
849 0 die "No context document\n";
850 }
851 1 my $t=$doc->createTextNode($text);
852 1 return XML::LibXML::NodeList->new($t);
853 }
854  
855 1 ▶sub XPATH_new_comment {
Subroutine XPATH_new_comment: called CC=4 SCAR=15.5
Pod undocumented
856 1 ▶ die "Wrong number of arguments for function xsh:new-comment(string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1 1
857 if (@_!=1);
858 1 my $text=literal_value(shift);
859 1 my $doc = $_xpc->getContextNode;
860 1 ▶ unless (ref($doc) and ref($doc = $doc->ownerDocument())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $doc and ref($doc = $doc->ownerDocument)) 0 1 1
Condition33%1/3
ref $doc and ref($doc = $doc->ownerDocument)
!ll&&!rl&&r
001
MC/DC0%0/2
ref $doc and ref($doc = $doc->ownerDocument)
ref $doc ref($doc = $doc->ownerDocument)
Truth table33%1/3
A and B
A= ref $docB= ref($doc = $doc->ownerDocument)
ABresult
0X0
100
111
861 0 die "No context document\n";
862 }
863 1 my $t=$doc->createComment($text);
864 1 return XML::LibXML::NodeList->new($t);
865 }
866  
867 2 ▶sub XPATH_new_cdata {
Subroutine XPATH_new_cdata: called CC=4 SCAR=15.5
Pod undocumented
868 2 ▶ die "Wrong number of arguments for function xsh:new-cdata(string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 2 2
869 if (@_!=1);
870 2 my $name=literal_value(shift);
871 2 my $doc = $_xpc->getContextNode;
872 2 ▶ unless (ref($doc) and ref($doc = $doc->ownerDocument())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $doc and ref($doc = $doc->ownerDocument)) 0 2 2
Condition33%1/3
ref $doc and ref($doc = $doc->ownerDocument)
!ll&&!rl&&r
002
MC/DC0%0/2
ref $doc and ref($doc = $doc->ownerDocument)
ref $doc ref($doc = $doc->ownerDocument)
Truth table33%1/3
A and B
A= ref $docB= ref($doc = $doc->ownerDocument)
ABresult
0X0
100
111
873 0 die "No context document\n";
874 }
875 2 my $t=$doc->createCDATASection($name);
876 2 return XML::LibXML::NodeList->new($t);
877 }
878  
879 1 ▶sub XPATH_new_pi {
Subroutine XPATH_new_pi: called CC=5 SCAR=18.7
Pod undocumented
880 1 ▶ die "Wrong number of arguments for function xsh:new-pi(string,[ string])!\n"
Branch: 1 branch
BranchTrueFalseTotal
if not @_ or @_ > 2 0 1 1
Condition33%1/3
not @_ or @_ > 2
l!l&&r!l&&!r
001
MC/DC0%0/2
not @_ or @_ > 2
not @_ @_ > 2
Truth table33%1/3
not(A) or B
A= not @_B= @_ > 2
ABresult
1X1
011
000
881 if (!@_ or @_>2);
882 1 my ($name,$value)=map { literal_value($_) } @_;
883 1 my $doc = $_xpc->getContextNode;
884 1 ▶ unless (ref($doc) and ref($doc = $doc->ownerDocument())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $doc and ref($doc = $doc->ownerDocument)) 0 1 1
Condition33%1/3
ref $doc and ref($doc = $doc->ownerDocument)
!ll&&!rl&&r
001
MC/DC0%0/2
ref $doc and ref($doc = $doc->ownerDocument)
ref $doc ref($doc = $doc->ownerDocument)
Truth table33%1/3
A and B
A= ref $docB= ref($doc = $doc->ownerDocument)
ABresult
0X0
100
111
885 0 die "No context document\n";
886 }
887 1 my $pi = $doc->createPI($name => $value);
888 1 return XML::LibXML::NodeList->new($pi);
889 }
890  
891 0 ▶sub XPATH_new_chunk {
Subroutine XPATH_new_chunk: not called CC=2 SCAR=17.9
Pod undocumented
892 0 ▶ die "Wrong number of arguments for function xsh:new-chunk(string,[ string])!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
893 if (@_!=1);
894 0 return XPATH_parse(@_);
895 }
896  
897 0 ▶sub XPATH_times {
Subroutine XPATH_times: not called CC=2 SCAR=17.9
Pod undocumented
898 0 ▶ die "Wrong number of arguments for function xsh:times(string,float)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
899 if (@_!=2);
900 0 my ($string,$times)=@_;
901 0 $times=literal_value($times);
902 0 $string=literal_value($string);
903 0 return XML::LibXML::Literal->new($times x $string);
904 }
905  
906 0 ▶sub XPATH_if {
Subroutine XPATH_if: not called CC=7 SCAR=40.3
Pod undocumented
907 0 ▶ die "Wrong number of arguments for function xsh:if(object,object,object)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 0 0
908 if (@_!=3);
909 0 my ($test, $if, $else)=@_;
910 0 ▶ if (ref($test) and
Branch: 1 branch
BranchTrueFalseTotal
if (ref $test and UNIVERSAL::isa($test, 'XML::LibXML::NodeList') && @$test || $test->to_literal->value or $test) { } 0 0 0
Condition0%0/12
ref $test and UNIVERSAL::isa($test, "XML::LibXML::NodeList") && @$test || $test->to_literal->value or $test
l!l&&r!l&&!r
000
ref $test and UNIVERSAL::isa($test, "XML::LibXML::NodeList") && @$test || $test->to_literal->value
!ll&&!rl&&r
000
UNIVERSAL::isa($test, "XML::LibXML::NodeList") and @$test or $test->to_literal->value
l!l&&r!l&&!r
000
UNIVERSAL::isa($test, "XML::LibXML::NodeList") and @$test
!ll&&!rl&&r
000
MC/DC0%0/5
ref $test and UNIVERSAL::isa($test, "XML::LibXML::NodeList") && @$test || $test->to_literal->value or $test
ref $test UNIVERSAL::isa($test, "XML::LibXML::NodeList") @$test $test->to_literal->value $test
Truth table0%0/9
A and B and C or D or E
A= ref $testB= UNIVERSAL::isa($test, "XML::LibXML::NodeList")C= @$testD= $test->to_literal->valueE= $test
ABCDEresult
111XX1
10X1X1
1101X1
0XXX11
10X011
110011
0XXX00
10X000
110000
911 (UNIVERSAL::isa($test,'XML::LibXML::NodeList') and @$test
912 or $test->to_literal->value)
913 or $test) {
914 0 return $if;
915 } else {
916 0 return $else;
917 }
918 }
919  
920 0 ▶sub XPATH_id2 {
Subroutine XPATH_id2: not called CC=6 SCAR=37.4
Pod undocumented
921 0 ▶ die "Wrong number of arguments for function xsh:id2(object,string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
922 if (@_!=2);
923 0 my ($nl, $id)=@_;
924 0 ▶ die "Wrong type of argument 1 for function xsh:id2(object,string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nl and UNIVERSAL::isa($nl, "XML::LibXML::NodeList") 0 0 0
Condition0%0/3
ref $nl and UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $nl and UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
ref $nl UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
Truth table0%0/3
A and B
A= ref $nlB= UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
ABresult
0X0
100
111
925 if (!ref($nl) or not UNIVERSAL::isa($nl,"XML::LibXML::NodeList"));
926 0 ▶ die "Argument 2 for function xsh:id2(object,string) isn't a valid qname!\n"
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ /\'/ 0 0 0
927 if ($id =~ /\'/);
928 0 my $res=XML::LibXML::NodeList->new();
929 0 ▶ if ($nl->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl->[0]) 0 0 0
930 0 push @$res, $nl->[0]->findnodes("id('".$id."')");
931 }
932 0 return $res;
933 }
934  
935 0 ▶sub XPATH_lineno {
Subroutine XPATH_lineno: not called CC=5 SCAR=34.0
Pod undocumented
936 0 ▶ die "Wrong number of arguments for function xsh:lineno(node-set)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
937 if (@_!=1);
938 0 my ($nl, $id)=@_;
939 0 ▶ die "Wrong type of argument 1 for function xsh:lineno(node-set)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nl and UNIVERSAL::isa($nl, "XML::LibXML::NodeList") 0 0 0
Condition0%0/3
ref $nl and UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $nl and UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
ref $nl UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
Truth table0%0/3
A and B
A= ref $nlB= UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
ABresult
0X0
100
111
940 if (!ref($nl) or not UNIVERSAL::isa($nl,"XML::LibXML::NodeList"));
941 0 my $res=-1;
942 0 ▶ if ($nl->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl->[0]) 0 0 0
943 0 $res=$nl->[0]->line_number;
944 }
945 0 return XML::LibXML::Number->new($res);
946 }
947  
948 0 ▶sub XPATH_document {
Subroutine XPATH_document: not called CC=14 SCAR=53.5
Pod undocumented
949 0 ▶ die "Wrong number of arguments for function xsh:document(string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
950 if (@_!=1);
951 0 my $URI = shift;
952 0 my $abs;
953 0 my @files = _files();
954 0 for my $f (@files) {
955 0 ▶ return XML::LibXML::NodeList->new($f->[0])
Branch: 1 branch
BranchTrueFalseTotal
if $f->[0]->URI eq $URI 0 0 0
956 if ($f->[0]->URI eq $URI);
957 }
958 0 ▶ unless (_is_absolute($URI)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_absolute($URI)) 0 0 0
959 0 $URI = File::Spec->rel2abs(_tilde_expand($URI));
960 0 for my $f (@files) {
961 0 ▶ return XML::LibXML::NodeList->new($f->[0])
Branch: 1 branch
BranchTrueFalseTotal
if $f->[0]->URI eq $URI 0 0 0
962 if ($f->[0]->URI eq $URI);
963 }
964 }
965 0 my $is_URL = _is_url($URI);
966 0 for my $f (@files) {
967 0 my $f_URI = $f->[0]->URI;
968 0 ▶ return XML::LibXML::NodeList->new($f->[0])
Branch: 1 branch
BranchTrueFalseTotal
if _is_url($f_URI) and not $is_URL and $f_URI eq 'file://' . $URI or not _is_url($f_URI) and $is_URL and $URI eq 'file://' . $f_URI 0 0 0
Condition0%0/15
_is_url($f_URI) and not $is_URL and $f_URI eq 'file://' . $URI or not _is_url($f_URI) and $is_URL and $URI eq 'file://' . $f_URI
l!l&&r!l&&!r
000
_is_url($f_URI) and not $is_URL and $f_URI eq 'file://' . $URI
!ll&&!rl&&r
000
_is_url($f_URI) and not $is_URL
!ll&&!rl&&r
000
not _is_url($f_URI) and $is_URL and $URI eq 'file://' . $f_URI
!ll&&!rl&&r
000
not _is_url($f_URI) and $is_URL
!ll&&!rl&&r
000
MC/DC0%0/6
_is_url($f_URI) and not $is_URL and $f_URI eq 'file://' . $URI or not _is_url($f_URI) and $is_URL and $URI eq 'file://' . $f_URI
_is_url($f_URI) not $is_URL $f_URI eq 'file://' . $URI not _is_url($f_URI) $is_URL $URI eq 'file://' . $f_URI
Truth table0%0/13
A and not(B) and C or not(D) and E and F
A= _is_url($f_URI)B= not $is_URLC= $f_URI eq 'file://' . $URID= not _is_url($f_URI)E= $is_URLF= $URI eq 'file://' . $f_URI
ABCDEFresult
111XXX1
0XX1111
10X1111
1101111
0XX0XX0
0XX10X0
0XX1100
10X0XX0
10X10X0
10X1100
1100XX0
11010X0
1101100
969 if (_is_url($f_URI) and !$is_URL and $f_URI eq 'file://'.$URI
970 or
971 !_is_url($f_URI) and $is_URL and $URI eq 'file://'.$f_URI);
972 }
973  
974 0 return XML::LibXML::NodeList->new();
975 }
976  
977 0 ▶sub XPATH_documents {
Subroutine XPATH_documents: not called CC=3 SCAR=24.8
Pod undocumented
978 0 ▶ die "Wrong number of arguments for function xsh:documents()!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 0 0 0 0
979 if (@_!=0);
980 0 my $res = XML::LibXML::NodeList->new();
981 0 for my $f (_files()) {
982 0 $res->push($f->[0]);
983 }
984 0 return $res;
985 }
986  
987 0 ▶sub XPATH_lookup {
Subroutine XPATH_lookup: not called CC=6 SCAR=34.8
Pod undocumented
988 0 ▶ die "Wrong number of arguments for function xsh:lookup(string,string)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
989 if (@_!=2);
990 0 my $name = shift;
991 0 my $key = shift;
992 0 my $res;
993 0 $name=~s/^\$//;
994 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
995 0 my $lex = lex_var($name);
996 0 ▶ if ($lex) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lex) { } 0 0 0
elsif (defined ${"XML::XSH2::Map::$name";}) { } 0 0 0
997 0 $res = $$lex
998 0 } elsif (defined(${"XML::XSH2::Map::$name"})) {
999 0 $res = ${"XML::XSH2::Map::$name"};
1000 } else {
1001 0 die "xsh:lookup(): variable '\$$name' not defined\n";
1002 }
1003
1004 0 ▶ if (ref($res) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq 'HASH') { } 0 0 0
1005 0 my $val = $res->{to_literal($key)};
1006 0 ▶ if (defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
1007 0 return $val;
1008 } else {
1009 0 return XML::LibXML::NodeList->new();
1010 }
1011 } else {
1012 0 return XML::LibXML::NodeList->new();
1013 }
1014 }
1015  
1016 0 ▶sub XPATH_span {
Subroutine XPATH_span: not called CC=10 SCAR=47.0
Pod undocumented
1017 0 ▶ die "Wrong number of arguments for function xsh:span(node-set,node-set)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
1018 if (@_!=2);
1019 # the first argument is a start node and
1020 # the second is an end node;
1021 # only the first argument is taken from each node-set!
1022 #
1023 # returns span of sibling nodes "between" them (inclusively).
1024 # it is an error if the start and end nodes are not siblings.
1025  
1026 0 my ($start,$end)=@_;
1027 0 for my $nl ($start,$end) {
1028 0 ▶ die "Wrong type of argument in function xsh:span(node-set,node-set)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nl and UNIVERSAL::isa($nl, "XML::LibXML::NodeList") 0 0 0
Condition0%0/3
ref $nl and UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $nl and UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
ref $nl UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
Truth table0%0/3
A and B
A= ref $nlB= UNIVERSAL::isa($nl, "XML::LibXML::NodeList")
ABresult
0X0
100
111
1029 if (!ref($nl) or not UNIVERSAL::isa($nl,"XML::LibXML::NodeList"));
1030 }
1031 0 ($start,$end) = map { $_->[0] } ($start,$end);
1032 0 ▶ if ($start and $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start and $end) { } 0 0 0
Condition0%0/3
$start and $end
!ll&&!rl&&r
000
MC/DC0%0/2
$start and $end
$start $end
Truth table0%0/3
A and B
A= $startB= $end
ABresult
0X0
100
111
1033 0 ▶ if ($start->parentNode->isSameNode($end->parentNode)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start->parentNode->isSameNode($end->parentNode)) { } 0 0 0
1034 0 my @nodes = ();
1035 0 ▶ do {{
Branch: 1 branch
BranchTrueFalseTotal
$start and do { do { { push @nodes, $start; if ($start->isSameNode($end)) { return "XML::LibXML::NodeList"->new_from_ref(\@nodes, 1); } ; $start = $start->nextSibling; } } } 0 0 0
1036 0 push @nodes, $start;
1037 0 ▶ if ($start->isSameNode($end)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start->isSameNode($end)) 0 0 0
1038 0 return XML::LibXML::NodeList->new_from_ref(\@nodes,1);
1039 }
1040 0 $start = $start->nextSibling();
1041 }} while ($start);
1042 0 return XML::LibXML::NodeList->new();
1043 } else {
1044 0 die "Start node and end node are not siblings at xsh:span(node-set,node-set)!\n"
1045 }
1046 } else {
1047 0 return XML::LibXML::NodeList->new();
1048 }
1049 }
1050  
1051 0 ▶sub XPATH_context {
Subroutine XPATH_context: not called CC=6 SCAR=37.4
Pod undocumented
1052 0 ▶ die "Wrong number of arguments for function xsh:context(node-set,float,float)!\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 or @_ > 3 0 0 0
Condition0%0/3
@_ < 2 or @_ > 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2 @_ > 3
Truth table0%0/3
A or B
A= @_ < 2B= @_ > 3
ABresult
1X1
011
000
1053 if (@_<2 or @_>3);
1054 # returns a span of nodes consisting of a given number of nodes
1055 # before the given context node, the context node itself and a given number of nodes
1056 # following the context node
1057  
1058 # $context ... preceding-sibling::node()[position()<$before] | . | following-sibling::node()[position<$after]
1059 0 my ($context,$before,$after)=@_;
1060 0 ▶ die "Wrong type of argument in function xsh:context(node-set,float,float)!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $context and UNIVERSAL::isa($context, "XML::LibXML::NodeList") 0 0 0
Condition0%0/3
ref $context and UNIVERSAL::isa($context, "XML::LibXML::NodeList")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $context and UNIVERSAL::isa($context, "XML::LibXML::NodeList")
ref $context UNIVERSAL::isa($context, "XML::LibXML::NodeList")
Truth table0%0/3
A and B
A= ref $contextB= UNIVERSAL::isa($context, "XML::LibXML::NodeList")
ABresult
0X0
100
111
1061 if (!ref($context) or not UNIVERSAL::isa($context,"XML::LibXML::NodeList"));
1062 0 $before = int($before);
1063 0 ▶ $after = defined ($after) ? $before : int($after);
Branch: 1 branch
BranchTrueFalseTotal
defined $after ? : 0 0 0
1064 0 return scalar($_xpc->findnodes("preceding-sibling::node()[position()<$before] | . | following-sibling::node()[position()<$after]",$context->[0]));
1065 }
1066  
1067 # ===================== END OF XPATH EXT FUNC ================
1068  
1069 0 ▶sub get_flags_as_vars {
Subroutine get_flags_as_vars: not called CC=1 SCAR=0.0
Pod undocumented
1070 13 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
1071 13 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
1072 0 return Data::Dumper->Dump([map eval, @PARAM_VARS],\@PARAM_VARS);
1073 }
1074  
1075 0 ▶sub list_flags {
Subroutine list_flags: not called CC=19 SCAR=56.7
Pod undocumented
1076 0 my ($opts) = @_;
1077 0 $opts = _ev_opts($opts);
1078 0 ▶ if ($opts->{variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'variables'}) { } 0 0 0
1079 13 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
1080 13 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
1081 0 out(get_flags_as_vars());
1082 } else {
1083 0 ▶ out("validation ".(get_validation() or "0").";\n");
Condition0%0/2
get_validation() || '0'
l!l
00
MC/DC0%0/1
get_validation() || '0'
get_validation()
Truth table0%0/2
A
A= get_validation()
Aresult
11
00
1084 0 ▶ out("recovering ".(get_recovering() or "0").";\n");
Condition0%0/2
get_recovering() || '0'
l!l
00
MC/DC0%0/1
get_recovering() || '0'
get_recovering()
Truth table0%0/2
A
A= get_recovering()
Aresult
11
00
1085 0 ▶ out("parser_expands_entities ".(get_expand_entities() or "0").";\n");
Condition0%0/2
get_expand_entities() || '0'
l!l
00
MC/DC0%0/1
get_expand_entities() || '0'
get_expand_entities()
Truth table0%0/2
A
A= get_expand_entities()
Aresult
11
00
1086 0 ▶ out("parser_expands_xinclude ".(get_expand_xinclude() or "0").";\n");
Condition0%0/2
get_expand_xinclude() || '0'
l!l
00
MC/DC0%0/1
get_expand_xinclude() || '0'
get_expand_xinclude()
Truth table0%0/2
A
A= get_expand_xinclude()
Aresult
11
00
1087 0 ▶ out("keep_blanks ".(get_keep_blanks() or "0").";\n");
Condition0%0/2
get_keep_blanks() || '0'
l!l
00
MC/DC0%0/1
get_keep_blanks() || '0'
get_keep_blanks()
Truth table0%0/2
A
A= get_keep_blanks()
Aresult
11
00
1088 0 ▶ out("pedantic_parser ".(get_pedantic_parser() or "0").";\n");
Condition0%0/2
get_pedantic_parser() || '0'
l!l
00
MC/DC0%0/1
get_pedantic_parser() || '0'
get_pedantic_parser()
Truth table0%0/2
A
A= get_pedantic_parser()
Aresult
11
00
1089 0 ▶ out("load_ext_dtd ".(get_load_ext_dtd() or "0").";\n");
Condition0%0/2
get_load_ext_dtd() || '0'
l!l
00
MC/DC0%0/1
get_load_ext_dtd() || '0'
get_load_ext_dtd()
Truth table0%0/2
A
A= get_load_ext_dtd()
Aresult
11
00
1090 0 ▶ out("complete_attributes ".(get_complete_attributes() or "0").";\n");
Condition0%0/2
get_complete_attributes() || '0'
l!l
00
MC/DC0%0/1
get_complete_attributes() || '0'
get_complete_attributes()
Truth table0%0/2
A
A= get_complete_attributes()
Aresult
11
00
1091 0 ▶ out("indent ".(get_indent() or "0").";\n");
Condition0%0/2
get_indent() || '0'
l!l
00
MC/DC0%0/1
get_indent() || '0'
get_indent()
Truth table0%0/2
A
A= get_indent()
Aresult
11
00
1092 0 ▶ out("empty_tags ".(get_empty_tags() or "0").";\n");
Condition0%0/2
get_empty_tags() || '0'
l!l
00
MC/DC0%0/1
get_empty_tags() || '0'
get_empty_tags()
Truth table0%0/2
A
A= get_empty_tags()
Aresult
11
00
1093 0 ▶ out("skip_dtd ".(get_skip_dtd() or "0").";\n");
Condition0%0/2
get_skip_dtd() || '0'
l!l
00
MC/DC0%0/1
get_skip_dtd() || '0'
get_skip_dtd()
Truth table0%0/2
A
A= get_skip_dtd()
Aresult
11
00
1094 0 ▶ out(((get_backups() ? "backups" : "nobackups"),";\n"));
Branch: 1 branch
BranchTrueFalseTotal
get_backups() ? : 0 0 0
1095 0 ▶ out((($QUIET ? "quiet" : "verbose"),";\n"));
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::QUIET ? : 0 0 0
1096 0 ▶ out((($DEBUG ? "debug" : "nodebug"),";\n"));
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::DEBUG ? : 0 0 0
1097 0 ▶ out((($TEST_MODE ? "run-mode" : "test-mode"),";\n"));
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::TEST_MODE ? : 0 0 0
1098 0 ▶ out("switch_to_new_documents ".(get_cdonopen() or "0").";\n");
Condition0%0/2
get_cdonopen() || '0'
l!l
00
MC/DC0%0/1
get_cdonopen() || '0'
get_cdonopen()
Truth table0%0/2
A
A= get_cdonopen()
Aresult
11
00
1099 0 out("encoding '$ENCODING';\n");
1100 0 out("query_encoding '$QUERY_ENCODING';\n");
1101 0 ▶ out("xpath_completion ".(get_xpath_completion() or "0").";\n");
Condition0%0/2
get_xpath_completion() || '0'
l!l
00
MC/DC0%0/1
get_xpath_completion() || '0'
get_xpath_completion()
Truth table0%0/2
A
A= get_xpath_completion()
Aresult
11
00
1102 0 out("xpath_axis_completion \'".get_xpath_axis_completion()."';\n");
1103 }
1104 }
1105  
1106 383 ▶sub toUTF8 {
Subroutine toUTF8: called CC=3 SCAR=11.2
Pod undocumented
1107 # encode/decode from UTF8 returns undef if string not marked as utf8
1108 # by perl (for example ascii)
1109 # return $_[1];
1110 383 my $res=eval { encodeToUTF8($_[0],$_[1]) };
1111 383 ▶ if ($@ =~ /^SIGINT/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^SIGINT/) { } 0 383 383
1112 0 die $@
1113 } else {
1114 383 undef $@;
1115 }
1116 383 ▶ return defined($res) ? $res : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $res ? : 375 8 383
1117 }
1118  
1119 214 ▶sub fromUTF8 {
Subroutine fromUTF8: called CC=3 SCAR=11.2
Pod undocumented
1120 # encode/decode from UTF8 returns undef if string not marked as utf8
1121 # by perl (for example ascii)
1122 # return $_[1];
1123 214 my $res=eval { decodeFromUTF8($_[0],$_[1]) };
1124 214 ▶ if ($@ =~ /^SIGINT/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^SIGINT/) { } 0 214 214
1125 0 die $@
1126 } else {
1127 214 undef $@;
1128 }
1129 214 ▶ return defined($res) ? $res : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $res ? : 206 8 214
1130 }
1131  
1132 # return true if given string is a XSH command name
1133 10 ▶sub is_command {
Subroutine is_command: called CC=3 SCAR=11.1
Pod undocumented
1134 10 my ($test)=@_;
1135 10 foreach my $cmd (@XML::XSH2::CompletionList::XSH_COMMANDS) {
1136 1860 ▶ return 1 if $cmd eq $test;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq $test 0 1860 1860
1137 }
1138 10 return 0;
1139 }
1140  
1141 # set current script name
1142 0 ▶sub xsh_set_script {
Subroutine xsh_set_script: not called CC=1 SCAR=6.9
Pod undocumented
1143 0 $SCRIPT=$_[0];
1144 }
1145  
1146 0 ▶sub benchtime {
Subroutine benchtime: not called CC=1 SCAR=6.9
Pod undocumented
1147 0 my ($t0,$t1)=@_;
1148 0 Benchmark::timestr(Benchmark::timediff($t0,$t1));
1149 }
1150  
1151 # evaluate a XSH command
1152 367 ▶sub xsh {
Subroutine xsh: called CC=8 SCAR=22.1
Pod undocumented
1153 367 ▶ print STDERR "Benchmark: running script $SCRIPT\n" if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 367 367
1154 367 ▶ require Benchmark if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 367 367
1155 367 my ($t0,$t1);
1156 367 ▶ $t0 = Benchmark->new() if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 367 367
1157 367 ▶ unless (ref($_xsh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $XML::XSH2::Functions::_xsh) 8 359 367
1158 8 xsh_init();
1159 8 xsh_rd_parser_init();
1160 }
1161 367 ▶ $t1 = Benchmark->new() if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 367 367
1162 367 ▶ print STDERR "Benchmark: init xsh took:",benchtime($t1,$t0),"\n" if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 367 367
1163 367 ▶ if (ref($_xsh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $XML::XSH2::Functions::_xsh) { } 367 0 367
1164 367 my $code=join "",map toUTF8($QUERY_ENCODING,$_),@_;
1165 367 return run($code);
1166 } else {
1167 0 die "XSH init failed!\n";
1168 }
1169 }
1170  
1171 367 ▶sub run {
Subroutine run: called CC=13 SCAR=27.9
Pod undocumented
1172 367 my ($code) = @_;
1173 367 ▶ return 1 if ($code=~/^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /^\s*$/ 1 366 367
1174 366 ▶ require Benchmark if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1175 366 ▶ my $t0 = Benchmark->new() if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1176 366 my $pt = $_xsh->startrule($code);
1177 366 ▶ my $t1 = Benchmark->new() if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1178 366 ▶ print STDERR "Benchmark: xsh parsing took:",benchtime($t1,$t0),"\n" if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1179  
1180 # __debug "Post processing parse tree";
1181 366 ▶ $t0 = Benchmark->new() if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1182 366 post_process_parse_tree($pt);
1183 366 ▶ $t1 = Benchmark->new() if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1184 366 ▶ print STDERR "Benchmark: compile took:",benchtime($t1,$t0),"\n" if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1185 366 ▶ dump_parse_tree($pt) if defined $DUMP;
Branch: 1 branch
BranchTrueFalseTotal
if defined $XML::XSH2::Functions::DUMP 0 366 366
1186 366 ▶ $t0 = Benchmark->new() if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1187 366 my $result=run_commands($pt,1);
1188 366 ▶ $t1 = Benchmark->new() if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1189 366 ▶ print STDERR "Benchmark: execution took:",benchtime($t1,$t0),"\n" if $BENCHMARK;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::BENCHMARK 0 366 366
1190 366 return $result;
1191 }
1192  
1193 0 ▶sub dump_parse_tree {
Subroutine dump_parse_tree: not called CC=9 SCAR=43.3
Pod undocumented
1194 0 my ($pt) = shift;
1195 13 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
1196 0 local $Data::Dumper::Purity=1;
1197
1198 0 my $dump = '';
1199  
1200 0 ▶ unless ($DUMP_APPEND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::DUMP_APPEND) { } 0 0 0
1201 0 $dump .= <<"EOS";
1202 require XML::XSH2;
1203 XML::XSH2::Functions->VERSION( '$VERSION' );
1204  
1205 EOS
1206  
1207 0 $dump .= <<'EOS';
1208 {
1209 package XML::XSH2::Functions;
1210  
1211 # initialize context
1212 xsh_init();
1213  
1214 # prepare ARGV
1215 @XML::XSH2::Map::ARGV=@ARGV;
1216 for (@ARGV,@XML::XSH2::Map::ARGV) {
1217 $_=toUTF8($QUERY_ENCODING,$_)
1218 }
1219 # XPath variant of perlish {@ARGV} ($ARGV[1] is the first arg though)
1220 $XML::XSH2::Map::ARGV = XML::LibXML::NodeList->new(
1221 map { cast_value_to_objects($_) } @XML::XSH2::Map::ARGV
1222 );
1223  
1224 EOS
1225 {
1226 0 local $TEST_MODE;
1227 0 $dump.= get_flags_as_vars();
1228 }
1229 } else {
1230 0 $dump.=<<'EOS'
1231 {
1232 package XML::XSH2::Functions;
1233 EOS
1234 }
1235 0 $dump.= "# script $SCRIPT\n\n";
1236 0 local $Data::Dumper::Deparse=1;
1237 0 $dump.= Data::Dumper->Dump([$pt],['parse_tree']);
1238 0 $dump.= "\nXML::XSH2::Functions::run_commands(\$parse_tree);\n";
1239 0 $dump.= "};\n\n";
1240 0 ▶ if (ref($DUMP) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $XML::XSH2::Functions::DUMP eq 'SCALAR') { } 0 0 0
1241 0 ▶ if ($DUMP_APPEND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::DUMP_APPEND) { } 0 0 0
1242 0 $$DUMP.=$dump;
1243 } else {
1244 0 $$DUMP=$dump;
1245 }
1246 } else {
1247 0 ▶ print STDERR "Saving compiled '$SCRIPT' to '$DUMP'\n" unless $QUIET;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::XSH2::Functions::QUIET 0 0 0
1248 0 ▶ open my $f, ($DUMP_APPEND ? '>>' : '>'), $DUMP || die "Can't dump parse tree to '$DUMP': $!";
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::DUMP_APPEND ? : 0 0 0
Condition0%0/2
$XML::XSH2::Functions::DUMP || die("Can't dump parse tree to '$XML::XSH2::Functions::DUMP': $!")
l!l
00
MC/DC0%0/1
$XML::XSH2::Functions::DUMP || die("Can't dump parse tree to '$XML::XSH2::Functions::DUMP': $!")
$XML::XSH2::Functions::DUMP
Truth table0%0/2
A
A= $XML::XSH2::Functions::DUMP
Aresult
11
00
1249 0 require Config;
1250 0 ▶ print {$f} "$Config::Config{startperl}\n" unless $DUMP_APPEND;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::XSH2::Functions::DUMP_APPEND 0 0 0
1251 0 print {$f} $dump;
1252 0 close $f;
1253 0 ▶ chmod 0755, $DUMP unless $DUMP_APPEND;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::XSH2::Functions::DUMP_APPEND 0 0 0
1254 }
1255 0 $DUMP_APPEND = 1;
1256 }
1257  
1258 6007 ▶sub post_process_parse_tree {
Subroutine post_process_parse_tree: called CC=20 SCAR=30.4
Pod undocumented
1259 6007 my ($pt)=@_;
1260 # __debug "[ $pt";
1261 6007 ▶ if (ref($pt) eq 'XML::XSH2::Command') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pt eq 'XML::XSH2::Command') { } 669 5338 6007
elsif (ref $pt eq 'ARRAY') { } 1439 3899 5338
1262 # __debug "COMMAND: @$pt\n";
1263 669 my ($line,$column,$offset,$script,$cmd,@args)=@$pt;
1264 669 ▶ unless (ref($cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $cmd) 669 0 669
1265 669 my $spec = $COMMANDS{$cmd};
1266 669 ▶ $spec = $COMMANDS{$spec} if ($spec and !ref($spec));
Branch: 1 branch
BranchTrueFalseTotal
if $spec and not ref $spec 16 653 669
Condition100%3/3
$spec and not ref $spec
!ll&&!rl&&r
12952416
MC/DC100%2/2
$spec and not ref $spec
$spec not ref $spec
Truth table100%3/3
A and not(B)
A= $specB= not ref $spec
ABresult
0X0
100
111
1267 669 ▶ if ($spec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec) { } 540 129 669
1268 540 my ($func,$minargs,$maxargs,$optspec,@extraargs)=@$spec;
1269 540 my @opts;
1270 # print STDERR ("matched $cmd\n");
1271 540 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 540 35 575
1272 540 ▶ if ($optspec and $args[0]=~/^--(.*)$|^:(.)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optspec and $args[0] =~ /^--(.*)$|^:(.)$/) { } 35 505 540
Condition100%3/3
$optspec and $args[0] =~ /^--(.*)$|^:(.)$/
!ll&&!rl&&r
35515035
MC/DC100%2/2
$optspec and $args[0] =~ /^--(.*)$|^:(.)$/
$optspec $args[0] =~ /^--(.*)$|^:(.)$/
Truth table100%3/3
A and B
A= $optspecB= $args[0] =~ /^--(.*)$|^:(.)$/
ABresult
0X0
100
111
1273 35 ▶ my $opt = defined($1) ? $1 : $optspec->{$2}; # resolve short opt
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 27 8 35
1274 35 ▶ if (exists($optspec->{$opt})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $optspec->{$opt}) { } 35 0 35
1275 35 shift(@args);
1276 35 ▶ if ($optspec->{$opt} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($optspec->{$opt} ne '') { } 22 13 35
1277 22 push @opts, $optspec->{$opt}.'/'.$opt;
1278 22 push @opts,shift(@args);
1279 } else {
1280 13 push @opts, '/'.$opt,1;
1281 }
1282 } else {
1283 0 die "$script: Compile error: unknown option '$args[0]' for command '$cmd' at line $line column $column offset $offset\n";
1284 }
1285 } else {
1286 505 last;
1287 }
1288 }
1289 540 ▶ if (@args<$minargs) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args < $minargs) { } 0 540 540
elsif (defined $maxargs and @args > $maxargs) { } 0 629 629
1290 0 die "$script: Compile error: too few arguments (have ",$#args+1,", expect $minargs) for command '$cmd' at line $line column $column offset $offset\n";
1291 ▶ } elsif (defined($maxargs) and @args>$maxargs) {
Condition66%2/3
defined $maxargs and @args > $maxargs
!ll&&!rl&&r
894510
MC/DC0%0/2
defined $maxargs and @args > $maxargs
defined $maxargs @args > $maxargs
Truth table66%2/3
A and B
A= defined $maxargsB= @args > $maxargs
ABresult
0X0
100
111
1292 0 die "$script: Compile error: too many arguments (have ",$#args+1,", expect $maxargs) for command '$cmd' at line $line column $column offset $offset\n";
1293 }
1294 540 foreach (@args) {
1295 725 ▶ if (ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "ARRAY") 1 724 725
1296 1 post_process_parse_tree($_);
1297 }
1298 }
1299 540 @$pt=($line,$column,$offset,$script,$func,\@opts,@args,@extraargs);
1300 } else {
1301 # print STDERR ("$cmd is a sub call?\n");
1302 129 @$pt=($line,$column,$offset,$script,'call',{},1,$cmd,@args);
1303 }
1304 }
1305 } elsif (ref($pt) eq 'ARRAY') {
1306 # __debug "Processing array @$pt\n";
1307 1439 ▶ for(my $i=0;$i<=$#$pt;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $pt->$#*) 5432 1439 6871
1308 5432 ▶ post_process_parse_tree($pt->[$i]) if ref($pt);
Branch: 1 branch
BranchTrueFalseTotal
if ref $pt 5432 0 5432
1309 }
1310 }
1311 # __debug "]";
1312 }
1313  
1314 # setup output stream
1315 0 ▶sub xsh_set_output {
Subroutine xsh_set_output: not called CC=2 SCAR=17.9
Pod undocumented
1316 0 $OUT=$_[0];
1317 0 binmode $OUT;
1318 0 binmode $OUT, ':bytes';
1319 0 ▶ open STDOUT, ">&", $OUT or warn "Can't dup STDOUT\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $XML::XSH2::Functions::OUT or warn "Can't dup STDOUT\n" 0 0 0
1320 0 select $OUT;
1321 0 return 1;
1322 }
1323  
1324 # get output stream
1325 0 ▶sub xsh_get_output {
Subroutine xsh_get_output: not called CC=1 SCAR=6.9
Pod undocumented
1326 0 return $OUT;
1327 }
1328  
1329 220 ▶sub cast_objects_to_values {
Subroutine cast_objects_to_values: called CC=4 SCAR=13.9
Pod undocumented
1330 return map {
1331 282 ▶ if (ref($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 74 208 282
1332 74 ▶ UNIVERSAL::can($_,'textContent') ? $_->textContent() : $_->value();
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($_, 'textContent') ? : 74 0 74
1333 208 } else { $_ }
1334 } map {
1335 220 ▶ UNIVERSAL::isa($_,'XML::LibXML::NodeList') ? @$_ : $_;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, 'XML::LibXML::NodeList') ? : 37 208 245
1336 } @_;
1337 }
1338  
1339 12 ▶sub create_xsh_result_element {
Subroutine create_xsh_result_element: called CC=1 SCAR=0.0
Pod undocumented
1340 12 my $res_doc=XML::LibXML::Document->new();
1341 12 my $res_el=$res_doc->createElementNS($XML::XSH2::xshNS,'xsh:result');
1342 12 $res_doc->setDocumentElement($res_el);
1343 12 return $res_el;
1344 }
1345  
1346 286 ▶sub cast_value_to_objects {
Subroutine cast_value_to_objects: called CC=12 SCAR=27.8
Pod undocumented
1347 286 my ($val, $res_el, $wrap)=@_;
1348 286 ▶ if (UNIVERSAL::isa($val,"XML::LibXML::NodeList")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, 'XML::LibXML::NodeList')) { } 266 20 286
elsif (UNIVERSAL::isa($val, 'XML::LibXML::Node')) { } 8 12 20
1349 266 return @$val;
1350 } elsif (UNIVERSAL::isa($val,"XML::LibXML::Node")) {
1351 8 return ($val);
1352 }
1353 12 ▶ $res_el = create_xsh_result_element() unless (ref($res_el));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res_el 12 0 12
1354 12 my $res_doc = $res_el->ownerDocument;
1355 12 my $el;
1356 my $res;
1357 12 ▶ if (!ref($val)) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $val) { } 7 5 12
elsif (UNIVERSAL::isa($val, 'XML::LibXML::Literal')) { } 4 1 5
elsif (UNIVERSAL::isa($val, 'XML::LibXML::Boolean')) { } 0 1 1
elsif (UNIVERSAL::isa($val, 'XML::LibXML::Number')) { } 1 0 1
elsif (ref $val eq 'ARRAY') { } 0 0 0
1358 7 ▶ if ($val =~ /^\s*[+-]?(\d+(\.\d*)?|\.\d+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^\s*[+-]?(\d+(\.\d*)?|\.\d+)\s*$/) { } 6 1 7
1359 6 $el = $res_doc->createElementNS($XML::XSH2::xshNS,'xsh:number');
1360 } else {
1361 1 $el = $res_doc->createElementNS($XML::XSH2::xshNS,'xsh:string');
1362 }
1363 7 $el->appendText($val);
1364 7 $res = $el->firstChild;
1365 } elsif (UNIVERSAL::isa($val,"XML::LibXML::Literal")) {
1366 4 $el = $res_doc->createElementNS($XML::XSH2::xshNS,'xsh:string');
1367 4 $el->appendText($val->value);
1368 4 $res = $el->firstChild;
1369 } elsif (UNIVERSAL::isa($val,"XML::LibXML::Boolean")) {
1370 0 $el = $res_doc->createElementNS($XML::XSH2::xshNS,'xsh:boolean');
1371 0 ▶ $el->appendText($val ? 'true' : '');
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
1372 0 $res = $el->firstChild;
1373 } elsif (UNIVERSAL::isa($val,"XML::LibXML::Number")) {
1374 1 $el = $res_doc->createElementNS($XML::XSH2::xshNS,'xsh:number');
1375 1 $el->appendText($val->value);
1376 1 $res = $el->firstChild;
1377 } elsif (ref($val) eq 'ARRAY') {
1378 0 $el = $res_doc->createElementNS($XML::XSH2::xshNS,'xsh:array');
1379 0 map { cast_value_to_objects($_,$el) } @$val;
1380 0 $res=$el;
1381 } else {
1382 0 die("don't know how to cast object '$val' to nodeset");
1383 0 return ();
1384 }
1385 12 $res_el->appendChild($el);
1386 12 ▶ return $wrap ? $el : $res;
Branch: 1 branch
BranchTrueFalseTotal
$wrap ? : 5 7 12
1387 }
1388  
1389 2 ▶sub expr {
Subroutine expr: called CC=1 SCAR=0.0
Pod undocumented
1390 2 my $opts = shift;
1391 2 &_ev;
1392 }
1393  
1394 # evaluate given XPath or Perl expression
1395 1532 ▶sub _ev {
Subroutine _ev: called CC=15 SCAR=32.1
1396 1532 my ($exp,$map,$in_place)=@_;
1397 1532 ▶ return undef unless defined $exp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exp 3 1529 1532
1398 1529 ▶ utf8::upgrade($exp) unless ref($exp);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $exp 1526 3 1529
1399 1529 ▶ if (ref($exp) eq 'ARRAY') {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $exp eq 'ARRAY') { } 3 1526 1529
elsif ($exp =~ /^<<(.)/) { } 1 1525 1526
elsif ($exp =~ /^(?:\d*\.\d+|\d+)$/) { } 81 1444 1525
elsif ($exp =~ /^{/) { } 89 1355 1444
elsif ($exp eq '') { } 0 1355 1355
1400 3 return run_commands($exp,0,1);
1401 } elsif ($exp =~ /^<<(.)/) {
1402 # inline document
1403 1 ▶ if ($1 eq "{") {
Branch: 3 branches
BranchTrueFalseTotal
if ($1 eq '{') { } 0 1 1
elsif ($1 eq '(') { } 0 1 1
elsif ($1 eq q[']) { } 0 1 1
1404 0 return perl_eval($',$map,$in_place);
1405 } elsif ($1 eq "(") {
1406 0 my $ret = eval { $_xpc->find(_expand($')); };
1407 0 _check_err($@,1,1);
1408 0 ▶ if (ref($ret) and UNIVERSAL::isa($ret,'XML::LibXML::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret and UNIVERSAL::isa($ret, 'XML::LibXML::Literal')) { } 0 0 0
Condition0%0/3
ref $ret and UNIVERSAL::isa($ret, "XML::LibXML::Literal")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ret and UNIVERSAL::isa($ret, "XML::LibXML::Literal")
ref $ret UNIVERSAL::isa($ret, "XML::LibXML::Literal")
Truth table0%0/3
A and B
A= ref $retB= UNIVERSAL::isa($ret, "XML::LibXML::Literal")
ABresult
0X0
100
111
1409 0 return $ret->value;
1410 } else {
1411 0 return $ret;
1412 }
1413 } elsif ($1 eq "'") {
1414 0 return $';
1415 } else {
1416 1 return _expand($');
1417 }
1418 } elsif ($exp =~ /^(?:\d*\.\d+|\d+)$/) { # a number/float literal
1419 81 return 0+$exp;
1420 } elsif ($exp =~ /^{/) { # a perl expression
1421 89 return perl_eval($exp,$map,$in_place);
1422 } elsif ($exp eq "") { # empty
1423 0 return "";
1424 } else { # an xpath expression
1425 1355 my $ret = eval { $_xpc->find(_expand($exp)); };
1426 1355 _check_err($@,1,1);
1427 1355 ▶ if (ref($ret) and UNIVERSAL::isa($ret,'XML::LibXML::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret and UNIVERSAL::isa($ret, 'XML::LibXML::Literal')) { } 624 731 1355
Condition66%2/3
ref $ret and UNIVERSAL::isa($ret, "XML::LibXML::Literal")
!ll&&!rl&&r
0731624
MC/DC50%1/2
ref $ret and UNIVERSAL::isa($ret, "XML::LibXML::Literal")
ref $ret UNIVERSAL::isa($ret, "XML::LibXML::Literal")
Truth table66%2/3
A and B
A= ref $retB= UNIVERSAL::isa($ret, "XML::LibXML::Literal")
ABresult
0X0
100
111
1428 624 return $ret->value;
1429 } else {
1430 731 return $ret;
1431 }
1432 }
1433 }
1434  
1435 # Evaluate given XPath or Perl expression to a node-list. Evaluate the
1436 # expression using _ev. If the result is a plain scalar string, it is
1437 # re-evaluated as XPath. If the result is a node or node-list object
1438 # or a perl array containing nodes, it is cast to node-list and
1439 # returned. Otherwise an error is reported.
1440  
1441 267 ▶sub _ev_nodelist {
Subroutine _ev_nodelist: called CC=2 SCAR=6.9
1442 267 my @res = map { cast_value_to_objects($_) } _ev($_[0]);
1443 267 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 13 254 267
1444 13 return @res;
1445 } else {
1446 254 XML::LibXML::NodeList->new(@res);
1447 }
1448 }
1449  
1450 1 ▶sub _ev_list {
Subroutine _ev_list: called CC=7 SCAR=33.2
1451 1 my $exp = $_[0];
1452 1 ▶ if (ref($exp) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exp eq "ARRAY") 0 1 1
1453 0 $exp = run_commands($exp, 0, 1);
1454 1 ▶ } if ($exp =~ /^<<(.)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exp =~ /^<<(.)/) { } 0 1 1
elsif ($exp =~ /^{/) { } 1 0 1
1455 # inline document
1456 0 ▶ if ($1 eq "{") {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '{') { } 0 0 0
1457 0 return perl_eval($');
1458 } else {
1459 0 $exp = $';
1460 }
1461 } elsif ($exp =~ /^{/) {
1462 1 return (perl_eval($exp));
1463 } else {
1464 0 $exp = _expand($exp);
1465 }
1466 0 my $val = eval { $_xpc->find($exp); };
1467 0 _check_err($@,1,1);
1468 0 ▶ if (UNIVERSAL::isa($val,"XML::LibXML::NodeList")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, 'XML::LibXML::NodeList')) { } 0 0 0
elsif (UNIVERSAL::isa($val, 'XML::LibXML::Node')) { } 0 0 0
1469 0 return @$val;
1470 } elsif (UNIVERSAL::isa($val,"XML::LibXML::Node")) {
1471 0 return ($val);
1472 }
1473  
1474 0 return ();
1475 }
1476  
1477  
1478 # evaluate given XPath or Perl expression and return the text content
1479 # of the result
1480 254 ▶sub _ev_literal {
Subroutine _ev_literal: called CC=2 SCAR=6.9
1481 254 my ($exp,$map,$in_place)=@_;
1482 254 ▶ return "" if $exp eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $exp eq "" 28 226 254
1483 226 my $val = _ev($exp,$map,$in_place);
1484 226 return to_literal($val);
1485 }
1486  
1487 242 ▶sub to_literal {
Subroutine to_literal: called CC=9 SCAR=30.5
Pod undocumented
1488 242 my ($obj)=@_;
1489 242 ▶ if (!ref($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $obj) { } 211 31 242
1490 211 my $r=$obj;
1491 211 utf8::upgrade($r);
1492 211 return $r;
1493 } else {
1494 31 ▶ if (UNIVERSAL::isa($obj,'XML::LibXML::NodeList')) {
Branch: 5 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($obj, 'XML::LibXML::NodeList')) { } 0 31 31
elsif (UNIVERSAL::isa($obj, 'XML::LibXML::Element')) { } 8 23 31
elsif (UNIVERSAL::isa($obj, 'XML::LibXML::Attr')) { } 0 23 23
elsif (UNIVERSAL::isa($obj, 'XML::LibXML::Node')) { } 0 23 23
elsif (ref($obj) =~ /^XML::LibXML/) { } 23 0 23
1495 0 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif ($obj->[0]) { } 0 0 0
1496 0 return map { literal_value($_->to_literal) } @$obj;
1497 } elsif ($obj->[0]) {
1498 0 return literal_value($obj->[0]->to_literal);
1499 } else {
1500 0 return '';
1501 }
1502 } elsif (UNIVERSAL::isa($obj,'XML::LibXML::Element')) {
1503 8 return $obj->textContent();
1504 } elsif (UNIVERSAL::isa($obj,'XML::LibXML::Attr')) {
1505 0 return $obj->getValue();
1506 } elsif (UNIVERSAL::isa($obj,'XML::LibXML::Node')) {
1507 0 return $obj->getData();
1508 } elsif(ref($obj)=~/^XML::LibXML/) {
1509 23 return literal_value($obj);
1510 } else {
1511 0 my $r = "$obj";
1512 0 utf8::upgrade($r);
1513 0 return $r;
1514 }
1515 }
1516 }
1517  
1518 # evaluate given XPath or Perl expression and return the text content
1519 # of the result
1520 318 ▶sub _ev_count {
Subroutine _ev_count: called CC=6 SCAR=18.5
1521 318 my ($exp)=@_;
1522 318 ▶ return "" if $exp eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $exp eq "" 0 318 318
1523 318 my $result = _ev($exp);
1524 318 ▶ if (!ref($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $result) { } 50 268 318
1525 50 return $result;
1526 } else {
1527 268 ▶ if (UNIVERSAL::isa($result,'XML::LibXML::NodeList')) {
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($result, 'XML::LibXML::NodeList')) { } 61 207 268
elsif (UNIVERSAL::isa($result, 'XML::LibXML::Node')) { } 0 207 207
elsif (ref($result) =~ /^XML::LibXML/) { } 207 0 207
1528 61 return $result->size();
1529 } elsif (UNIVERSAL::isa($result,'XML::LibXML::Node')) {
1530 0 return 1;
1531 } elsif(ref($result)=~/^XML::LibXML/) {
1532 207 return literal_value($result);
1533 }
1534 }
1535 }
1536  
1537  
1538 # evaluate given expression to obtain a node-set, take
1539 # and return the owner document of the first node
1540  
1541 5 ▶sub _ev_doc {
Subroutine _ev_doc: called CC=3 SCAR=11.2
1542 5 my ($exp)=@_;
1543 5 ▶ $exp = "." if $exp eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $exp eq "" 3 2 5
1544 5 my ($node)=_ev_nodelist($exp)->pop();
1545 5 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 5 0 5
1546 5 return $_xml_module->owner_document($node);
1547 } else {
1548 0 _warn("Expression '$exp' returns no node");
1549 }
1550 }
1551  
1552 0 ▶sub _doc {
Subroutine _doc: not called CC=2 SCAR=17.9
1553 0 my ($obj)=@_;
1554 0 my ($node) = map { cast_value_to_objects($_) } $obj;
1555 0 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 0 0 0
1556 0 return $_xml_module->owner_document($node);
1557 } else {
1558 0 _warn("Cannot cast object to a document");
1559 }
1560 }
1561  
1562 247 ▶sub _ev_string {
Subroutine _ev_string: called CC=2 SCAR=6.9
1563 247 my ($exp,$map,$in_place)=@_;
1564 247 ▶ if ($exp=~/^\s*(?:&|<<|{)|[\'\"\[\]\(\)\@]|::|\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp =~ /^\s*(?:&|<<|{)|[\'\"\[\]\(\)\@]|::|\$/) { } 140 107 247
1565 140 return _ev_literal($exp,$map,$in_place);
1566 } else {
1567 107 return _expand($exp);
1568 }
1569 }
1570  
1571 53 ▶sub xsh_parse_string {
Subroutine xsh_parse_string: called CC=6 SCAR=21.6
Pod undocumented
1572 53 ▶ my $format=$_[1] || $DEFAULT_FORMAT;
Condition66%2/3
$_[1] || $XML::XSH2::Functions::DEFAULT_FORMAT
l!l&&r!l&&!r
4670
MC/DC0%0/2
$_[1] || $XML::XSH2::Functions::DEFAULT_FORMAT
$_[1] $XML::XSH2::Functions::DEFAULT_FORMAT
Truth table66%2/3
A || B
A= $_[1]B= $XML::XSH2::Functions::DEFAULT_FORMAT
ABresult
1X1
011
000
1573 53 local $VALIDATION=0;
1574 53 ▶ if ($format eq 'xml') {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'xml') { } 53 0 53
elsif ($format eq 'html') { } 0 0 0
elsif ($format eq 'docbook') { } 0 0 0
1575 53 my $xmldecl;
1576 53 ▶ $xmldecl="<?xml version='1.0' encoding='utf-8'?>" unless $_[0]=~/^\s*\<\?xml /;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\s*\<\?xml / 52 1 53
1577 53 return $_xml_module->parse_string($_parser,$xmldecl.$_[0]);
1578 } elsif ($format eq 'html') {
1579 0 return $_xml_module->parse_html_string($_parser,$_[0]);
1580 } elsif ($format eq 'docbook') {
1581 0 return $_xml_module->parse_sgml_string($_parser,$_[0]);
1582 }
1583 }
1584  
1585 0 ▶sub xsh_xml_parser {
Subroutine xsh_xml_parser: not called CC=2 SCAR=17.9
Pod undocumented
1586 0 ▶ xsh_init() unless ref($_parser);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $XML::XSH2::Functions::_parser 0 0 0
1587 0 return $_parser;
1588 }
1589  
1590 # store a pointer to an XSH-Grammar parser
1591 0 ▶sub xsh_set_parser {
Subroutine xsh_set_parser: not called CC=1 SCAR=6.9
Pod undocumented
1592 0 $_xsh=$_[0];
1593 0 return 1;
1594 }
1595  
1596 # print version info
1597 0 ▶sub print_version {
Subroutine print_version: not called CC=3 SCAR=24.8
Pod undocumented
1598 0 my $opts = shift;
1599 0 out("Main program: $::VERSION $::REVISION\n");
1600 0 out("XML::XSH2::Functions: $VERSION $REVISION\n");
1601 0 out("XML::LibXML: $XML::LibXML::VERSION\n");
1602 # out($_xml_module->module(),"\t",$_xml_module->version(),"\n");
1603 0 ▶ out("XML::LibXSLT $XML::LibXSLT::VERSION\n")
Branch: 1 branch
BranchTrueFalseTotal
if defined $XML::LibXSLT::VERSION 0 0 0
1604 if defined($XML::LibXSLT::VERSION);
1605 0 ▶ out("XML::LibXML::XPathContext $XML::LibXML::XPathContext::VERSION\n")
Branch: 1 branch
BranchTrueFalseTotal
if defined $XML::LibXML::XPathContext::VERSION 0 0 0
1606 if defined($XML::LibXML::XPathContext::VERSION);
1607 0 return 1;
1608 }
1609  
1610 # print a list of all open files
1611 1 ▶sub _files {
Subroutine _files: called CC=9 SCAR=22.7
1612 # out(map { "$_ = $_files{$_}\n" } sort keys %_files);
1613 1 my @ret;
1614 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1615 my %seen;
1616 1 foreach my $var (keys %{"XML::XSH2::Map::"}) {
1617 53 my $value = ${"XML::XSH2::Map::".$var};
1618 53 ▶ if (ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 4 49 53
1619 4 ▶ $value = $value->[0] if (UNIVERSAL::isa($value,'XML::LibXML::NodeList')
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($value, "XML::LibXML::NodeList") and $value->size == 1 0 4 4
Condition33%1/3
UNIVERSAL::isa($value, "XML::LibXML::NodeList") and $value->size == 1
!ll&&!rl&&r
400
MC/DC0%0/2
UNIVERSAL::isa($value, "XML::LibXML::NodeList") and $value->size == 1
UNIVERSAL::isa($value, "XML::LibXML::NodeList") $value->size == 1
Truth table33%1/3
A and B
A= UNIVERSAL::isa($value, "XML::LibXML::NodeList")B= $value->size == 1
ABresult
0X0
100
111
1620 and $value->size()==1);
1621 4 ▶ if (UNIVERSAL::isa($value,'XML::LibXML::Node') and
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "XML::LibXML::Node") and $XML::XSH2::Functions::_xml_module->is_document($value) and not exists $seen{$$value}) 2 2 4
Condition66%4/6
UNIVERSAL::isa($value, "XML::LibXML::Node") and $XML::XSH2::Functions::_xml_module->is_document($value) and not exists $seen{$$value}
!ll&&!rl&&r
202
UNIVERSAL::isa($value, "XML::LibXML::Node") and $XML::XSH2::Functions::_xml_module->is_document($value)
!ll&&!rl&&r
202
MC/DC33%1/3
UNIVERSAL::isa($value, "XML::LibXML::Node") and $XML::XSH2::Functions::_xml_module->is_document($value) and not exists $seen{$$value}
UNIVERSAL::isa($value, "XML::LibXML::Node") $XML::XSH2::Functions::_xml_module->is_document($value) not exists $seen{$$value}
Truth table50%2/4
A and B and not(C)
A= UNIVERSAL::isa($value, "XML::LibXML::Node")B= $XML::XSH2::Functions::_xml_module->is_document($value)C= not exists $seen{$$value}
ABCresult
0XX0
10X0
1100
1111
1622 $_xml_module->is_document($value) and !exists($seen{$$value})) {
1623 2 push @ret, [$value, $var];
1624 2 $seen{$$value}=undef;
1625 }
1626 }
1627 }
1628 1 my $cur_doc = $_xml_module->owner_document(xsh_context_node());
1629 1 ▶ if (!exists($seen{$$cur_doc})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $seen{$$cur_doc}) 0 1 1
1630 0 push @ret, [$cur_doc, undef];
1631 }
1632 1 return @ret;
1633 }
1634  
1635  
1636 1 ▶sub files {
Subroutine files: called CC=4 SCAR=14.8
Pod undocumented
1637 1 my $opts = shift;
1638 1 for my $f (_files) {
1639 2 ▶ out((defined($f->[1]) ? "\$".$f->[1] . " := " : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $f->[1] ? : 2 0 2
$XML::XSH2::Functions::_xml_module->document_type($f->[0]) eq 'html' ? : 0 2 2
1640 'open ',
1641 ($_xml_module->document_type($f->[0]) eq 'html' ?
1642 '--format html ' : ()),
1643 "'".$f->[0]->URI()."';\n");
1644 }
1645 }
1646  
1647  
1648 34 ▶sub close_undef_value {
Subroutine close_undef_value: called CC=6 SCAR=19.6
Pod undocumented
1649 34 my ($doc,$value)=@_;
1650 34 ▶ if (ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 6 28 34
1651 6 ▶ if (UNIVERSAL::isa($value,'XML::LibXML::NodeList')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, 'XML::LibXML::NodeList')) { } 0 6 6
elsif (UNIVERSAL::isa($value, 'XML::LibXML::Node') and $doc->isSameNode($XML::XSH2::Functions::_xml_module->owner_document($value))) { } 1 7 8
1652 @$value = grep
1653 0 ▶ {eval { $doc->isSameNode($_xml_module->owner_document($_)) ? 0 : 1}}
Branch: 1 branch
BranchTrueFalseTotal
$doc->isSameNode($XML::XSH2::Functions::_xml_module->owner_document($_)) ? : 0 0 0
1654 @$value;
1655 ▶ } elsif (UNIVERSAL::isa($value,'XML::LibXML::Node')
Condition100%3/3
UNIVERSAL::isa($value, "XML::LibXML::Node") and $doc->isSameNode($XML::XSH2::Functions::_xml_module->owner_document($value))
!ll&&!rl&&r
231
MC/DC100%2/2
UNIVERSAL::isa($value, "XML::LibXML::Node") and $doc->isSameNode($XML::XSH2::Functions::_xml_module->owner_document($value))
UNIVERSAL::isa($value, "XML::LibXML::Node") $doc->isSameNode($XML::XSH2::Functions::_xml_module->owner_document($value))
Truth table100%3/3
A and B
A= UNIVERSAL::isa($value, "XML::LibXML::Node")B= $doc->isSameNode($XML::XSH2::Functions::_xml_module->owner_document($value))
ABresult
0X0
100
111
1656 and $doc->isSameNode($_xml_module->owner_document($value))) {
1657 1 undef $value;
1658 }
1659 }
1660 34 return $value;
1661 }
1662  
1663 1 ▶sub close_doc {
Subroutine close_doc: called CC=8 SCAR=22.8
Pod undocumented
1664 1 my ($opts,$exp)=@_;
1665 1 my $doc = _ev_doc($exp);
1666 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1667 1 foreach my $var (keys %{"XML::XSH2::Map::"}) {
1668 55 my $value = ${"XML::XSH2::Map::".$var};
1669 55 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 21 34 55
1670 34 ▶ undef ${"XML::XSH2::Map::".$var}
Branch: 1 branch
BranchTrueFalseTotal
unless defined close_undef_value($doc, $value) 1 33 34
1671 unless defined(close_undef_value($doc,$value));
1672 }
1673 1 foreach my $lex_context (@$lexical_variables) {
1674 1 my ($name,$value);
1675 1 ▶ while (($name,$value) = each %$lex_context) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$lex_context) 0 1 1
1676 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
1677 0 $value = close_undef_value($doc,$value);
1678 0 ▶ unless (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
1679 0 $lex_context->{$name} = undef
1680 }
1681 }
1682 }
1683 1 return 1;
1684 }
1685  
1686 334 ▶sub xpath_var_lookup {
Subroutine xpath_var_lookup: called CC=7 SCAR=20.5
Pod undocumented
1687 334 my ($data,$name,$ns)=@_;
1688 14 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1689 334 my $res;
1690 334 ▶ if ($ns ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns ne '') { } 0 334 334
1691 0 $res = $XML::XSH2::Map::NAMESPACED_VARIABLES{$ns." ".$name};
1692 0 ▶ unless (defined ($res)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
1693 0 die "Undefined variable '\%$name' in namespace `$ns'\n";
1694 }
1695 } else {
1696 334 my $lex = lex_var($name);
1697 334 ▶ if ($lex) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lex) { } 37 297 334
elsif (defined ${"XML::XSH2::Map::$name";}) { } 297 0 297
1698 37 $res = $$lex
1699 297 } elsif (defined(${"XML::XSH2::Map::$name"})) {
1700 297 $res = ${"XML::XSH2::Map::$name"};
1701 } else {
1702 0 die "Undefined variable '\$$name'\n";
1703 }
1704 }
1705 334 ▶ if (ref($res) and UNIVERSAL::isa($res,'XML::LibXML::Node')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res and UNIVERSAL::isa($res, 'XML::LibXML::Node')) { } 26 308 334
Condition100%3/3
ref $res and UNIVERSAL::isa($res, "XML::LibXML::Node")
!ll&&!rl&&r
16913926
MC/DC100%2/2
ref $res and UNIVERSAL::isa($res, "XML::LibXML::Node")
ref $res UNIVERSAL::isa($res, "XML::LibXML::Node")
Truth table100%3/3
A and B
A= ref $resB= UNIVERSAL::isa($res, "XML::LibXML::Node")
ABresult
0X0
100
111
1706 26 return XML::LibXML::NodeList->new($res);
1707 } else {
1708 308 return $res;
1709 }
1710 }
1711  
1712 1165 ▶sub lex_var {
Subroutine lex_var: called CC=3 SCAR=11.0
Pod undocumented
1713 1165 my ($n)=@_;
1714 1165 ▶ for (my $i=$#$lexical_variables; $i>=0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 2293 693 2986
1715 2293 ▶ return \$lexical_variables->[$i]{$n} if exists($lexical_variables->[$i]{$n});
Branch: 1 branch
BranchTrueFalseTotal
if exists $XML::XSH2::Functions::lexical_variables->[$i]{$n} 472 1821 2293
1716 }
1717 693 return undef;
1718 }
1719  
1720 # return a value of the given XSH variable
1721 91 ▶sub var_value {
Subroutine var_value: called CC=6 SCAR=19.7
Pod undocumented
1722 91 my ($var) = @_;
1723 91 ▶ if ($var=~/^\$(\$.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($var =~ /^\$(\$.*)/) { } 0 91 91
elsif ($var =~ /^\$?(.*)/) { } 91 0 91
1724 0 my $name = var_value($1);
1725 0 ▶ die "Dereferencing $var to a non-ID: $name\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*/ 0 0 0
1726 if ($name !~ /((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*/);
1727 0 return var_value(q($).$name);
1728 } elsif ($var=~/^\$?(.*)/) {
1729 14 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
1730 91 my $lex = lex_var($1);
1731 91 ▶ if ($lex) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lex) { } 48 43 91
elsif (defined ${"XML::XSH2::Map::$1";}) { } 36 7 43
1732 48 return $$lex
1733 43 } elsif (defined(${"XML::XSH2::Map::$1"})) {
1734 36 return ${"XML::XSH2::Map::$1"};
1735 }
1736 } else {
1737 0 return undef;
1738 }
1739 }
1740  
1741 0 ▶sub string_vars {
Subroutine string_vars: not called CC=1 SCAR=1.7
Pod undocumented
1742 14 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
1743 0 return sort grep { defined(${"XML::XSH2::Map::$_"}) } keys %{"XML::XSH2::Map::"};
1744 }
1745  
1746 # print a list of XSH variables and their values
1747 #
1748 # KNOWN BUG: $ARGV doesn't map to the correct i.e. (no) package
1749 #
1750  
1751 0 ▶sub variables {
Subroutine variables: not called CC=5 SCAR=29.7
Pod undocumented
1752 0 my $opts = shift;
1753 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1754 0 foreach (string_vars()) {
1755 0 my $value = var_value(q($).$_);
1756 0 ▶ if (!defined($value)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $value) { } 0 0 0
elsif (ref $value) { } 0 0 0
elsif (0 + $value eq $value) { } 0 0 0
1757 0 out(qq(\$$_={ )."undef".qq( };\n));
1758 } elsif (ref($value)) {
1759 0 out(qq(\$$_={ ).var_value(q($).$_).qq( };\n));
1760 } elsif (0+$value eq $value) {
1761 0 out(qq(\$$_=).var_value(q($).$_).qq(;\n));
1762 } else {
1763 0 out(qq(\$$_=\').var_value(q($).$_).qq(\';\n));
1764 }
1765 }
1766 0 return 1;
1767 }
1768  
1769 10 ▶sub echo {
Subroutine echo: called CC=4 SCAR=14.9
Pod undocumented
1770 10 my $opts = _ev_opts(shift);
1771 my $val = join(($opts->{nospace} ? "" : " ") ,
1772 10 ▶ (map _ev_string($_),@_)).($opts->{nonl} ? "" : "\n");
Branch: 2 branches
BranchTrueFalseTotal
$opts->{'nospace'} ? : 0 10 10
$opts->{'nonl'} ? : 0 10 10
1773 10 ▶ $opts->{stderr} ? (print STDERR $val) : out($val);
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'stderr'} ? : 0 10 10
1774 10 return 1;
1775 }
1776 14 ▶sub set_quiet { shift if @_>1; $QUIET=$_[0]; return 1; }
Subroutine set_quiet: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 7 7 14
1777 0 ▶sub set_debug { shift if @_>1; $DEBUG=$_[0]; return 1; }
Subroutine set_debug: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
1778 0 ▶sub set_compile_only_mode { shift if @_>1; $TEST_MODE=$_[0]; return 1; }
Subroutine set_compile_only_mode: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
1779  
1780 16 ▶sub test_enc {
Subroutine test_enc: called CC=3 SCAR=14.2
Pod undocumented
1781 16 my ($enc)=@_;
1782 16 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined toUTF8($enc, '') and defined fromUTF8($enc, '')) { } 16 0 16
Condition33%1/3
defined toUTF8($enc, "") and defined fromUTF8($enc, "")
!ll&&!rl&&r
0016
MC/DC0%0/2
defined toUTF8($enc, "") and defined fromUTF8($enc, "")
defined toUTF8($enc, "") defined fromUTF8($enc, "")
Truth table33%1/3
A and B
A= defined toUTF8($enc, "")B= defined fromUTF8($enc, "")
ABresult
0X0
100
111
1783 defined(toUTF8($enc,'')) and defined(fromUTF8($enc,''))
1784 ) {
1785 # print STDERR "OK\n";
1786 16 return 1;
1787 } else {
1788 # print STDERR "NOT-OK\n";
1789 0 _err("Error: Cannot convert between $enc and utf-8\n");
1790 0 return 0;
1791 }
1792 }
1793  
1794 16 ▶sub set_encoding {
Subroutine set_encoding: called CC=2 SCAR=7.0
Pod undocumented
1795 16 ▶ shift if @_>1; # opts
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 16 0 16
1796 # print STDERR "ENCOD: @_\n";
1797 16 my $enc=_ev_string($_[0]);
1798 16 my $ok=test_enc($enc);
1799 16 $ENCODING=$enc;
1800 16 return 1;
1801 # my $ok=test_enc($enc);
1802 # if ($ok) {
1803 # $ENCODING=$enc;
1804 # # $enc = "encoding($enc)" unless $enc eq 'utf8';
1805 # # binmode $OUT;
1806 # # binmode $OUT,":$enc";
1807 # # binmode STDOUT;
1808 # # binmode STDOUT,":$enc";
1809 # # binmode STDERR;
1810 # # binmode STDERR,":$enc";
1811 # # print "Setting encoding to :$enc\n";
1812 # }
1813 # return $ok;
1814 }
1815  
1816 0 ▶sub set_qencoding {
Subroutine set_qencoding: not called CC=2 SCAR=17.9
Pod undocumented
1817 0 my ($opts,$exp)=@_;
1818 0 my $enc=_ev_string($exp);
1819 0 my $ok=test_enc($enc);
1820 0 ▶ $QUERY_ENCODING=$enc if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
1821 0 return $ok;
1822 }
1823  
1824 0 ▶sub sigint {
Subroutine sigint: not called CC=2 SCAR=17.9
Pod undocumented
1825 0 ▶ if ($TRAP_SIGINT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::TRAP_SIGINT) { } 0 0 0
1826 0 print STDERR "\nCtrl-C pressed. \n";
1827 0 die "SIGINT";
1828 } else {
1829 0 print STDERR "\nCtrl-C pressed. \n";
1830 0 exit 1;
1831 }
1832 }
1833  
1834 0 ▶sub sigpipe {
Subroutine sigpipe: not called CC=2 SCAR=17.9
Pod undocumented
1835 0 ▶ if ($TRAP_SIGPIPE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::TRAP_SIGPIPE) { } 0 0 0
1836 0 die "SIGPIPE";
1837 } else {
1838 0 _err('broken pipe (SIGPIPE)');
1839 0 exit 1;
1840 }
1841 }
1842  
1843 0 ▶sub flagsigint {
Subroutine flagsigint: not called CC=1 SCAR=6.9
Pod undocumented
1844 0 print STDERR "\nCtrl-C pressed. \n";
1845 0 $_sigint=1;
1846 }
1847  
1848 1372 ▶sub propagate_flagsigint {
Subroutine propagate_flagsigint: called CC=2 SCAR=10.5
Pod undocumented
1849 1372 ▶ if ($_sigint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_sigint) 0 1372 1372
1850 0 $_sigint=0;
1851 0 die 'SIGINT';
1852 }
1853 }
1854  
1855  
1856 0 ▶sub convertFromDocEncoding ($$\$) {
Subroutine convertFromDocEncoding: not called CC=1 SCAR=6.9
Pod undocumented
1857 0 my ($doc,$encoding,$str)=@_;
1858 0 return fromUTF8($encoding, toUTF8($_xml_module->doc_encoding($doc), $str));
1859 }
1860  
1861 5 ▶sub _rt_position {
Subroutine _rt_position: called CC=2 SCAR=6.9
1862 5 ▶ return "$RT_SCRIPT line $RT_LINE, column $RT_COLUMN,".
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::RT_LINE == 1 ? : 4 1 5
1863 ($RT_LINE==1 ? "" : " offset $RT_OFFSET.");
1864 }
1865  
1866 0 ▶sub _err {
Subroutine _err: not called CC=2 SCAR=17.9
1867 0 ▶ print STDERR @_," at ",_rt_position(),"\n" if $ERRORS;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::ERRORS 0 0 0
1868 }
1869  
1870 1 ▶sub _warn {
Subroutine _warn: called CC=2 SCAR=7.6
1871 1 ▶ print STDERR "Warning: ",@_," at ",_rt_position(),"\n" if $WARNINGS;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::WARNINGS 0 1 1
1872 }
1873  
1874  
1875 # if the argument is non-void then print it and return 0; return 1 otherwise
1876 2389 ▶sub _check_err {
Subroutine _check_err: called CC=13 SCAR=37.5
1877 2389 my ($err,$survive_int,$remove_at)=@_;
1878 2389 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 63 2326 2389
1879 # cleanup the error message
1880 63 $err =~ s/^XPathContext: error coming back from perl-dispatcher in pm file\.\s*//;
1881 63 ▶ if ($remove_at and !ref($err)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove_at and not ref $err) 0 63 63
Condition33%1/3
$remove_at and not ref $err
!ll&&!rl&&r
6300
MC/DC0%0/2
$remove_at and not ref $err
$remove_at not ref $err
Truth table33%1/3
A and not(B)
A= $remove_atB= not ref $err
ABresult
0X0
100
111
1882 0 $err=~s/ at (?:.|\n)*$//;
1883 }
1884  
1885 63 ▶ if ($err=~/^SIGINT/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err =~ /^SIGINT/) { } 0 63 63
elsif ($XML::XSH2::Functions::_die_on_err) { } 63 0 63
1886 0 ▶ if ($survive_int) {
Branch: 2 branches
BranchTrueFalseTotal
if ($survive_int) { } 0 0 0
elsif (ref $err) { } 0 0 0
1887 0 $err=~s/ at (?:.|\n)*$//;
1888 0 _err($err);
1889 0 return 0;
1890 } elsif (ref($err)) {
1891 0 die $err; # propagate
1892 } else {
1893 0 chomp $err;
1894 0 ▶ unless ($err=~/ at (?:.|\n)*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err =~ / at (?:.|\n)*$/) 0 0 0
1895 0 $err.=" at "._rt_position();
1896 }
1897 0 die $err."\n"; # propagate
1898 }
1899 } elsif ($_die_on_err) {
1900 63 ▶ if ($err=~/^SIGPIPE/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err =~ /^SIGPIPE/) { } 0 63 63
elsif (ref $err) { } 40 23 63
1901 0 _err('broken pipe (SIGPIPE)');
1902 } elsif (ref($err)) {
1903 40 die $err; # propagate
1904 } else {
1905 23 chomp $err;
1906 23 ▶ unless ($err=~/ at (?:.|\n)*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err =~ / at (?:.|\n)*$/) 5 18 23
1907 5 $err.=" at "._rt_position();
1908 }
1909 23 die $err."\n"; # propagate
1910 }
1911 } else {
1912 0 ▶ if ($err=~/^SIGPIPE/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /^SIGPIPE/) { } 0 0 0
1913 0 _err('broken pipe (SIGPIPE)');
1914 } else {
1915 0 _err($err);
1916 }
1917 0 return 0;
1918 }
1919 }
1920 2326 return 1;
1921 }
1922  
1923  
1924 # return current node for given document or document root if
1925 # current node is not from the given document
1926 118 ▶sub xsh_context_node {
Subroutine xsh_context_node: called CC=1 SCAR=0.0
Pod undocumented
1927 118 return $_xpc->getContextNode;
1928 }
1929  
1930 0 ▶sub xsh_context_var {
Subroutine xsh_context_var: not called CC=2 SCAR=17.9
Pod undocumented
1931 0 my $node = xsh_context_node();
1932 0 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
1933 0 return xsh_search_docvar($node);
1934 }
1935 0 return "";
1936 }
1937  
1938  
1939 # set current node to given XPath
1940 27 ▶sub set_local_xpath {
Subroutine set_local_xpath: called CC=2 SCAR=6.9
Pod undocumented
1941 27 my ($opts,$exp)=@_;
1942 27 ▶ $exp = "/" if ($exp eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $exp eq "" 7 20 27
1943 27 _set_context([_ev_nodelist($exp)->shift()]);
1944 27 return 1;
1945 }
1946  
1947 61 ▶sub cannon_name {
Subroutine cannon_name: called CC=5 SCAR=26.8
Pod undocumented
1948 61 my ($node)=@_;
1949 61 my $local_name =$node->localname();
1950 61 my $uri = $node->namespaceURI();
1951 61 ▶ if ($uri ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri ne "") 0 61 61
1952 0 my $prefix=$node->prefix;
1953 #if ($prefix eq '') {
1954 0 my %r = reverse %_ns;
1955 0 $prefix = $r{ $uri };
1956 0 ▶ if ($prefix ne '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefix ne '') { } 0 0 0
elsif (my $parent = $node->parentNode) { } 0 0 0
1957 0 return $prefix.':'.$local_name
1958 } elsif(my $parent = $node->parentNode) {
1959 0 $prefix = $parent->lookupNamespacePrefix($uri);
1960 0 ▶ if ($prefix ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix ne "") 0 0 0
1961 0 return $prefix.':'.$local_name
1962 }
1963 }
1964 0 return '*[name()="'.$node->getName().'"]';
1965 }
1966 61 return $local_name;
1967 }
1968  
1969 # return XPath identifying a node within its parent's subtree
1970 97 ▶sub node_address {
Subroutine node_address: called CC=14 SCAR=28.6
Pod undocumented
1971 97 ▶ my $node = shift || $_xpc->getContextNode();
Condition33%1/3
shift() || $XML::XSH2::Functions::_xpc->getContextNode
l!l&&r!l&&!r
9700
MC/DC0%0/2
shift() || $XML::XSH2::Functions::_xpc->getContextNode
shift() $XML::XSH2::Functions::_xpc->getContextNode
Truth table33%1/3
A || B
A= shift()B= $XML::XSH2::Functions::_xpc->getContextNode
ABresult
1X1
011
000
1972 97 my $no_parent = shift;
1973 97 my $name;
1974 97 ▶ if ($_xml_module->is_element($node)) {
Branch: 5 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($node)) { } 61 36 97
elsif ($XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node)) { } 5 31 36
elsif ($XML::XSH2::Functions::_xml_module->is_comment($node)) { } 0 31 31
elsif ($XML::XSH2::Functions::_xml_module->is_pi($node)) { } 0 31 31
elsif ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 0 31 31
1975 61 $name=cannon_name($node);
1976 ▶ } elsif ($_xml_module->is_text($node) or
Condition66%2/3
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
l!l&&r!l&&!r
5031
MC/DC50%1/2
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
$XML::XSH2::Functions::_xml_module->is_text($node) $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
Truth table66%2/3
A or B
A= $XML::XSH2::Functions::_xml_module->is_text($node)B= $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
ABresult
1X1
011
000
1977 $_xml_module->is_cdata_section($node)) {
1978 5 $name="text()";
1979 } elsif ($_xml_module->is_comment($node)) {
1980 0 $name="comment()";
1981 } elsif ($_xml_module->is_pi($node)) {
1982 0 $name="processing-instruction()";
1983 } elsif ($_xml_module->is_attribute($node)) {
1984 0 return "@".cannon_name($node);
1985 }
1986
1987 97 ▶ if (!$no_parent and $node->parentNode) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $no_parent and $node->parentNode) { } 66 31 97
Condition66%2/3
not $no_parent and $node->parentNode
!ll&&!rl&&r
03166
MC/DC50%1/2
not $no_parent and $node->parentNode
not $no_parent $node->parentNode
Truth table66%2/3
not(A) and B
A= not $no_parentB= $node->parentNode
ABresult
0X0
100
111
1988 66 my @children;
1989 # if ($_xml_module->is_element($node)) {
1990 # @children=$_xpc->findnodes("./$name",$node->parentNode);
1991 # } else {
1992 66 my $context = $_xpc->getContextNode;
1993 66 @children= eval { $_xpc->findnodes("./$name",$node->parentNode) };
1994 # }
1995 66 ▶ if (@children == 1 and $_xml_module->xml_equal($node,$children[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1 and $XML::XSH2::Functions::_xml_module->xml_equal($node, $children[0])) 45 21 66
Condition66%2/3
@children == 1 and $XML::XSH2::Functions::_xml_module->xml_equal($node, $children[0])
!ll&&!rl&&r
21045
MC/DC50%1/2
@children == 1 and $XML::XSH2::Functions::_xml_module->xml_equal($node, $children[0])
@children == 1 $XML::XSH2::Functions::_xml_module->xml_equal($node, $children[0])
Truth table66%2/3
A and B
A= @children == 1B= $XML::XSH2::Functions::_xml_module->xml_equal($node, $children[0])
ABresult
0X0
100
111
1996 45 return "$name";
1997 }
1998 21 ▶ for (my $pos=0;$pos<@children;$pos++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < @children) 106 0 106
1999 106 ▶ return "$name"."[".($pos+1)."]"
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::_xml_module->xml_equal($node, $children[$pos]) 21 85 106
2000 if ($_xml_module->xml_equal($node,$children[$pos]));
2001 }
2002 0 return "??$name??";
2003 } else {
2004 31 return ();
2005 }
2006 }
2007  
2008 # parent element (even for attributes)
2009 170 ▶sub tree_parent_node {
Subroutine tree_parent_node: called CC=2 SCAR=7.6
Pod undocumented
2010 170 my $node=$_[0];
2011 170 ▶ if ($_xml_module->is_attribute($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 0 170 170
2012 0 return $node->ownerElement();
2013 } else {
2014 170 return $node->parentNode();
2015 }
2016 }
2017  
2018 # get node's ID
2019 0 ▶sub node_id {
Subroutine node_id: not called CC=6 SCAR=37.4
Pod undocumented
2020 0 my ($node)=@_;
2021 0 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
2022 0 for my $attr ($node->attributes) {
2023 0 ▶ if ($attr->can('isId') and $attr->isId) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->can("isId") and $attr->isId) 0 0 0
Condition0%0/3
$attr->can("isId") and $attr->isId
!ll&&!rl&&r
000
MC/DC0%0/2
$attr->can("isId") and $attr->isId
$attr->can("isId") $attr->isId
Truth table0%0/3
A and B
A= $attr->can("isId")B= $attr->isId
ABresult
0X0
100
111
2024 0 my $value = $attr->value;
2025 0 ▶ return $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
2026 }
2027 }
2028 }
2029 0 return undef;
2030 }
2031  
2032 # return canonical xpath for the given or current node
2033 31 ▶sub pwd {
Subroutine pwd: called CC=8 SCAR=24.1
Pod undocumented
2034 31 ▶ my $node=shift || $_xpc->getContextNode();
Condition66%2/3
shift() || $XML::XSH2::Functions::_xpc->getContextNode
l!l&&r!l&&!r
14170
MC/DC0%0/2
shift() || $XML::XSH2::Functions::_xpc->getContextNode
shift() $XML::XSH2::Functions::_xpc->getContextNode
Truth table66%2/3
A || B
A= shift()B= $XML::XSH2::Functions::_xpc->getContextNode
ABresult
1X1
011
000
2035 31 my $use_id = shift;
2036 31 ▶ return undef unless ref($node);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 31 31
2037 31 ▶ return $node->nodePath() if !$STRICT_PWD and UNIVERSAL::can($node,'nodePath');
Branch: 1 branch
BranchTrueFalseTotal
if not $XML::XSH2::Functions::STRICT_PWD and UNIVERSAL::can($node, "nodePath") 0 31 31
Condition33%1/3
not $XML::XSH2::Functions::STRICT_PWD and UNIVERSAL::can($node, "nodePath")
!ll&&!rl&&r
3100
MC/DC0%0/2
not $XML::XSH2::Functions::STRICT_PWD and UNIVERSAL::can($node, "nodePath")
not $XML::XSH2::Functions::STRICT_PWD UNIVERSAL::can($node, "nodePath")
Truth table33%1/3
not(A) and B
A= not $XML::XSH2::Functions::STRICT_PWDB= UNIVERSAL::can($node, "nodePath")
ABresult
0X0
100
111
2038 31 my @pwd=();
2039 31 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$node and do { do { if ($use_id) { my $id = node_id($node); if (defined $id) { return join("/", "id('${id}')", @pwd); } ; } ; unshift @pwd, node_address($node); $node = tree_parent_node($node) } } 66 31 97
2040 97 ▶ if ($use_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_id) 0 97 97
2041 0 my $id = node_id($node);
2042 0 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 0 0
2043 0 return join "/","id('$id')",@pwd;
2044 }
2045 }
2046 97 unshift @pwd,node_address($node);
2047 97 $node=tree_parent_node($node);
2048 } while ($node);
2049 31 my $pwd="/".join "/",@pwd;
2050 31 return $pwd;
2051 }
2052  
2053 # return canonical xpath for current node (encoded)
2054 0 ▶sub xsh_pwd {
Subroutine xsh_pwd: not called CC=3 SCAR=24.8
Pod undocumented
2055 0 ▶ shift if $_[0] && !ref($_[0]); # package name
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and not ref $_[0] 0 0 0
Condition0%0/3
$_[0] and not ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and not ref $_[0]
$_[0] not ref $_[0]
Truth table0%0/3
A and not(B)
A= $_[0]B= not ref $_[0]
ABresult
0X0
100
111
2056 0 &pwd;
2057 }
2058  
2059 # print current node's xpath
2060 17 ▶sub print_pwd {
Subroutine print_pwd: called CC=2 SCAR=7.2
Pod undocumented
2061 17 my $opts = _ev_opts(shift);
2062
2063 17 my $pwd=pwd(undef, $opts->{id});
2064 17 ▶ if ($pwd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pwd) { } 17 0 17
2065 17 out("$pwd\n");
2066 17 return $pwd;
2067 } else {
2068 0 return 0;
2069 }
2070 }
2071  
2072 # return base file-name of a given path
2073 0 ▶sub _base_filename {
Subroutine _base_filename: not called CC=2 SCAR=17.9
2074 0 my ($fn)=@_;
2075 0 ▶ ($^O eq 'Win32') ? ($fn =~ m{([^\\]*)$}) : ($fn =~ m{([^/]*)$});
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'Win32' ? : 0 0 0
2076 0 return $1;
2077 }
2078  
2079 # evaluate variable and xpath expresions in a given string
2080 1497 ▶sub _expand {
Subroutine _expand: called CC=9 SCAR=22.9
2081 1497 my ($l,$vars)=@_;
2082 1497 my $k;
2083 15 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2084 1497 $l=~/^/o;
2085 1497 ▶ while ($l !~ /\G$/gsco) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($l =~ /\G$/cogs) 2170 1497 3667
2086 2170 ▶ if ($l=~/\G\\\$\{/gsco) {
Branch: 6 branches
BranchTrueFalseTotal
if ($l =~ /\G\\\$\{/cogs) { } 3 2167 2170
elsif ($l =~ /\G\$\{(\$?[a-zA-Z_][a-zA-Z0-9_]*)\}/cogs) { } 69 2098 2167
elsif ($vars and $l =~ /\G(\$\$?[a-zA-Z_][a-zA-Z0-9_]*)/cogs) { } 0 2098 2098
elsif ($l =~ /\G\$\{\{(.*?)\}\}/cogs) { } 0 2098 2098
elsif ($l =~ /\G\$\{\((.+?)\)\}/cogs) { } 9 2089 2098
elsif ($l =~ /\G(\$(?!\{)|\\(?!\$\{)|[^\\\$]+)/cogs) { } 2089 0 2089
2087 3 $k.='${';
2088 } elsif ($l=~/\G\$\{(\$?[a-zA-Z_][a-zA-Z0-9_]*)\}/gsco) {
2089 69 $k.=var_value(q($).$1);
2090 ▶ } elsif ($vars and $l=~/\G(\$\$?[a-zA-Z_][a-zA-Z0-9_]*)/gsco) {
Condition33%1/3
$vars and $l =~ /\G(\$\$?[a-zA-Z_][a-zA-Z0-9_]*)/cogs
!ll&&!rl&&r
209800
MC/DC0%0/2
$vars and $l =~ /\G(\$\$?[a-zA-Z_][a-zA-Z0-9_]*)/cogs
$vars $l =~ /\G(\$\$?[a-zA-Z_][a-zA-Z0-9_]*)/cogs
Truth table33%1/3
A and B
A= $varsB= $l =~ /\G(\$\$?[a-zA-Z_][a-zA-Z0-9_]*)/cogs
ABresult
0X0
100
111
2091 0 $k.=var_value($1);
2092 } elsif ($l=~/\G\$\{\{(.*?)\}\}/gsco) {
2093 0 $k.=perl_eval($1);
2094 } elsif ($l=~/\G\$\{\((.+?)\)\}/gsco) {
2095 9 $k.=_ev_literal($1);
2096 } elsif ($l=~/\G(\$(?!\{)|\\(?!\$\{)|[^\\\$]+)/gsco) {
2097 # skip to the next \ or $
2098 2089 $k.=$1;
2099 }
2100 }
2101 1497 return $k;
2102 }
2103  
2104 # expand one or all parameters (according to return context)
2105 20 ▶sub expand {
Subroutine expand: called CC=2 SCAR=9.2
Pod undocumented
2106 0 ▶ return wantarray ? (map { _expand($_) } @_) : _expand($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 20 20
2107 }
2108  
2109 # return a reference to a variable storage
2110 0 ▶sub _get_var_ref {
Subroutine _get_var_ref: not called CC=5 SCAR=30.7
2111 0 my ($name,$value)=@_;
2112 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2113 0 ▶ if ($name=~/^\$(\$.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /^\$(\$.*)/) { } 0 0 0
elsif ($name =~ /^\$((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/) { } 0 0 0
2114 0 my $prev = $name;
2115 0 $name = var_value($1);
2116 0 ▶ die "Dereferencing $prev to a non-ID: $name\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/ 0 0 0
2117 if ($name !~ /((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/);
2118 0 return _get_var_ref(q($).$name);
2119 } elsif ($name=~/^\$((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/) {
2120 0 my $lex = lex_var($1);
2121 0 ▶ return $lex if ($lex);
Branch: 1 branch
BranchTrueFalseTotal
if $lex 0 0 0
2122 0 return \${"XML::XSH2::Map::$1"};
2123 } else {
2124 0 die "Invalid variable name $name\n"
2125 }
2126 0 return 1;
2127 }
2128  
2129 # assign a value to a variable
2130 740 ▶sub _assign {
Subroutine _assign: called CC=10 SCAR=27.5
2131 740 my ($name,$value,$op)=@_;
2132 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2133 740 ▶ if ($name=~/^\$(\$.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /^\$(\$.*)/) { } 0 740 740
elsif ($name =~ /^\$?((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/) { } 740 0 740
2134 0 my $prev = $name;
2135 0 $name = var_value($1);
2136 0 ▶ die "Dereferencing $prev to a non-ID: $name\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/ 0 0 0
2137 if ($name !~ /((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/);
2138 0 return _assign(q($).$name,$value,$op);
2139 } elsif ($name=~/^\$?((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/) {
2140 740 ▶ $op = '=' unless $op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 438 302 740
2141 740 my $lex = lex_var($1);
2142 740 ▶ if ($lex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lex) { } 387 353 740
2143 387 ▶ eval '$$lex'.$op.'$value'; die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 387 387
2144 0 ▶ print STDERR "lexical \$$1=",${"XML::XSH2::Map::$1"},"\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 387 387
2145 } else {
2146 353 ▶ eval '${"XML::XSH2::Map::$1"}'.$op.'$value'; die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 353 353
2147 0 ▶ print STDERR "\$$1=",${"XML::XSH2::Map::$1"},"\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 353 353
2148 }
2149 } else {
2150 0 die "Invalid variable name $name\n"
2151 }
2152 }
2153  
2154 # undefine global or localized variable
2155 0 ▶sub _undef {
Subroutine _undef: not called CC=4 SCAR=26.1
2156 0 my ($name)=@_;
2157 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2158 0 ▶ if ($name=~/^\$(\$.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /^\$(\$.*)/) { } 0 0 0
elsif ($name =~ /^\$((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/) { } 0 0 0
2159 0 my $prev = $name;
2160 0 $name = var_value($1);
2161 0 ▶ die "Dereferencing $prev to a non-ID: $name\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/ 0 0 0
2162 if ($name !~ /((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/);
2163 0 return _undef(q($).$name);
2164 } elsif ($name=~/^\$((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/) {
2165 0 undef ${"XML::XSH2::Map::$1"};
2166 } else {
2167 0 die "Invalid variable name $name\n"
2168 }
2169 0 return 1;
2170 }
2171  
2172 # undefine lexical, global or localized variable
2173 sub undefine {
2174 my ($name)=@_;
2175 if ($name=~/^\$(\$.*)/) {
2176 my $prev = $name;
2177 $name = var_value($1);
2178 die "Dereferencing $prev to a non-ID: $name\n"
2179 if ($name !~ /((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/);
2180 return undefine(q($).$name);
2181 } elsif ($name=~/^\$((?:::)?[a-zA-Z_][a-zA-Z0-9_]*)*$/) {
2182 my $lex = lex_var($1);
2183 if ($lex) {
2184 undef $$lex;
2185 } else {
2186 15 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
2187 undef ${"XML::XSH2::Map::".$1};
2188 }
2189 } else {
2190 die "Invalid variable name $name\n"
2191 }
2192 return 1;
2193 }
2194  
2195 363 ▶sub literal_value {
Subroutine literal_value: called CC=2 SCAR=6.9
Pod undocumented
2196 363 ▶ return ref($_[0]) ? $_[0]->value() : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 239 124 363
2197 }
2198  
2199 # evaluate xpath and assign the result to a variable
2200 254 ▶sub xpath_assign {
Subroutine xpath_assign: called CC=3 SCAR=11.0
Pod undocumented
2201 254 my ($exp,$op,$type,$name)=@_;
2202 254 ▶ if ($type eq 'my') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'my') { } 1 253 254
elsif ($type eq 'local') { } 11 242 253
2203 1 store_lex_variables(0,$name);
2204 } elsif ($type eq 'local') {
2205 11 store_variables(0,$name);
2206 }
2207 254 my $val = _ev($exp);
2208 254 _assign($name,$val,$op);
2209 254 return 1;
2210 }
2211  
2212 48 ▶sub command_assign {
Subroutine command_assign: called CC=3 SCAR=12.3
Pod undocumented
2213 48 my ($command,$op,$type,$name)=@_;
2214 48 ▶ if ($type eq 'my') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'my') { } 0 48 48
elsif ($type eq 'local') { } 0 48 48
2215 0 store_lex_variables(0,$name);
2216 } elsif ($type eq 'local') {
2217 0 store_variables(0,$name);
2218 }
2219 48 $op =~ s/\s*:\s*//;
2220 48 _assign($name,run_commands([$command],0,1),$op);
2221 48 return 1;
2222 }
2223  
2224 1 ▶sub make_local {
Subroutine make_local: called CC=2 SCAR=6.9
Pod undocumented
2225 1 foreach (@_) {
2226 1 xpath_assign(undef,'=','local',$_);
2227 }
2228 }
2229  
2230 0 ▶sub get_stored_nodelists {
Subroutine get_stored_nodelists: not called CC=1 SCAR=6.9
Pod undocumented
2231 0 return grep { ref($_) } map { @$_ } @stored_variables;
2232 }
2233  
2234 1051 ▶sub store_variables {
Subroutine store_variables: called CC=6 SCAR=18.7
Pod undocumented
2235 1051 my ($new,@vars)=@_;
2236 1051 my $pool;
2237 1051 ▶ if ($new) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new) { } 1040 11 1051
elsif (@XML::XSH2::Functions::stored_variables and ref $XML::XSH2::Functions::stored_variables[$#XML::XSH2::Functions::stored_variables]) { } 11 0 11
2238 1040 $pool=[];
2239 ▶ } elsif (@stored_variables and ref($stored_variables[$#stored_variables])) {
Condition33%1/3
@XML::XSH2::Functions::stored_variables and ref $XML::XSH2::Functions::stored_variables[$#XML::XSH2::Functions::stored_variables]
!ll&&!rl&&r
0011
MC/DC0%0/2
@XML::XSH2::Functions::stored_variables and ref $XML::XSH2::Functions::stored_variables[$#XML::XSH2::Functions::stored_variables]
@XML::XSH2::Functions::stored_variables ref $XML::XSH2::Functions::stored_variables[$#XML::XSH2::Functions::stored_variables]
Truth table33%1/3
A and B
A= @XML::XSH2::Functions::stored_variablesB= ref $XML::XSH2::Functions::stored_variables[$#XML::XSH2::Functions::stored_variables]
ABresult
0X0
100
111
2240 11 $pool=$stored_variables[$#stored_variables];
2241 } else {
2242 0 _warn "Ignoring attempt to make a local variable outside a localizable context!";
2243 0 return 0;
2244 }
2245  
2246 1051 foreach (@vars) {
2247 18 my $value=var_value($_);
2248 18 push @$pool, $_ => $value;
2249 }
2250 1051 ▶ push @stored_variables, $pool if ($new);
Branch: 1 branch
BranchTrueFalseTotal
if $new 1040 11 1051
2251  
2252 1051 return 1;
2253 }
2254  
2255 1288 ▶sub store_lex_variables {
Subroutine store_lex_variables: called CC=5 SCAR=17.1
Pod undocumented
2256 1288 my $new = shift;
2257 1288 my $pool;
2258 1288 ▶ if ($new) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new) { } 1287 1 1288
elsif (@$XML::XSH2::Functions::lexical_variables) { } 1 0 1
2259 1287 $pool={};
2260 1287 push @$lexical_variables, $pool;
2261 } elsif (@$lexical_variables) {
2262 1 $pool=$lexical_variables->[$#$lexical_variables]
2263 } else {
2264 0 _warn "Ignoring attempt to make a lexical variable outside a lexical context!";
2265 0 return 0;
2266 }
2267 1288 foreach (@_) {
2268 387 ▶ if (/^\s*\$?([^\$]*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\$?([^\$]*)/) { } 387 0 387
2269 387 $pool->{$1} = undef;
2270 } else {
2271 0 die "Invalid lexical variable name $_\n";
2272 }
2273 }
2274 }
2275  
2276 1287 ▶sub restore_lex_variables {
Subroutine restore_lex_variables: called CC=2 SCAR=9.2
Pod undocumented
2277 1287 ▶ unless (ref(pop @$lexical_variables)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref pop @$XML::XSH2::Functions::lexical_variables) 0 1287 1287
2278 0 __bug("Lexical variable pool is empty, which was not expected!\n");
2279 }
2280 }
2281  
2282 7 ▶sub create_block_var {
Subroutine create_block_var: called CC=3 SCAR=14.2
Pod undocumented
2283 7 my ($var,$local) = @_;
2284 7 ▶ if ($local =~ /local/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($local =~ /local/) { } 7 0 7
elsif ($local =~ /my/) { } 0 0 0
2285 7 store_variables(1,$var);
2286 } elsif ($local=~/my/) {
2287 0 store_lex_variables(1,$var);
2288 }
2289 }
2290  
2291 7 ▶sub destroy_block_var {
Subroutine destroy_block_var: called CC=3 SCAR=15.4
Pod undocumented
2292 7 ▶ if ($_[0] =~ /local/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /local/) { } 7 0 7
elsif ($_[0] =~ /my/) { } 0 0 0
2293 7 restore_variables();
2294 } elsif ($_[0]=~/my/) {
2295 0 restore_lex_variables();
2296 }
2297 }
2298  
2299 1040 ▶sub restore_variables {
Subroutine restore_variables: called CC=5 SCAR=18.0
Pod undocumented
2300 1040 my $pool=pop @stored_variables;
2301 1040 ▶ unless (ref($pool)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $pool) 0 1040 1040
2302 0 __bug("Local variable pool is empty, which was not expected!\n");
2303 0 return 0;
2304 }
2305 1040 ▶ while (@$pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pool) 18 1040 1058
2306 18 my ($value,$name)=(pop(@$pool), pop(@$pool));
2307 18 ▶ if ($name =~ m/^\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\$/) { } 18 0 18
2308 18 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 18 0 18
2309 18 _assign($name,$value);
2310 } else {
2311 0 _undef($name);
2312 }
2313 } else {
2314 0 __bug("Invalid variable name '$_'\n");
2315 }
2316 }
2317 1040 return 1;
2318 }
2319  
2320 120 ▶sub _prepare_result_nl {
Subroutine _prepare_result_nl: called CC=2 SCAR=6.9
2321 # my ($opts)=@_;
2322 # return undef unless ref($opts);
2323 # my ($append, $var);
2324 # if (exists($opts->{'append-result'})) {
2325 # $append = 1;
2326 # $var = $opts->{'append-result'};
2327 # } elsif (exists($opts->{result})) {
2328 # $append = 0;
2329 # $var = $opts->{result};
2330 # } elsif (exists($opts->{'append-result'})) {
2331 # $append = 1;
2332 # $var = $opts->{'append-result'};
2333 # } else {
2334 # return undef;
2335 # }
2336 # my $rl;
2337 # if ($var ne "") {
2338 # $rl = var_value($var);
2339 # unless ($append and (UNIVERSAL::isa($rl,'XML::LibXML::NodeList') or ref($rl) eq 'ARRAY')) {
2340 # _assign($var,XML::LibXML::NodeList->new());
2341 # $rl = var_value($var);
2342 # }
2343 # }
2344 # return $rl;
2345 120 ▶ if ($_want_returns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_want_returns) { } 16 104 120
2346 16 return XML::LibXML::NodeList->new();
2347 } else {
2348 104 return undef;
2349 }
2350 }
2351  
2352  
2353 52 ▶sub count_xpath {
Subroutine count_xpath: called CC=6 SCAR=18.5
Pod undocumented
2354 52 my ($exp)=@_;
2355 52 my $result = _ev($exp);
2356 52 ▶ if (ref($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result) { } 36 16 52
2357 36 ▶ if (UNIVERSAL::isa($result,'XML::LibXML::NodeList')) {
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($result, 'XML::LibXML::NodeList')) { } 4 32 36
elsif (UNIVERSAL::isa($result, 'XML::LibXML::Literal')) { } 0 32 32
elsif (UNIVERSAL::isa($result, 'XML::LibXML::Number') or UNIVERSAL::isa($result, 'XML::LibXML::Boolean')) { } 32 0 32
2358 4 return $result->size();
2359 } elsif (UNIVERSAL::isa($result,'XML::LibXML::Literal')) {
2360 0 return $result->value();
2361 ▶ } elsif (UNIVERSAL::isa($result,'XML::LibXML::Number') or
Condition66%2/3
UNIVERSAL::isa($result, "XML::LibXML::Number") or UNIVERSAL::isa($result, "XML::LibXML::Boolean")
l!l&&r!l&&!r
10220
MC/DC0%0/2
UNIVERSAL::isa($result, "XML::LibXML::Number") or UNIVERSAL::isa($result, "XML::LibXML::Boolean")
UNIVERSAL::isa($result, "XML::LibXML::Number") UNIVERSAL::isa($result, "XML::LibXML::Boolean")
Truth table66%2/3
A or B
A= UNIVERSAL::isa($result, "XML::LibXML::Number")B= UNIVERSAL::isa($result, "XML::LibXML::Boolean")
ABresult
1X1
011
000
2362 UNIVERSAL::isa($result,'XML::LibXML::Boolean')) {
2363 32 return $result->value();
2364 }
2365 } else {
2366 16 return $result;
2367 }
2368 }
2369  
2370 31 ▶sub new_doc {
Subroutine new_doc: called CC=2 SCAR=7.2
Pod undocumented
2371 31 my ($opts,$root_element)=@_;
2372 31 $opts = _ev_opts($opts);
2373 31 $root_element = _ev_string($root_element);
2374 31 ▶ my $format= $opts->{format} || $DEFAULT_FORMAT;
Condition33%1/3
$opts->{'format'} || $XML::XSH2::Functions::DEFAULT_FORMAT
l!l&&r!l&&!r
0310
MC/DC0%0/2
$opts->{'format'} || $XML::XSH2::Functions::DEFAULT_FORMAT
$opts->{'format'} $XML::XSH2::Functions::DEFAULT_FORMAT
Truth table33%1/3
A || B
A= $opts->{'format'}B= $XML::XSH2::Functions::DEFAULT_FORMAT
ABresult
1X1
011
000
2375 31 create_doc(undef, $root_element, $format);
2376 }
2377  
2378 # create new document
2379 53 ▶sub create_doc {
Subroutine create_doc: called CC=4 SCAR=13.9
Pod undocumented
2380 53 my ($id, $root_element, $format, $filename)=@_;
2381 # TODO: $format argument is not used by the grammar
2382 53 my $doc;
2383 53 ▶ $root_element="<$root_element/>" unless ($root_element=~/^\s*</);
Branch: 1 branch
BranchTrueFalseTotal
unless $root_element =~ /^\s*</ 25 28 53
2384 53 $root_element=~s/^\s+//;
2385 53 $doc=xsh_parse_string($root_element,$format);
2386 53 ▶ set_doc($id,$doc,$filename) if defined($id);
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 22 31 53
2387 53 $_newdoc++;
2388  
2389 53 ▶ _set_context([$doc]) if $SWITCH_TO_NEW_DOCUMENTS;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::SWITCH_TO_NEW_DOCUMENTS 53 0 53
2390 53 return $doc;
2391 }
2392  
2393 # bind a document with a given id and filename
2394 22 ▶sub set_doc {
Subroutine set_doc: called CC=1 SCAR=0.0
Pod undocumented
2395 22 my ($id,$doc,$file)=@_;
2396 # $_doc{$id}=$doc;
2397 # $_files{$id}=$file;
2398 22 _assign($id,$doc);
2399 22 set_doc_URI($doc,$file);
2400 22 return $doc;
2401 }
2402  
2403 0 ▶sub set_filename {
Subroutine set_filename: not called CC=2 SCAR=17.9
Pod undocumented
2404 0 my ($opts,$file, $doc)=@_;
2405 0 $file = _tilde_expand(_ev_string($file));
2406 0 ▶ $doc = _ev_doc(defined($doc) ? $doc : '.');
Branch: 1 branch
BranchTrueFalseTotal
defined $doc ? : 0 0 0
2407 0 set_doc_URI($doc,$file);
2408 }
2409  
2410 22 ▶sub set_doc_URI {
Subroutine set_doc_URI: called CC=4 SCAR=14.1
Pod undocumented
2411 22 my ($doc,$file)=@_;
2412 22 ▶ $doc->setBaseURI($file)
Branch: 1 branch
BranchTrueFalseTotal
if defined $file and ref $doc and UNIVERSAL::can($doc, "setBaseURI") 15 7 22
Condition66%4/6
defined $file and ref $doc and UNIVERSAL::can($doc, "setBaseURI")
!ll&&!rl&&r
7015
defined $file and ref $doc
!ll&&!rl&&r
7015
MC/DC33%1/3
defined $file and ref $doc and UNIVERSAL::can($doc, "setBaseURI")
defined $file ref $doc UNIVERSAL::can($doc, "setBaseURI")
Truth table50%2/4
A and B and C
A= defined $fileB= ref $docC= UNIVERSAL::can($doc, "setBaseURI")
ABCresult
0XX0
10X0
1100
1111
2413 if (defined($file) and ref($doc) and
2414 UNIVERSAL::can($doc,'setBaseURI'));
2415 22 return $doc->URI;
2416 }
2417  
2418  
2419 0 ▶sub xsh_search_docvar {
Subroutine xsh_search_docvar: not called CC=8 SCAR=39.8
Pod undocumented
2420 0 my ($node)=@_;
2421 0 my $doc = $_xml_module->owner_document($node);
2422 0 ▶ return undef unless ref($doc);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc 0 0 0
2423 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2424 0 foreach my $var (keys %{"XML::XSH2::Map::"}) {
2425 0 my $value = ${"XML::XSH2::Map::".$var};
2426 0 ▶ if (ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 0 0
2427 0 ▶ $value = $value->[0] if (UNIVERSAL::isa($value,'XML::LibXML::NodeList')
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($value, "XML::LibXML::NodeList") and $value->size == 1 0 0 0
Condition0%0/3
UNIVERSAL::isa($value, "XML::LibXML::NodeList") and $value->size == 1
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($value, "XML::LibXML::NodeList") and $value->size == 1
UNIVERSAL::isa($value, "XML::LibXML::NodeList") $value->size == 1
Truth table0%0/3
A and B
A= UNIVERSAL::isa($value, "XML::LibXML::NodeList")B= $value->size == 1
ABresult
0X0
100
111
2428 and $value->size()==1);
2429 0 ▶ if (UNIVERSAL::isa($value,'XML::LibXML::Document')
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "XML::LibXML::Document") and $value->isSameNode($doc)) 0 0 0
Condition0%0/3
UNIVERSAL::isa($value, "XML::LibXML::Document") and $value->isSameNode($doc)
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($value, "XML::LibXML::Document") and $value->isSameNode($doc)
UNIVERSAL::isa($value, "XML::LibXML::Document") $value->isSameNode($doc)
Truth table0%0/3
A and B
A= UNIVERSAL::isa($value, "XML::LibXML::Document")B= $value->isSameNode($doc)
ABresult
0X0
100
111
2430 and $value->isSameNode($doc)) {
2431 0 return "\$".$var;
2432 }
2433 }
2434 }
2435 }
2436  
2437 0 ▶sub index_doc {
Subroutine index_doc: not called CC=4 SCAR=30.0
Pod undocumented
2438 0 my ($opts,$exp)=@_;
2439 0 ▶ $exp = '.' if $exp eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $exp eq "" 0 0 0
2440 0 my $doc = _ev_doc($exp);
2441 0 my $result;
2442 0 ▶ if ($doc->can('indexElements')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->can('indexElements')) { } 0 0 0
2443 0 $result = $doc->indexElements;
2444 0 ▶ print STDERR $result." elements indexed.\n" unless $QUIET;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::XSH2::Functions::QUIET 0 0 0
2445 } else {
2446 0 _warn "Indexing not supported by installed version of XML::LibXML\n";
2447 }
2448 0 return $result;
2449 }
2450  
2451 0 ▶sub _is_url {
Subroutine _is_url: not called CC=2 SCAR=17.9
2452 0 ▶ return ($_[0] =~ m(^\s*[[:alnum:]]+://)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ m[^\s*[[:alnum:]]+://] ? : 0 0 0
2453 }
2454 0 ▶sub _is_absolute {
Subroutine _is_absolute: not called CC=4 SCAR=30.0
2455 0 my ($path) = @_;
2456 0 ▶ return ($path eq '-' or
Branch: 1 branch
BranchTrueFalseTotal
$path eq '-' || _is_url($path) || 'File::Spec'->file_name_is_absolute($path) ? : 0 0 0
Condition0%0/6
$path eq "-" or _is_url($path) or "File::Spec"->file_name_is_absolute($path)
l!l&&r!l&&!r
000
$path eq "-" or _is_url($path)
l!l&&r!l&&!r
000
MC/DC0%0/3
$path eq "-" or _is_url($path) or "File::Spec"->file_name_is_absolute($path)
$path eq "-" _is_url($path) "File::Spec"->file_name_is_absolute($path)
Truth table0%0/4
A or B or C
A= $path eq "-"B= _is_url($path)C= "File::Spec"->file_name_is_absolute($path)
ABCresult
1XX1
01X1
0011
0000
2457 _is_url($path) or
2458 File::Spec->file_name_is_absolute($path)) ? 1 : 0;
2459 }
2460  
2461 # create a new document by parsing a file
2462 1 ▶sub open_doc {
Subroutine open_doc: called CC=53 SCAR=61.7
Pod undocumented
2463 1 my ($opts,$src)=@_;
2464 1 $opts = _ev_opts($opts);
2465  
2466 1 ▶ if (exists($opts->{file})+exists($opts->{pipe})+
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"file"} + exists $opts->{"pipe"} + exists $opts->{"string"} > 1) 0 1 1
2467 exists($opts->{string})>1) {
2468 0 die "'open' may have only one input flag: --file | ".
2469 "--pipe | --string\n";
2470 }
2471 1 ▶ my $format= $opts->{format} || $DEFAULT_FORMAT;
Condition33%1/3
$opts->{'format'} || $XML::XSH2::Functions::DEFAULT_FORMAT
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts->{'format'} || $XML::XSH2::Functions::DEFAULT_FORMAT
$opts->{'format'} $XML::XSH2::Functions::DEFAULT_FORMAT
Truth table33%1/3
A || B
A= $opts->{'format'}B= $XML::XSH2::Functions::DEFAULT_FORMAT
ABresult
1X1
011
000
2472 1 ▶ if ($format !~ /^xml$|^html$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format =~ /^xml$|^html$/) 0 1 1
2473 0 die "Unknown --format for command open: '$format'! Use 'xml' or 'html'.\n";
2474 }
2475  
2476 1 foreach my $o (qw(switch-to validate recover expand-entities xinclude
2477 keep-blanks pedantic load-ext-dtd complete-attributes)) {
2478 die "Can't use --$o and --no-$o together\n"
2479 9 ▶ if ($opts->{'no-'.$o} and $opts->{$o});
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{'no-' . $o} and $opts->{$o} 0 9 9
Condition33%1/3
$$opts{'no-' . $o} and $opts->{$o}
!ll&&!rl&&r
900
MC/DC0%0/2
$$opts{'no-' . $o} and $opts->{$o}
$$opts{'no-' . $o} $opts->{$o}
Truth table33%1/3
A and B
A= $$opts{'no-' . $o}B= $opts->{$o}
ABresult
0X0
100
111
2480 }
2481 1 ▶ local $SWITCH_TO_NEW_DOCUMENTS = 1 if $opts->{'switch-to'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"switch-to"} 0 1 1
2482 1 ▶ local $SWITCH_TO_NEW_DOCUMENTS = 0 if $opts->{'no-switch-to'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-switch-to"} 0 1 1
2483 1 ▶ local $VALIDATION = 1 if $opts->{validate};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"validate"} 0 1 1
2484 1 ▶ local $VALIDATION = 0 if $opts->{'no-validate'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-validate"} 0 1 1
2485 1 ▶ local $RECOVERING = 1 if $opts->{recover};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"recover"} 0 1 1
2486 1 ▶ local $RECOVERING = 0 if $opts->{'no-recover'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-recover"} 0 1 1
2487 1 ▶ local $PARSER_EXPANDS_ENTITIES = 1 if $opts->{'expand-entities'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"expand-entities"} 0 1 1
2488 1 ▶ local $PARSER_EXPANDS_ENTITIES = 0 if $opts->{'no-expand-entities'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-expand-entities"} 0 1 1
2489 1 ▶ local $KEEP_BLANKS = 1 if $opts->{'keep-blanks'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"keep-blanks"} 0 1 1
2490 1 ▶ local $KEEP_BLANKS = 0 if $opts->{'no-keep-blanks'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-keep-blanks"} 0 1 1
2491 1 ▶ local $PEDANTIC_PARSER = 1 if $opts->{pedantic};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"pedantic"} 0 1 1
2492 1 ▶ local $PEDANTIC_PARSER = 0 if $opts->{'no-pedantic'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-pedantic"} 0 1 1
2493 1 ▶ local $LOAD_EXT_DTD = 1 if $opts->{'load-ext-dtd'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"load-ext-dtd"} 0 1 1
2494 1 ▶ local $LOAD_EXT_DTD = 0 if $opts->{'no-load-ext-dtd'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-load-ext-dtd"} 0 1 1
2495 1 ▶ local $PARSER_COMPLETES_ATTRIBUTES = 1 if $opts->{'complete-attributes'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"complete-attributes"} 0 1 1
2496 1 ▶ local $PARSER_COMPLETES_ATTRIBUTES = 0 if $opts->{'no-complete-attributes'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-complete-attributes"} 0 1 1
2497 1 ▶ local $PARSER_EXPANDS_XINCLUDE = 1 if $opts->{'xinclude'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"xinclude"} 0 1 1
2498 1 ▶ local $PARSER_EXPANDS_XINCLUDE = 0 if $opts->{'no-xinclude'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-xinclude"} 0 1 1
2499  
2500 1 my ($source) = grep exists($opts->{$_}),qw(file pipe string);
2501 1 my $file;
2502 1 ▶ unless ($source eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq 'string') { } 0 1 1
2503 1 $file = _tilde_expand(_ev_string($src));
2504 # $file=~s{^(\~[^\/]*)}{(glob($1))[0]}eg;
2505 1 ▶ if ($source eq 'file' and !_is_absolute($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq "file" and not _is_absolute($file)) 0 1 1
Condition33%1/3
$source eq "file" and not _is_absolute($file)
!ll&&!rl&&r
100
MC/DC0%0/2
$source eq "file" and not _is_absolute($file)
$source eq "file" not _is_absolute($file)
Truth table33%1/3
A and not(B)
A= $source eq "file"B= not _is_absolute($file)
ABresult
0X0
100
111
2506 0 $file = File::Spec->rel2abs($file);
2507 }
2508 1 ▶ print STDERR "open [$file]\n" if "$DEBUG";
Branch: 1 branch
BranchTrueFalseTotal
if "$XML::XSH2::Functions::DEBUG" 0 1 1
2509 1 ▶ if ($file eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq "") 0 1 1
2510 0 die "filename is empty (hint: \$variable := open file-name)\n";
2511 }
2512 } else {
2513 0 $file = _ev_string($src);
2514 0 ▶ print STDERR "open [<STRING>]\n" if "$DEBUG";
Branch: 1 branch
BranchTrueFalseTotal
if "$XML::XSH2::Functions::DEBUG" 0 0 0
2515 0 ▶ if ($file eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq "") 0 0 0
2516 0 die "string is empty\n";
2517 }
2518 }
2519  
2520 1 ▶ if (($source ne 'file') or
Branch: 1 branch
BranchTrueFalseTotal
if ($source ne 'file' or -f $file or $file eq '-' or $file =~ /^[a-z]+:/) { } 1 0 1
Condition33%3/9
$source ne "file" or -f $file or $file eq "-" or $file =~ /^[a-z]+:/
l!l&&r!l&&!r
100
$source ne "file" or -f $file or $file eq "-"
l!l&&r!l&&!r
100
$source ne "file" or -f $file
l!l&&r!l&&!r
100
MC/DC0%0/4
$source ne "file" or -f $file or $file eq "-" or $file =~ /^[a-z]+:/
$source ne "file" -f $file $file eq "-" $file =~ /^[a-z]+:/
Truth table20%1/5
A or B or C or D
A= $source ne "file"B= -f $fileC= $file eq "-"D= $file =~ /^[a-z]+:/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
2521 (-f $file) or $file eq "-" or
2522 ($file=~/^[a-z]+:/)) {
2523 1 ▶ unless ("$QUIET") {
Branch: 1 branch
BranchTrueFalseTotal
unless ("$XML::XSH2::Functions::QUIET") 1 0 1
2524 1 ▶ if ($source eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq 'string') { } 0 1 1
2525 0 print STDERR "parsing string\n";
2526 } else {
2527 1 print STDERR "parsing $file\n";
2528 }
2529 }
2530 1 my $doc;
2531 1 ▶ if ($source eq 'pipe') {
Branch: 2 branches
BranchTrueFalseTotal
if ($source eq 'pipe') { } 0 1 1
elsif ($source eq 'string') { } 0 1 1
2532 0 ▶ open my $F,"$file|" || die "Can't open pipe: $!\n";
Condition0%0/2
"$file|" || die("Can't open pipe: $!\n")
l!l
00
MC/DC0%0/1
"$file|" || die("Can't open pipe: $!\n")
"$file|"
Truth table0%0/2
A
A= "$file|"
Aresult
11
00
2533 0 ▶ $F || die "Cannot open pipe to $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$F or die "Cannot open pipe to ${file}: $!\n" 0 0 0
2534 0 eval {
2535 0 ▶ if ($format eq 'xml') {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'xml') { } 0 0 0
elsif ($format eq 'html') { } 0 0 0
elsif ($format eq 'docbook') { } 0 0 0
2536 0 $doc=$_xml_module->parse_fh($_parser,$F);
2537 } elsif ($format eq 'html') {
2538 0 $doc=$_xml_module->parse_html_fh($_parser,$F);
2539 } elsif ($format eq 'docbook') {
2540 0 $doc=$_xml_module->parse_sgml_fh($_parser,$F,$QUERY_ENCODING);
2541 }
2542 };
2543 0 close $F;
2544 0 _check_err($@,1,1);
2545 } elsif ($source eq 'string') {
2546 0 my $root_element=$file;
2547 0 ▶ $root_element="<$root_element/>" unless ($root_element=~/^\s*</);
Branch: 1 branch
BranchTrueFalseTotal
unless $root_element =~ /^\s*</ 0 0 0
2548 0 $root_element=~s/^\s+//;
2549 0 eval {
2550 0 $doc=xsh_parse_string($root_element,$format);
2551 };
2552 0 _check_err($@,1,1);
2553 0 ▶ die "Failed to parse string\n" unless (ref($doc));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc 0 0 0
2554 0 $_newdoc++;
2555 } else {
2556 1 eval {
2557 1 ▶ if ($format eq 'xml') {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'xml') { } 1 0 1
elsif ($format eq 'html') { } 0 0 0
elsif ($format eq 'docbook') { } 0 0 0
2558 1 $doc=$_xml_module->parse_file($_parser,$file);
2559 } elsif ($format eq 'html') {
2560 0 $doc=$_xml_module->parse_html_file($_parser,$file);
2561 } elsif ($format eq 'docbook') {
2562 0 $doc=$_xml_module->parse_sgml_file($_parser,$file,$QUERY_ENCODING);
2563 }
2564 };
2565 1 _check_err($@,1,1);
2566 1 ▶ die "Failed to parse $file as $format\n" unless (ref($doc));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc 0 1 1
2567 }
2568 1 ▶ print STDERR "done.\n" unless "$QUIET";
Branch: 1 branch
BranchTrueFalseTotal
unless "$XML::XSH2::Functions::QUIET" 1 0 1
2569 1 ▶ _set_context([$doc]) if $SWITCH_TO_NEW_DOCUMENTS;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::SWITCH_TO_NEW_DOCUMENTS 1 0 1
2570 1 return $doc;
2571 } else {
2572 0 die "file doesn't exist: $file\n";
2573 0 return 0;
2574 }
2575 }
2576  
2577 0 ▶sub open_io_file {
Subroutine open_io_file: not called CC=3 SCAR=24.8
Pod undocumented
2578 0 my ($file)=@_;
2579 0 ▶ if ($file=~/^\s*[|>]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /^\s*[|>]/) { } 0 0 0
elsif ($file =~ /.gz\s*$/) { } 0 0 0
2580 0 return IO::File->new($file);
2581 } elsif ($file=~/.gz\s*$/) {
2582 0 return IO::File->new("| gzip -c > $file");
2583 } else {
2584 0 return IO::File->new(">$file");
2585 }
2586 }
2587  
2588 0 ▶sub is_xinclude {
Subroutine is_xinclude: not called CC=4 SCAR=30.0
Pod undocumented
2589 0 my ($node)=@_;
2590 return
2591 0 ▶ $_xml_module->is_xinclude_start($node) ||
Condition0%0/9
$XML::XSH2::Functions::_xml_module->is_xinclude_start($node) || $XML::XSH2::Functions::_xml_module->is_element($node) && $node->namespaceURI eq $XML::XSH2::Functions::Xinclude_prefix && $node->localname eq 'include'
l!l&&r!l&&!r
000
$XML::XSH2::Functions::_xml_module->is_element($node) and $node->namespaceURI eq $XML::XSH2::Functions::Xinclude_prefix and $node->localname eq "include"
!ll&&!rl&&r
000
$XML::XSH2::Functions::_xml_module->is_element($node) and $node->namespaceURI eq $XML::XSH2::Functions::Xinclude_prefix
!ll&&!rl&&r
000
MC/DC0%0/4
$XML::XSH2::Functions::_xml_module->is_xinclude_start($node) || $XML::XSH2::Functions::_xml_module->is_element($node) && $node->namespaceURI eq $XML::XSH2::Functions::Xinclude_prefix && $node->localname eq 'include'
$XML::XSH2::Functions::_xml_module->is_xinclude_start($node) $XML::XSH2::Functions::_xml_module->is_element($node) $node->namespaceURI eq $XML::XSH2::Functions::Xinclude_prefix $node->localname eq "include"
Truth table0%0/5
A || B and C and D
A= $XML::XSH2::Functions::_xml_module->is_xinclude_start($node)B= $XML::XSH2::Functions::_xml_module->is_element($node)C= $node->namespaceURI eq $XML::XSH2::Functions::Xinclude_prefixD= $node->localname eq "include"
ABCDresult
1XXX1
01111
00XX0
010X0
01100
2592 ($_xml_module->is_element($node) and
2593 $node->namespaceURI() eq $Xinclude_prefix and
2594 $node->localname() eq 'include');
2595 }
2596  
2597 0 ▶sub xinclude_start_tag {
Subroutine xinclude_start_tag: not called CC=2 SCAR=17.9
Pod undocumented
2598 0 my ($xi)=@_;
2599 0 my %xinc = map { $_->nodeName() => $_->value() } $xi->attributes();
2600 0 ▶ $xinc{parse}='xml' if ($xinc{parse} eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $xinc{"parse"} eq "" 0 0 0
2601 0 return "<".$xi->nodeName()." xmlns:".$xi->prefix()."=\"".$Xinclude_prefix."\" href=\"".$xinc{href}."\" parse=\"".$xinc{parse}."\">";
2602 }
2603  
2604 0 ▶sub xinclude_end_tag {
Subroutine xinclude_end_tag: not called CC=1 SCAR=6.9
Pod undocumented
2605 0 my ($xi)=@_;
2606 0 return "</".$xi->nodeName().">";
2607 }
2608  
2609 0 ▶sub xinclude_print {
Subroutine xinclude_print: not called CC=29 SCAR=67.7
Pod undocumented
2610 0 my ($doc,$F,$node,$enc)=@_;
2611 0 ▶ return unless ref($node);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 0 0
2612 0 ▶ if ($_xml_module->is_element($node) || $_xml_module->is_document($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node)) { } 0 0 0
Condition0%0/3
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node)
l!l&&r!l&&!r
000
MC/DC0%0/2
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node)
$XML::XSH2::Functions::_xml_module->is_element($node) $XML::XSH2::Functions::_xml_module->is_document($node)
Truth table0%0/3
A or B
A= $XML::XSH2::Functions::_xml_module->is_element($node)B= $XML::XSH2::Functions::_xml_module->is_document($node)
ABresult
1X1
011
000
2613 0 ▶ $F->print(fromUTF8($enc,start_tag($node))) if $_xml_module->is_element($node);
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::_xml_module->is_element($node) 0 0 0
2614 0 my $child=$node->firstChild();
2615 0 ▶ while ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 0 0 0
2616 0 ▶ if (is_xinclude($child)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_xinclude($child)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_xinclude_end($child)) { } 0 0 0
2617 0 my %xinc = map { $_->nodeName() => $_->value() } $child->attributes();
2618 0 ▶ $xinc{parse}||='xml';
Condition0%0/2
$xinc{"parse"} ||= "xml"
l!l
00
MC/DC0%0/1
$xinc{"parse"} ||= "xml"
$xinc{"parse"}
Truth table0%0/2
A
A= $xinc{"parse"}
Aresult
11
00
2619 0 ▶ $xinc{encoding}||=$enc; # may be used even to convert included XML
Condition0%0/3
$xinc{"encoding"} ||= $enc
l!l&&r!l&&!r
000
MC/DC0%0/2
$xinc{"encoding"} ||= $enc
$xinc{"encoding"} $enc
Truth table0%0/3
A ||= B
A= $xinc{"encoding"}B= $enc
ABresult
1X1
011
000
2620 0 my $elements=0;
2621 0 my @nodes=();
2622 0 my $node;
2623 0 my $expanded=$_xml_module->is_xinclude_start($child);
2624 0 ▶ if ($expanded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expanded) { } 0 0 0
2625 0 $node=$child->nextSibling(); # in case of special XINCLUDE node
2626 } else {
2627 0 $node=$child->firstChild(); # in case of include element from XInclude NS
2628 }
2629 0 my $nested=0;
2630 0 ▶ while ($node and not($_xml_module->is_xinclude_end($node)
Branch: 1 branch
BranchTrueFalseTotal
if ($node and not $XML::XSH2::Functions::_xml_module->is_xinclude_end($node) && $nested == 0 && $expanded) 0 0 0
Condition0%0/9
$node and not $XML::XSH2::Functions::_xml_module->is_xinclude_end($node) && $nested == 0 && $expanded
!ll&&!rl&&r
000
$XML::XSH2::Functions::_xml_module->is_xinclude_end($node) && $nested == 0 && $expanded
!ll&&!rl&&r
000
$XML::XSH2::Functions::_xml_module->is_xinclude_end($node) and $nested == 0
!ll&&!rl&&r
000
MC/DC0%0/4
$node and not $XML::XSH2::Functions::_xml_module->is_xinclude_end($node) && $nested == 0 && $expanded
$node $XML::XSH2::Functions::_xml_module->is_xinclude_end($node) $nested == 0 $expanded
Truth table0%0/5
A and not(B and C && D)
A= $nodeB= $XML::XSH2::Functions::_xml_module->is_xinclude_end($node)C= $nested == 0D= $expanded
ABCDresult
0XXX0
11110
10XX1
110X1
11101
2631 and $nested==0
2632 and $expanded)) {
2633 0 ▶ if ($_xml_module->is_xinclude_start($node)) { $nested++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_xinclude_start($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_xinclude_end($node)) { } 0 0 0
2634 0 elsif ($_xml_module->is_xinclude_end($node)) { $nested-- }
2635 0 push @nodes,$node;
2636 0 ▶ $elements++ if $_xml_module->is_element($node);
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::_xml_module->is_element($node) 0 0 0
2637 0 $node=$node->nextSibling();
2638 }
2639 0 ▶ if ($nested>0) {
Branch: 5 branches
BranchTrueFalseTotal
if ($nested > 0) { } 0 0 0
elsif (not $node and $XML::XSH2::Functions::_xml_module->is_xinclude_start($child)) { } 0 0 0
elsif ($xinc{'parse'} ne 'text' and $elements == 0) { } 0 0 0
elsif ($xinc{'parse'} ne 'xml' and $elements > 1) { } 0 0 0
elsif ($xinc{'parse'} eq 'text' and $elements > 0) { } 0 0 0
2640 0 print STDERR "Error: Unbalanced nested XInclude nodes.\n",
2641 " Ignoring this XInclude span!\n";
2642 0 $F->print("<!-- ".fromUTF8($enc,xinclude_start_tag($child))." -->");
2643 ▶ } elsif (!$node and $_xml_module->is_xinclude_start($child)) {
Condition0%0/3
not $node and $XML::XSH2::Functions::_xml_module->is_xinclude_start($child)
!ll&&!rl&&r
000
MC/DC0%0/2
not $node and $XML::XSH2::Functions::_xml_module->is_xinclude_start($child)
not $node $XML::XSH2::Functions::_xml_module->is_xinclude_start($child)
Truth table0%0/3
not(A) and B
A= not $nodeB= $XML::XSH2::Functions::_xml_module->is_xinclude_start($child)
ABresult
0X0
100
111
2644 0 print STDERR "Error: XInclude end node not found.\n",
2645 " Ignoring this XInclude span!\n";
2646 0 $F->print("<!-- ".fromUTF8($enc,xinclude_start_tag($child))." -->");
2647 ▶ } elsif ($xinc{parse} ne 'text' and $elements==0) {
Condition0%0/3
$xinc{"parse"} ne "text" and $elements == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$xinc{"parse"} ne "text" and $elements == 0
$xinc{"parse"} ne "text" $elements == 0
Truth table0%0/3
A and B
A= $xinc{"parse"} ne "text"B= $elements == 0
ABresult
0X0
100
111
2648 0 print STDERR "Warning: XInclude: No elements found in XInclude span.\n",
2649 " Ignoring whole XInclude span!\n";
2650 0 $F->print("<!-- ".fromUTF8($enc,xinclude_start_tag($child))." -->");
2651 ▶ } elsif ($xinc{parse} ne 'xml' and $elements>1) {
Condition0%0/3
$xinc{"parse"} ne "xml" and $elements > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$xinc{"parse"} ne "xml" and $elements > 1
$xinc{"parse"} ne "xml" $elements > 1
Truth table0%0/3
A and B
A= $xinc{"parse"} ne "xml"B= $elements > 1
ABresult
0X0
100
111
2652 0 print STDERR "Error: XInclude: More than one element found in XInclude span.\n",
2653 " Ignoring whole XInclude span!\n";
2654 0 $F->print("<!-- ".fromUTF8($enc,xinclude_start_tag($child))." -->");
2655 ▶ } elsif ($xinc{parse} eq 'text' and $elements>0) {
Condition0%0/3
$xinc{"parse"} eq "text" and $elements > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$xinc{"parse"} eq "text" and $elements > 0
$xinc{"parse"} eq "text" $elements > 0
Truth table0%0/3
A and B
A= $xinc{"parse"} eq "text"B= $elements > 0
ABresult
0X0
100
111
2656 0 print STDERR "Warning: XInclude: Element(s) found in textual XInclude span.\n",
2657 " Skipping whole XInclude span!\n";
2658 0 $F->print("<!-- ".fromUTF8($enc,xinclude_start_tag($child))." -->");
2659 } else {
2660 0 $F->print(fromUTF8($enc,xinclude_start_tag($child)));
2661 0 save_xinclude_chunk($doc,\@nodes,$xinc{href},$xinc{parse},$xinc{encoding});
2662 0 $F->print(fromUTF8($enc,xinclude_end_tag($child)));
2663 0 ▶ $child=$node if ($expanded); # jump to XINCLUDE end node
Branch: 1 branch
BranchTrueFalseTotal
if $expanded 0 0 0
2664 }
2665 } elsif ($_xml_module->is_xinclude_end($child)) {
2666 0 $F->print("<!-- ".fromUTF8($enc,xinclude_end_tag($child))." -->");
2667 } else {
2668 0 xinclude_print($doc,$F,$child,$enc); # call recursion
2669 }
2670 0 $child=$child->nextSibling();
2671 }
2672 0 ▶ $F->print(fromUTF8($enc,end_tag($node))) if $_xml_module->is_element($node);
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::_xml_module->is_element($node) 0 0 0
2673 } else {
2674 0 $F->print(fromUTF8($enc,$_xml_module->toStringUTF8($node,$INDENT)));
2675 }
2676 }
2677  
2678 4 ▶sub _xml_decl {
Subroutine _xml_decl: called CC=8 SCAR=26.3
2679 4 my ($doc,$version,$enc) = @_;
2680 4 ▶ $version=($doc->can('getVersion') ? $doc->getVersion() : '1.0')
Branch: 2 branches
BranchTrueFalseTotal
if $doc and not defined $version 4 0 4
$doc->can('getVersion') ? : 4 0 4
Condition33%1/3
$doc and not defined $version
!ll&&!rl&&r
004
MC/DC0%0/2
$doc and not defined $version
$doc not defined $version
Truth table33%1/3
A and not(B)
A= $docB= not defined $version
ABresult
0X0
100
111
2681 if ($doc and !defined $version);
2682 4 ▶ $enc=($doc->can('getEncoding') ? $doc->getEncoding() : undef)
Branch: 2 branches
BranchTrueFalseTotal
if $doc and not defined $enc 4 0 4
$doc->can('getEncoding') ? : 4 0 4
Condition33%1/3
$doc and not defined $enc
!ll&&!rl&&r
004
MC/DC0%0/2
$doc and not defined $enc
$doc not defined $enc
Truth table33%1/3
A and not(B)
A= $docB= not defined $enc
ABresult
0X0
100
111
2683 if ($doc and !defined $enc);
2684 4 ▶ return "<?xml version='$version'".(defined($enc) ? " encoding='$enc'?>" : "?>");
Branch: 1 branch
BranchTrueFalseTotal
defined $enc ? : 4 0 4
2685 }
2686  
2687 0 ▶sub save_xinclude_chunk {
Subroutine save_xinclude_chunk: not called CC=7 SCAR=40.3
Pod undocumented
2688 0 my ($doc,$nodes,$file,$parse,$enc)=@_;
2689  
2690 0 ▶ return unless @$nodes>0;
Branch: 1 branch
BranchTrueFalseTotal
unless @$nodes > 0 0 0 0
2691  
2692 0 ▶ if ($BACKUPS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::BACKUPS) 0 0 0
2693 0 eval { rename $file, $file."~"; };
2694 0 _check_err($@);
2695 }
2696 0 my $F=open_io_file($file);
2697 0 ▶ $F || die "Cannot open $file\n";
Branch: 1 branch
BranchTrueFalseTotal
$F or die "Cannot open $file\n" 0 0 0
2698  
2699 0 ▶ if ($parse eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse eq 'text') { } 0 0 0
2700 0 foreach my $node (@$nodes) {
2701 0 $F->print(fromUTF8($enc,literal_value($node->to_literal)));
2702 }
2703 } else {
2704 0 $F->print(_xml_decl($doc,undef,$enc),"\n");
2705 0 foreach my $node (@$nodes) {
2706 0 xinclude_print($doc,$F,$node,$enc);
2707 }
2708 0 $F->print("\n");
2709 }
2710 0 $F->close();
2711 }
2712  
2713 # save a document
2714 0 ▶sub save_doc {
Subroutine save_doc: not called CC=72 SCAR=85.3
Pod undocumented
2715 0 my ($opts,$exp)=@_;
2716 0 $opts = _ev_opts($opts);
2717 0 my ($doc,$node);
2718 0 ▶ if ($opts->{subtree}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'subtree'}) { } 0 0 0
2719 0 ▶ $exp ||= '.';
Condition0%0/2
$exp ||= "."
l!l
00
MC/DC0%0/1
$exp ||= "."
$exp
Truth table0%0/2
A
A= $exp
Aresult
11
00
2720 0 ($node)=_ev_nodelist($exp)->pop();
2721 0 ▶ $doc = $_xml_module->owner_document($node) if $node
Branch: 1 branch
BranchTrueFalseTotal
if $node 0 0 0
2722 } else {
2723 0 $node = $doc = _ev_doc($exp);
2724 }
2725 0 ▶ die "No document to save\n" unless ($node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
2726  
2727 0 ▶ $opts->{file} = _tilde_expand($opts->{file}) if exists($opts->{file});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"file"} 0 0 0
2728 0 ▶ if (exists($opts->{file})+exists($opts->{pipe})+
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"file"} + exists $opts->{"pipe"} + exists $opts->{"print"} + exists $opts->{"string"} > 1) 0 0 0
2729 exists($opts->{print})+exists($opts->{string})>1) {
2730 0 die "'save' may have only one output flag: --file | ".
2731 "--pipe | --print | --string\n";
2732 }
2733 0 foreach my $o (qw(indent skip-dtd empty-tags skip-xmldecl backup)) {
2734 die "Can't use --$o and --no-$o together\n"
2735 0 ▶ if ($opts->{'no-'.$o} and $opts->{$o});
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{'no-' . $o} and $opts->{$o} 0 0 0
Condition0%0/3
$$opts{'no-' . $o} and $opts->{$o}
!ll&&!rl&&r
000
MC/DC0%0/2
$$opts{'no-' . $o} and $opts->{$o}
$$opts{'no-' . $o} $opts->{$o}
Truth table0%0/3
A and B
A= $$opts{'no-' . $o}B= $opts->{$o}
ABresult
0X0
100
111
2736 }
2737  
2738 0 ▶ local $INDENT=1 if $opts->{indent};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"indent"} 0 0 0
2739 0 ▶ local $XML::LibXML::skipDTD = 1 if $opts->{'skip-dtd'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"skip-dtd"} 0 0 0
2740 0 ▶ local $XML::LibXML::setTagCompression = 1 if $opts->{'empty-tags'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"empty-tags"} 0 0 0
2741 0 ▶ local $XML::LibXML::skipXMLDeclaration = 1 if $opts->{'skip-xmldecl'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"skip-xmldecl"} 0 0 0
2742  
2743 0 ▶ local $INDENT=0 if $opts->{'no-indent'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-indent"} 0 0 0
2744 0 ▶ local $XML::LibXML::skipDTD = 0 if $opts->{'no-skip-dtd'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-skip-dtd"} 0 0 0
2745 0 ▶ local $XML::LibXML::setTagCompression = 0 if $opts->{'no-empty-tags'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-empty-tags"} 0 0 0
2746 0 ▶ local $XML::LibXML::skipXMLDeclaration = 0 if $opts->{'no-skip-xmldecl'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-skip-xmldecl"} 0 0 0
2747  
2748 0 ▶ local $BACKUPS = 0 if $opts->{'no-backup'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-backup"} 0 0 0
2749 0 ▶ local $BACKUPS = 1 if $opts->{'backup'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"backup"} 0 0 0
2750  
2751 #__debug("$XML::LibXML::skipXMLDeclaration\n");
2752  
2753 0 my $format = $DEFAULT_FORMAT;
2754  
2755 0 ▶ if (exists($opts->{format})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"format"}) 0 0 0
2756 0 $format=lc($opts->{format});
2757 }
2758 0 ▶ if (exists($opts->{xinclude})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"xinclude"}) 0 0 0
2759 0 ▶ if ($format eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "html") 0 0 0
2760 0 die "'save --xinclude' can only be used with XML format\n"
2761 }
2762 0 $format = 'xinclude';
2763 }
2764  
2765 0 ▶ die "'save --subtree' can't be used with HTML format\n" if ($format eq 'html' and $opts->{subtree});
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "html" and $opts->{"subtree"} 0 0 0
Condition0%0/3
$format eq "html" and $opts->{"subtree"}
!ll&&!rl&&r
000
MC/DC0%0/2
$format eq "html" and $opts->{"subtree"}
$format eq "html" $opts->{"subtree"}
Truth table0%0/3
A and B
A= $format eq "html"B= $opts->{"subtree"}
ABresult
0X0
100
111
2766  
2767 0 my ($target) = grep exists($opts->{$_}),qw(file pipe string print);
2768 0 ▶ $target = 'file' unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 0 0
2769 0 ▶ my $file; $file = $opts->{$target} if $target;
Branch: 1 branch
BranchTrueFalseTotal
if $target 0 0 0
2770 0 ▶ if ($target eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target eq "file") 0 0 0
2771 0 ▶ if ($file eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq '') { } 0 0 0
2772 0 $file=$doc->URI;
2773 } else {
2774 0 ▶ $doc->setBaseURI($file) if $doc->can('setBaseURI');
Branch: 1 branch
BranchTrueFalseTotal
if $doc->can("setBaseURI") 0 0 0
2775 }
2776 0 ▶ if ($BACKUPS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::BACKUPS) 0 0 0
2777 0 eval { rename $file, $file."~"; };
2778 0 _check_err($@);
2779 }
2780 }
2781  
2782 0 ▶ my $enc = $opts->{encoding} || $_xml_module->doc_encoding($doc) || 'utf-8';
Condition0%0/5
$opts->{'encoding'} || $XML::XSH2::Functions::_xml_module->doc_encoding($doc) || 'utf-8'
l!l
00
$opts->{"encoding"} or $XML::XSH2::Functions::_xml_module->doc_encoding($doc)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'encoding'} || $XML::XSH2::Functions::_xml_module->doc_encoding($doc) || 'utf-8'
$opts->{"encoding"} $XML::XSH2::Functions::_xml_module->doc_encoding($doc)
Truth table0%0/3
A or B
A= $opts->{"encoding"}B= $XML::XSH2::Functions::_xml_module->doc_encoding($doc)
ABresult
1X1
011
000
2783 0 ▶ print STDERR "saving to $target $file as $format (encoding $enc)\n" if "$DEBUG";
Branch: 1 branch
BranchTrueFalseTotal
if "$XML::XSH2::Functions::DEBUG" 0 0 0
2784  
2785 0 ▶ if ($format eq 'xinclude') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'xinclude') { } 0 0 0
elsif ($opts->{'subtree'}) { } 0 0 0
2786 0 ▶ if ($target ne 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target ne 'file') { } 0 0 0
2787 0 die "Target '".uc($target)."' not supported with 'save --xinclude'\n";
2788 } else {
2789 0 ▶ if ($doc->{subtree}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{'subtree'}) { } 0 0 0
2790 0 save_xinclude_chunk($doc,[$node],$file,'xml',$enc);
2791 } else {
2792 0 save_xinclude_chunk($doc,[$doc->childNodes()],$file,'xml',$enc);
2793 }
2794 }
2795 } elsif ($opts->{subtree}) {
2796 0 ▶ die "Unsupported format '$format'\n" unless $format eq 'xml';
Branch: 1 branch
BranchTrueFalseTotal
unless $format eq "xml" 0 0 0
2797 0 ▶ my $string =
Branch: 1 branch
BranchTrueFalseTotal
$target ne 'string' && lc($enc) =~ /^utf-?8$/i ? : 0 0 0
Condition0%0/3
$target ne "string" and lc($enc) =~ /^utf-?8$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$target ne "string" and lc($enc) =~ /^utf-?8$/i
$target ne "string" lc($enc) =~ /^utf-?8$/i
Truth table0%0/3
A and B
A= $target ne "string"B= lc($enc) =~ /^utf-?8$/i
ABresult
0X0
100
111
2798 _xml_decl($doc,undef,$enc)."\n".
2799 (($target ne 'string' and lc($enc) =~ /^utf-?8$/i) ?
2800 $node->toString($INDENT) : fromUTF8($enc,$node->toString($INDENT)))."\n";
2801 0 ▶ if ($target eq 'file') {
Branch: 4 branches
BranchTrueFalseTotal
if ($target eq 'file') { } 0 0 0
elsif ($target eq 'pipe') { } 0 0 0
elsif ($target eq 'string') { } 0 0 0
elsif ($target eq 'print') { } 0 0 0
2802 0 ▶ open my $F, '>', $file || die "Cannot open file $file\n";
Condition0%0/2
$file || die("Cannot open file $file\n")
l!l
00
MC/DC0%0/1
$file || die("Cannot open file $file\n")
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
2803 0 print {$F} ($string);
2804 0 close $F;
2805 } elsif ($target eq 'pipe') {
2806 0 $file=~s/^\s*\|?//g;
2807 0 ▶ open my $F,"| $file" || die "Cannot open pipe to $file\n";
Condition0%0/2
"| $file" || die("Cannot open pipe to $file\n")
l!l
00
MC/DC0%0/1
"| $file" || die("Cannot open pipe to $file\n")
"| $file"
Truth table0%0/2
A
A= "| $file"
Aresult
11
00
2808 0 print {$F} ($string);
2809 0 close $F;
2810 } elsif ($target eq 'string') {
2811 0 return $string;
2812 } elsif ($target eq 'print') {
2813 0 out($string);
2814 }
2815 } else {
2816 0 ▶ if ($format eq 'xml') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'xml') { } 0 0 0
elsif ($format eq 'html') { } 0 0 0
2817 0 ▶ if (lc($_xml_module->doc_encoding($doc)) ne lc($enc)
Branch: 1 branch
BranchTrueFalseTotal
if (lc $XML::XSH2::Functions::_xml_module->doc_encoding($doc) ne lc $enc and not $XML::XSH2::Functions::_xml_module->doc_encoding($doc) eq "" && lc $enc eq "utf-8") 0 0 0
Condition0%0/6
lc $XML::XSH2::Functions::_xml_module->doc_encoding($doc) ne lc $enc and not $XML::XSH2::Functions::_xml_module->doc_encoding($doc) eq "" && lc $enc eq "utf-8"
!ll&&!rl&&r
000
$XML::XSH2::Functions::_xml_module->doc_encoding($doc) eq '' && lc $enc eq 'utf-8'
!ll&&!rl&&r
000
MC/DC0%0/3
lc $XML::XSH2::Functions::_xml_module->doc_encoding($doc) ne lc $enc and not $XML::XSH2::Functions::_xml_module->doc_encoding($doc) eq "" && lc $enc eq "utf-8"
lc $XML::XSH2::Functions::_xml_module->doc_encoding($doc) ne lc $enc $XML::XSH2::Functions::_xml_module->doc_encoding($doc) eq '' lc $enc eq 'utf-8'
Truth table0%0/4
A and not(B && C)
A= lc $XML::XSH2::Functions::_xml_module->doc_encoding($doc) ne lc $encB= $XML::XSH2::Functions::_xml_module->doc_encoding($doc) eq ''C= lc $enc eq 'utf-8'
ABCresult
0XX0
1110
10X1
1101
2818 and not($_xml_module->doc_encoding($doc) eq "" and
2819 lc($enc) eq 'utf-8')
2820 ) {
2821 0 $_xml_module->set_encoding($doc,$enc);
2822 }
2823 0 ▶ if ($target eq 'file') {
Branch: 4 branches
BranchTrueFalseTotal
if ($target eq 'file') { } 0 0 0
elsif ($target eq 'pipe') { } 0 0 0
elsif ($target eq 'string') { } 0 0 0
elsif ($target eq 'print') { } 0 0 0
2824 0 ▶ if ($file=~/\.gz\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.gz\s*$/) { } 0 0 0
2825 0 $doc->setCompression(6);
2826 } else {
2827 0 $doc->setCompression(-1);
2828 }
2829 0 ▶ $doc->toFile($file,$INDENT)
Branch: 1 branch
BranchTrueFalseTotal
$doc->toFile($file, $XML::XSH2::Functions::INDENT) or _err("Saving $file failed!") 0 0 0
2830 or _err("Saving $file failed!"); # should be document-encoding encoded
2831 # TODO: we should set the URL here
2832 } elsif ($target eq 'pipe') {
2833 0 $file=~s/^\s*\|?//g;
2834 0 ▶ open my $F,"| $file" || die "Cannot open pipe to $file\n";
Condition0%0/2
"| $file" || die("Cannot open pipe to $file\n")
l!l
00
MC/DC0%0/1
"| $file" || die("Cannot open pipe to $file\n")
"| $file"
Truth table0%0/2
A
A= "| $file"
Aresult
11
00
2835 0 $doc->toFH($F,$INDENT);
2836 0 close $F;
2837 } elsif ($target eq 'string') {
2838 0 return toUTF8($_xml_module->doc_encoding($doc),
2839 $doc->toString($INDENT));
2840 } elsif ($target eq 'print') {
2841 0 out($doc->toString($INDENT));
2842 }
2843 } elsif ($format eq 'html') {
2844 0 my $F;
2845 0 ▶ if ($target eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target eq 'string') { } 0 0 0
2846 15 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
2847 0 _assign($1,'');
2848 0 ▶ my $out=
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::XSH2::Functions::_xml_module->has_dtd($doc) 0 0 0
2849 "<!DOCTYPE HTML ".
2850 "PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
2851 unless ($_xml_module->has_dtd($doc));
2852 0 $out.=toUTF8($_xml_module->doc_encoding($doc), $doc->toStringHTML());
2853 0 return $out;
2854 } else {
2855 0 ▶ if ($target eq 'file') {
Branch: 3 branches
BranchTrueFalseTotal
if ($target eq 'file') { } 0 0 0
elsif ($target eq 'pipe') { } 0 0 0
elsif ($target eq 'print') { } 0 0 0
2856 0 ▶ ($F=open_io_file($file)) || die "Cannot open $file\n";
Branch: 1 branch
BranchTrueFalseTotal
$F = open_io_file($file) or die "Cannot open $file\n" 0 0 0
2857 # TODO: we should set the URL here
2858 } elsif ($target eq 'pipe') {
2859 0 $file=~s/^\s*\|?//g;
2860 0 open $F,"| $file";
2861 0 ▶ $F || die "Cannot open pipe to $file\n";
Branch: 1 branch
BranchTrueFalseTotal
$F or die "Cannot open pipe to $file\n" 0 0 0
2862 } elsif ($target eq 'print') {
2863 0 $F=$OUT;
2864 }
2865 0 ▶ $F->print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::XSH2::Functions::_xml_module->has_dtd($doc) 0 0 0
2866 unless ($_xml_module->has_dtd($doc));
2867 0 ▶ $F->print(fromUTF8($enc, toUTF8($_xml_module->doc_encoding($doc) || 'utf-8',
Condition0%0/2
$XML::XSH2::Functions::_xml_module->doc_encoding($doc) || 'utf-8'
l!l
00
MC/DC0%0/1
$XML::XSH2::Functions::_xml_module->doc_encoding($doc) || 'utf-8'
$XML::XSH2::Functions::_xml_module->doc_encoding($doc)
Truth table0%0/2
A
A= $XML::XSH2::Functions::_xml_module->doc_encoding($doc)
Aresult
11
00
2868 $doc->toStringHTML())));
2869  
2870 0 ▶ $F->close() unless $target eq 'print';
Branch: 1 branch
BranchTrueFalseTotal
unless $target eq "print" 0 0 0
2871 }
2872 } else {
2873 0 die "Unknown format '$format'\n";
2874 }
2875 }
2876 0 ▶ print STDERR "Document saved into $target '$file'.\n" unless ($@ or $target eq 'print' or "$QUIET");
Branch: 1 branch
BranchTrueFalseTotal
unless $@ or $target eq "print" or "$XML::XSH2::Functions::QUIET" 0 0 0
Condition0%0/6
$@ or $target eq "print" or "$XML::XSH2::Functions::QUIET"
l!l&&r!l&&!r
000
$@ or $target eq "print"
l!l&&r!l&&!r
000
MC/DC0%0/3
$@ or $target eq "print" or "$XML::XSH2::Functions::QUIET"
$@ $target eq "print" "$XML::XSH2::Functions::QUIET"
Truth table0%0/4
A or B or C
A= $@B= $target eq "print"C= "$XML::XSH2::Functions::QUIET"
ABCresult
1XX1
01X1
0011
0000
2877 0 return 1;
2878 }
2879  
2880  
2881 # create start tag for an element
2882  
2883 ###
2884 ### Workaround of a bug in XML::LibXML:
2885 ### getNamespaces, getName returns prefix only,
2886 ### prefix returns prefix not xmlns, getAttributes contains also namespaces
2887 ### findnodes('namespace::*') returns (namespaces,undef)
2888 ###
2889  
2890 18 ▶sub start_tag {
Subroutine start_tag: called CC=4 SCAR=16.1
Pod undocumented
2891 18 my ($element,$fold_attrs)=@_;
2892 return "<".$element->nodeName().
2893 0 ($fold_attrs ? ((grep { $_->nodeName() ne "xsh:fold" }
2894 $element->attributes()) ? " ..." : "") :
2895 18 ▶ join("",map { " ".$_->nodeName()."=\"".$_->nodeValue()."\"" }
Branch: 3 branches
BranchTrueFalseTotal
$fold_attrs ? : 0 18 18
grep({$_->nodeName ne 'xsh:fold';} $element->attributes) ? : 0 0 0
$element->hasChildNodes ? : 13 5 18
2896 $element->attributes())
2897 )
2898 # findnodes('attribute::*'))
2899 # . join("",map { " xmlns:".$_->getName()."=\"".$_->nodeValue()."\"" }
2900 # $element->can('getNamespaces') ?
2901 # $element->getNamespaces() :
2902 # $element->findnodes('namespace::*')
2903 # )
2904 .($element->hasChildNodes() ? ">" : "/>");
2905 }
2906  
2907 # create close tag for an element
2908 13 ▶sub end_tag {
Subroutine end_tag: called CC=2 SCAR=7.2
Pod undocumented
2909 13 my ($element)=@_;
2910 13 ▶ return $element->hasChildNodes() ? "</".$element->getName().">" : "";
Branch: 1 branch
BranchTrueFalseTotal
$element->hasChildNodes ? : 13 0 13
2911 }
2912  
2913 # convert a subtree to an XML string to the given depth
2914 64 ▶sub to_string {
Subroutine to_string: called CC=26 SCAR=39.5
Pod undocumented
2915 64 my ($node,$depth,$folding,$fold_attrs)=@_;
2916 64 my $result;
2917 64 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 64 0 64
2918 64 ▶ if (ref($node) and $_xml_module->is_element($node) and $folding and
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node and $XML::XSH2::Functions::_xml_module->is_element($node) and $folding and $node->hasAttributeNS($XML::XSH2::xshNS, "fold")) 0 64 64
Condition55%5/9
ref $node and $XML::XSH2::Functions::_xml_module->is_element($node) and $folding and $node->hasAttributeNS($XML::XSH2::xshNS, "fold")
!ll&&!rl&&r
6400
ref $node and $XML::XSH2::Functions::_xml_module->is_element($node) and $folding
!ll&&!rl&&r
26380
ref $node and $XML::XSH2::Functions::_xml_module->is_element($node)
!ll&&!rl&&r
02638
MC/DC0%0/4
ref $node and $XML::XSH2::Functions::_xml_module->is_element($node) and $folding and $node->hasAttributeNS($XML::XSH2::xshNS, "fold")
ref $node $XML::XSH2::Functions::_xml_module->is_element($node) $folding $node->hasAttributeNS($XML::XSH2::xshNS, "fold")
Truth table40%2/5
A and B and C and D
A= ref $nodeB= $XML::XSH2::Functions::_xml_module->is_element($node)C= $foldingD= $node->hasAttributeNS($XML::XSH2::xshNS, "fold")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2919 $node->hasAttributeNS($XML::XSH2::xshNS,'fold')) {
2920 0 ▶ if ($depth>=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth >= 0) { } 0 0 0
2921 0 $depth = min($depth,$node->getAttributeNS($XML::XSH2::xshNS,'fold'));
2922 } else {
2923 0 $depth = $node->getAttributeNS($XML::XSH2::xshNS,'fold');
2924 }
2925 }
2926  
2927 64 ▶ if ($depth<0 and $folding==0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($depth < 0 and $folding == 0) { } 31 33 64
elsif (ref $node and $XML::XSH2::Functions::_xml_module->is_element($node) and $depth == 0) { } 7 26 33
elsif (ref $node and $XML::XSH2::Functions::_xml_module->is_document($node) and $depth == 0) { } 0 26 26
elsif ($depth > 0 or $folding) { } 15 11 26
Condition66%2/3
$depth < 0 and $folding == 0
!ll&&!rl&&r
33031
MC/DC50%1/2
$depth < 0 and $folding == 0
$depth < 0 $folding == 0
Truth table66%2/3
A and B
A= $depth < 0B= $folding == 0
ABresult
0X0
100
111
2928 31 ▶ $result=ref($node) ? $_xml_module->toStringUTF8($node,$INDENT) : $node;
Branch: 1 branch
BranchTrueFalseTotal
ref $node ? : 31 0 31
2929 ▶ } elsif (ref($node) and $_xml_module->is_element($node) and $depth==0) {
Condition83%5/6
ref $node and $XML::XSH2::Functions::_xml_module->is_element($node) and $depth == 0
!ll&&!rl&&r
15117
ref $node and $XML::XSH2::Functions::_xml_module->is_element($node)
!ll&&!rl&&r
01518
MC/DC66%2/3
ref $node and $XML::XSH2::Functions::_xml_module->is_element($node) and $depth == 0
ref $node $XML::XSH2::Functions::_xml_module->is_element($node) $depth == 0
Truth table75%3/4
A and B and C
A= ref $nodeB= $XML::XSH2::Functions::_xml_module->is_element($node)C= $depth == 0
ABCresult
0XX0
10X0
1100
1111
2930 7 ▶ $result=start_tag($node,$fold_attrs).
Branch: 1 branch
BranchTrueFalseTotal
$node->hasChildNodes ? : 2 5 7
2931 ($node->hasChildNodes() ? "...".end_tag($node) : "");
2932 ▶ } elsif (ref($node) and $_xml_module->is_document($node) and $depth==0) {
Condition66%4/6
ref $node and $XML::XSH2::Functions::_xml_module->is_document($node) and $depth == 0
!ll&&!rl&&r
2240
ref $node and $XML::XSH2::Functions::_xml_module->is_document($node)
!ll&&!rl&&r
0224
MC/DC0%0/3
ref $node and $XML::XSH2::Functions::_xml_module->is_document($node) and $depth == 0
ref $node $XML::XSH2::Functions::_xml_module->is_document($node) $depth == 0
Truth table50%2/4
A and B and C
A= ref $nodeB= $XML::XSH2::Functions::_xml_module->is_document($node)C= $depth == 0
ABCresult
0XX0
10X0
1100
1111
2933 0 $result=to_string($node,1,$folding,$fold_attrs);
2934 ▶ } elsif ($depth>0 or $folding) {
Condition66%2/3
$depth > 0 or $folding
l!l&&r!l&&!r
15011
MC/DC50%1/2
$depth > 0 or $folding
$depth > 0 $folding
Truth table66%2/3
A or B
A= $depth > 0B= $folding
ABresult
1X1
011
000
2935 15 ▶ if (!ref($node)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $node) { } 0 15 15
elsif ($XML::XSH2::Functions::_xml_module->is_element($node)) { } 11 4 15
elsif ($XML::XSH2::Functions::_xml_module->is_document($node)) { } 4 0 4
2936 0 $result=$node;
2937 } elsif ($_xml_module->is_element($node)) {
2938 $result= start_tag($node).
2939 11 join("",map { to_string($_,$depth-1,$folding,$fold_attrs) } $node->childNodes).
2940 end_tag($node);
2941 } elsif ($_xml_module->is_document($node)) {
2942 4 ▶ if ($node->can('getVersion') and $node->can('getEncoding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->can("getVersion") and $node->can("getEncoding")) 4 0 4
Condition33%1/3
$node->can("getVersion") and $node->can("getEncoding")
!ll&&!rl&&r
004
MC/DC0%0/2
$node->can("getVersion") and $node->can("getEncoding")
$node->can("getVersion") $node->can("getEncoding")
Truth table33%1/3
A and B
A= $node->can("getVersion")B= $node->can("getEncoding")
ABresult
0X0
100
111
2943 4 $result=_xml_decl($node,undef,undef)."\n";
2944 }
2945 $result.=
2946 5 join("\n",map { to_string($_,$depth-1,$folding,$fold_attrs) }
2947 4 ▶ grep { $SKIP_DTD ? !$_xml_module->is_dtd($_) : 1 } $node->childNodes);
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::SKIP_DTD ? : 0 5 5
2948 } else {
2949 0 $result=$_xml_module->toStringUTF8($node,$INDENT);
2950 }
2951 } else {
2952 11 ▶ $result = ref($node) ? $_xml_module->toStringUTF8($node,$INDENT) : $node;
Branch: 1 branch
BranchTrueFalseTotal
ref $node ? : 11 0 11
2953 }
2954 }
2955 64 return $result;
2956 }
2957  
2958 # list nodes matching given XPath argument to a given depth
2959 33 ▶sub list {
Subroutine list: called CC=15 SCAR=32.0
Pod undocumented
2960 33 my ($opts,$exp)=@_;
2961 33 my $opts = _ev_opts($opts);
2962 33 ▶ $opts->{depth} = ($exp eq '' ? 1 : -1) unless exists($opts->{depth});
Branch: 2 branches
BranchTrueFalseTotal
unless exists $opts->{"depth"} 32 1 33
$exp eq '' ? : 15 17 32
2963 33 ▶ $exp = '.' if $exp eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $exp eq "" 15 18 33
2964 33 ▶ if ($opts->{noindent} and $opts->{indent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"noindent"} and $opts->{"indent"}) 0 33 33
Condition33%1/3
$opts->{"noindent"} and $opts->{"indent"}
!ll&&!rl&&r
3300
MC/DC0%0/2
$opts->{"noindent"} and $opts->{"indent"}
$opts->{"noindent"} $opts->{"indent"}
Truth table33%1/3
A and B
A= $opts->{"noindent"}B= $opts->{"indent"}
ABresult
0X0
100
111
2965 0 die "Can't use --indent and --no-indent together\n";
2966 }
2967 33 ▶ local $INDENT=1 if $opts->{indent};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"indent"} 0 33 33
2968 33 ▶ local $INDENT=0 if $opts->{'no-indent'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"no-indent"} 0 33 33
2969 33 my $ql=_ev_nodelist($exp);
2970 33 foreach my $node (@$ql) {
2971 46 ▶ print STDERR "checking for folding\n" if "$DEBUG";
Branch: 1 branch
BranchTrueFalseTotal
if "$XML::XSH2::Functions::DEBUG" 0 46 46
2972 my $fold=$opts->{fold} &&
2973 46 ▶ ($_xml_module->is_element($node) || $_xml_module->is_document($node)) &&
Condition22%2/9
$opts->{'fold'} && ($XML::XSH2::Functions::_xml_module->is_element($node) || $XML::XSH2::Functions::_xml_module->is_document($node)) && $node->findvalue("count(.//\@*[local-name()='fold' and namespace-uri()='$XML::XSH2::xshNS'])")
!ll&&!rl&&r
4600
$opts->{"fold"} and $XML::XSH2::Functions::_xml_module->is_element($node) || $XML::XSH2::Functions::_xml_module->is_document($node)
!ll&&!rl&&r
4600
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node)
l!l&&r!l&&!r
000
MC/DC0%0/4
$opts->{'fold'} && ($XML::XSH2::Functions::_xml_module->is_element($node) || $XML::XSH2::Functions::_xml_module->is_document($node)) && $node->findvalue("count(.//\@*[local-name()='fold' and namespace-uri()='$XML::XSH2::xshNS'])")
$opts->{"fold"} $XML::XSH2::Functions::_xml_module->is_element($node) $XML::XSH2::Functions::_xml_module->is_document($node) $node->findvalue("count(.//\@*[local-name()='fold' and namespace-uri()='$XML::XSH2::xshNS'])")
Truth table16%1/6
A and B or C && D
A= $opts->{"fold"}B= $XML::XSH2::Functions::_xml_module->is_element($node)C= $XML::XSH2::Functions::_xml_module->is_document($node)D= $node->findvalue("count(.//\@*[local-name()='fold' and namespace-uri()='$XML::XSH2::xshNS'])")
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
2974 $node->findvalue("count(.//\@*[local-name()='fold' and namespace-uri()='$XML::XSH2::xshNS'])");
2975 46 ▶ print STDERR "folding: $fold\n" if "$DEBUG";
Branch: 1 branch
BranchTrueFalseTotal
if "$XML::XSH2::Functions::DEBUG" 0 46 46
2976 46 out (to_string($node,$opts->{depth},$fold,$opts->{'fold-attrs'}),"\n");
2977 }
2978 33 ▶ print STDERR "\nFound ",scalar(@$ql)," node(s).\n" unless "$QUIET";
Branch: 1 branch
BranchTrueFalseTotal
unless "$XML::XSH2::Functions::QUIET" 0 33 33
2979  
2980 33 return 1;
2981 }
2982  
2983 # list namespaces in scope of the given nodes
2984 0 ▶sub list_namespaces {
Subroutine list_namespaces: not called CC=12 SCAR=50.5
Pod undocumented
2985 0 my ($opts,$exp) = @_;
2986 0 $opts = _ev_opts($opts);
2987 0 ▶ my $ql= ($opts->{registered} and $exp eq "") ? [] : _ev_nodelist(defined $exp ? $exp : '.');
Branch: 2 branches
BranchTrueFalseTotal
$opts->{'registered'} && $exp eq '' ? : 0 0 0
defined $exp ? : 0 0 0
Condition0%0/3
$opts->{"registered"} and $exp eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"registered"} and $exp eq ""
$opts->{"registered"} $exp eq ""
Truth table0%0/3
A and B
A= $opts->{"registered"}B= $exp eq ""
ABresult
0X0
100
111
2988 0 foreach my $node (@$ql) {
2989 0 my $n=$node;
2990 0 my %namespaces;
2991 0 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 0 0 0
2992 0 foreach my $ns ($n->getNamespaces) {
2993 $namespaces{$ns->localname()}=$ns->value()
2994 0 ▶ unless (exists($namespaces{$ns->localname()}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $namespaces{$ns->localname} 0 0 0
2995 }
2996 0 $n=$n->parentNode();
2997 }
2998 0 out(pwd($node),":\n");
2999 0 foreach (sort { $a cmp $b } keys %namespaces) {
3000 out(" xmlns", ($_ ne "" ? ":" : ""),
3001 $_,"=\"",
3002 0 ▶ $namespaces{$_},"\"\n");
Branch: 1 branch
BranchTrueFalseTotal
$_ ne '' ? : 0 0 0
3003 }
3004 0 out("\n");
3005 }
3006 0 ▶ if ($opts->{registered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"registered"}) 0 0 0
3007 0 for (sort keys(%_ns)) {
3008 0 out(qq(register-namespace $_ "$_ns{$_}";\n));
3009 }
3010 }
3011 0 return 1;
3012 }
3013  
3014 0 ▶sub mark_fold {
Subroutine mark_fold: not called CC=7 SCAR=40.3
Pod undocumented
3015 0 my ($opts,$exp)=@_;
3016 0 $opts = _ev_opts($opts);
3017 0 ▶ $opts->{depth} = 0 if $opts->{depth} eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"depth"} eq "" 0 0 0
3018 0 ▶ $exp = "." if $exp eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $exp eq "" 0 0 0
3019 0 foreach my $node (_ev_nodelist($exp)) {
3020 0 ▶ if ($_xml_module->is_element($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($node)) 0 0 0
3021 0 my $doc=$node->ownerDocument;
3022 0 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 0 0 0
3023 # pre-declare xsh namespace
3024 0 my $root=$doc->getDocumentElement;
3025 0 ▶ $root->setAttribute('xmlns:xsh',$XML::XSH2::xshNS) if $root;
Branch: 1 branch
BranchTrueFalseTotal
if $root 0 0 0
3026 }
3027 0 $node->setAttributeNS($XML::XSH2::xshNS,'xsh:fold',$opts->{depth});
3028 }
3029 }
3030 0 return 1;
3031 }
3032  
3033 0 ▶sub mark_unfold {
Subroutine mark_unfold: not called CC=4 SCAR=30.0
Pod undocumented
3034 0 my ($opts,$exp)=@_;
3035 0 foreach my $node (_ev_nodelist($exp)) {
3036 0 ▶ if ($_xml_module->is_element($node) and $node->hasAttributeNS($XML::XSH2::xshNS,'fold')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($node) and $node->hasAttributeNS($XML::XSH2::xshNS, "fold")) 0 0 0
Condition0%0/3
$XML::XSH2::Functions::_xml_module->is_element($node) and $node->hasAttributeNS($XML::XSH2::xshNS, "fold")
!ll&&!rl&&r
000
MC/DC0%0/2
$XML::XSH2::Functions::_xml_module->is_element($node) and $node->hasAttributeNS($XML::XSH2::xshNS, "fold")
$XML::XSH2::Functions::_xml_module->is_element($node) $node->hasAttributeNS($XML::XSH2::xshNS, "fold")
Truth table0%0/3
A and B
A= $XML::XSH2::Functions::_xml_module->is_element($node)B= $node->hasAttributeNS($XML::XSH2::xshNS, "fold")
ABresult
0X0
100
111
3037 0 remove_node($node->getAttributeNodeNS($XML::XSH2::xshNS,'fold'));
3038 }
3039 }
3040 0 return 1;
3041 }
3042  
3043 # canonicalize nodes matching given XPath
3044 0 ▶sub c14n {
Subroutine c14n: not called CC=4 SCAR=30.0
Pod undocumented
3045 0 my ($opts,$exp)=@_;
3046 0 $opts = _ev_opts($opts);
3047 0 ▶ $exp ||= '.';
Condition0%0/2
$exp ||= "."
l!l
00
MC/DC0%0/1
$exp ||= "."
$exp
Truth table0%0/2
A
A= $exp
Aresult
11
00
3048 0 my $ql = _ev_nodelist($exp);
3049 0 foreach my $node (@$ql) {
3050 0 out($node->toStringC14N($opts->{comments},$opts->{filter}),"\n");
3051 }
3052 0 ▶ print STDERR "\nFound ",scalar(@$ql)," node(s).\n" unless "$QUIET";
Branch: 1 branch
BranchTrueFalseTotal
unless "$XML::XSH2::Functions::QUIET" 0 0 0
3053 0 return 1;
3054  
3055 }
3056  
3057 # print canonical xpaths identifying nodes matching given XPath
3058 1 ▶sub locate {
Subroutine locate: called CC=3 SCAR=11.0
Pod undocumented
3059 1 my ($opts,$exp)=@_;
3060 1 $opts = _ev_opts($opts);
3061 1 my $ql= _ev_nodelist($exp);
3062 1 foreach (@$ql) {
3063 9 out(pwd($_,$opts->{id}),"\n");
3064 }
3065 1 ▶ print STDERR "\nFound ",scalar(@$ql)," node(s).\n" unless "$QUIET";
Branch: 1 branch
BranchTrueFalseTotal
unless "$XML::XSH2::Functions::QUIET" 0 1 1
3066 1 return 1;
3067 }
3068  
3069 # print line numbers of matching nodes
3070 0 ▶sub print_lineno {
Subroutine print_lineno: not called CC=2 SCAR=17.9
Pod undocumented
3071 0 my ($opts,$exp)=@_;
3072 0 $opts = _ev_opts($opts);
3073 0 my $ql=_ev_nodelist($exp);
3074 0 foreach (@$ql) {
3075 0 out($_->line_number,"\n");
3076 }
3077 0 return 1;
3078 }
3079  
3080 # remove nodes matching given XPath from a document and
3081 # remove all their descendants from all nodelists
3082 5 ▶sub prune {
Subroutine prune: called CC=3 SCAR=11.0
Pod undocumented
3083 5 my ($opts,$exp)=@_;
3084 5 my $i=0;
3085 5 my $ql=_ev_nodelist($exp);
3086 5 foreach my $node (@$ql) {
3087 8 remove_node($node,get_keep_blanks());
3088 8 $i++;
3089 }
3090 5 ▶ print STDERR "removed $i node(s)\n" unless "$QUIET";
Branch: 1 branch
BranchTrueFalseTotal
unless "$XML::XSH2::Functions::QUIET" 0 5 5
3091 5 return $i;
3092 }
3093  
3094 # evaluate given perl expression
3095 0 ▶sub eval_substitution {
Subroutine eval_substitution: not called CC=3 SCAR=24.8
Pod undocumented
3096 0 my ($val,$expr)=@_;
3097 0 ▶ local $_ = $val if defined($val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
3098  
3099 0 eval lexicalize("$expr");
3100 0 ▶ die $@ if $@; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
3101 0 return $_;
3102 }
3103  
3104 # sort given nodelist according to the given xsh code and perl code
3105 11 ▶sub perlsort {
Subroutine perlsort: called CC=14 SCAR=27.2
Pod undocumented
3106 11 my ($opts,$exp)=@_;
3107 11 my $opts = _hash_opts($opts);
3108 11 my $list = _ev_nodelist($exp);
3109 11 my @list;
3110 11 my $old_context = _save_context();
3111 11 my $pos=1;
3112 11 my $rl = _prepare_result_nl();
3113 11 ▶ if ($opts->{compare}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"compare"}) 5 6 11
3114 5 foreach (qw(numeric descending)) {
3115 10 ▶ die "sort cannot use --$_ with --compare at the same time\n" if (exists($opts->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{$_} 0 10 10
3116 }
3117 }
3118 11 eval {
3119 11 ▶ if (defined($opts->{key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'key'}) { } 9 2 11
3120 9 foreach my $node (@$list) {
3121 36 _set_context([$node,0+@$list,$pos]);
3122 36 push @list,[$node, _ev_literal($opts->{key})];
3123 36 $pos++;
3124 }
3125 } else {
3126 2 @list = map { [$_,to_literal($_)] } @$list;
3127 }
3128 11 ▶ if ($opts->{numeric}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'numeric'}) { } 1 10 11
elsif ($opts->{'compare'}) { } 5 5 10
3129 1 ▶ if ($opts->{descending}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'descending'}) { } 0 1 1
3130 0 @$rl = map { $_->[0] } sort { $b->[1] <=> $a->[1] } @list;
3131 } else {
3132 1 @$rl = map { $_->[0] } sort { $a->[1] <=> $b->[1] } @list;
3133 }
3134 } elsif ($opts->{compare}) {
3135 20 @$rl = map { $_->[0] }
3136 sort {
3137 5 local $XML::XSH2::Map::a = $a->[1];
3138 23 local $XML::XSH2::Map::b = $b->[1];
3139 23 my $result=eval lexicalize($opts->{compare});
3140 23 ▶ die $@ if ($@); # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 23 23
3141 $result;
3142 } @list;
3143 } else {
3144 5 ▶ if ($opts->{descending}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'descending'}) { } 1 4 5
3145 1 ▶ if ($opts->{locale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'locale'}) { } 0 1 1
3146 15 ▶ use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
3147 0 @$rl = map { $_->[0] } sort { $b->[1] cmp $a->[1] } @list;
3148 } else {
3149 1 @$rl = map { $_->[0] } sort { $b->[1] cmp $a->[1] } @list;
3150 }
3151 } else {
3152 4 ▶ if ($opts->{locale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'locale'}) { } 0 4 4
3153 15 ▶ use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
3154 0 @$rl = map { $_->[0] } sort { $a->[1] cmp $b->[1] } @list;
3155 } else {
3156 4 @$rl = map { $_->[0] } sort { $a->[1] cmp $b->[1] } @list;
3157 }
3158 }
3159 }
3160 };
3161 11 my $err = $@;
3162 11 do {
3163 11 local $SIG{INT}=\&flagsigint;
3164 11 _set_context($old_context);
3165 11 propagate_flagsigint();
3166 };
3167 11 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 11 11
3168  
3169 11 return $rl;
3170 }
3171  
3172 # Evaluate given expression over every node matching given XPath
3173 # and substitute content with the result.
3174 # The element is passed to the expression by its name or value in the $_
3175 # variable.
3176 2 ▶sub perlmap {
Subroutine perlmap: called CC=19 SCAR=43.1
Pod undocumented
3177 2 my ($opts, $mapexp, $exp)=@_;
3178 2 $opts = _ev_opts($opts);
3179 2 my $ql=_ev_nodelist($exp);
3180 2 my $old_context = _save_context();
3181 2 my $pos=1;
3182 2 my $size = @$ql;
3183 2 my $in_place = $opts->{'in-place'};
3184 2 ▶ @$ql = reverse @$ql if $opts->{reverse};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"reverse"} 0 2 2
3185 2 eval {
3186 2 foreach my $node (@$ql) {
3187 11 _set_context([$node,$size,$pos++]);
3188 11 ▶ if ($_xml_module->is_attribute($node)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 1 10 11
elsif ($XML::XSH2::Functions::_xml_module->is_element($node)) { } 0 10 10
elsif ($node->can('setData') and $node->can('getData')) { } 10 0 10
3189 1 my $val = _ev_literal($mapexp, $node->getValue(),$in_place);
3190 1 ▶ $node->setValue($val) if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 1 0 1
3191 } elsif ($_xml_module->is_element($node)) {
3192 0 my $value = _ev($mapexp, $node->textContent(),$in_place);
3193 0 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
3194 # prune content
3195 0 for my $child ($node->childNodes()) {
3196 0 $child->unbindNode();
3197 }
3198 0 ▶ if (ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 0 0 0
3199 0 ▶ if (UNIVERSAL::isa($value,'XML::LibXML::NodeList')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, 'XML::LibXML::NodeList')) { } 0 0 0
elsif (UNIVERSAL::isa($value, 'XML::LibXML::Node')) { } 0 0 0
3200 0 foreach my $n (@$value) {
3201 0 ▶ if ($_xml_module->is_document_fragment($n) or
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_document_fragment($n) or $n->parentNode and $XML::XSH2::Functions::_xml_module->is_document_fragment($n->parentNode)) { } 0 0 0
Condition0%0/6
$XML::XSH2::Functions::_xml_module->is_document_fragment($n) or $n->parentNode and $XML::XSH2::Functions::_xml_module->is_document_fragment($n->parentNode)
l!l&&r!l&&!r
000
$n->parentNode and $XML::XSH2::Functions::_xml_module->is_document_fragment($n->parentNode)
!ll&&!rl&&r
000
MC/DC0%0/3
$XML::XSH2::Functions::_xml_module->is_document_fragment($n) or $n->parentNode and $XML::XSH2::Functions::_xml_module->is_document_fragment($n->parentNode)
$XML::XSH2::Functions::_xml_module->is_document_fragment($n) $n->parentNode $XML::XSH2::Functions::_xml_module->is_document_fragment($n->parentNode)
Truth table0%0/4
A or B and C
A= $XML::XSH2::Functions::_xml_module->is_document_fragment($n)B= $n->parentNodeC= $XML::XSH2::Functions::_xml_module->is_document_fragment($n->parentNode)
ABCresult
1XX1
0111
00X0
0100
3202 $n->parentNode and
3203 $_xml_module->is_document_fragment($n->parentNode)) {
3204 # it's a fragment
3205 0 $node->appendChild($n);
3206 } else {
3207 # safely insert a copy
3208 0 insert_node($n,$node,undef,'into',undef,undef);
3209 }
3210 }
3211 } elsif (UNIVERSAL::isa($value,'XML::LibXML::Node')) {
3212 0 insert_node($value,$node,undef,'into',undef,undef);
3213 } else {
3214 0 $node->appendTextNode(to_literal($value));
3215 }
3216 } else {
3217 0 $node->appendTextNode($value);
3218 }
3219 }
3220 ▶ } elsif ($node->can('setData') and $node->can('getData')) {
Condition33%1/3
$node->can("setData") and $node->can("getData")
!ll&&!rl&&r
0010
MC/DC0%0/2
$node->can("setData") and $node->can("getData")
$node->can("setData") $node->can("getData")
Truth table33%1/3
A and B
A= $node->can("setData")B= $node->can("getData")
ABresult
0X0
100
111
3221 10 my $val = _ev_literal($mapexp, $node->getData(),$in_place);
3222 10 ▶ $node->setData($val) if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 10 0 10
3223 }
3224 }
3225 };
3226 2 my $err = $@;
3227 {
3228 2 local $SIG{INT}=\&flagsigint;
3229 2 _set_context($old_context);
3230 2 propagate_flagsigint();
3231 }
3232 2 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 2 2
3233  
3234 2 return 1;
3235 }
3236  
3237 0 ▶sub hash {
Subroutine hash: not called CC=4 SCAR=30.0
Pod undocumented
3238 0 my ($opts, $mapexp, $exp)=@_;
3239 0 $opts = _ev_opts($opts);
3240 0 my $ql=_ev_nodelist($exp);
3241 0 my $old_context = _save_context();
3242 0 my $pos=1;
3243 0 my $size = @$ql;
3244 0 my $hash = {};
3245 0 eval {
3246 0 foreach my $node (@$ql) {
3247 0 _set_context([$node,$size,$pos++]);
3248 0 my $key = _ev($mapexp, $node);
3249 0 ▶ if (exists($hash->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$key}) { } 0 0 0
3250 0 $hash->{$key}->push($node);
3251 } else {
3252 0 $hash->{$key} = XML::LibXML::NodeList->new($node);
3253 }
3254 }
3255 };
3256 0 my $err = $@;
3257 {
3258 0 local $SIG{INT}=\&flagsigint;
3259 0 _set_context($old_context);
3260 0 propagate_flagsigint();
3261 }
3262 0 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
3263  
3264 0 return $hash;
3265 }
3266  
3267  
3268 #
3269 87 ▶sub _ev_namespace {
Subroutine _ev_namespace: called CC=3 SCAR=13.3
3270 87 my ($val)=@_;
3271 87 ▶ if (UNIVERSAL::isa($val,'XML::LibXML::NodeList')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, 'XML::LibXML::NodeList')) { } 0 87 87
elsif (ref $val) { } 0 87 87
3272 0 die "Namespace cannot be specified as a node-set!";
3273 } elsif (ref($val)) {
3274 0 return to_literal($val);
3275 } else {
3276 87 return $val;
3277 }
3278 }
3279  
3280 4 ▶sub perlrename {
Subroutine perlrename: called CC=15 SCAR=29.7
Pod undocumented
3281 4 my ($opts, $nameexp, $exp)=@_;
3282 4 $opts = _ev_opts($opts);
3283 4 my $ns = _ev_namespace($opts->{namespace});
3284 4 my $ql=_ev_nodelist($exp);
3285 4 my $old_context = _save_context();
3286 4 my $pos=1;
3287 4 my $size = @$ql;
3288 4 ▶ @$ql = reverse @$ql if $opts->{reverse};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"reverse"} 0 4 4
3289 4 my $in_place = $opts->{'in-place'};
3290 4 eval {
3291 4 foreach my $node (@$ql) {
3292 14 _set_context([$node,$size,$pos++]);
3293 14 ▶ if ($_xml_module->is_attribute($node) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($node) or $XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_pi($node)) 14 0 14
Condition83%5/6
$XML::XSH2::Functions::_xml_module->is_attribute($node) or $XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_pi($node)
l!l&&r!l&&!r
1310
$XML::XSH2::Functions::_xml_module->is_attribute($node) or $XML::XSH2::Functions::_xml_module->is_element($node)
l!l&&r!l&&!r
1121
MC/DC0%0/3
$XML::XSH2::Functions::_xml_module->is_attribute($node) or $XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_pi($node)
$XML::XSH2::Functions::_xml_module->is_attribute($node) $XML::XSH2::Functions::_xml_module->is_element($node) $XML::XSH2::Functions::_xml_module->is_pi($node)
Truth table75%3/4
A or B or C
A= $XML::XSH2::Functions::_xml_module->is_attribute($node)B= $XML::XSH2::Functions::_xml_module->is_element($node)C= $XML::XSH2::Functions::_xml_module->is_pi($node)
ABCresult
1XX1
01X1
0011
0000
3294 $_xml_module->is_element($node) ||
3295 $_xml_module->is_pi($node)) {
3296 14 ▶ if ($node->can('setName')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->can('setName')) { } 14 0 14
3297 14 my $name=$node->getName();
3298 14 my $old_name = $name;
3299 14 $name = _ev_string($nameexp,$name,$in_place);
3300 14 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 14 0 14
3301 # If it is an attribute, check there is no attribute
3302 # with the same name already.
3303 14 ▶ if ($_xml_module->is_attribute($node) and
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($node) and $old_name ne $name and $node->getOwnerElement->hasAttributeNS($ns || $node->namespaceURI, $name)) { } 0 14 14
Condition55%5/9
$XML::XSH2::Functions::_xml_module->is_attribute($node) and $old_name ne $name and $node->getOwnerElement->hasAttributeNS($ns || $node->namespaceURI, $name)
!ll&&!rl&&r
1310
$XML::XSH2::Functions::_xml_module->is_attribute($node) and $old_name ne $name
!ll&&!rl&&r
1301
$ns || $node->namespaceURI
l!l&&r!l&&!r
001
MC/DC0%0/3
$XML::XSH2::Functions::_xml_module->is_attribute($node) and $old_name ne $name and $node->getOwnerElement->hasAttributeNS($ns || $node->namespaceURI, $name)
$XML::XSH2::Functions::_xml_module->is_attribute($node) $old_name ne $name $node->getOwnerElement->hasAttributeNS($ns || $node->namespaceURI, $name)
MC/DC0%0/2
$ns || $node->namespaceURI
$ns $node->namespaceURI
Truth table50%2/4
A and B and C
A= $XML::XSH2::Functions::_xml_module->is_attribute($node)B= $old_name ne $nameC= $node->getOwnerElement->hasAttributeNS($ns || $node->namespaceURI, $name)
ABCresult
0XX0
10X0
1100
1111
Truth table33%1/3
A || B
A= $nsB= $node->namespaceURI
ABresult
1X1
011
000
3304 $old_name ne $name and
3305 $node->getOwnerElement()
3306 ->hasAttributeNS($ns || $node->namespaceURI(), $name)) {
3307 0 _err "Cannot rename attribute '$old_name' to '$name': ",
3308 "An attribute with same name already exists!";
3309 } else {
3310 14 $node->setName($name);
3311 14 ▶ if (defined($ns) && $node->nodeName=~/^([^:]+):(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns and $node->nodeName =~ /^([^:]+):(.*)$/) 0 14 14
Condition33%1/3
defined $ns and $node->nodeName =~ /^([^:]+):(.*)$/
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $ns and $node->nodeName =~ /^([^:]+):(.*)$/
defined $ns $node->nodeName =~ /^([^:]+):(.*)$/
Truth table33%1/3
A and B
A= defined $nsB= $node->nodeName =~ /^([^:]+):(.*)$/
ABresult
0X0
100
111
3312 0 $node->setNamespace($ns,$1,1);
3313 }
3314 }
3315 }
3316 } else {
3317 0 _err "Node renaming not supported by ",ref($node);
3318 }
3319 }
3320 }
3321 };
3322 4 my $err = $@;
3323 {
3324 4 local $SIG{INT}=\&flagsigint;
3325 4 _set_context($old_context);
3326 4 propagate_flagsigint();
3327 }
3328 4 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 4 4
3329  
3330 4 return 1;
3331 }
3332  
3333  
3334 ############### AUXILIARY FUNCTIONS ###############
3335  
3336 29 ▶sub set_attr_ns {
Subroutine set_attr_ns: called CC=2 SCAR=7.6
Pod undocumented
3337 29 my ($node,$ns,$name,$value)=@_;
3338 29 ▶ if ($ns eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns eq '') { } 29 0 29
3339 29 $node->setAttribute($name,$value);
3340 } else {
3341 0 $node->setAttributeNS("$ns",$name,$value);
3342 }
3343 }
3344  
3345 # return NS prefix used in the given name
3346 30 ▶sub name_prefix {
Subroutine name_prefix: called CC=2 SCAR=9.2
Pod undocumented
3347 30 ▶ if ($_[0]=~/^([^:]+):/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^([^:]+):/) 0 30 30
3348 0 return $1;
3349 }
3350 }
3351  
3352 # try to safely clone a node
3353 97 ▶sub node_copy {
Subroutine node_copy: called CC=8 SCAR=22.7
Pod undocumented
3354 97 my ($node,$ns,$dest_doc,$dest)=@_;
3355  
3356 97 my $copy;
3357 97 ▶ if ($_xml_module->is_element($node) and !$node->hasChildNodes) {
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($node) and not $node->hasChildNodes) { } 25 72 97
elsif ($XML::XSH2::Functions::_xml_module->is_document_fragment($node)) { } 0 72 72
Condition100%3/3
$XML::XSH2::Functions::_xml_module->is_element($node) and not $node->hasChildNodes
!ll&&!rl&&r
274525
MC/DC100%2/2
$XML::XSH2::Functions::_xml_module->is_element($node) and not $node->hasChildNodes
$XML::XSH2::Functions::_xml_module->is_element($node) not $node->hasChildNodes
Truth table100%3/3
A and not(B)
A= $XML::XSH2::Functions::_xml_module->is_element($node)B= not $node->hasChildNodes
ABresult
0X0
100
111
3358 # -- prepare NS
3359 25 ▶ $ns=$node->namespaceURI() if ($ns eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "" 25 0 25
3360 25 my $prefix = name_prefix($node->getName);
3361 25 ▶ if ($ns eq "" and $prefix ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns eq "" and $prefix ne "") 0 25 25
Condition33%1/3
$ns eq "" and $prefix ne ""
!ll&&!rl&&r
0250
MC/DC0%0/2
$ns eq "" and $prefix ne ""
$ns eq "" $prefix ne ""
Truth table33%1/3
A and B
A= $ns eq ""B= $prefix ne ""
ABresult
0X0
100
111
3362 0 $ns=$dest->lookupNamespaceURI($prefix);
3363 }
3364 # --
3365 $copy=new_element($dest_doc,$node->getName(),$ns,
3366 25 ▶ [map { [$_->nodeName(),$_->nodeValue(),
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::_xml_module->is_attribute($_) ? : 20 0 20
3367 $_xml_module->is_attribute($_) ?
3368 $_->namespaceURI() : ""
3369 ] } $node->attributes],$dest);
3370 } elsif ($_xml_module->is_document_fragment($node)) {
3371 0 $copy=$_parser->parse_xml_chunk($node->toString());
3372 } else {
3373 72 $copy=$_xml_module->clone_node($dest_doc,$node);
3374 }
3375 }
3376  
3377 # get element-children of a node (e.g. of a document fragment)
3378 0 ▶sub get_subelements {
Subroutine get_subelements: not called CC=1 SCAR=6.9
Pod undocumented
3379 0 my ($docfrag)=@_;
3380 0 return grep { $_xml_module->is_element($_) } $docfrag->childNodes();
3381 }
3382  
3383 4 ▶sub get_following_siblings {
Subroutine get_following_siblings: called CC=2 SCAR=6.9
Pod undocumented
3384 4 my ($node)=@_;
3385 4 my @siblings;
3386 4 $node=$node->nextSibling();
3387 4 ▶ while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 2 4 6
3388 2 push @siblings,$node;
3389 2 $node=$node->nextSibling();
3390 }
3391 4 return @siblings;
3392 }
3393  
3394 # create new document element before the given nodelist
3395 7 ▶sub new_document_element {
Subroutine new_document_element: called CC=2 SCAR=6.9
Pod undocumented
3396 7 my ($doc,$node,@nodelist)=@_;
3397 7 $doc->setDocumentElement($node);
3398 7 foreach my $n (reverse @nodelist) {
3399 7 $doc->removeChild($n);
3400 7 $doc->insertAfter($n,$node);
3401 }
3402 }
3403  
3404 # replace document element with a new one
3405 3 ▶sub replace_document_element {
Subroutine replace_document_element: called CC=1 SCAR=0.0
Pod undocumented
3406 3 my ($old, $new)=@_;
3407 3 my $doc=$_xml_module->owner_document($old);
3408 3 my @after_nodes = $old->findnodes('following::node()');
3409 3 $old->unbindNode();
3410 3 new_document_element($doc,$new,@after_nodes);
3411 }
3412  
3413 # safely insert source node after, before or instead of the
3414 # destination node. Safety means here that nodes inserted on the
3415 # document level are given special care. the source node may only be
3416 # a document fragment, element, text, CDATA, Comment, Entity or
3417 # a PI (i.e. not an attribute).
3418  
3419 86 ▶sub safe_insert {
Subroutine safe_insert: called CC=21 SCAR=37.9
Pod undocumented
3420 86 my ($source,$dest,$where) = @_;
3421 86 my $parent=$dest->parentNode();
3422 86 ▶ return unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 86 86
3423 86 ▶ if ($_xml_module->is_document($parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_document($parent)) { } 7 79 86
3424  
3425 # placing a node on the document-level
3426 # SOURCE: Element
3427 7 ▶ if ($_xml_module->is_element($source)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($source)) { } 4 3 7
elsif ($XML::XSH2::Functions::_xml_module->is_pi($source) or $XML::XSH2::Functions::_xml_module->is_comment($source) or $XML::XSH2::Functions::_xml_module->is_entity_reference($source) or $XML::XSH2::Functions::_xml_module->is_document_fragment($source)) { } 3 0 3
3428 4 ▶ if ($where eq 'after') {
Branch: 3 branches
BranchTrueFalseTotal
if ($where eq 'after') { } 0 4 4
elsif ($where eq 'before') { } 0 4 4
elsif ($where eq 'replace') { } 4 0 4
3429 0 ▶ if ($parent->getDocumentElement()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->getDocumentElement) { } 0 0 0
3430 0 die("Error: cannot insert another element into /:\n",
3431 " there's one document element already!");
3432 } else {
3433 0 new_document_element($parent,$source,
3434 get_following_siblings($dest));
3435 }
3436 0 return 'keep';
3437 } elsif ($where eq 'before') {
3438 0 ▶ if ($parent->getDocumentElement()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->getDocumentElement) { } 0 0 0
3439 0 die("Error: cannot insert another element into /:\n",
3440 " there's one document element already!");
3441 } else {
3442 0 new_document_element($parent,$source,
3443 $dest,get_following_siblings($dest));
3444 }
3445 0 return 'keep';
3446 } elsif ($where eq 'replace') {
3447 # maybe we are loosing the document element here !
3448 4 ▶ if ($parent->getDocumentElement()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->getDocumentElement) { } 2 2 4
3449 2 ▶ if ($_xml_module->is_element($dest)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($dest)) { } 2 0 2
3450 2 my @nextnodes = get_following_siblings($dest);
3451 2 $dest->unbindNode();
3452 2 new_document_element($parent,$source, @nextnodes);
3453 } else {
3454 0 die("Error: cannot insert another element into /:\n",
3455 " there's one document element already!");
3456 }
3457 } else {
3458 2 new_document_element($parent,$source,
3459 $dest,get_following_siblings($dest));
3460 }
3461 4 return 'remove';
3462 }
3463 } # SOURCE: PI or Comment or DocFragment with PI's or Comments
3464 ▶ elsif ($_xml_module->is_pi($source) ||
Condition44%4/9
$XML::XSH2::Functions::_xml_module->is_pi($source) or $XML::XSH2::Functions::_xml_module->is_comment($source) or $XML::XSH2::Functions::_xml_module->is_entity_reference($source) or $XML::XSH2::Functions::_xml_module->is_document_fragment($source)
l!l&&r!l&&!r
300
$XML::XSH2::Functions::_xml_module->is_pi($source) or $XML::XSH2::Functions::_xml_module->is_comment($source) or $XML::XSH2::Functions::_xml_module->is_entity_reference($source)
l!l&&r!l&&!r
300
$XML::XSH2::Functions::_xml_module->is_pi($source) or $XML::XSH2::Functions::_xml_module->is_comment($source)
l!l&&r!l&&!r
210
MC/DC0%0/4
$XML::XSH2::Functions::_xml_module->is_pi($source) or $XML::XSH2::Functions::_xml_module->is_comment($source) or $XML::XSH2::Functions::_xml_module->is_entity_reference($source) or $XML::XSH2::Functions::_xml_module->is_document_fragment($source)
$XML::XSH2::Functions::_xml_module->is_pi($source) $XML::XSH2::Functions::_xml_module->is_comment($source) $XML::XSH2::Functions::_xml_module->is_entity_reference($source) $XML::XSH2::Functions::_xml_module->is_document_fragment($source)
Truth table40%2/5
A or B or C or D
A= $XML::XSH2::Functions::_xml_module->is_pi($source)B= $XML::XSH2::Functions::_xml_module->is_comment($source)C= $XML::XSH2::Functions::_xml_module->is_entity_reference($source)D= $XML::XSH2::Functions::_xml_module->is_document_fragment($source)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3465 $_xml_module->is_comment($source) ||
3466 $_xml_module->is_entity_reference($source) ||
3467 $_xml_module->is_document_fragment($source)) {
3468 # placing a node into an element
3469 3 ▶ if ($where eq 'after') {
Branch: 3 branches
BranchTrueFalseTotal
if ($where eq 'after') { } 1 2 3
elsif ($where eq 'before') { } 1 1 2
elsif ($where eq 'replace') { } 1 0 1
3470 1 $parent->insertAfter($source,$dest);
3471 1 return 'keep';
3472 } elsif ($where eq 'before') {
3473 1 $parent->insertBefore($source,$dest);
3474 1 return 'keep';
3475 } elsif ($where eq 'replace') {
3476 # maybe we are loosing the document element here !
3477 1 $parent->insertBefore($source,$dest);
3478 1 return 'remove';
3479 }
3480 } else {
3481 0 die("Error: cannot insert node ",ref($source)," on a document level");
3482 }
3483 } else {
3484 79 ▶ if ($where eq 'after') {
Branch: 3 branches
BranchTrueFalseTotal
if ($where eq 'after') { } 51 28 79
elsif ($where eq 'before') { } 0 28 28
elsif ($where eq 'replace') { } 28 0 28
3485 51 $parent->insertAfter($source,$dest);
3486 51 return 'keep';
3487 } elsif ($where eq 'before') {
3488 0 $parent->insertBefore($source,$dest);
3489 0 return 'keep';
3490 } elsif ($where eq 'replace') {
3491 28 $parent->insertBefore($source,$dest);
3492 28 return 'remove';
3493 }
3494 }
3495 }
3496  
3497 0 ▶sub _expand_fragment {
Subroutine _expand_fragment: not called CC=2 SCAR=17.9
3498 0 ▶ return $_xml_module->is_document_fragment($_[0]) ?
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::_xml_module->is_document_fragment($_[0]) ? : 0 0 0
3499 $_[0]->childNodes : $_[0];
3500 }
3501  
3502 0 ▶sub _is_attached {
Subroutine _is_attached: not called CC=5 SCAR=34.0
3503 0 my ($node)=@_;
3504 0 ▶ while ($node and !$_xml_module->is_document_fragment($node)
Branch: 1 branch
BranchTrueFalseTotal
if ($node and not $XML::XSH2::Functions::_xml_module->is_document_fragment($node) and not $XML::XSH2::Functions::_xml_module->is_document($node)) 0 0 0
Condition0%0/6
$node and not $XML::XSH2::Functions::_xml_module->is_document_fragment($node) and not $XML::XSH2::Functions::_xml_module->is_document($node)
!ll&&!rl&&r
000
$node and not $XML::XSH2::Functions::_xml_module->is_document_fragment($node)
!ll&&!rl&&r
000
MC/DC0%0/3
$node and not $XML::XSH2::Functions::_xml_module->is_document_fragment($node) and not $XML::XSH2::Functions::_xml_module->is_document($node)
$node not $XML::XSH2::Functions::_xml_module->is_document_fragment($node) not $XML::XSH2::Functions::_xml_module->is_document($node)
Truth table0%0/4
A and not(B) and not(C)
A= $nodeB= not $XML::XSH2::Functions::_xml_module->is_document_fragment($node)C= not $XML::XSH2::Functions::_xml_module->is_document($node)
ABCresult
0XX0
10X0
1100
1111
3505 and !$_xml_module->is_document($node)) {
3506 0 $node=$node->parentNode;
3507 }
3508 0 ▶ return $node && !$_xml_module->is_document_fragment($node);
Condition0%0/3
$node && !$XML::XSH2::Functions::_xml_module->is_document_fragment($node)
!ll&&!rl&&r
000
MC/DC0%0/2
$node && !$XML::XSH2::Functions::_xml_module->is_document_fragment($node)
$node !$XML::XSH2::Functions::_xml_module->is_document_fragment($node)
Truth table0%0/3
A && not(B)
A= $nodeB= !$XML::XSH2::Functions::_xml_module->is_document_fragment($node)
ABresult
0X0
100
111
3509 }
3510  
3511 0 ▶sub set_namespace {
Subroutine set_namespace: not called CC=7 SCAR=40.3
Pod undocumented
3512 0 my ($opts,$uri)=@_;
3513 0 $opts = _ev_opts($opts);
3514 0 $uri = _ev_string($uri);
3515 0 my $node = $_xpc->getContextNode;
3516 0 my $prefix = $opts->{prefix};
3517 0 ▶ unless ($_xml_module->is_element($node) ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_attribute($node)) 0 0 0
Condition0%0/3
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_attribute($node)
l!l&&r!l&&!r
000
MC/DC0%0/2
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_attribute($node)
$XML::XSH2::Functions::_xml_module->is_element($node) $XML::XSH2::Functions::_xml_module->is_attribute($node)
Truth table0%0/3
A or B
A= $XML::XSH2::Functions::_xml_module->is_element($node)B= $XML::XSH2::Functions::_xml_module->is_attribute($node)
ABresult
1X1
011
000
3518 $_xml_module->is_attribute($node)) {
3519 0 die "set_namespace: namespaces can only be set for element and attribute nodes\n";
3520 }
3521 0 ▶ if (defined $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix) { } 0 0 0
3522 0 my $declaredURI = $node->lookupNamespaceURI($prefix);
3523 0 ▶ if (defined $declaredURI and $declaredURI eq $uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $declaredURI and $declaredURI eq $uri) { } 0 0 0
Condition0%0/3
defined $declaredURI and $declaredURI eq $uri
!ll&&!rl&&r
000
MC/DC0%0/2
defined $declaredURI and $declaredURI eq $uri
defined $declaredURI $declaredURI eq $uri
Truth table0%0/3
A and B
A= defined $declaredURIB= $declaredURI eq $uri
ABresult
0X0
100
111
3524 0 return $node->setNamespace($uri,$prefix);
3525 } else {
3526 0 die "Namespace error: prefix '$prefix' already used for the namespace '$declaredURI'\n";
3527 }
3528 } else {
3529 0 ▶ if (defined($prefix = $node->lookupNamespacePrefix($uri))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($prefix = $node->lookupNamespacePrefix($uri))) { } 0 0 0
3530 0 return $node->setNamespace($uri,$prefix);
3531 } else {
3532 0 die "Namespace error: use declare-ns command to declare a prefix for '$uri' first\n";
3533 }
3534 }
3535 }
3536  
3537 0 ▶sub declare_namespace {
Subroutine declare_namespace: not called CC=4 SCAR=30.0
Pod undocumented
3538 0 my ($opts,$prefix,$uri)=@_;
3539 0 $prefix = _ev_string($prefix);
3540 0 $uri = _ev_string($uri);
3541 0 my $node = $_xpc->getContextNode;
3542 0 ▶ unless ($_xml_module->is_element($node)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($XML::XSH2::Functions::_xml_module->is_element($node)) 0 0 0
3543 0 die "declare-ns: namespaces can only be declared on element nodes\n";
3544 }
3545 0 my $declaredURI = $node->lookupNamespaceURI($prefix);
3546 0 ▶ if (defined $declaredURI and $declaredURI ne $uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $declaredURI and $declaredURI ne $uri) 0 0 0
Condition0%0/3
defined $declaredURI and $declaredURI ne $uri
!ll&&!rl&&r
000
MC/DC0%0/2
defined $declaredURI and $declaredURI ne $uri
defined $declaredURI $declaredURI ne $uri
Truth table0%0/3
A and B
A= defined $declaredURIB= $declaredURI ne $uri
ABresult
0X0
100
111
3547 0 die "Namespace error: prefix '$prefix' already used for the namespace '$declaredURI'\n";
3548 }
3549 0 $node->setNamespace($uri,$prefix,0);
3550 }
3551  
3552 0 ▶sub change_namespace_prefix {
Subroutine change_namespace_prefix: not called CC=5 SCAR=34.0
Pod undocumented
3553 0 my ($opts,$new,$old)=@_;
3554 0 ▶ $old = _ev_string($old) if $old;
Branch: 1 branch
BranchTrueFalseTotal
if $old 0 0 0
3555 0 $new = _ev_string($new);
3556 0 my $node = $_xpc->getContextNode;
3557 0 ▶ if ($node && $_xml_module->is_element($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node and $XML::XSH2::Functions::_xml_module->is_element($node)) { } 0 0 0
Condition0%0/3
$node and $XML::XSH2::Functions::_xml_module->is_element($node)
!ll&&!rl&&r
000
MC/DC0%0/2
$node and $XML::XSH2::Functions::_xml_module->is_element($node)
$node $XML::XSH2::Functions::_xml_module->is_element($node)
Truth table0%0/3
A and B
A= $nodeB= $XML::XSH2::Functions::_xml_module->is_element($node)
ABresult
0X0
100
111
3558 0 ▶ $old = $node->prefix unless defined $old;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old 0 0 0
3559 0 return $node->setNamespaceDeclPrefix($old,$new);
3560 } else {
3561 0 _err("The context node is not an element");
3562 }
3563 }
3564  
3565 0 ▶sub change_namespace_uri {
Subroutine change_namespace_uri: not called CC=5 SCAR=34.0
Pod undocumented
3566 0 my ($opts,$uri,$prefix)=@_;
3567 0 ▶ $prefix = _ev_string($prefix) if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
3568 0 $uri = _ev_string($uri);
3569 0 my $node = $_xpc->getContextNode;
3570 0 ▶ if ($node && $_xml_module->is_element($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node and $XML::XSH2::Functions::_xml_module->is_element($node)) { } 0 0 0
Condition0%0/3
$node and $XML::XSH2::Functions::_xml_module->is_element($node)
!ll&&!rl&&r
000
MC/DC0%0/2
$node and $XML::XSH2::Functions::_xml_module->is_element($node)
$node $XML::XSH2::Functions::_xml_module->is_element($node)
Truth table0%0/3
A and B
A= $nodeB= $XML::XSH2::Functions::_xml_module->is_element($node)
ABresult
0X0
100
111
3571 0 ▶ $prefix = $node->prefix unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 0 0
3572 0 return $node->setNamespaceDeclURI($prefix,$uri);
3573 } else {
3574 0 _err("The context node is not an element");
3575 }
3576 }
3577  
3578 # use the XPathToXML module to build
3579 # up a XML structure
3580 8 ▶sub xpath_set {
Subroutine xpath_set: called CC=8 SCAR=32.9
Pod undocumented
3581 8 my ($opts,$exp,$value)=@_;
3582 8 require XML::XSH2::XPathToXML;
3583 8 my $xtx = XML::XSH2::XPathToXML->new(namespaces => \%_ns,
3584 XPathContext => $_xpc,
3585 node => xsh_context_node(),
3586 );
3587 8 $value = _ev($value);
3588 8 $exp = _expand($exp);
3589 8 ▶ if (ref($value) and UNIVERSAL::isa($value,'XML::LibXML::NodeList')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and UNIVERSAL::isa($value, 'XML::LibXML::NodeList')) { } 0 8 8
Condition66%2/3
ref $value and UNIVERSAL::isa($value, "XML::LibXML::NodeList")
!ll&&!rl&&r
350
MC/DC0%0/2
ref $value and UNIVERSAL::isa($value, "XML::LibXML::NodeList")
ref $value UNIVERSAL::isa($value, "XML::LibXML::NodeList")
Truth table66%2/3
A and B
A= ref $valueB= UNIVERSAL::isa($value, "XML::LibXML::NodeList")
ABresult
0X0
100
111
3590 0 my $result = $xtx->createNode($exp);
3591 0 ▶ if ($_xml_module->is_element($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($result)) { } 0 0 0
3592 # if it's an element, try to clone or attach given nodes
3593 0 foreach my $node (@$value) {
3594 0 ▶ if ($_xml_module->is_document_fragment($node) or
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_document_fragment($node) or $node->parentNode and $XML::XSH2::Functions::_xml_module->is_document_fragment($node->parentNode)) { } 0 0 0
Condition0%0/6
$XML::XSH2::Functions::_xml_module->is_document_fragment($node) or $node->parentNode and $XML::XSH2::Functions::_xml_module->is_document_fragment($node->parentNode)
l!l&&r!l&&!r
000
$node->parentNode and $XML::XSH2::Functions::_xml_module->is_document_fragment($node->parentNode)
!ll&&!rl&&r
000
MC/DC0%0/3
$XML::XSH2::Functions::_xml_module->is_document_fragment($node) or $node->parentNode and $XML::XSH2::Functions::_xml_module->is_document_fragment($node->parentNode)
$XML::XSH2::Functions::_xml_module->is_document_fragment($node) $node->parentNode $XML::XSH2::Functions::_xml_module->is_document_fragment($node->parentNode)
Truth table0%0/4
A or B and C
A= $XML::XSH2::Functions::_xml_module->is_document_fragment($node)B= $node->parentNodeC= $XML::XSH2::Functions::_xml_module->is_document_fragment($node->parentNode)
ABCresult
1XX1
0111
00X0
0100
3595 $node->parentNode and
3596 $_xml_module->is_document_fragment($node->parentNode)) {
3597 # it's a fragment
3598 0 $result->appendChild($node);
3599 } else {
3600 # safely insert a copy
3601 0 insert_node($node,$result,undef,'into',undef,undef);
3602 }
3603 }
3604 } else {
3605 0 $result->setValue(to_literal($value));
3606 }
3607 0 return $result;
3608 } else {
3609 8 return $xtx->createNode($exp,to_literal($value));
3610 }
3611 }
3612  
3613 # insert given node to given destination performing
3614 # node-type conversion if necessary
3615 134 ▶sub insert_node {
Subroutine insert_node: called CC=98 SCAR=78.4
Pod undocumented
3616 134 my ($node,$dest,$dest_doc,$where,$ns,$rl)=@_;
3617 134 ▶ if ($_xml_module->is_document($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_document($node)) 0 134 134
3618 0 die "Error: Can't insert/copy/move document nodes!\n";
3619 }
3620 134 ▶ if (!defined($dest_doc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dest_doc) 134 0 134
3621 134 $dest_doc = $_xml_module->owner_document($dest);
3622 }
3623 # destination: Attribute
3624 134 ▶ if ($_xml_module->is_attribute($dest)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($dest)) { } 26 108 134
elsif ($XML::XSH2::Functions::_xml_module->is_document($dest)) { } 0 108 108
elsif ($XML::XSH2::Functions::_xml_module->is_element($dest)) { } 92 16 108
elsif ($XML::XSH2::Functions::_xml_module->is_text($dest) or $XML::XSH2::Functions::_xml_module->is_cdata_section($dest) or $XML::XSH2::Functions::_xml_module->is_comment($dest) or $XML::XSH2::Functions::_xml_module->is_pi($dest) or $XML::XSH2::Functions::_xml_module->is_entity_reference($dest)) { } 16 0 16
3625 # source: Text, CDATA, Comment, Entity, Element
3626 26 ▶ if ($_xml_module->is_text($node) ||
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node) or $XML::XSH2::Functions::_xml_module->is_comment($node) or $XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_pi($node)) { } 23 3 26
elsif ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 3 0 3
Condition91%11/12
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node) or $XML::XSH2::Functions::_xml_module->is_comment($node) or $XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_pi($node)
l!l&&r!l&&!r
2033
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node) or $XML::XSH2::Functions::_xml_module->is_comment($node) or $XML::XSH2::Functions::_xml_module->is_element($node)
l!l&&r!l&&!r
6146
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node) or $XML::XSH2::Functions::_xml_module->is_comment($node)
l!l&&r!l&&!r
3320
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
l!l&&r!l&&!r
3023
MC/DC80%4/5
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node) or $XML::XSH2::Functions::_xml_module->is_comment($node) or $XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_pi($node)
$XML::XSH2::Functions::_xml_module->is_text($node) $XML::XSH2::Functions::_xml_module->is_cdata_section($node) $XML::XSH2::Functions::_xml_module->is_comment($node) $XML::XSH2::Functions::_xml_module->is_element($node) $XML::XSH2::Functions::_xml_module->is_pi($node)
Truth table83%5/6
A or B or C or D or E
A= $XML::XSH2::Functions::_xml_module->is_text($node)B= $XML::XSH2::Functions::_xml_module->is_cdata_section($node)C= $XML::XSH2::Functions::_xml_module->is_comment($node)D= $XML::XSH2::Functions::_xml_module->is_element($node)E= $XML::XSH2::Functions::_xml_module->is_pi($node)
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
3627 $_xml_module->is_cdata_section($node) ||
3628 $_xml_module->is_comment($node) ||
3629 $_xml_module->is_element($node) ||
3630 $_xml_module->is_pi($node)) {
3631 23 ▶ my $val = $_xml_module->is_element($node) ?
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::_xml_module->is_element($node) ? : 14 9 23
3632 $node->textContent() : $node->getData();
3633 23 ▶ if ($where eq 'replace' or $where eq 'into') {
Branch: 3 branches
BranchTrueFalseTotal
if ($where eq 'replace' or $where eq 'into') { } 0 23 23
elsif ($where eq 'before' or $where eq 'prepend') { } 0 23 23
elsif ($where eq 'after' or $where eq 'append') { } 23 0 23
Condition33%1/3
$where eq "replace" or $where eq "into"
l!l&&r!l&&!r
0023
MC/DC0%0/2
$where eq "replace" or $where eq "into"
$where eq "replace" $where eq "into"
Truth table33%1/3
A or B
A= $where eq "replace"B= $where eq "into"
ABresult
1X1
011
000
3634 0 $val=~s/^\s+|\s+$//g;
3635 # xcopy will replace the value several times, which may not be intended
3636 0 set_attr_ns($dest->ownerElement(),$dest->namespaceURI(),$dest->getName(),$val);
3637 0 ▶ push @$rl,$dest->ownerElement()->getAttributeNodeNS($dest->namespaceURI(),$dest->getName()) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3638 0 return 'keep'; # as opposed to 'remove'
3639 ▶ } elsif ($where eq 'before' or $where eq 'prepend') {
Condition33%1/3
$where eq "before" or $where eq "prepend"
l!l&&r!l&&!r
0023
MC/DC0%0/2
$where eq "before" or $where eq "prepend"
$where eq "before" $where eq "prepend"
Truth table33%1/3
A or B
A= $where eq "before"B= $where eq "prepend"
ABresult
1X1
011
000
3640 0 $val=~s/^\s+//g;
3641 0 set_attr_ns($dest->ownerElement(),$dest->namespaceURI(),$dest->getName(),
3642 $val.$dest->getValue());
3643 0 ▶ push @$rl,$dest->ownerElement()->getAttributeNodeNS($dest->namespaceURI(),$dest->getName()) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3644 ▶ } elsif ($where eq 'after' or $where eq 'append') {
Condition33%1/3
$where eq "after" or $where eq "append"
l!l&&r!l&&!r
2300
MC/DC0%0/2
$where eq "after" or $where eq "append"
$where eq "after" $where eq "append"
Truth table33%1/3
A or B
A= $where eq "after"B= $where eq "append"
ABresult
1X1
011
000
3645 23 $val=~s/\s+$//g;
3646 23 set_attr_ns($dest->ownerElement(),$dest->namespaceURI(),$dest->getName(),
3647 $dest->getValue().$val);
3648 23 ▶ push @$rl,$dest->ownerElement()->getAttributeNodeNS($dest->namespaceURI(),$dest->getName()) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 23 23
3649 }
3650  
3651 }
3652 # source: Attribute
3653 elsif ($_xml_module->is_attribute($node)) {
3654 3 my $name=$node->getName();
3655 3 my $value = $node->getValue();
3656 3 ▶ if ($where eq 'replace' or $where eq 'after' or $where eq 'before') {
Branch: 1 branch
BranchTrueFalseTotal
if ($where eq 'replace' or $where eq 'after' or $where eq 'before') { } 3 0 3
Condition33%2/6
$where eq "replace" or $where eq "after" or $where eq "before"
l!l&&r!l&&!r
300
$where eq "replace" or $where eq "after"
l!l&&r!l&&!r
030
MC/DC0%0/3
$where eq "replace" or $where eq "after" or $where eq "before"
$where eq "replace" $where eq "after" $where eq "before"
Truth table25%1/4
A or B or C
A= $where eq "replace"B= $where eq "after"C= $where eq "before"
ABCresult
1XX1
01X1
0011
0000
3657 # -- prepare NS
3658 3 ▶ $ns=$node->namespaceURI() if ($ns eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "" 3 0 3
3659 3 ▶ if ($ns eq "" and name_prefix($name) ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns eq "" and name_prefix($name) ne "") 0 3 3
Condition33%1/3
$ns eq "" and name_prefix($name) ne ""
!ll&&!rl&&r
030
MC/DC0%0/2
$ns eq "" and name_prefix($name) ne ""
$ns eq "" name_prefix($name) ne ""
Truth table33%1/3
A and B
A= $ns eq ""B= name_prefix($name) ne ""
ABresult
0X0
100
111
3660 0 $ns=$dest->lookupNamespaceURI(name_prefix($name))
3661 }
3662 # --
3663 3 my $elem=$dest->ownerElement();
3664 3 set_attr_ns($elem,"$ns",$name,$value);
3665 3 ▶ push @$rl,$elem->getAttributeNodeNS("$ns",$name) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 3 3
3666 3 ▶ if ($where eq 'replace' and $name ne $dest->getName()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where eq 'replace' and $name ne $dest->getName) { } 0 3 3
Condition33%1/3
$where eq "replace" and $name ne $dest->getName
!ll&&!rl&&r
300
MC/DC0%0/2
$where eq "replace" and $name ne $dest->getName
$where eq "replace" $name ne $dest->getName
Truth table33%1/3
A and B
A= $where eq "replace"B= $name ne $dest->getName
ABresult
0X0
100
111
3667 0 return 'remove'; # remove the destination node in the end
3668 } else {
3669 3 return 'keep'; # no need to remove the destination node
3670 }
3671 } else {
3672 # -- prepare NS
3673 0 $ns=$dest->namespaceURI(); # given value of $ns is ignored here
3674 # --
3675 0 ▶ if ($where eq 'append') {
Branch: 3 branches
BranchTrueFalseTotal
if ($where eq 'append') { } 0 0 0
elsif ($where eq 'into') { } 0 0 0
elsif ($where eq 'prepend') { } 0 0 0
3676 0 set_attr_ns($dest->ownerElement(),"$ns",$dest->getName,$dest->getValue().$value);
3677 } elsif ($where eq 'into') {
3678 0 set_attr_ns($dest->ownerElement(),"$ns",$dest->getName(),$value);
3679 } elsif ($where eq 'prepend') {
3680 0 set_attr_ns($dest->ownerElement(),"$ns",$dest->getName(),$value.$dest->getValue());
3681 }
3682 0 ▶ push @$rl,$dest->ownerElement()->getAttributeNodeNS("$ns",$dest->getName()) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3683 }
3684 } else {
3685 0 _err("Warning: Ignoring incompatible nodes in insert/copy/move operation:\n",
3686 ref($node)," $where ",ref($dest),"!");
3687 0 return 1;
3688 }
3689 }
3690 # destination: Document
3691 elsif ($_xml_module->is_document($dest)) {
3692 # source: Attribute, Text, CDATA
3693 0 ▶ if ($_xml_module->is_attribute($node) or
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($node) or $XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_element($node)) { } 0 0 0
Condition0%0/6
$XML::XSH2::Functions::_xml_module->is_attribute($node) or $XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
l!l&&r!l&&!r
000
$XML::XSH2::Functions::_xml_module->is_attribute($node) or $XML::XSH2::Functions::_xml_module->is_text($node)
l!l&&r!l&&!r
000
MC/DC0%0/3
$XML::XSH2::Functions::_xml_module->is_attribute($node) or $XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
$XML::XSH2::Functions::_xml_module->is_attribute($node) $XML::XSH2::Functions::_xml_module->is_text($node) $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
Truth table0%0/4
A or B or C
A= $XML::XSH2::Functions::_xml_module->is_attribute($node)B= $XML::XSH2::Functions::_xml_module->is_text($node)C= $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
ABCresult
1XX1
01X1
0011
0000
3694 $_xml_module->is_text($node) or
3695 $_xml_module->is_cdata_section($node)
3696 ) {
3697 0 _err("Warning: Ignoring incompatible nodes in insert/copy/move operation:\n",
3698 ref($node)," $where ",ref($dest),"!");
3699 0 return 1;
3700 } elsif ($_xml_module->is_element($node)) {
3701 # source: Element
3702 0 my $copy=node_copy($node,$ns,$dest_doc,$dest);
3703 0 my $destnode;
3704 my $newwhere;
3705 0 ▶ if ($where =~ /^(?:after|append|into)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($where =~ /^(?:after|append|into)/) { } 0 0 0
elsif ($where =~ /^(?:before|prepend)/) { } 0 0 0
elsif ($where eq 'replace') { } 0 0 0
3706 0 $newwhere='after';
3707 0 $destnode=$dest->lastChild();
3708 } elsif ($where =~ /^(?:before|prepend)/) {
3709 0 $newwhere='before';
3710 0 $destnode=$dest->firstChild();
3711 } elsif ($where eq 'replace') {
3712 0 _err("Warning: Ignoring incompatible nodes in insert/copy/move operation:\n",
3713 ref($node)," $where ",ref($dest),"!");
3714 0 return 1;
3715 }
3716 0 ▶ push @$rl,_expand_fragment($copy) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3717 0 ▶ if ($destnode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destnode) { } 0 0 0
3718 0 return safe_insert($copy,$destnode,$newwhere);
3719 } else {
3720 0 new_document_element($dest,$copy);
3721 0 return 1;
3722 }
3723 } else {
3724 # source: Chunk, PI, Comment, Entity
3725 0 my $copy=node_copy($node,$ns,$dest_doc,$dest);
3726 0 ▶ if ($where =~ /^(?:after|append|into)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($where =~ /^(?:after|append|into)/) { } 0 0 0
elsif ($where =~ /^(?:before|prepend)/) { } 0 0 0
elsif ($where eq 'replace') { } 0 0 0
3727 # rather than appendChild which does not work
3728 # for Chunks!
3729 0 $dest->insertAfter($copy,$dest->lastChild());
3730 } elsif ($where =~ /^(?:before|prepend)/) {
3731 0 $dest->insertBefore($copy,$dest->firstChild());
3732 } elsif ($where eq 'replace') {
3733 0 _err("Warning: Ignoring incompatible nodes in insert/copy/move operation:\n",
3734 ref($node)," $where ",ref($dest),"!");
3735 0 return 1;
3736 }
3737 0 ▶ push @$rl,_expand_fragment($copy) if (defined($rl));
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3738 }
3739 }
3740 # destination: Element
3741 elsif ($_xml_module->is_element($dest)) {
3742 # source: Attribute
3743 92 ▶ if ($_xml_module->is_attribute($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 2 90 92
3744 # -- prepare NS
3745 2 ▶ $ns=$node->namespaceURI() if ($ns eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "" 2 0 2
3746 2 ▶ if ($ns eq "" and name_prefix($node->getName) ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns eq "" and name_prefix($node->getName) ne "") 0 2 2
Condition33%1/3
$ns eq "" and name_prefix($node->getName) ne ""
!ll&&!rl&&r
020
MC/DC0%0/2
$ns eq "" and name_prefix($node->getName) ne ""
$ns eq "" name_prefix($node->getName) ne ""
Truth table33%1/3
A and B
A= $ns eq ""B= name_prefix($node->getName) ne ""
ABresult
0X0
100
111
3747 0 $ns=$dest->lookupNamespaceURI(name_prefix($node->getName))
3748 }
3749 # --
3750 2 ▶ if ($where eq 'into' or $where eq 'append' or $where eq 'prepend') {
Branch: 2 branches
BranchTrueFalseTotal
if ($where eq 'into' or $where eq 'append' or $where eq 'prepend') { } 2 0 2
elsif ($where eq 'replace') { } 0 0 0
Condition33%2/6
$where eq "into" or $where eq "append" or $where eq "prepend"
l!l&&r!l&&!r
200
$where eq "into" or $where eq "append"
l!l&&r!l&&!r
200
MC/DC0%0/3
$where eq "into" or $where eq "append" or $where eq "prepend"
$where eq "into" $where eq "append" $where eq "prepend"
Truth table25%1/4
A or B or C
A= $where eq "into"B= $where eq "append"C= $where eq "prepend"
ABCresult
1XX1
01X1
0011
0000
3751 2 set_attr_ns($dest,"$ns",$node->getName(),$node->getValue());
3752 2 ▶ push @$rl,$dest->getAttributeNodeNS("$ns",$node->getName()) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 2 2
3753 } elsif ($where eq 'replace') {
3754 0 my $parent=$dest->parentNode();
3755 0 ▶ if ($_xml_module->is_element($parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($parent)) { } 0 0 0
3756 0 set_attr_ns($dest,"$ns",$node->getName(),$node->getValue());
3757 0 ▶ push @$rl,$dest->getAttributeNodeNS("$ns",$node->getName()) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3758 } else {
3759 0 _err("Warning: Cannot replace ",ref($node)," with ",ref($parent),
3760 ": parent node is not an element!");
3761 0 return 1;
3762 }
3763 0 return 'remove';
3764 } else {
3765 0 _err("Warning: Ignoring incompatible nodes in insert/copy/move operation:\n",
3766 ref($node)," $where ",ref($dest),"!");
3767 0 return 1;
3768 # # converting attribute to element
3769 # my $new=new_element($dest_doc,$node->getName(),$ns,$dest);
3770 # $new->appendText($node->getValue());
3771 # my $parent=$dest->parentNode();
3772 # if ($_xml_module->is_element($parent)) {
3773 # if ($where eq 'before' or $where eq 'after') {
3774 # safe_insert($new,$dest,$where);
3775 # }
3776 # } elsif ($where eq 'append') {
3777 # $dest->appendChild($new);
3778 # } elsif ($where eq 'prepend') {
3779 # $dest->insertBefore($new,$dest->firstChild());
3780 # }
3781 }
3782 }
3783 # source: Any but Attribute
3784 else {
3785 90 my $copy=node_copy($node,$ns,$dest_doc,$dest);
3786 90 ▶ if ($where eq 'after' or $where eq 'before' or $where eq 'replace') {
Branch: 3 branches
BranchTrueFalseTotal
if ($where eq 'after' or $where eq 'before' or $where eq 'replace') { } 47 43 90
elsif ($where eq 'into' or $where eq 'append') { } 43 0 43
elsif ($where eq 'prepend') { } 0 0 0
Condition100%6/6
$where eq "after" or $where eq "before" or $where eq "replace"
l!l&&r!l&&!r
46143
$where eq "after" or $where eq "before"
l!l&&r!l&&!r
45144
MC/DC100%3/3
$where eq "after" or $where eq "before" or $where eq "replace"
$where eq "after" $where eq "before" $where eq "replace"
Truth table100%4/4
A or B or C
A= $where eq "after"B= $where eq "before"C= $where eq "replace"
ABCresult
1XX1
01X1
0011
0000
3787 47 ▶ push @$rl,_expand_fragment($copy) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 47 47
3788 47 return safe_insert($copy,$dest,$where);
3789 ▶ } elsif ($where eq 'into' or $where eq 'append') {
Condition33%1/3
$where eq "into" or $where eq "append"
l!l&&r!l&&!r
4300
MC/DC0%0/2
$where eq "into" or $where eq "append"
$where eq "into" $where eq "append"
Truth table33%1/3
A or B
A= $where eq "into"B= $where eq "append"
ABresult
1X1
011
000
3790 43 $dest->appendChild($copy);
3791 43 ▶ push @$rl,_expand_fragment($copy) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 43 43
3792 } elsif ($where eq 'prepend') {
3793 0 ▶ if ($dest->hasChildNodes()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest->hasChildNodes) { } 0 0 0
3794 0 $dest->insertBefore($copy,$dest->firstChild());
3795 } else {
3796 0 $dest->appendChild($copy);
3797 }
3798 0 ▶ push @$rl,_expand_fragment($copy) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3799 }
3800 }
3801 }
3802 # destination: Text, CDATA, Comment, PI
3803 ▶ elsif ($_xml_module->is_text($dest) ||
Condition33%4/12
$XML::XSH2::Functions::_xml_module->is_text($dest) or $XML::XSH2::Functions::_xml_module->is_cdata_section($dest) or $XML::XSH2::Functions::_xml_module->is_comment($dest) or $XML::XSH2::Functions::_xml_module->is_pi($dest) or $XML::XSH2::Functions::_xml_module->is_entity_reference($dest)
l!l&&r!l&&!r
1600
$XML::XSH2::Functions::_xml_module->is_text($dest) or $XML::XSH2::Functions::_xml_module->is_cdata_section($dest) or $XML::XSH2::Functions::_xml_module->is_comment($dest) or $XML::XSH2::Functions::_xml_module->is_pi($dest)
l!l&&r!l&&!r
1600
$XML::XSH2::Functions::_xml_module->is_text($dest) or $XML::XSH2::Functions::_xml_module->is_cdata_section($dest) or $XML::XSH2::Functions::_xml_module->is_comment($dest)
l!l&&r!l&&!r
1600
$XML::XSH2::Functions::_xml_module->is_text($dest) or $XML::XSH2::Functions::_xml_module->is_cdata_section($dest)
l!l&&r!l&&!r
1600
MC/DC0%0/5
$XML::XSH2::Functions::_xml_module->is_text($dest) or $XML::XSH2::Functions::_xml_module->is_cdata_section($dest) or $XML::XSH2::Functions::_xml_module->is_comment($dest) or $XML::XSH2::Functions::_xml_module->is_pi($dest) or $XML::XSH2::Functions::_xml_module->is_entity_reference($dest)
$XML::XSH2::Functions::_xml_module->is_text($dest) $XML::XSH2::Functions::_xml_module->is_cdata_section($dest) $XML::XSH2::Functions::_xml_module->is_comment($dest) $XML::XSH2::Functions::_xml_module->is_pi($dest) $XML::XSH2::Functions::_xml_module->is_entity_reference($dest)
Truth table16%1/6
A or B or C or D or E
A= $XML::XSH2::Functions::_xml_module->is_text($dest)B= $XML::XSH2::Functions::_xml_module->is_cdata_section($dest)C= $XML::XSH2::Functions::_xml_module->is_comment($dest)D= $XML::XSH2::Functions::_xml_module->is_pi($dest)E= $XML::XSH2::Functions::_xml_module->is_entity_reference($dest)
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
3804 $_xml_module->is_cdata_section($dest) ||
3805 $_xml_module->is_comment($dest) ||
3806 $_xml_module->is_pi($dest) ||
3807 $_xml_module->is_entity_reference($dest)
3808 ) {
3809 16 ▶ if ($where =~ /^(?:into|append|prepend)$/ and
Branch: 1 branch
BranchTrueFalseTotal
if ($where =~ /^(?:into|append|prepend)$/ and $XML::XSH2::Functions::_xml_module->is_entity_reference($dest) || $XML::XSH2::Functions::_xml_module->is_entity_reference($node)) 0 16 16
Condition50%3/6
$where =~ /^(?:into|append|prepend)$/ and $XML::XSH2::Functions::_xml_module->is_entity_reference($dest) || $XML::XSH2::Functions::_xml_module->is_entity_reference($node)
!ll&&!rl&&r
790
$XML::XSH2::Functions::_xml_module->is_entity_reference($dest) or $XML::XSH2::Functions::_xml_module->is_entity_reference($node)
l!l&&r!l&&!r
009
MC/DC0%0/3
$where =~ /^(?:into|append|prepend)$/ and $XML::XSH2::Functions::_xml_module->is_entity_reference($dest) || $XML::XSH2::Functions::_xml_module->is_entity_reference($node)
$where =~ /^(?:into|append|prepend)$/ $XML::XSH2::Functions::_xml_module->is_entity_reference($dest) $XML::XSH2::Functions::_xml_module->is_entity_reference($node)
Truth table50%2/4
A and B or C
A= $where =~ /^(?:into|append|prepend)$/B= $XML::XSH2::Functions::_xml_module->is_entity_reference($dest)C= $XML::XSH2::Functions::_xml_module->is_entity_reference($node)
ABCresult
0XX0
1000
11X1
1011
3810 ($_xml_module->is_entity_reference($dest) ||
3811 $_xml_module->is_entity_reference($node))) {
3812 0 _err("Warning: Ignoring incompatible nodes in insert/copy/move operation:\n",
3813 ref($node)," $where ",ref($dest),"!");
3814 0 return 1;
3815 }
3816 16 ▶ if ($where eq 'into') {
Branch: 4 branches
BranchTrueFalseTotal
if ($where eq 'into') { } 0 16 16
elsif ($where eq 'append') { } 9 7 16
elsif ($where eq 'prepend') { } 0 7 7
elsif ($where eq 'replace' and $XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 0 7 7
3817 0 ▶ my $value=$_xml_module->is_element($node) ?
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::_xml_module->is_element($node) ? : 0 0 0
3818 $node->textContent() : $node->getData();
3819 0 ▶ $value = "" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
3820 0 $dest->setData($value);
3821 0 ▶ push @$rl,$dest if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3822 } elsif ($where eq 'append') {
3823 9 ▶ my $value=$_xml_module->is_element($node) ?
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::_xml_module->is_element($node) ? : 0 9 9
3824 $node->textContent() : $node->getData();
3825 9 $dest->setData($dest->getData().$value);
3826 9 ▶ push @$rl,$dest if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 9 9
3827 } elsif ($where eq 'prepend') {
3828 0 ▶ my $value=$_xml_module->is_element($node) ?
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::_xml_module->is_element($node) ? : 0 0 0
3829 $node->textContent() : $node->getData();
3830 0 $dest->setData($value.$dest->getData());
3831 0 ▶ push @$rl,$dest if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3832 }
3833 # replace + source: Attribute
3834 ▶ elsif ($where eq 'replace' and $_xml_module->is_attribute($node)) {
Condition33%1/3
$where eq "replace" and $XML::XSH2::Functions::_xml_module->is_attribute($node)
!ll&&!rl&&r
700
MC/DC0%0/2
$where eq "replace" and $XML::XSH2::Functions::_xml_module->is_attribute($node)
$where eq "replace" $XML::XSH2::Functions::_xml_module->is_attribute($node)
Truth table33%1/3
A and B
A= $where eq "replace"B= $XML::XSH2::Functions::_xml_module->is_attribute($node)
ABresult
0X0
100
111
3835 0 my $parent=$dest->parentNode();
3836 # -- prepare NS
3837 0 ▶ $ns=$node->namespaceURI() if ($ns eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "" 0 0 0
3838 0 ▶ if ($ns eq "" and name_prefix($node->getName) ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns eq "" and name_prefix($node->getName) ne "") 0 0 0
Condition0%0/3
$ns eq "" and name_prefix($node->getName) ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$ns eq "" and name_prefix($node->getName) ne ""
$ns eq "" name_prefix($node->getName) ne ""
Truth table0%0/3
A and B
A= $ns eq ""B= name_prefix($node->getName) ne ""
ABresult
0X0
100
111
3839 0 $ns=$dest->lookupNamespaceURI(name_prefix($node->getName));
3840 }
3841 # --
3842 0 ▶ if ($_xml_module->is_element($parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($parent)) 0 0 0
3843 0 set_attr_ns($dest,"$ns",$node->getName(),$node->getValue());
3844 0 ▶ push @$rl,$dest->getAttributeNodeNS("$ns",$node->getName()) if defined($rl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
3845 }
3846 0 return 'remove';
3847 } else {
3848 7 my $parent=$dest->parentNode();
3849 7 my $new;
3850 # source: Attribute
3851 7 ▶ if ($_xml_module->is_attribute($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 0 7 7
3852 0 _err("Warning: Ignoring incompatible nodes in insert/copy/move operation:\n",
3853 ref($node)," $where ",ref($dest),"!");
3854 0 return 1;
3855 # # implicit conversion of attribute to element
3856 # # -- prepare NS
3857 # $ns=$node->namespaceURI() if ($ns eq "");
3858 # if ($ns eq "" and name_prefix($node->getName) ne "") {
3859 # $ns=$parent->lookupNamespaceURI(name_prefix($node->getName));
3860 # }
3861 # # --
3862 # $new=new_element($dest_doc,$node->getName(),$ns,$dest);
3863 # $new->appendText($node->getValue());
3864 }
3865 # source: All other
3866 else {
3867 7 $new=node_copy($node,$ns,$dest_doc,$dest);
3868 }
3869 7 ▶ if ($where =~ /^(?:after|before|replace)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where =~ /^(?:after|before|replace)$/) 7 0 7
3870 7 ▶ push @$rl,_expand_fragment($new) if defined $rl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 7 7
3871 7 return safe_insert($new,$dest,$where);
3872 }
3873 }
3874 } else {
3875 0 print STDERR "Warning: unsupported/unknown destination type: ",ref($dest),"\n";
3876 0 ▶ print STDERR substr($node->toString(),0,200), substr($dest->toString(),0,200),"\n" if ref($dest);
Branch: 1 branch
BranchTrueFalseTotal
if ref $dest 0 0 0
3877 }
3878 77 return 1;
3879 }
3880  
3881 # parse a string and create attribute nodes
3882 18 ▶sub create_attributes {
Subroutine create_attributes: called CC=11 SCAR=30.6
Pod undocumented
3883 18 my ($str)=@_;
3884 18 my (@ret,$value,$name);
3885 18 ▶ while ($str!~/\G$/gsco) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /\G$/cogs) 27 18 45
3886 27 ▶ if ($str=~/\G\s*([^ \n\r\t=]+)=/gsco) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /\G\s*([^ \n\r\t=]+)=/cogs) { } 27 0 27
elsif ($str =~ /\G(.*)/cogs) { } 0 0 0
3887 27 my $name=$1;
3888 27 ▶ print STDERR "attribute_name=$1\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 27 27
3889 27 ▶ if ($str=~/\G\"((?:[^\\\"]|\\.)*)\"/gsco or
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /\G\"((?:[^\\\"]|\\.)*)\"/cogs or $str =~ /\G\'((?:[^\\\']|\\.)*)\'/cogs or $str =~ /\G(.*?)(?=\s+[^ \n\r\t=]+=|\s*$)/cogs) { } 27 0 27
elsif ($str =~ /\G(\s*)$/cogs) { } 0 0 0
Condition83%5/6
$str =~ /\G\"((?:[^\\\"]|\\.)*)\"/cogs or $str =~ /\G\'((?:[^\\\']|\\.)*)\'/cogs or $str =~ /\G(.*?)(?=\s+[^ \n\r\t=]+=|\s*$)/cogs
l!l&&r!l&&!r
10170
$str =~ /\G\"((?:[^\\\"]|\\.)*)\"/cogs or $str =~ /\G\'((?:[^\\\']|\\.)*)\'/cogs
l!l&&r!l&&!r
1917
MC/DC0%0/3
$str =~ /\G\"((?:[^\\\"]|\\.)*)\"/cogs or $str =~ /\G\'((?:[^\\\']|\\.)*)\'/cogs or $str =~ /\G(.*?)(?=\s+[^ \n\r\t=]+=|\s*$)/cogs
$str =~ /\G\"((?:[^\\\"]|\\.)*)\"/cogs $str =~ /\G\'((?:[^\\\']|\\.)*)\'/cogs $str =~ /\G(.*?)(?=\s+[^ \n\r\t=]+=|\s*$)/cogs
Truth table75%3/4
A or B or C
A= $str =~ /\G\"((?:[^\\\"]|\\.)*)\"/cogsB= $str =~ /\G\'((?:[^\\\']|\\.)*)\'/cogsC= $str =~ /\G(.*?)(?=\s+[^ \n\r\t=]+=|\s*$)/cogs
ABCresult
1XX1
01X1
0011
0000
3890 $str=~/\G\'((?:[^\\\']|\\.)*)\'/gsco or
3891 $str=~/\G(.*?)(?=\s+[^ \n\r\t=]+=|\s*$)/gsco) {
3892 27 $value=$1;
3893 27 $value=~s/\\(.)/$1/g;
3894 27 ▶ print STDERR "creating $name='$value' attribute\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 27 27
3895 27 push @ret,[$name,$value];
3896 } elsif ($str=~/\G(\s*)$/gsco) {
3897 0 $value=$1;
3898 0 ▶ print STDERR "creating $name='$1' attribute\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
3899 0 push @ret,[$name,$value];
3900 0 last;
3901 } else {
3902 0 die "Invalid attribute specification near '".substr($str,pos($str))."'\n";
3903 }
3904 } elsif ($str =~ /\G(.*)/gsco) {
3905 0 die "Invalid attribute specification near '$1'\n"
3906 } else {
3907 0 last;
3908 }
3909 }
3910 18 return @ret;
3911 }
3912  
3913 25 ▶sub new_element {
Subroutine new_element: called CC=34 SCAR=59.7
Pod undocumented
3914 25 my ($doc,$name,$ns,$attrs,$dest)=@_;
3915 25 my $el;
3916 25 my ($prefix,$localname) = $name=~/^([^:>]+):(.*)$/;
3917 25 ▶ if ($prefix ne "" and $ns eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix ne "" and $ns eq "") 0 25 25
Condition33%1/3
$prefix ne "" and $ns eq ""
!ll&&!rl&&r
2500
MC/DC0%0/2
$prefix ne "" and $ns eq ""
$prefix ne "" $ns eq ""
Truth table33%1/3
A and B
A= $prefix ne ""B= $ns eq ""
ABresult
0X0
100
111
3918 0 die "Error: namespace error: undefined namespace prefix `$prefix'\n";
3919 }
3920 25 ▶ if ($dest && $_xml_module->is_element($dest)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dest and $XML::XSH2::Functions::_xml_module->is_element($dest)) { } 25 0 25
elsif ($ns ne '') { } 0 0 0
Condition33%1/3
$dest and $XML::XSH2::Functions::_xml_module->is_element($dest)
!ll&&!rl&&r
0025
MC/DC0%0/2
$dest and $XML::XSH2::Functions::_xml_module->is_element($dest)
$dest $XML::XSH2::Functions::_xml_module->is_element($dest)
Truth table33%1/3
A and B
A= $destB= $XML::XSH2::Functions::_xml_module->is_element($dest)
ABresult
0X0
100
111
3921 25 ▶ print STDERR "DEST is element\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 25 25
3922 25 $el=$dest->addNewChild($ns,$name);
3923
3924 25 ▶ if ($prefix eq "" and $ns eq "" and $dest->lookupNamespaceURI(undef) ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix eq '' and $ns eq '' and $dest->lookupNamespaceURI(undef) ne '') { } 0 25 25
Condition33%2/6
$prefix eq "" and $ns eq "" and $dest->lookupNamespaceURI(undef) ne ""
!ll&&!rl&&r
0250
$prefix eq "" and $ns eq ""
!ll&&!rl&&r
0025
MC/DC0%0/3
$prefix eq "" and $ns eq "" and $dest->lookupNamespaceURI(undef) ne ""
$prefix eq "" $ns eq "" $dest->lookupNamespaceURI(undef) ne ""
Truth table25%1/4
A and B and C
A= $prefix eq ""B= $ns eq ""C= $dest->lookupNamespaceURI(undef) ne ""
ABCresult
0XX0
10X0
1100
1111
3925 0 ▶ print STDERR "CLEAR Default NS\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
3926 0 $el->setNamespace('','',1);
3927 } else {
3928 25 ▶ print STDERR "prefix: $prefix, ns: $ns, lookup: ",$dest->lookupNamespaceURI(undef),".\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 25 25
3929 25 ▶ print STDERR $dest->toString(1),"\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 25 25
3930 }
3931 25 $el->unbindNode();
3932 } elsif ($ns ne '') {
3933 0 ▶ print STDERR "DEST is not element, NS: $ns\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
3934 0 $el=$doc->createElementNS($ns,$name);
3935 } else {
3936 0 ▶ print STDERR "DEST is not element no NS\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
3937 0 $el=$doc->createElement($name);
3938 }
3939 25 ▶ if (ref($attrs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs) 25 0 25
3940 25 foreach (@$attrs) {
3941 20 ▶ if ($ns ne "" and ($_->[0]=~/^\Q${prefix}\E:/)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ns ne '' and $_->[0] =~ /^\Q$prefix\E:/) { } 0 20 20
elsif ($_->[0] =~ /xmlns:(.*)/) { } 0 20 20
elsif ($_->[0] eq 'xmlns') { } 0 20 20
elsif ($_->[0] =~ /^([^:>]+):/) { } 0 20 20
Condition33%1/3
$ns ne "" and $_->[0] =~ /^\Q$prefix\E:/
!ll&&!rl&&r
2000
MC/DC0%0/2
$ns ne "" and $_->[0] =~ /^\Q$prefix\E:/
$ns ne "" $_->[0] =~ /^\Q$prefix\E:/
Truth table33%1/3
A and B
A= $ns ne ""B= $_->[0] =~ /^\Q$prefix\E:/
ABresult
0X0
100
111
3942 0 ▶ print STDERR "NS: $ns\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
3943 0 $el->setAttributeNS($ns,$_->[0],$_->[1]);
3944 } elsif ($_->[0] =~ "xmlns:(.*)") {
3945 0 ▶ print STDERR "xmlns: $1\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
3946 # don't redeclare NS if already declared on destination node
3947 0 ▶ unless ($_->[1] eq $ns or $dest->lookupNamespaceURI($1) eq $_->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_->[1] eq $ns or $dest->lookupNamespaceURI($1) eq $_->[2]) 0 0 0
Condition0%0/3
$_->[1] eq $ns or $dest->lookupNamespaceURI($1) eq $_->[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->[1] eq $ns or $dest->lookupNamespaceURI($1) eq $_->[2]
$_->[1] eq $ns $dest->lookupNamespaceURI($1) eq $_->[2]
Truth table0%0/3
A or B
A= $_->[1] eq $nsB= $dest->lookupNamespaceURI($1) eq $_->[2]
ABresult
1X1
011
000
3948 0 ▶ $el->setNamespace($_->[1],$1,0) unless ($_->[1] eq $ns);
Branch: 1 branch
BranchTrueFalseTotal
unless $_->[1] eq $ns 0 0 0
3949 }
3950 } elsif ($_->[0] eq "xmlns") {
3951 0 ▶ print STDERR "xmlns: @$_\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
3952 # don't redeclare NS if already declared on destination node
3953 0 ▶ unless ($->[1] eq $ns or $dest->lookupNamespaceURI('') eq $_->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($- > [1] eq $ns or $dest->lookupNamespaceURI("") eq $_->[2]) 0 0 0
Condition0%0/3
$- > [1] eq $ns or $dest->lookupNamespaceURI("") eq $_->[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
$- > [1] eq $ns or $dest->lookupNamespaceURI("") eq $_->[2]
$- > [1] eq $ns $dest->lookupNamespaceURI("") eq $_->[2]
Truth table0%0/3
A or B
A= $- > [1] eq $nsB= $dest->lookupNamespaceURI("") eq $_->[2]
ABresult
1X1
011
000
3954 0 ▶ $el->setNamespace($_->[1],'',0) unless ($_->[1] eq $ns);
Branch: 1 branch
BranchTrueFalseTotal
unless $_->[1] eq $ns 0 0 0
3955 }
3956 } elsif ($_->[0]=~/^([^:>]+):/) {
3957 0 my $lprefix=$1;
3958 0 ▶ if ($_->[2] ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[2] ne '') { } 0 0 0
3959 0 $el->setAttributeNS($_->[2],$_->[0],$_->[1]);
3960 } else {
3961 # add the attribute anyway (may have wrong qname!)
3962 0 $el->setAttribute($_->[0],$_->[1]);
3963 }
3964 } else {
3965 20 ▶ next if ($_->[0] eq "xmlns:$prefix" and $_->[1] eq $ns);
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] eq "xmlns:$prefix" and $_->[1] eq $ns 0 20 20
Condition33%1/3
$_->[0] eq "xmlns:$prefix" and $_->[1] eq $ns
!ll&&!rl&&r
2000
MC/DC0%0/2
$_->[0] eq "xmlns:$prefix" and $_->[1] eq $ns
$_->[0] eq "xmlns:$prefix" $_->[1] eq $ns
Truth table33%1/3
A and B
A= $_->[0] eq "xmlns:$prefix"B= $_->[1] eq $ns
ABresult
0X0
100
111
3966 20 $el->setAttribute($_->[0],$_->[1]); # what about other namespaces?
3967 }
3968 }
3969 }
3970 25 return $el;
3971 }
3972  
3973 # create nodes from their textual representation
3974 106 ▶sub create_nodes {
Subroutine create_nodes: called CC=43 SCAR=51.9
Pod undocumented
3975 106 my ($type,$str,$doc,$ns)=@_;
3976 106 my @nodes=();
3977 106 ▶ die "No document for create $type $str for.\n" unless ref($doc);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc 0 106 106
3978 106 ▶ die "Can't create $type from empty specification.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $str eq "" and not $type =~ /text|cdata|comment/ 0 106 106
Condition33%1/3
$str eq "" and not $type =~ /text|cdata|comment/
!ll&&!rl&&r
10600
MC/DC0%0/2
$str eq "" and not $type =~ /text|cdata|comment/
$str eq "" not $type =~ /text|cdata|comment/
Truth table33%1/3
A and not(B)
A= $str eq ""B= not $type =~ /text|cdata|comment/
ABresult
0X0
100
111
3979 if ($str eq "" and $type !~ /text|cdata|comment/);
3980 # return undef unless ($str ne "" and ref($doc));
3981 106 ▶ if ($type eq 'chunk') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'chunk') { } 9 97 106
3982 9 @nodes=map {$_->childNodes()}
3983 9 grep {ref($_)} ($_parser->parse_xml_chunk($str));
3984 } else {
3985 97 ▶ if ($type eq 'attribute') {
Branch: 7 branches
BranchTrueFalseTotal
if ($type eq 'attribute') { } 5 92 97
elsif ($type eq 'element') { } 57 35 92
elsif ($type eq 'text') { } 19 16 35
elsif ($type eq 'entity_reference') { } 0 16 16
elsif ($type eq 'cdata') { } 0 16 16
elsif ($type eq 'pi') { } 8 8 16
elsif ($type eq 'comment') { } 8 0 8
3986 5 foreach (create_attributes($str)) {
3987 5 my $at;
3988 5 ▶ if ($_->[0]=~/^([^:]+):/ and $1 ne 'xmlns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[0] =~ /^([^:]+):/ and $1 ne 'xmlns') { } 0 5 5
Condition33%1/3
$_->[0] =~ /^([^:]+):/ and $1 ne "xmlns"
!ll&&!rl&&r
500
MC/DC0%0/2
$_->[0] =~ /^([^:]+):/ and $1 ne "xmlns"
$_->[0] =~ /^([^:]+):/ $1 ne "xmlns"
Truth table33%1/3
A and B
A= $_->[0] =~ /^([^:]+):/B= $1 ne "xmlns"
ABresult
0X0
100
111
3989 0 ▶ $ns = get_registered_ns($1) if $ns eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "" 0 0 0
3990 0 ▶ die "Error: undefined namespace prefix `$1'\n" if ($ns eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "" 0 0 0
3991 0 $at=$doc->createAttributeNS($ns,$_->[0],$_->[1]);
3992 } else {
3993 5 $at=$doc->createAttribute($_->[0],$_->[1]);
3994 }
3995 5 push @nodes,$at;
3996 }
3997 } elsif ($type eq 'element') {
3998 57 my ($name,$attributes);
3999 57 ▶ if ($str=~/^\<?([^ \t\n\/\<\>]+)(\s+.*)?(?:\/?\>)?\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[^\<?([^ \t\n/\<\>]+)(\s+.*)?(?:/?\>)?\s*$]) { } 57 0 57
4000 57 ▶ print STDERR "element_name=$1\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 57 57
4001 57 ▶ print STDERR "attributes=$2\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 57 57
4002 57 my ($elt,$att)=($1,$2);
4003 57 my $el;
4004 57 ▶ if ($elt=~/^([^:>]+):(.*)$/ or $ns ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt =~ /^([^:>]+):(.*)$/ or $ns ne '') { } 3 54 57
Condition66%2/3
$elt =~ /^([^:>]+):(.*)$/ or $ns ne ""
l!l&&r!l&&!r
3054
MC/DC50%1/2
$elt =~ /^([^:>]+):(.*)$/ or $ns ne ""
$elt =~ /^([^:>]+):(.*)$/ $ns ne ""
Truth table66%2/3
A or B
A= $elt =~ /^([^:>]+):(.*)$/B= $ns ne ""
ABresult
1X1
011
000
4005 3 ▶ print STDERR "Name: $elt\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 3 3
4006 3 ▶ if ($ns eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns eq '') { } 0 3 3
4007 0 ▶ print STDERR "NS prefix registered as: $ns\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
4008 0 ▶ $ns = get_registered_ns($1) if $ns eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "" 0 0 0
4009 } else {
4010 3 ▶ print STDERR "NS: $ns\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 3 3
4011 }
4012 3 ▶ die "Error: undefined namespace prefix `$1'\n" if ($1 ne "" and $ns eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "" and $ns eq "" 0 3 3
Condition33%1/3
$1 ne "" and $ns eq ""
!ll&&!rl&&r
030
MC/DC0%0/2
$1 ne "" and $ns eq ""
$1 ne "" $ns eq ""
Truth table33%1/3
A and B
A= $1 ne ""B= $ns eq ""
ABresult
0X0
100
111
4013 3 $el=$doc->createElementNS($ns,$elt);
4014 } else {
4015 54 $el=$doc->createElement($elt);
4016 }
4017 57 ▶ if ($att ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($att ne "") 13 44 57
4018 13 $att=~s/\/?\>?$//;
4019 13 foreach (create_attributes($att)) {
4020 22 ▶ print STDERR "atribute: ",$_->[0],"=",$_->[1],"\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 22 22
4021 22 ▶ if ($elt=~/^([^:]+):/ and $1 ne 'xmlns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt =~ /^([^:]+):/ and $1 ne 'xmlns') { } 0 22 22
Condition33%1/3
$elt =~ /^([^:]+):/ and $1 ne "xmlns"
!ll&&!rl&&r
2200
MC/DC0%0/2
$elt =~ /^([^:]+):/ and $1 ne "xmlns"
$elt =~ /^([^:]+):/ $1 ne "xmlns"
Truth table33%1/3
A and B
A= $elt =~ /^([^:]+):/B= $1 ne "xmlns"
ABresult
0X0
100
111
4022 0 ▶ print STDERR "NS: $ns\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
4023 0 ▶ die "Error: undefined namespace prefix `$1'\n" if ($ns eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "" 0 0 0
4024 0 $el->setAttributeNS($ns,$_->[0],$_->[1]);
4025 } else {
4026 22 $el->setAttribute($_->[0],$_->[1]);
4027 }
4028 }
4029 }
4030 57 push @nodes,$el;
4031 # __debug("ns: $ns\n".$el->toString());
4032 } else {
4033 0 ▶ print STDERR "invalid element $str\n" unless "$QUIET";
Branch: 1 branch
BranchTrueFalseTotal
unless "$XML::XSH2::Functions::QUIET" 0 0 0
4034 }
4035 } elsif ($type eq 'text') {
4036 19 push @nodes,$doc->createTextNode($str);
4037 19 ▶ print STDERR "text=$str\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 19 19
4038 } elsif ($type eq 'entity_reference') {
4039 0 push @nodes,$doc->createEntityReference($str);
4040 0 ▶ print STDERR "entity_reference=$str\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
4041 } elsif ($type eq 'cdata') {
4042 0 push @nodes,$doc->createCDATASection($str);
4043 0 ▶ print STDERR "cdata=$str\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 0 0
4044 } elsif ($type eq 'pi') {
4045 8 my ($name,$data)=($str=~/^\s*(?:\<\?)?(\S+)(?:\s+(.*?)(?:\?\>)?)?$/);
4046 8 ▶ $data = "" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 8 0 8
4047 8 my $pi = $doc->createPI($name,$data);
4048 8 ▶ print STDERR "pi=<?$name ... $data?>\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 8 8
4049 8 push @nodes,$pi;
4050 # print STDERR "cannot add PI yet\n" if $DEBUG;
4051 } elsif ($type eq 'comment') {
4052 8 push @nodes,$doc->createComment($str);
4053 8 ▶ print STDERR "comment=$str\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 8 8
4054 } else {
4055 0 die "unknown type: $type\n";
4056 }
4057 }
4058 106 return @nodes;
4059 }
4060  
4061 0 ▶sub run_editor {
Subroutine run_editor: not called CC=4 SCAR=30.0
Pod undocumented
4062 0 my ($data,$editor,$encoding)=@_;
4063 0 ($editor) = grep {$_ ne ""} $editor,$ENV{VISUAL},$ENV{EDITOR},'vi';
4064 0 ▶ $encoding = $QUERY_ENCODING unless $encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $encoding 0 0 0
4065 0 my $dir = tempdir( CLEANUP => 1 );
4066 0 my ($fh, $filename) = tempfile( DIR => $dir );
4067 0 binmode $fh,'bytes';
4068 0 $fh->print(fromUTF8($encoding,$data));
4069 0 ▶ $fh->flush if $fh->can('flush');
Branch: 1 branch
BranchTrueFalseTotal
if $fh->can("flush") 0 0 0
4070 0 close($fh);
4071 0 ▶ if (system($editor." ".$filename) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system($editor . ' ' . $filename) == 0) { } 0 0 0
4072 0 open $fh,$filename;
4073 0 binmode $fh,'bytes';
4074 0 $data= join "",map toUTF8($encoding,$_),<$fh>;
4075 0 close $fh;
4076 } else {
4077 0 $data=undef;
4078 }
4079 0 unlink $filename;
4080 0 unlink $dir;
4081 0 return $data;
4082 }
4083  
4084 0 ▶sub ask_user {
Subroutine ask_user: not called CC=3 SCAR=24.8
Pod undocumented
4085 0 my ($question, $answers) = @_;
4086 0 print STDERR $question;
4087 0 STDERR->flush;
4088 0 my $reply = <STDIN>;
4089 0 chomp $reply;
4090 0 ▶ if ($answers ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($answers ne "") 0 0 0
4091 0 ▶ while ($reply !~ /^$answers$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply =~ /^$answers$/) 0 0 0
4092 0 print STDERR "Answer ",join("/",split(/\|/,$answers)),": ";
4093 0 STDERR->flush;
4094 0 $reply = <STDIN>;
4095 0 chomp $reply;
4096 }
4097 }
4098 0 return $reply;
4099 }
4100  
4101 ############### END OF AUXILIARY FUNCTIONS ###############
4102  
4103 0 ▶sub edit {
Subroutine edit: not called CC=51 SCAR=78.8
Pod undocumented
4104 0 my ($opts,$exp,$variable)=@_;
4105 0 $opts = _ev_opts($opts);
4106 0 my $rl = _prepare_result_nl();
4107 0 my $ql;
4108 0 ▶ unless ($variable) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($variable) 0 0 0
4109 0 ▶ $exp = '.' if $exp eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $exp eq "" 0 0 0
4110 0 $ql =_ev_nodelist($exp);
4111 0 ▶ unless (@$ql) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$ql) 0 0 0
4112 0 _warn("No nodes matching $exp");
4113 0 return $rl;
4114 }
4115 # prune nodes included in subtrees of already present nodes
4116 # cause they would get replaced anyway
4117 0 my %n;
4118 $ql = [ grep {
4119 0 my $d=$_; my $ret=1;
4120 0 ▶ while ($d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d) 0 0 0
4121 0 ▶ if (exists ($n{$$d})) { $ret = 0; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $n{$$d}) { } 0 0 0
4122 0 else { $d=$d->parentNode; }
4123 }
4124 0 $n{$$_}=1; $ret } @$ql ];
4125 }
4126 0 my $data;
4127 0 my $node_idx = 0;
4128 0 my $fix;
4129 my $node;
4130 0 ▶ my $nodes = scalar(@$ql) unless $variable;
Branch: 1 branch
BranchTrueFalseTotal
unless $variable 0 0 0
4131 0 ▶ while ($variable or ($node = $ql->[$node_idx++])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variable or $node = $$ql[$node_idx++]) 0 0 0
Condition0%0/3
$variable or $node = $$ql[$node_idx++]
l!l&&r!l&&!r
000
MC/DC0%0/2
$variable or $node = $$ql[$node_idx++]
$variable $node = $$ql[$node_idx++]
Truth table0%0/3
A or B
A= $variableB= $node = $$ql[$node_idx++]
ABresult
1X1
011
000
4132 0 ▶ if ($variable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variable) { } 0 0 0
4133 0 $data=_ev_literal($exp)
4134 } else {
4135 0 my $pwd = pwd($node);
4136 0 ▶ if ($fix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix) { } 0 0 0
4137 0 undef $fix;
4138 } else {
4139 0 ▶ if ($_xml_module->is_attribute($node)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node) or $XML::XSH2::Functions::_xml_module->is_text_or_cdata($node) or $XML::XSH2::Functions::_xml_module->is_comment($node) or $XML::XSH2::Functions::_xml_module->is_pi($node)) { } 0 0 0
4140 0 $data=$node->value;
4141 ▶ } elsif ($_xml_module->is_element($node) or
Condition0%0/12
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node) or $XML::XSH2::Functions::_xml_module->is_text_or_cdata($node) or $XML::XSH2::Functions::_xml_module->is_comment($node) or $XML::XSH2::Functions::_xml_module->is_pi($node)
l!l&&r!l&&!r
000
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node) or $XML::XSH2::Functions::_xml_module->is_text_or_cdata($node) or $XML::XSH2::Functions::_xml_module->is_comment($node)
l!l&&r!l&&!r
000
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node) or $XML::XSH2::Functions::_xml_module->is_text_or_cdata($node)
l!l&&r!l&&!r
000
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node)
l!l&&r!l&&!r
000
MC/DC0%0/5
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node) or $XML::XSH2::Functions::_xml_module->is_text_or_cdata($node) or $XML::XSH2::Functions::_xml_module->is_comment($node) or $XML::XSH2::Functions::_xml_module->is_pi($node)
$XML::XSH2::Functions::_xml_module->is_element($node) $XML::XSH2::Functions::_xml_module->is_document($node) $XML::XSH2::Functions::_xml_module->is_text_or_cdata($node) $XML::XSH2::Functions::_xml_module->is_comment($node) $XML::XSH2::Functions::_xml_module->is_pi($node)
Truth table0%0/6
A or B or C or D or E
A= $XML::XSH2::Functions::_xml_module->is_element($node)B= $XML::XSH2::Functions::_xml_module->is_document($node)C= $XML::XSH2::Functions::_xml_module->is_text_or_cdata($node)D= $XML::XSH2::Functions::_xml_module->is_comment($node)E= $XML::XSH2::Functions::_xml_module->is_pi($node)
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
4142 $_xml_module->is_document($node) or
4143 $_xml_module->is_text_or_cdata($node) or
4144 $_xml_module->is_comment($node) or
4145 $_xml_module->is_pi($node)) {
4146 0 ▶ $data=$_xml_module->toStringUTF8($node,$opts->{noindent} ? 0 :$INDENT);
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'noindent'} ? : 0 0 0
4147 } else {
4148 0 die("Cannot edit ".ref($node)."\n");
4149 }
4150 }
4151 $data="<!-- XSH-COMMENT: $pwd ".
4152 ($opts->{all} ? "($node_idx/$nodes) " : "")."-->\n"
4153 0 ▶ .$data unless $opts->{'no-comment'};
Branch: 2 branches
BranchTrueFalseTotal
unless $opts->{"no-comment"} 0 0 0
$opts->{'all'} ? : 0 0 0
4154 }
4155 0 my $replacement = run_editor($data,$opts->{editor},$opts->{encoding});
4156 0 ▶ $replacement =~ s/^\s*<!-- XSH-COMMENT: [^>]*-->[ \t]*\n?// unless $variable;
Branch: 1 branch
BranchTrueFalseTotal
unless $variable 0 0 0
4157 0 ▶ chomp $replacement unless $variable;
Branch: 1 branch
BranchTrueFalseTotal
unless $variable 0 0 0
4158 0 ▶ while ($replacement eq "" and not($opts->{'allow-empty'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replacement eq "" and not $opts->{"allow-empty"}) 0 0 0
Condition0%0/3
$replacement eq "" and not $opts->{"allow-empty"}
!ll&&!rl&&r
000
MC/DC0%0/2
$replacement eq "" and not $opts->{"allow-empty"}
$replacement eq "" not $opts->{"allow-empty"}
Truth table0%0/3
A and not(B)
A= $replacement eq ""B= not $opts->{"allow-empty"}
ABresult
0X0
100
111
4159 0 ▶ if (-t) {
Branch: 1 branch
BranchTrueFalseTotal
if (-t STDIN) { } 0 0 0
4160 0 my $response = ask_user("Result is empty! Is that correct? (yes/no/stop): ",
4161 "y|n|s|yes|no|stop");
4162 0 ▶ if ($response =~ /^y/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response =~ /^y/) { } 0 0 0
elsif ($response =~ /^s/) { } 0 0 0
4163 0 last;
4164 } elsif ($response =~ /^s/) {
4165 0 ▶ return $variable ? $data : $rl;
Branch: 1 branch
BranchTrueFalseTotal
$variable ? : 0 0 0
4166 } else {
4167 0 $replacement = run_editor($data,$opts->{editor},$opts->{encoding});
4168 0 $replacement =~ s/^\s*<!-- XSH-COMMENT: [^>]*-->[ \t]*\n?//;
4169 }
4170 } else {
4171 0 die("Result is empty, ignoring changes!\n".
4172 "Hint: use --allow-empty option or remove command.\n");
4173 }
4174 }
4175 0 ▶ if ($variable) {
Branch: 2 branches
BranchTrueFalseTotal
if ($variable) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 0 0 0
4176 0 ▶ if ($exp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp) 0 0 0
4177 0 _assign($exp,$replacement);
4178 }
4179 0 return $replacement;
4180 } elsif ($_xml_module->is_attribute($node)) {
4181 0 ▶ $node->setValue($replacement) if defined $replacement;
Branch: 1 branch
BranchTrueFalseTotal
if defined $replacement 0 0 0
4182 0 ▶ push @$rl, $node if defined $rl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
4183 } else {
4184 0 ▶ local $RECOVERING=$opts->{recover} ? 1 : $RECOVERING;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'recover'} ? : 0 0 0
4185 0 ▶ local $KEEP_BLANKS=$opts->{'keep-blanks'} ? 1 : !$INDENT;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'keep-blanks'} ? : 0 0 0
4186 0 my $chunk;
4187 0 ▶ if ($_xml_module->is_document($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_document($node)) { } 0 0 0
4188 0 $chunk = eval { $_xml_module->parse_string($_parser,$replacement) };
4189 } else {
4190 0 $chunk = eval { $_xml_module->parse_chunk($_parser,$replacement); };
4191 }
4192 0 ▶ if ($@ or not ref($chunk)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not ref $chunk) 0 0 0
Condition0%0/3
$@ or not ref $chunk
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not ref $chunk
$@ not ref $chunk
Truth table0%0/3
A or not(B)
A= $@B= not ref $chunk
ABresult
1X1
011
000
4193 0 ▶ if (-t) {
Branch: 1 branch
BranchTrueFalseTotal
if (-t STDIN) { } 0 0 0
4194 0 my $c = ask_user("$@\n"."Parse error! Press:\n".
4195 " 1 - continue with next node\n".
4196 " 2 - fix the error in the editor\n".
4197 " 3 - restart editor on this node (discarding changes)\n".
4198 " 4 - stop\n\n".
4199 "Your choice: ","1|2|3|4");
4200 0 ▶ if ($c == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($c == 1) { } 0 0 0
elsif ($c == 2) { } 0 0 0
elsif ($c == 3) { } 0 0 0
4201 0 next;
4202 } elsif ($c == 2) {
4203 0 $data = $replacement;
4204 0 $fix=1;
4205 0 redo;
4206 } elsif ($c == 3) {
4207 0 redo;
4208 } else {
4209 0 return $rl;
4210 }
4211 } else {
4212 0 die("$@"."Error parsing result, ignoring changes!\n");
4213 }
4214 }
4215 0 ▶ if ($_xml_module->is_document($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_document($node)) { } 0 0 0
4216 0 foreach my $child ($node->childNodes()) {
4217 0 $child->unbindNode();
4218 }
4219 0 foreach my $child ($chunk->childNodes()) {
4220 0 $child->unbindNode();
4221 0 ▶ if ($_xml_module->is_element($child)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($child)) { } 0 0 0
4222 0 $node->setDocumentElement($child);
4223 0 ▶ while (my $sibling = $child->nextSibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sibling = $child->nextSibling) 0 0 0
4224 0 $sibling->unbindNode();
4225 0 $node->insertBefore($sibling);
4226 }
4227 } else {
4228 0 $node->appendChild($child);
4229 }
4230 }
4231 0 ▶ push @$rl, $chunk->childNodes() if defined $rl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
4232 } else {
4233 0 ▶ if (insert_node($chunk,$node,undef,'replace',undef,$rl) eq 'remove') {
Branch: 1 branch
BranchTrueFalseTotal
if (insert_node($chunk, $node, undef, "replace", undef, $rl) eq "remove") 0 0 0
4234 0 remove_node($node);
4235  
4236 }
4237 }
4238 }
4239 } continue {
4240 0 ▶ last if (not(exists($opts->{all})) or $variable);
Branch: 1 branch
BranchTrueFalseTotal
if not exists $opts->{"all"} or $variable 0 0 0
Condition0%0/3
not exists $opts->{"all"} or $variable
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $opts->{"all"} or $variable
not exists $opts->{"all"} $variable
Truth table0%0/3
not(A) or B
A= not exists $opts->{"all"}B= $variable
ABresult
1X1
011
000
4241 }
4242 0 return $rl;
4243 }
4244  
4245 # copy nodes matching one XPath expression to locations determined by
4246 # other XPath expression
4247 27 ▶sub copy {
Subroutine copy: called CC=28 SCAR=36.3
Pod undocumented
4248 27 my ($opts,$fexp,$where,$texp,$all_to_all)=@_;
4249 27 my $fl;
4250 27 $opts = _ev_opts($opts);
4251 27 $fl=_ev_nodelist($fexp);
4252 27 ▶ unless (@$fl) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$fl) 1 26 27
4253 1 _warn("No nodes matching $fexp");
4254 1 return 1;
4255 }
4256 # respective copying
4257 26 my $rl=_prepare_result_nl();
4258 26 ▶ if ($opts->{respective}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'respective'}) { } 2 24 26
4259 2 my @rtl;
4260 2 my $old_context = _save_context();
4261 2 eval {
4262 2 my $pos=1;
4263 2 my $size = @$fl;
4264 2 foreach my $fp (@$fl) {
4265 10 _set_context([$fp,$size,$pos]);
4266 10 my $tl=_ev_nodelist($texp);
4267 10 ▶ unless (@$tl) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$tl) 0 10 10
4268 0 ▶ my $th = ($pos%10 == 1 ? "st" : $pos%10 == 2 ? "nd" : "th");
Branch: 2 branches
BranchTrueFalseTotal
$pos % 10 == 1 ? : 0 0 0
$pos % 10 == 2 ? : 0 0 0
4269 0 _warn("No nodes matching $texp for $pos$th node matching $fexp\n");
4270 }
4271 0 ▶ unless ($all_to_all) { @$tl = ($tl->[0]) }
Branch: 1 branch
BranchTrueFalseTotal
unless ($all_to_all) 0 10 10
4272 10 push @rtl, $tl;
4273 10 $pos++;
4274 }
4275 };
4276 2 my $err = $@;
4277 2 do {
4278 2 local $SIG{INT}=\&flagsigint;
4279 2 _set_context($old_context);
4280 2 propagate_flagsigint();
4281 };
4282 2 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 2 2
4283 2 ▶ my $reverse = $opts->{'preserve-order'} && $where=~/^(after|prepend)$/;
Condition66%2/3
$opts->{'preserve-order'} && $where =~ /^(after|prepend)$/
!ll&&!rl&&r
101
MC/DC50%1/2
$opts->{'preserve-order'} && $where =~ /^(after|prepend)$/
$opts->{'preserve-order'} $where =~ /^(after|prepend)$/
Truth table66%2/3
A && B
A= $opts->{'preserve-order'}B= $where =~ /^(after|prepend)$/
ABresult
0X0
100
111
4284 2 ▶ foreach my $fp ($reverse ? reverse @$fl : @$fl) {
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 1 1 2
4285 10 ▶ my $tl = $reverse ? pop(@rtl) : shift(@rtl);
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 5 5 10
4286 10 foreach my $tp (@$tl) {
4287 10 my $replace=0;
4288 10 ▶ $replace = ((insert_node($fp,$tp,undef,$where,undef,$rl)
Condition33%1/3
insert_node($fp, $tp, undef, $where, undef, $rl) eq 'remove' || $replace
l!l&&r!l&&!r
0010
MC/DC0%0/2
insert_node($fp, $tp, undef, $where, undef, $rl) eq 'remove' || $replace
insert_node($fp, $tp, undef, $where, undef, $rl) eq 'remove' $replace
Truth table33%1/3
A || B
A= insert_node($fp, $tp, undef, $where, undef, $rl) eq 'remove'B= $replace
ABresult
1X1
011
000
4289 eq 'remove') || $replace);
4290 10 ▶ if ($replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace) 0 10 10
4291 0 remove_node($tp);
4292 }
4293 }
4294 }
4295 } else {
4296 # non-respective copying
4297 24 my $tl=_ev_nodelist($texp);
4298 24 ▶ unless (@$tl) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$tl) 0 24 24
4299 0 _warn("No nodes matching $texp");
4300 0 return $rl;
4301 }
4302 24 ▶ if ($all_to_all) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_to_all) { } 13 11 24
4303 13 my $real_fl;
4304 13 ▶ if ($opts->{'preserve-order'} && $where=~/^(after|prepend)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'preserve-order'} and $where =~ /^(after|prepend)$/) { } 2 11 13
Condition66%2/3
$opts->{"preserve-order"} and $where =~ /^(after|prepend)$/
!ll&&!rl&&r
1102
MC/DC50%1/2
$opts->{"preserve-order"} and $where =~ /^(after|prepend)$/
$opts->{"preserve-order"} $where =~ /^(after|prepend)$/
Truth table66%2/3
A and B
A= $opts->{"preserve-order"}B= $where =~ /^(after|prepend)$/
ABresult
0X0
100
111
4305 2 $real_fl = [ reverse @$fl ];
4306 } else {
4307 11 $real_fl = $fl;
4308 }
4309 13 foreach my $tp (@$tl) {
4310 13 my $replace=0;
4311 13 foreach my $fp (@$real_fl) {
4312 41 ▶ $replace = ((insert_node($fp,$tp,undef,$where,undef,$rl)
Condition33%1/3
insert_node($fp, $tp, undef, $where, undef, $rl) eq 'remove' || $replace
l!l&&r!l&&!r
0041
MC/DC0%0/2
insert_node($fp, $tp, undef, $where, undef, $rl) eq 'remove' || $replace
insert_node($fp, $tp, undef, $where, undef, $rl) eq 'remove' $replace
Truth table33%1/3
A || B
A= insert_node($fp, $tp, undef, $where, undef, $rl) eq 'remove'B= $replace
ABresult
1X1
011
000
4313 eq 'remove') || $replace);
4314 }
4315 13 ▶ if ($replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace) 0 13 13
4316 0 remove_node($tp);
4317 }
4318 }
4319 } else {
4320 11 ▶ _warn("Different number of source and destination nodes.\n".
Branch: 1 branch
BranchTrueFalseTotal
if @$fl != @$tl 0 11 11
4321 "(Maybe you wanted to call xcopy/xmove?)\n".
4322 "Continuing anyway!") if (@$fl != @$tl);
4323 11 ▶ while (ref(my $fp=shift @$fl) and ref(my $tp=shift @$tl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref(my $fp = shift @$fl) and ref(my $tp = shift @$tl)) 11 11 22
Condition66%2/3
ref(my $fp = shift @$fl) and ref(my $tp = shift @$tl)
!ll&&!rl&&r
11011
MC/DC50%1/2
ref(my $fp = shift @$fl) and ref(my $tp = shift @$tl)
ref(my $fp = shift @$fl) ref(my $tp = shift @$tl)
Truth table66%2/3
A and B
A= ref(my $fp = shift @$fl)B= ref(my $tp = shift @$tl)
ABresult
0X0
100
111
4324 11 my $replace=insert_node($fp,$tp,undef,$where,undef,$rl);
4325 11 ▶ if ($replace eq 'remove') {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace eq "remove") 1 10 11
4326 1 remove_node($tp);
4327 }
4328 }
4329 }
4330 }
4331 26 return $rl;
4332 }
4333  
4334  
4335 # create new nodes from an expression and insert them to locations
4336 # identified by XPath
4337 60 ▶sub insert {
Subroutine insert: called CC=12 SCAR=28.1
Pod undocumented
4338 60 my ($opts,$type,$str,$where,$exp,$to_all)=@_;
4339 60 $opts = _ev_opts($opts);
4340 60 $str = _ev_string($str);
4341 60 my $ns = _ev_namespace($opts->{namespace});
4342 60 my $tl=_ev_nodelist($exp);
4343 60 ▶ unless (@$tl) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$tl) 0 60 60
4344 0 _warn("Expression '$exp' returns empty node-list");
4345 0 return 1;
4346 }
4347 60 my $rl = _prepare_result_nl();
4348 60 my @nodes;
4349 60 @nodes=grep {ref($_)} create_nodes($type,$str,$_xml_module->owner_document($tl->[0]),$ns);
4350 60 ▶ unless (@nodes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@nodes) 0 60 60
4351 0 _warn("Expression generates no nodes to insert");
4352 0 return $rl;
4353 }
4354 60 ▶ if ($to_all) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to_all) { } 1 59 60
elsif ($tl->[0]) { } 59 0 59
4355 1 foreach my $tp (@$tl) {
4356 2 my $replace=0;
4357 2 foreach my $node (@nodes) {
4358 2 ▶ $replace = (insert_node($node,$tp,undef,$where,undef,$rl) eq 'remove') || $replace;
Condition33%1/3
insert_node($node, $tp, undef, $where, undef, $rl) eq 'remove' || $replace
l!l&&r!l&&!r
002
MC/DC0%0/2
insert_node($node, $tp, undef, $where, undef, $rl) eq 'remove' || $replace
insert_node($node, $tp, undef, $where, undef, $rl) eq 'remove' $replace
Truth table33%1/3
A || B
A= insert_node($node, $tp, undef, $where, undef, $rl) eq 'remove'B= $replace
ABresult
1X1
011
000
4359 }
4360 2 ▶ if ($replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace) 0 2 2
4361 0 remove_node($tp);
4362 }
4363 }
4364 } elsif ($tl->[0]) {
4365 59 foreach my $node (@nodes) {
4366 70 ▶ if (ref($tl->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tl->[0]) 70 0 70
4367 70 ▶ if (insert_node($node,$tl->[0],undef,$where,undef,$rl) eq 'remove') {
Branch: 1 branch
BranchTrueFalseTotal
if (insert_node($node, $tl->[0], undef, $where, undef, $rl) eq "remove") 0 70 70
4368 0 remove_node($tl->[0]);
4369 }
4370 }
4371 }
4372 }
4373 60 return $rl;
4374 }
4375  
4376 # wrap nodes into a given element
4377 16 ▶sub wrap {
Subroutine wrap: called CC=32 SCAR=36.8
Pod undocumented
4378 16 my ($opts,$str,$exp)=@_;
4379 16 $opts = _ev_opts($opts);
4380 16 my $ns = _ev_namespace($opts->{namespace});
4381 16 $str = _ev_string($str);
4382  
4383 16 my $rl=_prepare_result_nl();
4384 16 my $ql=_ev_nodelist($exp);
4385 16 my %moved;
4386 16 foreach my $node (@$ql) {
4387 47 ▶ next if $moved{$$node};
Branch: 1 branch
BranchTrueFalseTotal
if $moved{$$node} 14 33 47
4388 33 my ($el) = create_nodes('element',$str,
4389 $_xml_module->owner_document($node),$ns);
4390 33 ▶ if ($opts->{inner}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'inner'}) { } 0 33 33
4391 0 ▶ if ($opts->{while} or $opts->{until}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"while"} or $opts->{"until"}) 0 0 0
Condition0%0/3
$opts->{"while"} or $opts->{"until"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"while"} or $opts->{"until"}
$opts->{"while"} $opts->{"until"}
Truth table0%0/3
A or B
A= $opts->{"while"}B= $opts->{"until"}
ABresult
1X1
011
000
4392 0 die "wrap: cannot use --while or --until together with --inner\n";
4393 }
4394 0 ▶ if ($_xml_module->is_element($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($node)) 0 0 0
4395 0 my @children = $node->childNodes;
4396 0 $node->appendChild($el);
4397 0 foreach my $child (@children) {
4398 0 $child->unbindNode();
4399 0 $el->appendChild($child);
4400 }
4401 }
4402 } else {
4403 33 ▶ if ($_xml_module->is_attribute($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 1 32 33
4404 1 ▶ if ($opts->{while} or $opts->{until}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"while"} or $opts->{"until"}) 0 1 1
Condition33%1/3
$opts->{"while"} or $opts->{"until"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$opts->{"while"} or $opts->{"until"}
$opts->{"while"} $opts->{"until"}
Truth table33%1/3
A or B
A= $opts->{"while"}B= $opts->{"until"}
ABresult
1X1
011
000
4405 0 _warn("wrap: ignoring --while or --until on an attribute");
4406 }
4407 1 my $parent=$node->ownerElement();
4408 1 $parent->insertBefore($el,$parent->firstChild());
4409 1 set_attr_ns($el,$node->namespaceURI(),
4410 $node->getName(),$node->getValue());
4411 1 $node->unbindNode();
4412 } else {
4413 32 my $parent = $node->parentNode();
4414 32 my $last = undef;
4415 32 ▶ unless ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent) 0 32 32
4416 0 die "wrap: cannot wrap node: ".pwd($node)." (node has no parent)\n";
4417 }
4418 # process --while and --until
4419 32 ▶ if (defined $opts->{while} or defined $opts->{until}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"while"} or defined $opts->{"until"}) 17 15 32
Condition100%3/3
defined $opts->{"while"} or defined $opts->{"until"}
l!l&&r!l&&!r
15215
MC/DC100%2/2
defined $opts->{"while"} or defined $opts->{"until"}
defined $opts->{"while"} defined $opts->{"until"}
Truth table100%3/3
A or B
A= defined $opts->{"while"}B= defined $opts->{"until"}
ABresult
1X1
011
000
4420 17 my $while = $opts->{while};
4421 17 my $until = $opts->{until};
4422 17 my $skip_comments = $opts->{'skip-comments'};
4423 17 my $skip_ws = $opts->{'skip-whitespace'};
4424 17 my $skip_pi = $opts->{'skip-pi'};
4425 17 my $next = $node->nextSibling;
4426 # evaluate $opts->{while} in the context of the following sibling
4427 17 ▶ if ($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 17 0 17
4428 17 my $old_context = _save_context();
4429 17 eval {
4430 # what should the size be? guess number of all following siblings
4431 17 my $pos=1;
4432 17 my $size = $node->findvalue('count(following-sibling::node())');
4433 17 ▶ while ($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 38 4 42
4434 38 ▶ unless (($skip_ws and $_xml_module->is_text($next) and $next->getData =~ /^\s*$/) or
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip_ws and $XML::XSH2::Functions::_xml_module->is_text($next) and $next->getData =~ /^\s*$/ or $skip_comments and $XML::XSH2::Functions::_xml_module->is_comment($next) or $skip_pi and $XML::XSH2::Functions::_xml_module->is_pi($next)) 31 7 38
Condition94%17/18
$skip_ws and $XML::XSH2::Functions::_xml_module->is_text($next) and $next->getData =~ /^\s*$/ or $skip_comments and $XML::XSH2::Functions::_xml_module->is_comment($next) or $skip_pi and $XML::XSH2::Functions::_xml_module->is_pi($next)
l!l&&r!l&&!r
5231
$skip_ws and $XML::XSH2::Functions::_xml_module->is_text($next) and $next->getData =~ /^\s*$/ or $skip_comments and $XML::XSH2::Functions::_xml_module->is_comment($next)
l!l&&r!l&&!r
4133
$skip_ws and $XML::XSH2::Functions::_xml_module->is_text($next) and $next->getData =~ /^\s*$/
!ll&&!rl&&r
3404
$skip_ws and $XML::XSH2::Functions::_xml_module->is_text($next)
!ll&&!rl&&r
2594
$skip_comments and $XML::XSH2::Functions::_xml_module->is_comment($next)
!ll&&!rl&&r
2941
$skip_pi and $XML::XSH2::Functions::_xml_module->is_pi($next)
!ll&&!rl&&r
2562
MC/DC85%6/7
$skip_ws and $XML::XSH2::Functions::_xml_module->is_text($next) and $next->getData =~ /^\s*$/ or $skip_comments and $XML::XSH2::Functions::_xml_module->is_comment($next) or $skip_pi and $XML::XSH2::Functions::_xml_module->is_pi($next)
$skip_ws $XML::XSH2::Functions::_xml_module->is_text($next) $next->getData =~ /^\s*$/ $skip_comments $XML::XSH2::Functions::_xml_module->is_comment($next) $skip_pi $XML::XSH2::Functions::_xml_module->is_pi($next)
Truth table31%7/22
A and B and C or D and E or F and G
A= $skip_wsB= $XML::XSH2::Functions::_xml_module->is_text($next)C= $next->getData =~ /^\s*$/D= $skip_commentsE= $XML::XSH2::Functions::_xml_module->is_comment($next)F= $skip_piG= $XML::XSH2::Functions::_xml_module->is_pi($next)
ABCDEFGresult
111XXXX1
0XX11XX1
10X11XX1
11011XX1
0XX0X111
0XX10111
10X0X111
10X10111
1100X111
11010111
0XX0X0X0
0XX0X100
0XX100X0
0XX10100
10X0X0X0
10X0X100
10X100X0
10X10100
1100X0X0
1100X100
110100X0
11010100
4435 ($skip_comments and $_xml_module->is_comment($next)) or
4436 ($skip_pi and $_xml_module->is_pi($next))) {
4437 31 _set_context([$next,$size,$pos]);
4438 31 ▶ if (defined $while) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $while) 23 8 31
4439 23 ▶ last if !_ev_count($while);
Branch: 1 branch
BranchTrueFalseTotal
unless _ev_count($while) 12 11 23
4440 }
4441 19 ▶ if (defined $until) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $until) 8 11 19
4442 8 my $res = _ev_count($until);
4443 8 ▶ last if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 1 7 8
4444 }
4445 18 $last = $next;
4446 18 $pos++;
4447 }
4448 25 $next = $next->nextSibling;
4449 }
4450 };
4451 17 my $err = $@;
4452 17 do {
4453 17 local $SIG{INT}=\&flagsigint;
4454 17 _set_context($old_context);
4455 17 propagate_flagsigint();
4456 };
4457 }
4458 }
4459 32 safe_insert($el,$node,'replace');
4460 32 $el->appendChild($node);
4461 32 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) 9 23 32
4462 9 my $next = $el->nextSibling;
4463 9 ▶ while ($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 23 0 23
4464 23 $next->unbindNode();
4465 23 $el->appendChild($next);
4466 23 $moved{$$next}=1;
4467 23 ▶ last if $next->isSameNode($last);
Branch: 1 branch
BranchTrueFalseTotal
if $next->isSameNode($last) 9 14 23
4468 14 $next = $el->nextSibling;
4469 }
4470 9 ▶ unless ($next) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next) 0 9 9
4471 0 _warn("wrap: something went wrong");
4472 }
4473 }
4474 }
4475 }
4476 33 ▶ push @$rl, $el if defined $rl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 9 24 33
4477 }
4478 16 return $rl;
4479 }
4480  
4481 # wrap span of nodes into a given element
4482 7 ▶sub wrap_span {
Subroutine wrap_span: called CC=18 SCAR=29.9
Pod undocumented
4483 7 my ($opts,$str,$xp_start,$xp_end)=@_;
4484 7 $opts = _ev_opts($opts);
4485 7 $str = _ev_string($str);
4486 7 my $ns = _ev_namespace($opts->{namespace});
4487 7 my $rl=_prepare_result_nl();
4488 7 my $ql_start=_ev_nodelist($xp_start);
4489 7 my $ql_end=_ev_nodelist($xp_end);
4490 7 ▶ if (@$ql_start != @$ql_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ql_start != @$ql_end) 0 7 7
4491 0 die "Error: there are ".scalar(@$ql_start)." start nodes, ".
4492 " but ".scalar(@$ql_end)." end nodes!\n";
4493 }
4494 7 ▶ for (my $i=0; $i<=$#$ql_start; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $ql_start->$#*) 13 7 20
4495 13 my $node = $ql_start->[$i];
4496 13 my $end_node = $ql_end->[$i];
4497 13 ▶ if (not($node->parentNode()) or not($end_node->parentNode())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->parentNode and $end_node->parentNode) 0 13 13
Condition33%1/3
$node->parentNode and $end_node->parentNode
!ll&&!rl&&r
0013
MC/DC0%0/2
$node->parentNode and $end_node->parentNode
$node->parentNode $end_node->parentNode
Truth table33%1/3
A and B
A= $node->parentNodeB= $end_node->parentNode
ABresult
0X0
100
111
4498 0 die "Error: cannot wrap document node\n";
4499 }
4500 13 foreach my $n ($node,$end_node) {
4501 26 ▶ if ($_xml_module->is_attribute($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_attribute($n)) 0 26 26
4502 0 die "Error: attribute node ".pwd($n).
4503 " cannot define a node span boundary\n";
4504 }
4505 }
4506 13 ▶ if (not $node->parentNode()->isSameNode($end_node->parentNode())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->parentNode->isSameNode($end_node->parentNode)) 0 13 13
4507 0 die "Error: start node ".pwd($node)." and end node ".
4508 pwd($end_node)." have different parents\n";
4509 }
4510 13 my ($el) = create_nodes('element',$str,
4511 $_xml_module->owner_document($node),$ns);
4512 13 my $parent = $node->parentNode();
4513 13 my @span;
4514 13 my $n=$node;
4515 13 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 23 0 23
4516 23 push @span,$n;
4517 23 ▶ last if ($n->isSameNode($end_node));
Branch: 1 branch
BranchTrueFalseTotal
if $n->isSameNode($end_node) 13 10 23
4518 10 $n=$n->nextSibling();
4519 }
4520 13 ▶ die "Error: Node ".pwd($end_node).
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 13 13
4521 " isn't following sibling of ".pwd($node)."!\n" unless $n;
4522 13 ▶ if ($_xml_module->is_document($parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_document($parent)) { } 3 10 13
4523 # check that document element is within the span
4524 3 my $docel=$parent->getDocumentElement();
4525 3 my $found=0;
4526 3 foreach my $n (@span) {
4527 5 ▶ if ($n->isSameNode($docel)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isSameNode($docel)) 3 2 5
4528 3 $found=1;
4529 3 last;
4530 }
4531 }
4532 3 ▶ die "Cannot wrap span: ".pwd($node).
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 3 3
4533 " .. ".pwd($end_node)." (document already has a root element)\n"
4534 unless $found;
4535 3 replace_document_element($docel,$el);
4536 3 foreach my $n (@span) {
4537 7 $n->unbindNode();
4538 7 $el->appendChild($n);
4539 }
4540 } else {
4541 10 $parent->insertBefore($el,$node);
4542 10 foreach my $n (@span) {
4543 16 $n->unbindNode();
4544 16 $el->appendChild($n);
4545 }
4546 }
4547 13 ▶ push @$rl, $el if defined $rl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 6 7 13
4548 }
4549 7 return $rl;
4550 }
4551  
4552  
4553 # normalize nodes
4554 0 ▶sub normalize_nodes {
Subroutine normalize_nodes: not called CC=2 SCAR=17.9
Pod undocumented
4555 0 my ($opts,$exp)=@_;
4556 0 my $ql=_ev_nodelist($exp);
4557 0 foreach (@$ql) {
4558 0 $_->normalize();
4559 }
4560 0 return 1;
4561 }
4562  
4563 0 ▶sub _trim_ws {
Subroutine _trim_ws: not called CC=1 SCAR=6.9
4564 0 my ($text)=@_;
4565 0 $text=~s/^\s*//;
4566 0 $text=~s/\s*$//;
4567 0 return $text;
4568 }
4569  
4570 # strip whitespace from given nodes
4571 0 ▶sub strip_ws {
Subroutine strip_ws: not called CC=25 SCAR=64.8
Pod undocumented
4572 0 my ($opts,$exp)=@_;
4573 0 my $ql=_ev_nodelist($exp);
4574 0 foreach my $node (@$ql) {
4575 0 ▶ if ($_xml_module->is_text($node)
Branch: 4 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node) or $XML::XSH2::Functions::_xml_module->is_comment($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_pi($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node)) { } 0 0 0
Condition0%0/6
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node) or $XML::XSH2::Functions::_xml_module->is_comment($node)
l!l&&r!l&&!r
000
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node)
l!l&&r!l&&!r
000
MC/DC0%0/3
$XML::XSH2::Functions::_xml_module->is_text($node) or $XML::XSH2::Functions::_xml_module->is_cdata_section($node) or $XML::XSH2::Functions::_xml_module->is_comment($node)
$XML::XSH2::Functions::_xml_module->is_text($node) $XML::XSH2::Functions::_xml_module->is_cdata_section($node) $XML::XSH2::Functions::_xml_module->is_comment($node)
Truth table0%0/4
A or B or C
A= $XML::XSH2::Functions::_xml_module->is_text($node)B= $XML::XSH2::Functions::_xml_module->is_cdata_section($node)C= $XML::XSH2::Functions::_xml_module->is_comment($node)
ABCresult
1XX1
01X1
0011
0000
4576 or
4577 $_xml_module->is_cdata_section($node)
4578 or
4579 $_xml_module->is_comment($node)
4580 ) {
4581 0 my $data=_trim_ws($node->getData());
4582 0 ▶ if ($data ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne '') { } 0 0 0
4583 0 ▶ $data = "" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
4584 0 $node->setData($data);
4585 } else {
4586 0 $node->unbindNode();
4587 }
4588 } elsif ($_xml_module->is_pi($node)) {
4589 0 $node->setData(_trim_ws($node->getData($node)));
4590 } elsif ($_xml_module->is_attribute($node)) {
4591 0 $node->setValue(_trim_ws($node->getValue));
4592 ▶ } elsif ($_xml_module->is_element($node) or
Condition0%0/3
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node)
l!l&&r!l&&!r
000
MC/DC0%0/2
$XML::XSH2::Functions::_xml_module->is_element($node) or $XML::XSH2::Functions::_xml_module->is_document($node)
$XML::XSH2::Functions::_xml_module->is_element($node) $XML::XSH2::Functions::_xml_module->is_document($node)
Truth table0%0/3
A or B
A= $XML::XSH2::Functions::_xml_module->is_element($node)B= $XML::XSH2::Functions::_xml_module->is_document($node)
ABresult
1X1
011
000
4593 $_xml_module->is_document($node)) {
4594 # traverse children, skip comments, strip text nodes
4595 # until first element or PI or text node containing
4596 # a non-ws character
4597 0 my $child=$node->firstChild();
4598 0 ▶ while ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 0 0 0
4599 0 ▶ if ($_xml_module->is_text($child) or
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_text($child) or $XML::XSH2::Functions::_xml_module->is_cdata_section($child)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_element($child) or $XML::XSH2::Functions::_xml_module->is_pi($child)) { } 0 0 0
Condition0%0/3
$XML::XSH2::Functions::_xml_module->is_text($child) or $XML::XSH2::Functions::_xml_module->is_cdata_section($child)
l!l&&r!l&&!r
000
MC/DC0%0/2
$XML::XSH2::Functions::_xml_module->is_text($child) or $XML::XSH2::Functions::_xml_module->is_cdata_section($child)
$XML::XSH2::Functions::_xml_module->is_text($child) $XML::XSH2::Functions::_xml_module->is_cdata_section($child)
Truth table0%0/3
A or B
A= $XML::XSH2::Functions::_xml_module->is_text($child)B= $XML::XSH2::Functions::_xml_module->is_cdata_section($child)
ABresult
1X1
011
000
4600 $_xml_module->is_cdata_section($child)) {
4601 0 my $data=_trim_ws($child->getData());
4602 0 ▶ if ($data ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne '') { } 0 0 0
4603 0 ▶ $data = "" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
4604 0 $child->setData($data);
4605 0 last;
4606 } else {
4607 0 $child->unbindNode();
4608 }
4609 ▶ } elsif ($_xml_module->is_element($child) or
Condition0%0/3
$XML::XSH2::Functions::_xml_module->is_element($child) or $XML::XSH2::Functions::_xml_module->is_pi($child)
l!l&&r!l&&!r
000
MC/DC0%0/2
$XML::XSH2::Functions::_xml_module->is_element($child) or $XML::XSH2::Functions::_xml_module->is_pi($child)
$XML::XSH2::Functions::_xml_module->is_element($child) $XML::XSH2::Functions::_xml_module->is_pi($child)
Truth table0%0/3
A or B
A= $XML::XSH2::Functions::_xml_module->is_element($child)B= $XML::XSH2::Functions::_xml_module->is_pi($child)
ABresult
1X1
011
000
4610 $_xml_module->is_pi($child)) {
4611 0 last;
4612 }
4613 0 $child=$child->nextSibling();
4614 }
4615 # traverse children (upwards), skip comments, strip text nodes
4616 # until first element or PI or text node containing a non-ws
4617 # character
4618 0 my $child=$node->lastChild();
4619 0 ▶ while ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 0 0 0
4620 0 ▶ if ($_xml_module->is_text($child) or
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_text($child) or $XML::XSH2::Functions::_xml_module->is_cdata_section($child)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_element($child) or $XML::XSH2::Functions::_xml_module->is_pi($child)) { } 0 0 0
Condition0%0/3
$XML::XSH2::Functions::_xml_module->is_text($child) or $XML::XSH2::Functions::_xml_module->is_cdata_section($child)
l!l&&r!l&&!r
000
MC/DC0%0/2
$XML::XSH2::Functions::_xml_module->is_text($child) or $XML::XSH2::Functions::_xml_module->is_cdata_section($child)
$XML::XSH2::Functions::_xml_module->is_text($child) $XML::XSH2::Functions::_xml_module->is_cdata_section($child)
Truth table0%0/3
A or B
A= $XML::XSH2::Functions::_xml_module->is_text($child)B= $XML::XSH2::Functions::_xml_module->is_cdata_section($child)
ABresult
1X1
011
000
4621 $_xml_module->is_cdata_section($child)) {
4622 0 my $data=_trim_ws($child->getData());
4623 0 ▶ if ($data ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne '') { } 0 0 0
4624 0 ▶ $data = "" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
4625 0 $child->setData($data);
4626 0 last;
4627 } else {
4628 0 $child->unbindNode();
4629 }
4630 ▶ } elsif ($_xml_module->is_element($child) or
Condition0%0/3
$XML::XSH2::Functions::_xml_module->is_element($child) or $XML::XSH2::Functions::_xml_module->is_pi($child)
l!l&&r!l&&!r
000
MC/DC0%0/2
$XML::XSH2::Functions::_xml_module->is_element($child) or $XML::XSH2::Functions::_xml_module->is_pi($child)
$XML::XSH2::Functions::_xml_module->is_element($child) $XML::XSH2::Functions::_xml_module->is_pi($child)
Truth table0%0/3
A or B
A= $XML::XSH2::Functions::_xml_module->is_element($child)B= $XML::XSH2::Functions::_xml_module->is_pi($child)
ABresult
1X1
011
000
4631 $_xml_module->is_pi($child)) {
4632 0 last;
4633 }
4634 0 $child=$child->previousSibling();
4635 }
4636 }
4637 }
4638 0 return 1;
4639 }
4640  
4641 # fetch document's DTD
4642 1 ▶sub get_dtd {
Subroutine get_dtd: called CC=1 SCAR=0.0
Pod undocumented
4643 1 my ($doc)=@_;
4644 1 my $dtd;
4645 1 $dtd=$_xml_module->get_dtd($doc,$QUIET);
4646  
4647 1 return $dtd;
4648 }
4649  
4650 # check document validity
4651 2 ▶sub validate_doc {
Subroutine validate_doc: called CC=43 SCAR=68.7
Pod undocumented
4652 2 my ($opts,$exp)=@_;
4653 2 my $doc = _ev_doc($exp);
4654 2 $opts = _ev_opts($opts);
4655 2 ▶ if ($opts->{dtd}+$opts->{schema}+$opts->{relaxng}>1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"dtd"} + $opts->{"schema"} + $opts->{"relaxng"} > 1) 0 2 2
4656 0 die "You can only specify one validation schema at a time\n";
4657 }
4658 2 ▶ if (grep(exists($opts->{$_}), qw(file doc string))>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(exists $opts->{$_}, ("file", "doc", "string")) > 1) 0 2 2
4659 0 die "You can only specify one of --file, --doc, --string at a time\n";
4660 }
4661 2 ▶ $opts->{dtd} = 1 unless $opts->{schema} or $opts->{relaxng};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"schema"} or $opts->{"relaxng"} 2 0 2
Condition33%1/3
$opts->{"schema"} or $opts->{"relaxng"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$opts->{"schema"} or $opts->{"relaxng"}
$opts->{"schema"} $opts->{"relaxng"}
Truth table33%1/3
A or B
A= $opts->{"schema"}B= $opts->{"relaxng"}
ABresult
1X1
011
000
4662 2 ▶ if (exists($opts->{public}) ne "" and not $opts->{dtd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"public"} ne "" and not $opts->{"dtd"}) 0 2 2
Condition33%1/3
exists $opts->{"public"} ne "" and not $opts->{"dtd"}
!ll&&!rl&&r
200
MC/DC0%0/2
exists $opts->{"public"} ne "" and not $opts->{"dtd"}
exists $opts->{"public"} ne "" not $opts->{"dtd"}
Truth table33%1/3
A and not(B)
A= exists $opts->{"public"} ne ""B= not $opts->{"dtd"}
ABresult
0X0
100
111
4663 0 die "--public ID can only be used for DTD validation (--dtd)\n";
4664 }
4665 2 ▶ $opts->{file} = _tilde_expand($opts->{file}) if exists($opts->{file});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"file"} 0 2 2
4666 2 my $ret = 0;
4667 2 ▶ if ($doc->can('is_valid')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->can('is_valid')) { } 2 0 2
4668 2 ▶ if (!$opts->{dtd} or exists($opts->{file}) or exists($opts->{string}) or
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{'dtd'} or exists $opts->{'file'} or exists $opts->{'string'} or exists $opts->{'doc'} or exists $opts->{'public'}) { } 0 2 2
Condition33%4/12
not $opts->{"dtd"} or exists $opts->{"file"} or exists $opts->{"string"} or exists $opts->{"doc"} or exists $opts->{"public"}
l!l&&r!l&&!r
002
not $opts->{"dtd"} or exists $opts->{"file"} or exists $opts->{"string"} or exists $opts->{"doc"}
l!l&&r!l&&!r
002
not $opts->{"dtd"} or exists $opts->{"file"} or exists $opts->{"string"}
l!l&&r!l&&!r
002
not $opts->{"dtd"} or exists $opts->{"file"}
l!l&&r!l&&!r
002
MC/DC0%0/5
not $opts->{"dtd"} or exists $opts->{"file"} or exists $opts->{"string"} or exists $opts->{"doc"} or exists $opts->{"public"}
not $opts->{"dtd"} exists $opts->{"file"} exists $opts->{"string"} exists $opts->{"doc"} exists $opts->{"public"}
Truth table16%1/6
not(A) or B or C or D or E
A= not $opts->{"dtd"}B= exists $opts->{"file"}C= exists $opts->{"string"}D= exists $opts->{"doc"}E= exists $opts->{"public"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
4669 exists($opts->{doc}) or exists($opts->{public})) {
4670 0 ▶ if ($opts->{dtd}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opts->{'dtd'}) { } 0 0 0
elsif ($opts->{'relaxng'}) { } 0 0 0
elsif ($opts->{'schema'}) { } 0 0 0
4671 0 my $dtd;
4672 0 ▶ eval { XML::LibXML::Dtd->can('new') } ||
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "XML::LibXML::Dtd"->can("new") } } or die "DTD validation not supported by your version of XML::LibXML\n" 0 0 0
4673 die "DTD validation not supported by your version of XML::LibXML\n";
4674 0 ▶ if (exists($opts->{file}) or exists($opts->{public})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts->{'file'} or exists $opts->{'public'}) { } 0 0 0
elsif (exists $opts->{'string'}) { } 0 0 0
Condition0%0/3
exists $opts->{"file"} or exists $opts->{"public"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $opts->{"file"} or exists $opts->{"public"}
exists $opts->{"file"} exists $opts->{"public"}
Truth table0%0/3
A or B
A= exists $opts->{"file"}B= exists $opts->{"public"}
ABresult
1X1
011
000
4675 0 $dtd=XML::LibXML::Dtd->new($opts->{public},$opts->{file});
4676 } elsif (exists($opts->{string})) {
4677 0 $dtd=XML::LibXML::Dtd->parse_string($opts->{string});
4678 } else {
4679 0 die "Can't use --doc with DTD validation\n";
4680 }
4681 0 ▶ if ($opts->{yesno}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'yesno'}) { } 0 0 0
4682 0 $ret = $doc->is_valid($dtd);
4683 0 ▶ out(($ret ? "yes\n" : "no\n"));
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 0 0 0
4684 } else {
4685 0 $doc->validate($dtd);
4686 0 $ret = 1;
4687 }
4688 } elsif ($opts->{relaxng}) {
4689 0 ▶ eval { XML::LibXML::RelaxNG->can('new') } ||
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "XML::LibXML::RelaxNG"->can("new") } } or die "RelaxNG validation not supported by your version of XML::LibXML\n" 0 0 0
4690 die "RelaxNG validation not supported by your version of XML::LibXML\n";
4691 0 my $rng;
4692 0 ▶ if (exists($opts->{file})) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $opts->{'file'}) { } 0 0 0
elsif (exists $opts->{'string'}) { } 0 0 0
elsif (exists $opts->{'doc'}) { } 0 0 0
4693 0 $rng=XML::LibXML::RelaxNG->new(location => $opts->{file});
4694 } elsif (exists($opts->{string})) {
4695 0 $rng=XML::LibXML::RelaxNG->new(string => $opts->{string});
4696 } elsif (exists($opts->{doc})) {
4697 0 my $rngdoc=_doc($opts->{doc});
4698 0 ▶ unless (ref($rngdoc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $rngdoc) 0 0 0
4699 0 die "--doc argument doesn't evaluate to a document!\n";
4700 }
4701 0 $rng=XML::LibXML::RelaxNG->new(DOM => $rngdoc);
4702 } else {
4703 0 die "No RelaxNG schema specified\n";
4704 }
4705 0 eval { $rng->validate($doc) };
4706 0 ▶ $ret = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
4707 0 ▶ if ($opts->{yesno}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'yesno'}) { } 0 0 0
4708 0 ▶ out($ret ? "yes\n" : "no\n");
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 0 0 0
4709 } else {
4710 0 ▶ die "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
4711 }
4712 } elsif ($opts->{schema}) {
4713 0 ▶ eval { XML::LibXML::Schema->can('new') } ||
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "XML::LibXML::Schema"->can("new") } } or die "Schema validation not supported by your version of XML::LibXML\n" 0 0 0
4714 die "Schema validation not supported by your version of XML::LibXML\n";
4715 0 my $xsd;
4716 0 ▶ if (exists($opts->{file})) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $opts->{'file'}) { } 0 0 0
elsif (exists $opts->{'string'}) { } 0 0 0
elsif ($opts->{'doc'}) { } 0 0 0
4717 0 $xsd=XML::LibXML::Schema->new(location => $opts->{file});
4718 } elsif (exists($opts->{string})) {
4719 0 $xsd=XML::LibXML::Schema->new(string => $opts->{string});
4720 } elsif ($opts->{doc}) {
4721 0 my $xsddoc=_doc($opts->{doc});
4722 0 ▶ unless (ref($xsddoc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $xsddoc) 0 0 0
4723 0 die "--doc argument doesn't evaluate to a document!\n";
4724 }
4725 0 $xsd=XML::LibXML::Schema->new(string => $xsddoc->toString());
4726 } else {
4727 0 die "No XSD schema specified\n";
4728 }
4729 0 eval { $xsd->validate($doc) };
4730 0 ▶ $ret = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
4731 0 ▶ if ($opts->{yesno}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'yesno'}) { } 0 0 0
4732 0 ▶ out($ret ? "yes\n" : "no\n");
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 0 0 0
4733 } else {
4734 0 ▶ die "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
4735 }
4736 }
4737 } else {
4738 2 ▶ if ($opts->{yesno}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'yesno'}) { } 1 1 2
4739 1 $ret = $doc->is_valid();
4740 1 ▶ out(($ret ? "yes\n" : "no\n"));
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 1 0 1
4741 } else {
4742 1 $doc->validate();
4743 1 $ret = 1;
4744 }
4745 }
4746 } else {
4747 0 die("Vaidation not supported by ",ref($doc));
4748 }
4749 2 return $ret;
4750 }
4751  
4752 # process XInclude elements in a document
4753 0 ▶sub process_xinclude {
Subroutine process_xinclude: not called CC=2 SCAR=17.9
Pod undocumented
4754 0 my ($opts, $exp)=@_;
4755 0 my $doc = _ev_doc($exp);
4756 0 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 0 0 0
4757 0 $_xml_module->doc_process_xinclude($_parser,$doc);
4758 }
4759 0 return 1;
4760 }
4761  
4762 # print document's DTD
4763 1 ▶sub list_dtd {
Subroutine list_dtd: called CC=3 SCAR=11.2
Pod undocumented
4764 1 my $opts = shift;
4765 1 my $doc = _ev_doc($_[0]);
4766 1 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 1 0 1
4767 1 my $dtd=get_dtd($doc);
4768 1 ▶ if ($dtd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dtd) 1 0 1
4769 1 out($_xml_module->toStringUTF8($dtd),"\n");
4770 }
4771 }
4772 1 return 1;
4773 }
4774  
4775 # set document's DTD
4776 0 ▶sub set_dtd {
Subroutine set_dtd: not called CC=12 SCAR=50.5
Pod undocumented
4777 0 my $opts = _ev_opts($_[0]);
4778 0 my $doc = _ev_doc($_[1]);
4779  
4780 0 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 0 0 0
4781 0 my $root = $opts->{name};
4782 0 my $public = $opts->{public};
4783 0 my $system = $opts->{system};
4784 0 ▶ if ((defined ($public) or defined ($system)) and !defined($root)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $public || defined $system and not defined $root) 0 0 0
Condition0%0/6
defined $public || defined $system and not defined $root
!ll&&!rl&&r
000
defined $public or defined $system
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $public || defined $system and not defined $root
defined $public defined $system not defined $root
Truth table0%0/5
A or B and not(C)
A= defined $publicB= defined $systemC= not defined $root
ABCresult
00X0
1X00
0100
1X11
0111
4785 0 ▶ if ($doc->getDocumentElement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->getDocumentElement) { } 0 0 0
4786 0 $root = $doc->getDocumentElement->nodeName();
4787 } else {
4788 0 die "No --name not specified and document has no root element\n"
4789 }
4790 }
4791 0 ▶ if ($doc->internalSubset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->internalSubset) 0 0 0
4792 0 $doc->removeInternalSubset();
4793 }
4794 0 ▶ if ($doc->externalSubset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->externalSubset) 0 0 0
4795 0 $doc->removeExternalSubset();
4796 }
4797 0 ▶ return 1 unless (defined $root or defined $public or defined $system);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root or defined $public or defined $system 0 0 0
Condition0%0/6
defined $root or defined $public or defined $system
l!l&&r!l&&!r
000
defined $root or defined $public
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $root or defined $public or defined $system
defined $root defined $public defined $system
Truth table0%0/4
A or B or C
A= defined $rootB= defined $publicC= defined $system
ABCresult
1XX1
01X1
0011
0000
4798 0 ▶ if ($opts->{internal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'internal'}) { } 0 0 0
4799 0 $doc->setInternalSubset($doc->createInternalSubset($root, $public,
4800 $system));
4801 } else {
4802 0 $doc->setInternalSubset($doc->createInternalSubset($root, $public,
4803 $system));
4804 }
4805 }
4806 0 return 1;
4807 }
4808  
4809  
4810 # print document's encoding
4811 0 ▶sub print_enc {
Subroutine print_enc: not called CC=2 SCAR=17.9
Pod undocumented
4812 0 my ($opts,$doc)=@_;
4813 0 my $doc = _ev_doc($doc);
4814 0 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 0 0 0
4815 0 out($_xml_module->doc_encoding($doc),"\n");
4816 }
4817 0 return 1;
4818 }
4819  
4820 0 ▶sub set_doc_enc {
Subroutine set_doc_enc: not called CC=2 SCAR=17.9
Pod undocumented
4821 0 my $opts = shift;
4822 0 my ($encoding,$doc)=(_ev_literal($_[0]),_ev_doc($_[1]));
4823 0 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 0 0 0
4824 0 $_xml_module->set_encoding($doc,$encoding);
4825 }
4826 0 return 1;
4827 }
4828  
4829 0 ▶sub set_doc_standalone {
Subroutine set_doc_standalone: not called CC=3 SCAR=24.8
Pod undocumented
4830 0 my $opts = shift;
4831 0 my ($standalone,$doc)=(_ev_literal($_[0]),_ev_doc($_[1]));
4832 0 ▶ $standalone=1 if $standalone=~/yes/i;
Branch: 1 branch
BranchTrueFalseTotal
if $standalone =~ /yes/i 0 0 0
4833 0 ▶ $standalone=0 if $standalone=~/no/i;
Branch: 1 branch
BranchTrueFalseTotal
if $standalone =~ /no/i 0 0 0
4834 0 $_xml_module->set_standalone($doc,$standalone);
4835 0 return 1;
4836 }
4837  
4838 0 ▶sub doc_info {
Subroutine doc_info: not called CC=2 SCAR=17.9
Pod undocumented
4839 0 my $opts = shift;
4840 0 my $doc = _ev_doc($_[0]);
4841 0 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 0 0 0
4842 # out("type=",$doc->nodeType,"\n");
4843 0 out("version=",$doc->version(),"\n");
4844 0 out("encoding=",$doc->encoding(),"\n");
4845 0 out("standalone=",$doc->standalone(),"\n");
4846 0 out("compression=",$doc->compression(),"\n");
4847 0 out("URI=",$doc->URI(),"\n");
4848 }
4849 }
4850  
4851 # create an identical copy of a document
4852 1 ▶sub clone {
Subroutine clone: called CC=2 SCAR=7.4
Pod undocumented
4853 1 my ($opts,$exp)=@_;
4854 1 my $doc = _ev_doc($exp);
4855 1 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) { } 1 0 1
4856 1 return _clone_xmldoc($doc);
4857 } else {
4858 0 return undef;
4859 }
4860 }
4861  
4862 # test if $nodea is an ancestor of $nodeb
4863 39 ▶sub is_ancestor_or_self {
Subroutine is_ancestor_or_self: called CC=3 SCAR=11.0
Pod undocumented
4864 39 my ($nodea,$nodeb)=@_;
4865 39 ▶ while ($nodeb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodeb) 73 38 111
4866 73 ▶ if ($_xml_module->xml_equal($nodea,$nodeb)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->xml_equal($nodea, $nodeb)) 1 72 73
4867 1 return 1;
4868 }
4869 72 $nodeb=tree_parent_node($nodeb);
4870 }
4871 }
4872  
4873 # remove node and all its surrounding whitespace textual siblings
4874 # from a document; remove all its descendant from all nodelists
4875 # change current element to the nearest ancestor
4876 39 ▶sub remove_node {
Subroutine remove_node: called CC=6 SCAR=18.6
Pod undocumented
4877 39 my ($node,$trim_space)=@_;
4878 39 ▶ if (is_ancestor_or_self($node,xsh_context_node())) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ancestor_or_self($node, xsh_context_node())) 1 38 39
4879 1 _set_context([tree_parent_node($node)]);
4880 }
4881 39 my $doc;
4882 39 $doc=$_xml_module->owner_document($node);
4883 39 ▶ if ($trim_space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trim_space) 8 31 39
4884 8 my $sibling=$node->nextSibling();
4885 8 ▶ if ($sibling and
Branch: 1 branch
BranchTrueFalseTotal
if ($sibling and $XML::XSH2::Functions::_xml_module->is_text($sibling) and $sibling->getData =~ /^\s+$/) 0 8 8
Condition50%3/6
$sibling and $XML::XSH2::Functions::_xml_module->is_text($sibling) and $sibling->getData =~ /^\s+$/
!ll&&!rl&&r
800
$sibling and $XML::XSH2::Functions::_xml_module->is_text($sibling)
!ll&&!rl&&r
620
MC/DC0%0/3
$sibling and $XML::XSH2::Functions::_xml_module->is_text($sibling) and $sibling->getData =~ /^\s+$/
$sibling $XML::XSH2::Functions::_xml_module->is_text($sibling) $sibling->getData =~ /^\s+$/
Truth table50%2/4
A and B and C
A= $siblingB= $XML::XSH2::Functions::_xml_module->is_text($sibling)C= $sibling->getData =~ /^\s+$/
ABCresult
0XX0
10X0
1100
1111
4886 $_xml_module->is_text($sibling) and
4887 $sibling->getData =~ /^\s+$/) {
4888 # remove_node_from_nodelists($sibling,$doc);
4889 0 $_xml_module->remove_node($sibling);
4890 }
4891 }
4892 # remove_node_from_nodelists($node,$doc);
4893 39 $_xml_module->remove_node($node);
4894 }
4895  
4896 # move nodes matching one XPath expression to locations determined by
4897 # other XPath expression
4898 10 ▶sub move {
Subroutine move: called CC=2 SCAR=6.9
Pod undocumented
4899 10 my $exp=$_[1]; #source xpath
4900 10 my $sourcenodes=_ev_nodelist($exp);
4901 10 my $res=copy(@_);
4902 10 foreach my $node (@$sourcenodes) {
4903 30 remove_node($node);
4904 }
4905 10 return $res;
4906 }
4907  
4908 # call a shell command and print out its output
4909 1 ▶sub sh_noev {
Subroutine sh_noev: called CC=1 SCAR=0.0
Pod undocumented
4910 1 system($_[0]);
4911 1 return 1;
4912 }
4913  
4914 1 ▶sub sh {
Subroutine sh: called CC=1 SCAR=0.0
Pod undocumented
4915 1 my $opts = shift;
4916 1 my $cmd=join " ",map { _ev_string($_) } @_;
4917 1 return system(fromUTF8($ENCODING, $cmd));
4918 }
4919  
4920 # print the result of evaluating an XPath expression in scalar context
4921 52 ▶sub print_count {
Subroutine print_count: called CC=2 SCAR=7.0
Pod undocumented
4922 52 my $opts = _ev_opts(shift);
4923 52 my $count=count_xpath(@_);
4924 52 ▶ out("$count\n") unless $opts->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"quiet"} 52 0 52
4925 52 return $count;
4926 }
4927  
4928 229 ▶sub perl_eval_command {
Subroutine perl_eval_command: called CC=1 SCAR=0.0
Pod undocumented
4929 229 shift; # opts
4930 229 &perl_eval;
4931 }
4932  
4933 319 ▶sub perl_eval {
Subroutine perl_eval: called CC=9 SCAR=22.0
Pod undocumented
4934 319 my ($exp,$map,$in_place)=@_;
4935 319 select $OUT;
4936 15 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4937 319 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 6 313 319
elsif (defined $map) { } 25 288 313
4938 6 my @result=eval(lexicalize($exp));
4939 6 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
4940 6 return @result;
4941 } elsif (defined $map) {
4942 25 ▶ if (ref($map)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $map) 0 25 25
4943 0 $map = to_literal($map);
4944 }
4945 25 ▶ if ($in_place) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_place) { } 3 22 25
4946 3 local $_ = $map;
4947 6 ▶ eval(lexicalize($exp));
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4948 3 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
4949 3 return $_;
4950 } else {
4951 # abraka dabra: some magic to make $_ read only
4952 22 local *_ = eval "\\'$map'";
4953 5 ▶ my $result=eval(lexicalize($exp));
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4954 22 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 22 22
4955 22 return $result;
4956 }
4957 } else {
4958 10 ▶ my $result=eval(lexicalize($exp));
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4959 288 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 5 283 288
4960 283 return $result;
4961 }
4962 }
4963  
4964 # evaluate a perl expression
4965 0 ▶sub print_eval {
Subroutine print_eval: not called CC=1 SCAR=6.9
Pod undocumented
4966 0 my ($expr)=@_;
4967 0 perl_eval($expr);
4968 0 return 1;
4969 }
4970  
4971 # change current directory
4972 0 ▶sub cd {
Subroutine cd: not called CC=3 SCAR=24.8
Pod undocumented
4973 0 my $dir = _tilde_expand(_ev_string($_[0]));
4974 0 ▶ unless (chdir $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (chdir $dir) { } 0 0 0
4975 0 print STDERR "Can't change directory to $dir\n";
4976 0 return 0;
4977 } else {
4978 0 ▶ out("$dir\n") unless "$QUIET";
Branch: 1 branch
BranchTrueFalseTotal
unless "$XML::XSH2::Functions::QUIET" 0 0 0
4979 }
4980 0 return 1;
4981 }
4982  
4983 # call methods from a list
4984 1036 ▶sub run_commands {
Subroutine run_commands: called CC=15 SCAR=27.1
Pod undocumented
4985 1036 ▶ return 0 unless ref($_[0]) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 3 1033 1036
4986 1033 my @cmds=@{$_[0]};
4987 1033 my $top_level=$_[1];
4988 1033 my $want_returns=$_[2];
4989 1033 my $trapsignals=$top_level;
4990 1033 my $result=undef;
4991  
4992 1033 my ($cmd,@params);
4993  
4994 # make sure errors throw exceptions
4995 1033 ▶ local $_die_on_err=1 unless ($top_level);
Branch: 1 branch
BranchTrueFalseTotal
unless $top_level 667 366 1033
4996 1033 ▶ local $_want_returns=1 if ($want_returns);
Branch: 1 branch
BranchTrueFalseTotal
if $want_returns 51 982 1033
4997  
4998 1033 store_variables(1);
4999 1033 store_lex_variables(1);
5000 15 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
5001 1033 eval {
5002 1033 ▶ local $SIG{INT}=\&sigint if $trapsignals;
Branch: 1 branch
BranchTrueFalseTotal
if $trapsignals 366 667 1033
5003 1033 ▶ local $SIG{PIPE}=\&sigpipe if $trapsignals;
Branch: 1 branch
BranchTrueFalseTotal
if $trapsignals 366 667 1033
5004 1033 foreach my $run (@cmds) {
5005 1494 ▶ if (ref($run) eq 'ARRAY' or ref($run) eq 'XML::XSH2::Command') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $run eq 'ARRAY' or ref $run eq 'XML::XSH2::Command') { } 1482 12 1494
Condition100%3/3
ref $run eq "ARRAY" or ref $run eq "XML::XSH2::Command"
l!l&&r!l&&!r
62885412
MC/DC100%2/2
ref $run eq "ARRAY" or ref $run eq "XML::XSH2::Command"
ref $run eq "ARRAY" ref $run eq "XML::XSH2::Command"
Truth table100%3/3
A or B
A= ref $run eq "ARRAY"B= ref $run eq "XML::XSH2::Command"
ABresult
1X1
011
000
5006 1482 ($RT_LINE,$RT_COLUMN,$RT_OFFSET,$RT_SCRIPT,$cmd,@params)=@$run;
5007 1482 ▶ if ($cmd eq "test-mode") { $TEST_MODE=1; $result=1; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "test-mode") 1 1481 1482
5008 1481 ▶ if ($cmd eq "run-mode") { $TEST_MODE=0; $result=1; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "run-mode") 1 1480 1481
5009 1480 ▶ next if $TEST_MODE;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::TEST_MODE 1 1479 1480
5010 1479 ▶ $result=$cmd->(@params) if defined($cmd);
Branch: 1 branch
BranchTrueFalseTotal
if defined $cmd 1479 0 1479
5011 } else {
5012 12 $result=0;
5013 }
5014 }
5015 };
5016 1033 my $err = $@;
5017 1033 do {
5018 1033 local $SIG{INT}=\&flagsigint;
5019 1033 restore_lex_variables();
5020 1033 restore_variables();
5021 1033 propagate_flagsigint();
5022 };
5023 1033 ▶ if (!$trapsignals and $err =~ /^SIGINT|^SIGPIPE/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $trapsignals and $err =~ /^SIGINT|^SIGPIPE/) { } 0 1033 1033
Condition66%2/3
not $trapsignals and $err =~ /^SIGINT|^SIGPIPE/
!ll&&!rl&&r
3666670
MC/DC0%0/2
not $trapsignals and $err =~ /^SIGINT|^SIGPIPE/
not $trapsignals $err =~ /^SIGINT|^SIGPIPE/
Truth table66%2/3
not(A) and B
A= not $trapsignalsB= $err =~ /^SIGINT|^SIGPIPE/
ABresult
0X0
100
111
5024 0 die $err
5025 } else {
5026 1033 _check_err($err,1);
5027 }
5028 970 return $result;
5029 }
5030  
5031 208 ▶sub run_string {
Subroutine run_string: called CC=2 SCAR=7.0
Pod undocumented
5032 208 ▶ xsh_rd_parser_init() unless $_xsh;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::XSH2::Functions::_xsh 0 208 208
5033 208 my $pt = $_xsh->startrule($_[0]);
5034 208 post_process_parse_tree($pt);
5035 208 return run_commands($pt,0);
5036 }
5037  
5038 25 ▶sub run_exp {
Subroutine run_exp: called CC=1 SCAR=0.0
Pod undocumented
5039 25 my ($opts,$exp)=@_;
5040 25 local $SCRIPT="<eval() called from $RT_SCRIPT line $RT_COLUMN>";
5041 25 run_string(_ev_literal($exp));
5042 }
5043  
5044 # redirect output and call methods from a list
5045 20 ▶sub pipe_command {
Subroutine pipe_command: called CC=10 SCAR=24.1
Pod undocumented
5046 20 ▶ return 1 if $TEST_MODE;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::TEST_MODE 0 20 20
5047  
5048 20 ▶ local $SIG{PIPE}=sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
5049 20 my ($cmd,$pipe)=@_;
5050  
5051 20 ▶ return 0 unless (ref($cmd) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cmd eq "ARRAY" 0 20 20
5052  
5053 20 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 20 20
5054 0 _warn("Output redirection not supported on Win32 - ignoring pipe!");
5055 0 return run_commands($cmd);
5056 }
5057 20 $pipe = expand($pipe);
5058 20 ▶ if ($pipe eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pipe eq "") 0 20 20
5059 0 die "Error: empty redirection\n";
5060 }
5061 20 my $out=$OUT;
5062 20 ▶ print STDERR "openning pipe $pipe\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 20 20
5063 20 my $pid;
5064 20 eval {
5065 15 ▶ use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5066 {
5067 20 local *O = *$out;
5068 20 my $P;
5069 20 ▶ $pid = open2('>&O',$P,$pipe) || die "cannot open pipe $pipe\n";
Condition50%1/2
open2('>&O', $P, $pipe) || die("cannot open pipe $pipe\n")
l!l
200
MC/DC0%0/1
open2('>&O', $P, $pipe) || die("cannot open pipe $pipe\n")
open2('>&O', $P, $pipe)
Truth table50%1/2
A
A= open2('>&O', $P, $pipe)
Aresult
11
00
5070 20 $OUT=$P;
5071 ## this is an approach to locate a bug in perl
5072 # local *NEWIN;
5073 # $pid = open2('>&O',\*NEWIN,$pipe) || die "cannot open pipe $pipe\n";
5074 # $OUT=\*NEWIN; #$P;
5075 # my $STDOUT=\*STDOUT;
5076 # for ($STDOUT,$OUT,$out) {
5077 # print STDERR "$_ => ".$_->fileno."\n";
5078 # }
5079 ## print $OUT "FOO\n";
5080 20 run_commands($cmd);
5081 }
5082 };
5083 # print STDERR "FILENO:",$OUT->fileno,"\n";
5084 # print STDERR `ls -l /proc/$$/fd/`;
5085 20 my $err=$@;
5086 20 do {
5087 20 local $SIG{INT}=\&flagsigint;
5088 20 ▶ if (UNIVERSAL::can($OUT,'flush')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($XML::XSH2::Functions::OUT, "flush")) 20 0 20
5089 20 flush $OUT;
5090 20 flush $OUT;
5091 }
5092 20 close $OUT;
5093 20 waitpid($pid,0);
5094 20 $OUT=$out;
5095 20 ▶ flush $OUT if UNIVERSAL::can($OUT,'flush');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($XML::XSH2::Functions::OUT, "flush") 20 0 20
5096 20 propagate_flagsigint();
5097 };
5098 20 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 20 20
5099 20 return 1;
5100 }
5101  
5102 # redirect output to a string and call methods from a list
5103 5 ▶sub string_pipe_command {
Subroutine string_pipe_command: called CC=6 SCAR=18.3
Pod undocumented
5104 5 my ($cmd,$name)=@_;
5105 5 ▶ return 0 unless (ref($cmd) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cmd eq "ARRAY" 0 5 5
5106 5 ▶ if ($name ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne "") 5 0 5
5107 5 my $out=$OUT;
5108 5 ▶ print STDERR "Pipe to $name\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Functions::DEBUG 0 5 5
5109 5 require IO::Scalar;
5110 5 $OUT=new IO::Scalar;
5111 5 eval {
5112 5 run_commands($cmd);
5113 };
5114 5 my $err;
5115 5 do {
5116 5 local $SIG{INT}=\&flagsigint;
5117 5 ▶ _assign($name,${$OUT->sref}) unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 5 0 5
5118 5 $OUT=$out;
5119 5 propagate_flagsigint();
5120 };
5121 5 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 5 5
5122 }
5123 5 return 0;
5124 }
5125  
5126  
5127 # call methods as long as given XPath returns positive value
5128 7 ▶sub while_statement {
Subroutine while_statement: called CC=11 SCAR=26.6
Pod undocumented
5129 7 my ($exp,$command)=@_;
5130 7 my $result=1;
5131 7 my $res;
5132 7 ▶ while ($res=_ev_count($exp)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res = _ev_count($exp)) 23 5 28
5133 29 eval {
5134 29 ▶ $result = run_commands($command) && $result;
Condition33%1/3
run_commands($command) && $result
!ll&&!rl&&r
0015
MC/DC0%0/2
run_commands($command) && $result
run_commands($command) $result
Truth table33%1/3
A && B
A= run_commands($command)B= $result
ABresult
0X0
100
111
5135 };
5136 29 ▶ if (ref($@) and UNIVERSAL::isa($@,'XML::XSH2::Internal::LoopTerminatingException')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $@ and UNIVERSAL::isa($@, 'XML::XSH2::Internal::LoopTerminatingException')) { } 14 15 29
elsif ($@) { } 0 15 15
Condition66%2/3
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
!ll&&!rl&&r
15014
MC/DC50%1/2
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
ref $@ UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
Truth table66%2/3
A and B
A= ref $@B= UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
ABresult
0X0
100
111
5137 14 ▶ if ($@->label =~ /^(?:next|last|redo)$/ and $@->[1]>1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@->label =~ /^(?:next|last|redo)$/ and $@->[1] > 1) 0 14 14
Condition33%1/3
$@->label =~ /^(?:next|last|redo)$/ and $@->[1] > 1
!ll&&!rl&&r
0140
MC/DC0%0/2
$@->label =~ /^(?:next|last|redo)$/ and $@->[1] > 1
$@->label =~ /^(?:next|last|redo)$/ $@->[1] > 1
Truth table33%1/3
A and B
A= $@->label =~ /^(?:next|last|redo)$/B= $@->[1] > 1
ABresult
0X0
100
111
5138 0 $@->[1]--;
5139 0 die $@; # propagate to a higher level
5140 }
5141 14 ▶ if ($@->label eq 'next') {
Branch: 3 branches
BranchTrueFalseTotal
if ($@->label eq 'next') { } 6 8 14
elsif ($@->label eq 'last') { } 2 6 8
elsif ($@->label eq 'redo') { } 6 0 6
5142 6 next;
5143 } elsif ($@->label eq 'last') {
5144 2 last;
5145 } elsif ($@->label eq 'redo') {
5146 6 redo;
5147 } else {
5148 0 die $@; # propagate
5149 }
5150 } elsif ($@) {
5151 0 die $@; # propagate
5152 }
5153 }
5154 7 return $result;
5155 }
5156  
5157 5 ▶sub throw_exception {
Subroutine throw_exception: called CC=1 SCAR=0.0
Pod undocumented
5158 5 my $opts = shift;
5159 5 die _ev_literal($_[0])."\n";
5160 }
5161  
5162 6 ▶sub try_catch {
Subroutine try_catch: called CC=10 SCAR=26.2
Pod undocumented
5163 6 my ($try,$catch,$var)=@_;
5164 6 my $result;
5165 6 eval {
5166 6 local $TRAP_SIGPIPE=1;
5167 6 local $SIG{INT}=\&sigint;
5168 6 local $SIG{PIPE}=\&sigpipe;
5169 # local $_die_on_err=1; # make sure errors cause an exception
5170 6 $result = run_commands($try);
5171 };
5172 6 ▶ if (ref($@) and UNIVERSAL::isa($@,'XML::XSH2::Internal::UncatchableException')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $@ and UNIVERSAL::isa($@, 'XML::XSH2::Internal::UncatchableException')) { } 0 6 6
elsif ($@) { } 6 0 6
Condition33%1/3
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::UncatchableException")
!ll&&!rl&&r
600
MC/DC0%0/2
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::UncatchableException")
ref $@ UNIVERSAL::isa($@, "XML::XSH2::Internal::UncatchableException")
Truth table33%1/3
A and B
A= ref $@B= UNIVERSAL::isa($@, "XML::XSH2::Internal::UncatchableException")
ABresult
0X0
100
111
5173 0 die $@; # propagate
5174 } elsif ($@) {
5175 6 my $err=$@;
5176 6 ▶ if ($err =~ /^SIGINT/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /^SIGINT/) { } 0 6 6
5177 0 die $err; # propagate sigint
5178 } else {
5179 6 ▶ chomp($err) unless ref($err);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err 6 0 6
5180 6 ▶ if (ref($var) and @{$var}>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $var and @{$var;} > 1) { } 6 0 6
Condition33%1/3
ref $var and @{$var;} > 1
!ll&&!rl&&r
006
MC/DC0%0/2
ref $var and @{$var;} > 1
ref $var @{$var;} > 1
Truth table33%1/3
A and B
A= ref $varB= @{$var;} > 1
ABresult
0X0
100
111
5181 6 create_block_var(@$var);
5182 6 _assign($var->[0],$err);
5183 6 eval {
5184 6 $result = run_commands($catch);
5185 };
5186 6 $err = $@;
5187 6 do {
5188 6 local $SIG{INT}=\&flagsigint;
5189 6 destroy_block_var($var->[1]);
5190 6 propagate_flagsigint();
5191 };
5192 6 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 6 6
5193 } else {
5194 0 ▶ _assign($var->[0],$@) if ref($var);
Branch: 1 branch
BranchTrueFalseTotal
if ref $var 0 0 0
5195 0 $result = run_commands($catch);
5196 }
5197 }
5198 }
5199 6 return $result;
5200 }
5201  
5202 12 ▶sub loop_next {
Subroutine loop_next: called CC=1 SCAR=0.0
Pod undocumented
5203 12 my $opts = shift;
5204 12 die XML::XSH2::Internal::LoopTerminatingException->new('next',_ev_literal(@_));
5205 }
5206 0 ▶sub loop_prev {
Subroutine loop_prev: not called CC=1 SCAR=6.9
Pod undocumented
5207 0 my $opts = shift;
5208 0 die XML::XSH2::Internal::LoopTerminatingException->new('prev',_ev_literal(@_));
5209 }
5210 12 ▶sub loop_redo {
Subroutine loop_redo: called CC=1 SCAR=0.0
Pod undocumented
5211 12 my $opts = shift;
5212 12 die XML::XSH2::Internal::LoopTerminatingException->new('redo',_ev_literal(@_));
5213 }
5214 4 ▶sub loop_last {
Subroutine loop_last: called CC=1 SCAR=0.0
Pod undocumented
5215 4 my $opts = shift;
5216 4 die XML::XSH2::Internal::LoopTerminatingException->new('last',_ev_literal(@_));
5217 }
5218  
5219 56 ▶sub _save_context {
Subroutine _save_context: called CC=2 SCAR=7.6
5220 56 ▶ return [xsh_context_node(),
Branch: 1 branch
BranchTrueFalseTotal
$XML::XSH2::Functions::_xpc->can('setContextSize') ? : 56 0 56
5221 $_xpc->can('setContextSize') ?
5222 ($_xpc->getContextSize(),$_xpc->getContextPosition()) :
5223 (undef,undef)];
5224 }
5225  
5226 300 ▶sub _set_context {
Subroutine _set_context: called CC=8 SCAR=22.7
5227 300 my ($node,$size,$pos)=@{$_[0]};
5228 300 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 300 0 300
5229 300 $_xpc->setContextNode($node);
5230 300 ▶ if (defined($size) and defined($pos) and $_xpc->can('setContextSize')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and defined $pos and $XML::XSH2::Functions::_xpc->can("setContextSize")) 218 82 300
Condition66%4/6
defined $size and defined $pos and $XML::XSH2::Functions::_xpc->can("setContextSize")
!ll&&!rl&&r
820218
defined $size and defined $pos
!ll&&!rl&&r
820218
MC/DC33%1/3
defined $size and defined $pos and $XML::XSH2::Functions::_xpc->can("setContextSize")
defined $size defined $pos $XML::XSH2::Functions::_xpc->can("setContextSize")
Truth table50%2/4
A and B and C
A= defined $sizeB= defined $posC= $XML::XSH2::Functions::_xpc->can("setContextSize")
ABCresult
0XX0
10X0
1100
1111
5231 218 ▶ die "invalid size $size\n" if ($size < -1);
Branch: 1 branch
BranchTrueFalseTotal
if $size < -1 0 218 218
5232 218 $_xpc->setContextSize($size);
5233 218 ▶ die "invalid position $pos (size is $size)\n" if ($pos < -1 or $pos>$size);
Branch: 1 branch
BranchTrueFalseTotal
if $pos < -1 or $pos > $size 0 218 218
Condition33%1/3
$pos < -1 or $pos > $size
l!l&&r!l&&!r
00218
MC/DC0%0/2
$pos < -1 or $pos > $size
$pos < -1 $pos > $size
Truth table33%1/3
A or B
A= $pos < -1B= $pos > $size
ABresult
1X1
011
000
5234 218 $_xpc->setContextPosition($pos);
5235 }
5236 } else {
5237 0 die "Trying to change current node to an undefined value\n";
5238 }
5239 }
5240  
5241  
5242 # call methods on every node matching an XPath
5243 14 ▶sub foreach_statement {
Subroutine foreach_statement: called CC=16 SCAR=28.5
Pod undocumented
5244 14 my ($exp,$command,$v)=@_;
5245 14 ▶ my ($var,$local) = ref($v) ? @$v : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 1 13 14
5246 14 my $old_context = _save_context();
5247 14 ▶ create_block_var($var,$local) if $var ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $var ne "" 1 13 14
5248 14 eval {
5249 14 ▶ my @ql = ($var ne "") ? _ev_list($exp) : _ev_nodelist($exp);
Branch: 1 branch
BranchTrueFalseTotal
$var ne '' ? : 1 13 14
5250 14 my $pos=1;
5251 14 my $size = @ql;
5252 14 foreach my $node (@ql) {
5253 57 ▶ if ($var ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($var ne '') { } 1 56 57
5254 1 _assign($var,$node);
5255 } else {
5256 56 _set_context([$node,$size,$pos]);
5257 }
5258 57 eval {
5259 57 run_commands($command);
5260 };
5261 57 ▶ if (ref($@) and UNIVERSAL::isa($@,'XML::XSH2::Internal::LoopTerminatingException')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $@ and UNIVERSAL::isa($@, 'XML::XSH2::Internal::LoopTerminatingException')) { } 14 43 57
elsif ($@) { } 0 43 43
Condition66%2/3
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
!ll&&!rl&&r
43014
MC/DC50%1/2
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
ref $@ UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
Truth table66%2/3
A and B
A= ref $@B= UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
ABresult
0X0
100
111
5262 14 ▶ if ($@->label =~ /^(?:next|last|redo)$/ and $@->[1]>1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@->label =~ /^(?:next|last|redo)$/ and $@->[1] > 1) 0 14 14
Condition33%1/3
$@->label =~ /^(?:next|last|redo)$/ and $@->[1] > 1
!ll&&!rl&&r
0140
MC/DC0%0/2
$@->label =~ /^(?:next|last|redo)$/ and $@->[1] > 1
$@->label =~ /^(?:next|last|redo)$/ $@->[1] > 1
Truth table33%1/3
A and B
A= $@->label =~ /^(?:next|last|redo)$/B= $@->[1] > 1
ABresult
0X0
100
111
5263 0 $@->[1]--;
5264 0 die $@; # propagate to a higher level
5265 }
5266 14 ▶ if ($@->label eq 'next') {
Branch: 3 branches
BranchTrueFalseTotal
if ($@->label eq 'next') { } 6 8 14
elsif ($@->label eq 'last') { } 2 6 8
elsif ($@->label eq 'redo') { } 6 0 6
5267 6 $pos++;
5268 6 next;
5269 } elsif ($@->label eq 'last') {
5270 2 last;
5271 } elsif ($@->label eq 'redo') {
5272 6 redo;
5273 } else {
5274 0 die $@; # propagate
5275 }
5276 } elsif ($@) {
5277 0 die $@; # propagate
5278 }
5279 43 $pos++;
5280 }
5281 };
5282 14 my $err = $@;
5283 14 do {
5284 14 local $SIG{INT}=\&flagsigint;
5285 14 _set_context($old_context);
5286 14 ▶ destroy_block_var($local) if ($var ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $var ne "" 1 13 14
5287 14 propagate_flagsigint();
5288 };
5289 14 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 14 14
5290 14 return 1;
5291 }
5292  
5293 # run commands if given XPath holds
5294 4 ▶sub if_statement {
Subroutine if_statement: called CC=4 SCAR=13.9
Pod undocumented
5295 4 my @cases=@_;
5296 4 foreach (@cases) {
5297 5 my ($exp,$command)=@$_;
5298 5 ▶ if (!defined($exp) or _ev_count($exp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $exp or _ev_count($exp)) 2 3 5
Condition100%3/3
not defined $exp or _ev_count($exp)
l!l&&r!l&&!r
113
MC/DC100%2/2
not defined $exp or _ev_count($exp)
not defined $exp _ev_count($exp)
Truth table100%3/3
not(A) or B
A= not defined $expB= _ev_count($exp)
ABresult
1X1
011
000
5299 2 return run_commands($command);
5300 }
5301 }
5302 2 return 1;
5303 }
5304  
5305 # run commands unless given XPath holds
5306 255 ▶sub unless_statement {
Subroutine unless_statement: called CC=3 SCAR=11.0
Pod undocumented
5307 255 my ($exp,$command,$else)=@_;
5308 255 ▶ unless (_ev_count($exp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ev_count($exp)) { } 230 25 255
5309 25 return run_commands($command);
5310 } else {
5311 230 ▶ return ref($else) ? run_commands($else->[1]) : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $else ? : 3 227 230
5312 }
5313 }
5314  
5315 1 ▶sub _clone_xmldoc {
Subroutine _clone_xmldoc: called CC=2 SCAR=7.6
5316 1 my ($doc)=@_;
5317 1 ▶ if (XML::LibXML::Document->can('cloneNode') ==
Branch: 1 branch
BranchTrueFalseTotal
if ('XML::LibXML::Document'->can('cloneNode') == 'XML::LibXML::Node'->can('cloneNode')) { } 0 1 1
5318 XML::LibXML::Node->can('cloneNode')) {
5319 # emulated clone
5320 0 return $_xml_module->new_parser->parse_string($doc->toString());
5321 } else {
5322  
5323 # native clone (if my patch ever gets into LibXML)
5324 1 return $doc->cloneNode(1);
5325 }
5326 }
5327  
5328 0 ▶sub xslt_compile {
Subroutine xslt_compile: not called CC=6 SCAR=37.4
Pod undocumented
5329 0 my ($as_doc,$stylefile,$want_doc)=@_;
5330 0 my $styledoc;
5331 0 ▶ if ($as_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as_doc) { } 0 0 0
5332 0 $styledoc = _ev_doc($stylefile);
5333 0 ▶ die "No XSL document: $stylefile\n" unless $styledoc;
Branch: 1 branch
BranchTrueFalseTotal
unless $styledoc 0 0 0
5334 } else {
5335 0 $stylefile = _tilde_expand(_ev_string($stylefile));
5336 0 ▶ if ((-f $stylefile) or ($stylefile=~/^[a-z]+:/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $stylefile or $stylefile =~ /^[a-z]+:/) { } 0 0 0
Condition0%0/3
-f $stylefile or $stylefile =~ /^[a-z]+:/
l!l&&r!l&&!r
000
MC/DC0%0/2
-f $stylefile or $stylefile =~ /^[a-z]+:/
-f $stylefile $stylefile =~ /^[a-z]+:/
Truth table0%0/3
A or B
A= -f $stylefileB= $stylefile =~ /^[a-z]+:/
ABresult
1X1
011
000
5337 0 $styledoc = XML::LibXML->new()->parse_file($stylefile);
5338 } else {
5339 0 die "File not exists '$stylefile'\n";
5340 }
5341 }
5342 0 require XML::LibXSLT;
5343 0 my $xsltparser=XML::LibXSLT->new();
5344 0 my $st = $xsltparser->parse_stylesheet($styledoc);
5345 0 ▶ if ($want_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_doc) { } 0 0 0
5346 0 return ($st, $styledoc);
5347 } else {
5348 0 return $st;
5349 }
5350 }
5351  
5352 # transform a document with an XSLT stylesheet
5353 # and create a new document from the result
5354 0 ▶sub xslt {
Subroutine xslt: not called CC=25 SCAR=64.8
Pod undocumented
5355 0 my ($opts,$stylefile)=(shift,shift);
5356 0 my $opts = _ev_opts($opts);
5357  
5358 0 my $st;
5359 0 ▶ if ($opts->{compile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"compile"}) 0 0 0
5360 0 ▶ if (@_ or $opts->{'precompiled'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ or $opts->{'precompiled'}) { } 0 0 0
Condition0%0/3
@_ or $opts->{"precompiled"}
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ or $opts->{"precompiled"}
@_ $opts->{"precompiled"}
Truth table0%0/3
A or B
A= @_B= $opts->{"precompiled"}
ABresult
1X1
011
000
5361 0 _warn("Document argument or --precompiled flag given. Ignoring --compile flag!");
5362 } else {
5363 0 return xslt_compile($opts->{'doc'},$stylefile);
5364 }
5365 }
5366  
5367 0 ▶ if ($opts->{'precompiled'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'precompiled'}) { } 0 0 0
5368 0 ▶ if ($stylefile =~ /^{/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stylefile =~ /^{/) { } 0 0 0
elsif ($stylefile =~ /^\$/) { } 0 0 0
5369 0 $st = _ev($stylefile);
5370 } elsif ($stylefile =~ /^\$/) {
5371 0 $st = var_value($stylefile);
5372 } else {
5373 0 die "Pre-compiled XSLT stylesheet can't be given only as perl expression or a variable: $stylefile\n";
5374 }
5375 0 ▶ unless (ref($st) and UNIVERSAL::isa($st,'XML::LibXSLT::Stylesheet') or
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $st and UNIVERSAL::isa($st, "XML::LibXSLT::Stylesheet") or UNIVERSAL::isa($st, "XML::LibXSLT::StylesheetWrapper")) 0 0 0
Condition0%0/6
ref $st and UNIVERSAL::isa($st, "XML::LibXSLT::Stylesheet") or UNIVERSAL::isa($st, "XML::LibXSLT::StylesheetWrapper")
l!l&&r!l&&!r
000
ref $st and UNIVERSAL::isa($st, "XML::LibXSLT::Stylesheet")
!ll&&!rl&&r
000
MC/DC0%0/3
ref $st and UNIVERSAL::isa($st, "XML::LibXSLT::Stylesheet") or UNIVERSAL::isa($st, "XML::LibXSLT::StylesheetWrapper")
ref $st UNIVERSAL::isa($st, "XML::LibXSLT::Stylesheet") UNIVERSAL::isa($st, "XML::LibXSLT::StylesheetWrapper")
Truth table0%0/5
A and B or C
A= ref $stB= UNIVERSAL::isa($st, "XML::LibXSLT::Stylesheet")C= UNIVERSAL::isa($st, "XML::LibXSLT::StylesheetWrapper")
ABCresult
11X1
0X11
1011
0X00
1000
5376 UNIVERSAL::isa($st,'XML::LibXSLT::StylesheetWrapper')) {
5377 0 die "Pre-compiled XSLT stylesheet doesn't appear to be a XML::LibXSLT::Stylesheet object: $stylefile\n";
5378 }
5379 } else {
5380 0 print STDERR "compiling\n";
5381 0 ($st,my $styledoc) = xslt_compile($opts->{doc},$stylefile,1);
5382 0 ▶ die "No XSLT document: $stylefile\n" unless $st;
Branch: 1 branch
BranchTrueFalseTotal
unless $st 0 0 0
5383 0 $stylefile = $styledoc->URI;
5384 }
5385  
5386 0 my $source;
5387 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
5388 0 $source = shift;
5389 } else {
5390 0 $source = '.';
5391 }
5392  
5393 0 my $doc = _ev_doc($source);
5394  
5395 0 my @params = expand(@_);
5396 0 ▶ print STDERR "running xslt on $doc stylesheet $stylefile params @params\n" if "$DEBUG";
Branch: 1 branch
BranchTrueFalseTotal
if "$XML::XSH2::Functions::DEBUG" 0 0 0
5397 0 ▶ die "No document to process with XSLT: $source\n" unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 0 0
5398 0 my %params;
5399 0 foreach my $p (@params) {
5400 0 ▶ $p=$1 while $p=~/^\s*\((.*)\)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
$p =~ /^\s*\((.*)\)\s*$/ and do { $p = $1 } 0 0 0
5401 0 ▶ if ($p=~/^\s*(\S+?)\s*=\s*(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /^\s*(\S+?)\s*=\s*(.*?)\s*$/) { } 0 0 0
5402 0 $params{$1}=$2;
5403 } else {
5404 0 die("Malformed XSLT parameter $p");
5405 }
5406 }
5407 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::DEBUG) 0 0 0
5408 0 print STDERR map { "$_ -> $params{$_} " } keys %params;
5409 0 print STDERR "\n";
5410 }
5411  
5412 0 ▶ my $rl = $opts->{'string'} ? undef : _prepare_result_nl();
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'string'} ? : 0 0 0
5413 0 ▶ if ($st) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st) { } 0 0 0
5414 0 $stylefile=~s/\..*$//;
5415 0 my $result = eval {
5416 0 $st->transform(_clone_xmldoc($doc),%params);
5417 };
5418 0 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 0 0 0
5419 0 ▶ _warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
5420 } else {
5421 0 ▶ die $@."\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
5422 }
5423 0 ▶ if ($opts->{'string'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'string'}) { } 0 0 0
5424 0 return $st->output_string($result);
5425 } else {
5426 0 set_doc_URI($result,
5427 _base_filename($stylefile).
5428 "_transformed_".
5429 _base_filename($result->URI()));
5430 0 ▶ push @$rl,$result if defined $rl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rl 0 0 0
5431 }
5432 } else {
5433 0 die "Failed to parse stylesheet '$stylefile'\n";
5434 }
5435 0 return $rl;
5436 }
5437  
5438 # perform xupdate processing over a document
5439 0 ▶sub xupdate {
Subroutine xupdate: not called CC=4 SCAR=30.0
Pod undocumented
5440 0 my ($opts,$xupdate_doc,$doc)=map { _ev_doc($_) } @_;
5441 0 ▶ if ($xupdate_doc and $doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xupdate_doc and $doc) { } 0 0 0
Condition0%0/3
$xupdate_doc and $doc
!ll&&!rl&&r
000
MC/DC0%0/2
$xupdate_doc and $doc
$xupdate_doc $doc
Truth table0%0/3
A and B
A= $xupdate_docB= $doc
ABresult
0X0
100
111
5442 0 require XML::XUpdate::LibXML;
5443 0 require XML::Normalize::LibXML;
5444 0 my $xupdate = XML::XUpdate::LibXML->new();
5445 0 $XML::XUpdate::LibXML::debug=1;
5446 0 $xupdate->process($doc->getDocumentElement(),$xupdate_doc);
5447 } else {
5448 0 ▶ if ($xupdate_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xupdate_doc) { } 0 0 0
5449 0 die "Expression '$_[0]' returns empty nodeset\n";
5450 } else {
5451 0 die "Expression '$_[1]' returns empty nodeset\n";
5452 }
5453 0 return 0;
5454 }
5455 }
5456  
5457 0 ▶sub call_return {
Subroutine call_return: not called CC=1 SCAR=6.9
Pod undocumented
5458 0 my $opts = shift;
5459 0 die XML::XSH2::Internal::SubTerminatingException->new('return',_ev($_[0]));
5460 }
5461  
5462 86 ▶sub call_command {
Subroutine call_command: called CC=1 SCAR=0.0
Pod undocumented
5463 86 my ($opts,$exp,@args)=@_;
5464 86 my $name = _ev_string($exp);
5465 86 call($opts,1,$name, @args);
5466 }
5467  
5468 # call a named set of commands
5469 254 ▶sub call {
Subroutine call: called CC=10 SCAR=25.2
Pod undocumented
5470 254 my ($opts,$eval_args, $name, @args)=@_;
5471 254 my $def = $_defs{$name};
5472 254 ▶ if (defined $def) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def) { } 254 0 254
5473 254 my @vars = @$def[2..$#$def];
5474 254 ▶ if (@vars < @args) {
Branch: 2 branches
BranchTrueFalseTotal
if (@vars < @args) { } 0 254 254
elsif (@vars > @args) { } 0 254 254
5475 0 _err("too many arguments [".join(";\n",@args)."] for subroutine '$name @vars'");
5476 } elsif (@vars > @args) {
5477 0 _err("too few arguments for subroutine '$name @vars'");
5478 }
5479 254 my $result;
5480 my %vars;
5481 254 foreach (@args) {
5482 386 ▶ $vars{ shift(@vars) }=$eval_args ? _ev($_) : $_;
Branch: 1 branch
BranchTrueFalseTotal
$eval_args ? : 386 0 386
5483 }
5484 254 my $prev_lex_context = $lexical_variables;
5485 254 $lexical_variables = $def->[1];
5486 254 store_lex_variables(1,keys(%vars));
5487 254 eval {
5488 254 foreach (keys(%vars)) {
5489 386 _assign($_,$vars{$_});
5490 }
5491 254 $result = run_commands($def->[0]);
5492 };
5493 254 my $err = $@;
5494 254 do {
5495 254 local $SIG{INT}=\&flagsigint;
5496 254 restore_lex_variables();
5497 254 $lexical_variables=$prev_lex_context;
5498 254 propagate_flagsigint();
5499 };
5500 254 ▶ if (ref($err) and UNIVERSAL::isa($err,'XML::XSH2::Internal::SubTerminatingException')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err and UNIVERSAL::isa($err, "XML::XSH2::Internal::SubTerminatingException")) 0 254 254
Condition33%1/3
ref $err and UNIVERSAL::isa($err, "XML::XSH2::Internal::SubTerminatingException")
!ll&&!rl&&r
25400
MC/DC0%0/2
ref $err and UNIVERSAL::isa($err, "XML::XSH2::Internal::SubTerminatingException")
ref $err UNIVERSAL::isa($err, "XML::XSH2::Internal::SubTerminatingException")
Truth table33%1/3
A and B
A= ref $errB= UNIVERSAL::isa($err, "XML::XSH2::Internal::SubTerminatingException")
ABresult
0X0
100
111
5501 0 my $ret = $err->[1];
5502 0 undef $err;
5503 0 return $ret;
5504 }
5505 254 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 7 247 254
5506 247 return $result;
5507 } else {
5508 0 die "ERROR: $name not defined\n";
5509 }
5510 }
5511  
5512 0 ▶sub undefine {
Subroutine undefine: not called CC=3 SCAR=20.8
Pod undocumented
5513 0 my ($name)=@_;
5514 0 ▶ if ($name =~ /^\s*\$(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\s*\$(.*)$/) { } 0 0 0
5515 0 _undef($name);
5516 0 my $lex = lex_var($1);
5517 0 ▶ if ($lex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lex) { } 0 0 0
5518 0 undef $$lex;
5519 } else {
5520 15 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
5521 0 undef ${"XML::XSH2::Map::".$1};
5522 }
5523 } else {
5524 0 delete $_defs{$name};
5525 }
5526 0 return 1;
5527 }
5528  
5529 # define a named set of commands
5530 10 ▶sub def {
Subroutine def: called CC=1 SCAR=0.0
Pod undocumented
5531 10 my ($name,$command,$args)=@_;
5532 10 $_defs{$name} = [ $command, [ @$lexical_variables ], @$args ];
5533 10 return 1;
5534 }
5535  
5536 # return a list of all definined subroutines
5537 0 ▶sub defs {
Subroutine defs: not called CC=1 SCAR=6.9
Pod undocumented
5538 0 return sort keys %_defs;
5539 }
5540  
5541 # list all defined subroutines
5542 1 ▶sub list_defs {
Subroutine list_defs: called CC=2 SCAR=6.9
Pod undocumented
5543 1 my $opts = shift;
5544 1 foreach (sort keys (%_defs)) {
5545 1 out(join(" ",$_,@{ $_defs{$_} }[2..$#{ $_defs{$_} }] ),"\n" );
5546 }
5547 1 return 1;
5548 }
5549  
5550 # load a file
5551 0 ▶sub load {
Subroutine load: not called CC=6 SCAR=37.4
Pod undocumented
5552 0 my ($file,$enc)=@_;
5553 0 my $l;
5554 0 ▶ print STDERR "loading file $file\n" unless "$QUIET";
Branch: 1 branch
BranchTrueFalseTotal
unless "$XML::XSH2::Functions::QUIET" 0 0 0
5555 0 ▶ $enc ||= $QUERY_ENCODING;
Condition0%0/3
$enc ||= $XML::XSH2::Functions::QUERY_ENCODING
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc ||= $XML::XSH2::Functions::QUERY_ENCODING
$enc $XML::XSH2::Functions::QUERY_ENCODING
Truth table0%0/3
A ||= B
A= $encB= $XML::XSH2::Functions::QUERY_ENCODING
ABresult
1X1
011
000
5556 0 ▶ if (-f $file and open my $f,"$file") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file and open my $f, "$file") { } 0 0 0
Condition0%0/3
-f $file and open my $f, "$file"
!ll&&!rl&&r
000
MC/DC0%0/2
-f $file and open my $f, "$file"
-f $file open my $f, "$file"
Truth table0%0/3
A and B
A= -f $fileB= open my $f, "$file"
ABresult
0X0
100
111
5557 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
5558 0 binmode $f,":encoding($enc)";
5559 }
5560 0 return join "",<$f>;
5561 } else {
5562 0 die "ERROR: couldn't open input file $file\n";
5563 }
5564 }
5565  
5566 # call XSH to evaluate commands from a given file
5567 0 ▶sub include {
Subroutine include: not called CC=3 SCAR=24.8
Pod undocumented
5568 0 my ($opts,$f,$conditionally)=@_;
5569 0 $f=_tilde_expand(_ev_string($f));
5570 # File should be relative to the current script URI.
5571 0 $f = XML::XSH2::Map::resolve_uri(URI::file->new($f),
5572 URI::file->new($SCRIPT))->file;
5573 0 $opts=_ev_opts($opts);
5574 0 ▶ if (!$conditionally || !$_includes{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $conditionally && $XML::XSH2::Functions::_includes{$f}) 0 0 0
Condition0%0/3
$conditionally && $XML::XSH2::Functions::_includes{$f}
!ll&&!rl&&r
000
MC/DC0%0/2
$conditionally && $XML::XSH2::Functions::_includes{$f}
$conditionally $XML::XSH2::Functions::_includes{$f}
Truth table0%0/3
A && B
A= $conditionallyB= $XML::XSH2::Functions::_includes{$f}
ABresult
0X0
100
111
5575 0 $_includes{$f}=1;
5576 0 my $l=load($f,$opts->{encoding});
5577 0 local $SCRIPT = $f;
5578 0 return run($l);
5579 }
5580 }
5581  
5582 # print help
5583  
5584 0 ▶sub apropos {
Subroutine apropos: not called CC=10 SCAR=47.0
Pod undocumented
5585 0 my ($opts,$query)=@_;
5586 0 $query = expand($query);
5587 0 $opts=_ev_opts($opts);
5588 0 ▶ if ($opts->{fulltext}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'fulltext'}) { } 0 0 0
5589 0 foreach my $k (sort keys %XML::XSH2::Help::HELP) {
5590 0 ▶ if ($opts->{regexp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'regexp'}) { } 0 0 0
5591 0 ▶ out("$k\n") if ($XML::XSH2::Help::HELP{$k}->[0]=~/$query/i);
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Help::HELP{$k}[0] =~ /$query/i 0 0 0
5592 } else {
5593 0 ▶ out("$k\n") if ($XML::XSH2::Help::HELP{$k}->[0]=~/\b\Q$query\E\b/i);
Branch: 1 branch
BranchTrueFalseTotal
if $XML::XSH2::Help::HELP{$k}[0] =~ /\b\Q$query\E\b/i 0 0 0
5594 }
5595 }
5596 } else {
5597 0 foreach my $k (sort keys %$XML::XSH2::Help::Apropos) {
5598 0 ▶ if ($opts->{regexp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'regexp'}) { } 0 0 0
5599 0 ▶ out("$k\n") if (($k." - ".$XML::XSH2::Help::Apropos->{$k})=~/$query/i);
Branch: 1 branch
BranchTrueFalseTotal
if ($k . ' - ' . $XML::XSH2::Help::Apropos->{$k}) =~ /$query/i 0 0 0
5600 } else {
5601 0 ▶ out("$k\n") if (($k." - ".$XML::XSH2::Help::Apropos->{$k})=~/\b\Q$query\E\b/i);
Branch: 1 branch
BranchTrueFalseTotal
if ($k . ' - ' . $XML::XSH2::Help::Apropos->{$k}) =~ /\b\Q$query\E\b/i 0 0 0
5602 }
5603 }
5604 }
5605 }
5606  
5607 0 ▶sub help {
Subroutine help: not called CC=5 SCAR=34.0
Pod undocumented
5608 0 my $opts = shift;
5609 0 my ($command)=expand @_;
5610 0 ▶ if ($command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command) { } 0 0 0
5611 0 ▶ if (exists($XML::XSH2::Help::HELP{$command})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $XML::XSH2::Help::HELP{$command}) { } 0 0 0
5612 0 out($XML::XSH2::Help::HELP{$command}->[0]);
5613 } else {
5614 my @possible =
5615 0 grep { index($_,$command)==0 }
5616 keys(%XML::XSH2::Help::HELP);
5617 0 my %h = map { $XML::XSH2::Help::HELP{$_} => $_ } @possible;
5618 0 ▶ if (keys(%h) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %h == 1) { } 0 0 0
elsif (keys %h > 1) { } 0 0 0
5619 0 out($XML::XSH2::Help::HELP{$possible[0]}->[0]);
5620 0 return 1;
5621 } elsif (keys(%h) > 1) {
5622 0 out("No help available on $command\n");
5623 0 out("Did you mean some of ", join(', ',@possible)," ?\n");
5624 } else {
5625 0 out("No help available on $command\n");
5626 0 return 0;
5627 }
5628 }
5629 } else {
5630 0 out($XML::XSH2::Help::HELP);
5631 }
5632 0 return 1;
5633 }
5634  
5635 # load catalog file to the parser
5636 0 ▶sub load_catalog {
Subroutine load_catalog: not called CC=1 SCAR=6.9
Pod undocumented
5637 0 my $opts = shift;
5638 0 $_xml_module->load_catalog($_parser,_tilde_expand(_ev_string($_[0])));
5639 0 return 1;
5640 }
5641  
5642 4 ▶sub stream_process_node {
Subroutine stream_process_node: called CC=10 SCAR=32.8
Pod undocumented
5643 4 my ($node,$command,$input)=@_;
5644 4 my $old_context = _save_context();
5645 4 eval {
5646 4 foreach (1) {
5647 4 _set_context([$node,1,1]);
5648 4 eval {
5649 4 run_commands($command);
5650 };
5651 4 ▶ if (ref($@) and UNIVERSAL::isa($@,'XML::XSH2::Internal::LoopTerminatingException')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $@ and UNIVERSAL::isa($@, 'XML::XSH2::Internal::LoopTerminatingException')) { } 0 4 4
elsif ($@) { } 0 4 4
Condition33%1/3
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
!ll&&!rl&&r
400
MC/DC0%0/2
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
ref $@ UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
Truth table33%1/3
A and B
A= ref $@B= UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
ABresult
0X0
100
111
5652 0 ▶ if ($@->label =~ /^(?:next|redo)$/ and $@->[1]>1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@->label =~ /^(?:next|redo)$/ and $@->[1] > 1) 0 0 0
Condition0%0/3
$@->label =~ /^(?:next|redo)$/ and $@->[1] > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$@->label =~ /^(?:next|redo)$/ and $@->[1] > 1
$@->label =~ /^(?:next|redo)$/ $@->[1] > 1
Truth table0%0/3
A and B
A= $@->label =~ /^(?:next|redo)$/B= $@->[1] > 1
ABresult
0X0
100
111
5653 0 $@->[1]--;
5654 0 die $@; # propagate to a higher level
5655 }
5656 0 ▶ if ($@->label eq 'next') {
Branch: 2 branches
BranchTrueFalseTotal
if ($@->label eq 'next') { } 0 0 0
elsif ($@->label eq 'redo') { } 0 0 0
5657 0 last;
5658 } elsif ($@->label eq 'redo') {
5659 0 redo;
5660 } else {
5661 0 die $@; # propagate
5662 }
5663 } elsif ($@) {
5664 0 die $@; # propagate
5665 }
5666 }
5667 };
5668 4 my $err = $@;
5669 4 do {
5670 4 local $SIG{INT}=\&flagsigint;
5671 4 _set_context($old_context);
5672 4 propagate_flagsigint();
5673 };
5674 4 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 4 4
5675 }
5676  
5677 2 ▶sub stream_process {
Subroutine stream_process: called CC=32 SCAR=50.9
Pod undocumented
5678 2 my ($opts, $process)=@_;
5679 2 $opts = _ev_opts($opts);
5680  
5681 2 require XML::Filter::DOMFilter::LibXML;
5682 2 require XML::LibXML::SAX;
5683 2 require XML::SAX::Writer;
5684  
5685 2 ▶ if (grep {/^input-/} keys %$opts>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^input-/;} keys %$opts > 1) 0 2 2
5686 0 die "Only one --input-xxxx parameter can be specified\n";
5687 }
5688 2 ▶ if (grep {/^output-/} grep { !/^output-encoding/ } keys %$opts>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^output-/;} grep({not /^output-encoding/;} keys %$opts > 1)) 0 2 2
5689 0 die "Only one --output-xxxx parameter can be specified\n";
5690 }
5691 2 ▶ if ($opts->{'no-output'} && grep /^output-/, keys %$opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"no-output"} and grep /^output-/, keys %$opts) 0 2 2
Condition33%1/3
$opts->{"no-output"} and grep /^output-/, keys %$opts
!ll&&!rl&&r
020
MC/DC0%0/2
$opts->{"no-output"} and grep /^output-/, keys %$opts
$opts->{"no-output"} grep /^output-/, keys %$opts
Truth table33%1/3
A and B
A= $opts->{"no-output"}B= grep /^output-/, keys %$opts
ABresult
0X0
100
111
5692 0 die "Can't combine --no-output with --output-xxxx\n";
5693 }
5694  
5695 2 my $out;
5696 my $termout;
5697 2 ▶ $opts->{'input-file'} = _tilde_expand($opts->{'input-file'}) if exists($opts->{'input-file'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"input-file"} 0 2 2
5698 2 ▶ $opts->{'output-file'} = _tilde_expand($opts->{'output-file'}) if exists($opts->{'output-file'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"output-file"} 0 2 2
5699 my $output = $opts->{'output-string'} || $opts->{'output-pipe'} ||
5700 2 ▶ $opts->{'output-file'} || undef;
Condition37%3/8
$opts->{'output-string'} || $opts->{'output-pipe'} || $opts->{'output-file'} || undef
l!l
02
$opts->{"output-string"} or $opts->{"output-pipe"} or $opts->{"output-file"}
l!l&&r!l&&!r
002
$opts->{"output-string"} or $opts->{"output-pipe"}
l!l&&r!l&&!r
002
MC/DC0%0/3
$opts->{'output-string'} || $opts->{'output-pipe'} || $opts->{'output-file'} || undef
$opts->{"output-string"} $opts->{"output-pipe"} $opts->{"output-file"}
Truth table25%1/4
A or B or C
A= $opts->{"output-string"}B= $opts->{"output-pipe"}C= $opts->{"output-file"}
ABCresult
1XX1
01X1
0011
0000
5701 my $input = $opts->{'input-string'} || $opts->{'input-pipe'} ||
5702 2 ▶ $opts->{'input-file'} || '-';
Condition50%4/8
$opts->{'input-string'} || $opts->{'input-pipe'} || $opts->{'input-file'} || '-'
l!l
20
$opts->{"input-string"} or $opts->{"input-pipe"} or $opts->{"input-file"}
l!l&&r!l&&!r
200
$opts->{"input-string"} or $opts->{"input-pipe"}
l!l&&r!l&&!r
110
MC/DC0%0/3
$opts->{'input-string'} || $opts->{'input-pipe'} || $opts->{'input-file'} || '-'
$opts->{"input-string"} $opts->{"input-pipe"} $opts->{"input-file"}
Truth table50%2/4
A or B or C
A= $opts->{"input-string"}B= $opts->{"input-pipe"}C= $opts->{"input-file"}
ABCresult
1XX1
01X1
0011
0000
5703  
5704 2 ▶ if (exists $opts->{'output-file'}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $opts->{'output-file'}) { } 0 2 2
elsif (exists $opts->{'output-pipe'}) { } 0 2 2
elsif (exists $opts->{'output-string'}) { } 0 2 2
5705 0 ▶ open $out,'>'.$output || die "Cannot open output file ".$output."\n";
Condition0%0/2
'>' . $output || die('Cannot open output file ' . $output . "\n")
l!l
00
MC/DC0%0/1
'>' . $output || die('Cannot open output file ' . $output . "\n")
'>' . $output
Truth table0%0/2
A
A= '>' . $output
Aresult
11
00
5706 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
5707 binmode ($out,
5708 ($opts->{'output-encoding'} ?
5709 0 ▶ ":encoding(".$opts->{'output-encoding'}.")" : ":utf8"));
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'output-encoding'} ? : 0 0 0
5710 }
5711 } elsif (exists $opts->{'output-pipe'}) {
5712 0 ▶ open $out,'| '.$output || die "Cannot open pipe to ".$output."\n";
Condition0%0/2
'| ' . $output || die('Cannot open pipe to ' . $output . "\n")
l!l
00
MC/DC0%0/1
'| ' . $output || die('Cannot open pipe to ' . $output . "\n")
'| ' . $output
Truth table0%0/2
A
A= '| ' . $output
Aresult
11
00
5713 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
5714 binmode ($out,
5715 ($opts->{'output-encoding'} ?
5716 0 ▶ ":encoding(".$opts->{'output-encoding'}.")" : ":utf8"));
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'output-encoding'} ? : 0 0 0
5717 }
5718 } elsif (exists $opts->{'output-string'}) {
5719 0 my $output = $opts->{'output-string'};
5720 0 ▶ if ($output =~ /^\$(\$?[a-zA-Z_][a-zA-Z0-9_]*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($output =~ /^\$(\$?[a-zA-Z_][a-zA-Z0-9_]*)$/) { } 0 0 0
elsif (ref($XML::XSH2::Functions::OUT) =~ /Term::ReadLine/) { } 0 0 0
5721 0 $out = _get_var_ref($output);
5722 } elsif (ref($OUT)=~/Term::ReadLine/) {
5723 0 $out = *$OUT;
5724 0 $termout=1;
5725 } else {
5726 0 $out = $OUT;
5727 0 $termout=1;
5728 }
5729 } else {
5730 2 $out = $output;
5731 }
5732 my $parser=XML::LibXML::SAX
5733 ->new( Handler =>
5734 XML::Filter::DOMFilter::LibXML
5735 ->new($opts->{'no-output'} ? ()
5736 : (Handler => XML::SAX::Writer::XML
5737 ->new(
5738 Output => $out,
5739 Writer => 'XML::SAX::Writer::XMLEnc'
5740 )),
5741 XPathContext => $_xpc,
5742 Process => [
5743 map {
5744 2 ▶ $_->[0] => [\&stream_process_node,$_->[1],
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'no-output'} ? : 2 0 2
5745 $input] }
5746 @$process
5747 ]
5748 )
5749 );
5750 2 my $old_context = _save_context();
5751 2 my $error;
5752 2 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { if (exists $opts->{"input-pipe"}) { open my $F, "$input|"; $F or die "Cannot open pipe to ${input}: $!\n"; $parser->parse_file($F); close $F; } elsif (exists $opts->{"input-string"}) { $parser->parse_string($input); } else { $parser->parse_uri($input); } ; if (exists $opts->{"output-pipe"}) { close $out; } ; if ($termout) { out("\n"); } ; 1 } } or $error = $@ 2 0 2
5753 2 ▶ if (exists $opts->{'input-pipe'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts->{'input-pipe'}) { } 1 1 2
elsif (exists $opts->{'input-string'}) { } 1 0 1
5754 1 open my $F,"$input|";
5755 1 ▶ $F || die "Cannot open pipe to $input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$F or die "Cannot open pipe to ${input}: $!\n" 1 0 1
5756 1 $parser->parse_file($F);
5757 1 close $F;
5758 } elsif (exists $opts->{'input-string'}) {
5759 1 $parser->parse_string($input);
5760 } else { #file
5761 0 $parser->parse_uri($input);
5762 }
5763 2 ▶ if (exists $opts->{'output-pipe'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"output-pipe"}) 0 2 2
5764 0 close($out);
5765 }
5766 0 ▶ if ($termout) { out("\n"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($termout) 0 2 2
5767 2 1 } or $error = $@;
5768 2 _set_context($old_context);
5769 2 ▶ die $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 2 2
5770  
5771 2 return 1
5772 }
5773  
5774 0 ▶sub iterate {
Subroutine iterate: not called CC=30 SCAR=68.4
Pod undocumented
5775 0 my ($code,$axis,$nodefilter,$filter)=@_;
5776  
5777 0 $axis =~ s/::$//;
5778 0 $axis=~s/-/_/g;
5779  
5780 0 ▶ $filter =~ s/^\[\s*((?:.|\n)*?)\s*\]$/$1/ if defined $filter;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filter 0 0 0
5781 0 my $test;
5782 0 ▶ if ($nodefilter eq "comment()") {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodefilter eq "comment()") 0 0 0
5783 0 $test = q{ $_xml_module->is_comment($_[0]) }
5784 0 ▶ } if ($nodefilter eq "text()") {
Branch: 4 branches
BranchTrueFalseTotal
if ($nodefilter eq 'text()') { } 0 0 0
elsif ($nodefilter =~ /processing-instruction\((\s*['"]([^'"]+)['"]\s*)?\)$/) { } 0 0 0
elsif ($nodefilter eq 'node()') { } 0 0 0
elsif ($nodefilter =~ /^(?:([^:]+):)?(.+)$/) { } 0 0 0
5785 0 $test = q{ $_xml_module->is_text_or_cdata($_[0]) }
5786 } elsif ($nodefilter =~ /processing-instruction\((\s*['"]([^'"]+)['"]\s*)?\)$/) {
5787 0 $test = q{ $_xml_module->is_pi($_[0]) };
5788 0 ▶ $test .= qq{ && (\$_[0]->nodeName eq '$1') } if $1 ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $1 ne "" 0 0 0
5789 } elsif ($nodefilter eq 'node()') {
5790 0 $test = '1 ';
5791 } elsif ($nodefilter =~ /^(?:([^:]+):)?(.+)$/) {
5792 0 $test = q{ $_xml_module->is_element($_[0]) };
5793 0 ▶ $test .= qq{ && (\$_[0]->getLocalName() eq '$2') } unless ($2 eq '*');
Branch: 1 branch
BranchTrueFalseTotal
unless $2 eq "*" 0 0 0
5794 0 ▶ if ($1 ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 ne "") 0 0 0
5795 0 my $ns = xsh_context_node()->lookupNamespaceURI($1);
5796 0 ▶ die("Unrecognized namespace prefix '$1:'!") if ($ns eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "" 0 0 0
5797 0 $test .= qq{ && (\$_[0]->namespaceURI() eq '$ns') };
5798 }
5799 }
5800  
5801 0 ▶ die("Position index filter not supported for iteration ([$filter])") if $filter =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $filter =~ /^\d+$/ 0 0 0
5802 0 ▶ if ($filter ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter ne "") 0 0 0
5803 0 $filter =~ s/\\/\\\\/g;
5804 0 $filter =~ s/'/\\'/g;
5805 0 $test .= qq{ && \$_xpc->find('$filter',\$_[0]) };
5806 }
5807 0 ▶ $test = "1" if $test eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $test eq "" 0 0 0
5808  
5809 0 my $filter_sub = eval "sub { $test }";
5810 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
5811 0 my $iterator;
5812 0 do {
5813 0 my $start=xsh_context_node();
5814 0 $iterator=XML::XSH2::Iterators->create_iterator($start,$axis,$filter_sub);
5815 };
5816 0 ▶ return 1 unless defined $iterator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iterator 0 0 0
5817  
5818 0 my $old_context=_save_context();
5819  
5820 0 my $count = 1;
5821 0 my $pos = 1;
5822 0 eval {
5823 0 ▶ ITER: while ($iterator->current()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iterator->current) 0 0 0
5824 0 _set_context([$iterator->current(),$count,$pos]);
5825 0 eval {
5826 0 run_commands($code);
5827 };
5828 0 ▶ if (ref($@) and UNIVERSAL::isa($@,'XML::XSH2::Internal::LoopTerminatingException')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $@ and UNIVERSAL::isa($@, 'XML::XSH2::Internal::LoopTerminatingException')) { } 0 0 0
elsif ($@) { } 0 0 0
Condition0%0/3
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $@ and UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
ref $@ UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
Truth table0%0/3
A and B
A= ref $@B= UNIVERSAL::isa($@, "XML::XSH2::Internal::LoopTerminatingException")
ABresult
0X0
100
111
5829 0 ▶ if ($@->label =~ /^(?:next|last|redo|prev)$/ and $@->[1]>1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@->label =~ /^(?:next|last|redo|prev)$/ and $@->[1] > 1) 0 0 0
Condition0%0/3
$@->label =~ /^(?:next|last|redo|prev)$/ and $@->[1] > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$@->label =~ /^(?:next|last|redo|prev)$/ and $@->[1] > 1
$@->label =~ /^(?:next|last|redo|prev)$/ $@->[1] > 1
Truth table0%0/3
A and B
A= $@->label =~ /^(?:next|last|redo|prev)$/B= $@->[1] > 1
ABresult
0X0
100
111
5830 0 $@->[1]--;
5831 0 die $@; # propagate to a higher level
5832 }
5833 0 ▶ if ($@->label eq 'next') {
Branch: 4 branches
BranchTrueFalseTotal
if ($@->label eq 'next') { } 0 0 0
elsif ($@->label eq 'prev') { } 0 0 0
elsif ($@->label eq 'last') { } 0 0 0
elsif ($@->label eq 'redo') { } 0 0 0
5834 0 $count ++; $pos ++;
5835 0 ▶ $iterator->next() || last;
Branch: 1 branch
BranchTrueFalseTotal
$iterator->next or last 0 0 0
5836 0 next;
5837 } elsif ($@->label eq 'prev') {
5838 0 $pos --;
5839 0 ▶ $iterator->prev() || die("No previous node to iterate to!");
Branch: 1 branch
BranchTrueFalseTotal
$iterator->prev or die "No previous node to iterate to!" 0 0 0
5840 0 next;
5841 } elsif ($@->label eq 'last') {
5842 0 last;
5843 } elsif ($@->label eq 'redo') {
5844 0 redo;
5845 } else {
5846 0 die $@; # propagate
5847 }
5848 } elsif ($@) {
5849 0 die $@; # propagate
5850 }
5851 0 $count ++; $pos ++;
5852 0 ▶ $iterator->next() || last;
Branch: 1 branch
BranchTrueFalseTotal
$iterator->next or last 0 0 0
5853 }
5854 };
5855 0 my $err = $@;
5856 0 do {
5857 0 local $SIG{INT}=\&flagsigint;
5858 0 _set_context($old_context);
5859 0 propagate_flagsigint();
5860 };
5861 0 ▶ die $err if $err; # propagate
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
5862 0 return 1;
5863 }
5864  
5865 # quit
5866 0 ▶sub quit {
Subroutine quit: not called CC=2 SCAR=17.9
Pod undocumented
5867 0 my $opts = shift;
5868 0 ▶ if (ref($_on_exit)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $XML::XSH2::Functions::_on_exit) 0 0 0
5869 0 &{$_on_exit->[0]}($_[0],@{$_on_exit}[1..$#$_on_exit]); # run on exit hook
5870 }
5871 0 exit(int($_[0]));
5872 }
5873  
5874 1 ▶sub register_ns {
Subroutine register_ns: called CC=2 SCAR=7.1
Pod undocumented
5875 1 my $opts = shift;
5876 1 my $prefix = _ev_string($_[0]);
5877 1 my $ns = _ev_string($_[1]);
5878  
5879 1 ▶ unless ($prefix=~m{^[-_.[:alpha:]][-_.[:alnum:]]*$}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prefix =~ /^[-_.[:alpha:]][-_.[:alnum:]]*$/) 0 1 1
5880 0 die "Invalid namespace prefix '$prefix'\n";
5881 }
5882 1 $_ns{$prefix}=$ns;
5883 1 $_xpc->registerNs($prefix,$ns);
5884 1 return 1;
5885 }
5886  
5887 0 ▶sub register_xsh_ns {
Subroutine register_xsh_ns: not called CC=1 SCAR=6.9
Pod undocumented
5888 0 my $opts = shift;
5889 0 register_ns($opts,_ev_string($_[0]),$XML::XSH2::xshNS);
5890 }
5891  
5892 0 ▶sub register_xhtml_ns {
Subroutine register_xhtml_ns: not called CC=1 SCAR=6.9
Pod undocumented
5893 0 my $opts = shift;
5894 0 register_ns($opts,_ev_string($_[0]),'http://www.w3.org/1999/xhtml');
5895 }
5896  
5897 0 ▶sub unregister_ns {
Subroutine unregister_ns: not called CC=1 SCAR=6.9
Pod undocumented
5898 0 my ($opts,$exp)=@_;
5899 0 my $prefix = _ev_string($exp);
5900 0 delete $_ns{$prefix};
5901 0 $_xpc->unregisterNs($prefix);
5902 0 return 1;
5903 }
5904  
5905 0 ▶sub get_registered_ns {
Subroutine get_registered_ns: not called CC=1 SCAR=6.9
Pod undocumented
5906 0 return $_ns{$_[0]};
5907 }
5908  
5909 0 ▶sub get_registered_prefix {
Subroutine get_registered_prefix: not called CC=1 SCAR=6.9
Pod undocumented
5910 0 my %r = reverse %_ns;
5911 0 return $r{$_[0]};
5912 }
5913  
5914 0 ▶sub register_func {
Subroutine register_func: not called CC=7 SCAR=40.3
Pod undocumented
5915 0 my ($opts,$name,$code)=@_;
5916 0 $name=_ev_string($name);
5917 0 my $sub;
5918 0 ▶ if ($code =~ /^\s*{/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code =~ /^\s*{/) { } 0 0 0
elsif ($code =~ /^\s*([A-Za-z_][A-Za-z_0-9]*(::[A-Za-z_][A-Za-z_0-9]*)*)\s*$/) { } 0 0 0
5919 0 my $lex = lexicalize("sub $code");
5920 0 $sub = eval($lex);
5921 } elsif ($code =~/^\s*([A-Za-z_][A-Za-z_0-9]*(::[A-Za-z_][A-Za-z_0-9]*)*)\s*$/) {
5922 0 ▶ if ($2 ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 ne '') { } 0 0 0
5923 0 $sub=\&{"$1"};
5924 } else {
5925 0 $sub=\&{"XML::XSH2::Map::$1"};
5926 }
5927 } else {
5928 0 $sub = eval(lexicalize("sub \{ $code \}"));
5929 }
5930 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
5931 0 ▶ if ($name =~ /^([^:]+):(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^([^:]+):(.*)$/) { } 0 0 0
5932 0 ▶ if (exists($_ns{$1})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $XML::XSH2::Functions::_ns{$1}) { } 0 0 0
5933 0 $_func{"$2\n$_ns{$1}"}=$sub;
5934 0 $_xpc->registerFunctionNS($2, $_ns{$1}, $sub);
5935 } else {
5936 0 die "Registration failed: unknown namespace prefix $1!\n";
5937 }
5938 } else {
5939 0 $_func{$name}=$sub;
5940 0 $_xpc->registerFunction($name, $sub);
5941 }
5942 0 return 1;
5943 }
5944  
5945 0 ▶sub unregister_func {
Subroutine unregister_func: not called CC=3 SCAR=24.8
Pod undocumented
5946 0 my ($opts,$name)=@_;
5947  
5948 0 ▶ if ($name =~ /^([^:]+):(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^([^:]+):(.*)$/) { } 0 0 0
5949 0 ▶ if (exists($_ns{$1})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $XML::XSH2::Functions::_ns{$1}) { } 0 0 0
5950 0 delete $_func{"$2\n$_ns{$1}"};
5951 0 $_xpc->unregisterFunctionNS($2, $_ns{$1});
5952 } else {
5953 0 die "Registration failed: unknown namespace prefix $1!\n";
5954 }
5955 } else {
5956 0 delete $_func{$name};
5957 0 $_xpc->unregisterFunction($name);
5958 }
5959 0 return 1;
5960 }
5961  
5962 1 ▶sub node_type {
Subroutine node_type: called CC=12 SCAR=44.7
Pod undocumented
5963 1 my ($node)=@_;
5964 1 ▶ return undef unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 1 1
5965 1 ▶ if ($_xml_module->is_element($node)) {
Branch: 10 branches
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xml_module->is_element($node)) { } 0 1 1
elsif ($XML::XSH2::Functions::_xml_module->is_attribute($node)) { } 1 0 1
elsif ($XML::XSH2::Functions::_xml_module->is_text($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_cdata_section($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_pi($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_entity_reference($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_document($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_document_fragment($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_comment($node)) { } 0 0 0
elsif ($XML::XSH2::Functions::_xml_module->is_namespace($node)) { } 0 0 0
5966 0 return 'element';
5967 } elsif ($_xml_module->is_attribute($node)) {
5968 1 return 'attribute';
5969 } elsif ($_xml_module->is_text($node)) {
5970 0 return 'text';
5971 } elsif ($_xml_module->is_cdata_section($node)) {
5972 0 return 'cdata';
5973 } elsif ($_xml_module->is_pi($node)) {
5974 0 return 'pi';
5975 } elsif ($_xml_module->is_entity_reference($node)) {
5976 0 return 'entity_reference';
5977 } elsif ($_xml_module->is_document($node)) {
5978 0 return 'document';
5979 } elsif ($_xml_module->is_document_fragment($node)) {
5980 0 return 'chunk';
5981 } elsif ($_xml_module->is_comment($node)) {
5982 0 return 'comment';
5983 } elsif ($_xml_module->is_namespace($node)) {
5984 0 return 'namespace';
5985 } else {
5986 0 return 'unknown';
5987 }
5988 }
5989  
5990 #######################################################################
5991 #######################################################################
5992  
5993  
5994 package XML::XSH2::Map;
5995  
5996 14 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5997 15 import XML::XSH2::Functions ':param_vars';
5998
5999  
6000 15 *fromUTF8 = *XML::XSH2::Functions::fromUTF8;
6001 15 *toUTF8 = *XML::XSH2::Functions::toUTF8;
6002 }
6003  
6004 0 ▶sub call {
Subroutine call: not called CC=1 SCAR=6.9
6005 0 XML::XSH2::Functions::call({},0,@_);
6006 }
6007  
6008 19 ▶sub serialize {
Subroutine serialize: called CC=5 SCAR=17.1
6009 19 my $exp=$_[0];
6010 19 my $ql;
6011 19 ▶ if (ref($exp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exp) { } 14 5 19
6012 14 ▶ if (UNIVERSAL::isa($exp,'XML::LibXML::NodeList')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($exp, 'XML::LibXML::NodeList')) { } 14 0 14
elsif (UNIVERSAL::isa($exp, 'XML::LibXML::Node')) { } 0 0 0
6013 14 $ql=$exp;
6014 } elsif (UNIVERSAL::isa($exp,'XML::LibXML::Node')) {
6015 0 $ql=[$exp];
6016 } else {
6017 0 $ql=&XML::XSH2::Functions::_ev_nodelist($exp);
6018 }
6019 } else {
6020 5 $ql=&XML::XSH2::Functions::_ev_nodelist($exp);
6021 }
6022 19 my $result='';
6023 19 foreach (@$ql) {
6024 21 $result.=$_->toString();
6025 }
6026 19 return $result;
6027 }
6028  
6029 0 ▶sub literal {
Subroutine literal: not called CC=3 SCAR=24.8
6030 0 ▶ my $xp=$_[0] || current();
Condition0%0/3
$_[0] || current()
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] || current()
$_[0] current()
Truth table0%0/3
A || B
A= $_[0]B= current()
ABresult
1X1
011
000
6031 0 ▶ return XML::XSH2::Functions::to_literal(ref($xp) ? $xp : XML::XSH2::Functions::_ev($xp));
Branch: 1 branch
BranchTrueFalseTotal
ref $xp ? : 0 0 0
6032 }
6033  
6034 0 ▶sub type {
Subroutine type: not called CC=7 SCAR=40.3
6035 0 my ($xp)=@_;
6036 0 my $ql;
6037 0 ▶ unless (ref($xp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $xp) { } 0 0 0
if (do { ref $xp eq 'ARRAY' or UNIVERSAL::isa($xp, 'XML::LibXML::NodeList') }) { } 0 0 0
6038 0 ▶ $xp='.' if $xp eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $xp eq "" 0 0 0
6039 0 $ql = &XML::XSH2::Functions::_ev_nodelist($xp);
6040 ▶ } elsif (ref($xp) eq 'ARRAY' or
Condition0%0/3
ref $xp eq "ARRAY" or UNIVERSAL::isa($xp, "XML::LibXML::NodeList")
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $xp eq "ARRAY" or UNIVERSAL::isa($xp, "XML::LibXML::NodeList")
ref $xp eq "ARRAY" UNIVERSAL::isa($xp, "XML::LibXML::NodeList")
Truth table0%0/3
A or B
A= ref $xp eq "ARRAY"B= UNIVERSAL::isa($xp, "XML::LibXML::NodeList")
ABresult
1X1
011
000
6041 UNIVERSAL::isa($xp,'XML::LibXML::NodeList')) {
6042 0 $ql = $xp;
6043 } else {
6044 0 $ql = [$xp];
6045 }
6046 0 my @result;
6047 0 foreach (@$ql) {
6048 0 push @result,&XML::XSH2::Functions::node_type($_);
6049 0 ▶ return $result[0] unless (wantarray);
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
6050 }
6051 0 return @result;
6052 }
6053  
6054 0 ▶sub nodelist {
Subroutine nodelist: not called CC=1 SCAR=6.9
6055 return XML::LibXML::NodeList->new(map {
6056 0 XML::XSH2::Functions::cast_value_to_objects($_)
6057 } @_);
6058 }
6059  
6060 0 ▶sub xpath {
Subroutine xpath: not called CC=5 SCAR=34.0
6061 0 my ($exp, $node) = @_;
6062 0 ▶ $node = $node->[0] if (UNIVERSAL::isa($node,'XML::LibXML::NodeList'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($node, "XML::LibXML::NodeList") 0 0 0
6063 0 my $r = $XML::XSH2::Functions::_xpc->find($exp,$node);
6064 # my $r = XML::XSH2::Functions::_ev($_[0]);
6065 0 ▶ if (wantarray and ref($r) and UNIVERSAL::isa($r,'XML::LibXML::NodeList')) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and ref $r and UNIVERSAL::isa($r, 'XML::LibXML::NodeList')) { } 0 0 0
Condition0%0/6
wantarray and ref $r and UNIVERSAL::isa($r, "XML::LibXML::NodeList")
!ll&&!rl&&r
000
wantarray and ref $r
!ll&&!rl&&r
000
MC/DC0%0/3
wantarray and ref $r and UNIVERSAL::isa($r, "XML::LibXML::NodeList")
wantarray ref $r UNIVERSAL::isa($r, "XML::LibXML::NodeList")
Truth table0%0/4
A and B and C
A= wantarrayB= ref $rC= UNIVERSAL::isa($r, "XML::LibXML::NodeList")
ABCresult
0XX0
10X0
1100
1111
6066 0 return @$r;
6067 } else {
6068 0 return $r;
6069 }
6070 }
6071  
6072 8 *echo = *XML::XSH2::Functions::out;
6073  
6074 183 ▶sub xsh {
Subroutine xsh: called CC=1 SCAR=0.0
6075 183 my ($p, $s, $l)=caller;
6076 183 local $XML::XSH2::Functions::SCRIPT="<xsh() called from $s line $l>";
6077 183 XML::XSH2::Functions::run_string(join "",XML::XSH2::Functions::cast_objects_to_values(@_));
6078 }
6079  
6080 8 ▶sub current {
Subroutine current: called CC=1 SCAR=0.0
6081 8 return XML::XSH2::Functions::xsh_context_node();
6082 }
6083  
6084 0 ▶sub position {
Subroutine position: not called CC=2 SCAR=17.9
6085 0 ▶ if ($XML::XSH2::Functions::_xpc->can('getContextPosition')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::XSH2::Functions::_xpc->can('getContextPosition')) { } 0 0 0
6086 0 return $XML::XSH2::Functions::_xpc->getContextPosition();
6087 } else {
6088 0 die "Sorry, installed XML::LibXML::XPathContext version doesn't support proximity position\n";
6089 }
6090 }
6091  
6092 8 *count = *XML::XSH2::Functions::count_xpath;
6093 8 *xml_list = *serialize;
6094  
6095 0 ▶sub resolve_uri {
Subroutine resolve_uri: not called CC=2 SCAR=17.9
6096 0 my ($rel,$base)=@_;
6097 0 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) { } 0 0 0
6098 0 return URI->new_abs($rel,URI->new($base)->abs(URI::file->cwd));
6099 } else {
6100 0 return URI->new_abs($rel,URI::file->cwd);
6101 }
6102 }
6103  
6104 #######################################################################
6105 #######################################################################
6106  
6107 package XML::XSH2::Internal::Exception;
6108  
6109 28 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
6110 28 ▶ my $class=(ref($_[0]) || $_[0]);
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
0280
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
6111 28 shift;
6112 28 return bless [@_], $class;
6113 }
6114  
6115 0 ▶sub set_label {
Subroutine set_label: not called CC=1 SCAR=6.9
6116 0 my ($label)=@_;
6117 0 return $_[0]->[0]=$label;
6118 }
6119  
6120 84 ▶sub label {
Subroutine label: called CC=1 SCAR=0.0
6121 84 return $_[0]->[0];
6122 }
6123  
6124 0 ▶sub value {
Subroutine value: not called CC=1 SCAR=6.9
6125 0 my ($index)=@_;
6126 0 return $_[0]->[$index];
6127 }
6128  
6129 0 ▶sub set_value {
Subroutine set_value: not called CC=1 SCAR=6.9
6130 0 my ($index,$value)=@_;
6131 0 return $_[0]->[$index]=$value;
6132 }
6133  
6134 package XML::XSH2::Internal::UncatchableException;
6135 15 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
6136 8 @ISA=qw(XML::XSH2::Internal::Exception);
6137  
6138 package XML::XSH2::Internal::LoopTerminatingException;
6139 15 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
6140 8 @ISA=qw(XML::XSH2::Internal::UncatchableException);
6141  
6142 package XML::XSH2::Internal::SubTerminatingException;
6143 15 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
6144 8 @ISA=qw(XML::XSH2::Internal::UncatchableException);
6145  
6146  
6147 #######################################################################
6148 #######################################################################
6149  
6150 package # hide from PAUSE
6151 XML::SAX::Writer::XMLEnc;
6152 14 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
6153 8 @ISA=qw(XML::SAX::Writer::XML);
6154  
6155 0 ▶sub xml_decl {
Subroutine xml_decl: not called CC=2 SCAR=17.9
6156 0 my ($self,$data) = @_;
6157 0 ▶ if ($data->{Encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"Encoding"}) 0 0 0
6158 0 $self->{EncodeTo}=$data->{Encoding};
6159 0 $self->setConverter();
6160 }
6161 0 $self->SUPER::xml_decl($data);
6162 }
6163  
6164  
6165 # taken from Variable::Alias
6166 package XML::XSH2::VarAlias;
6167 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6168 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6169 13 ▶use Tie::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
6170 12 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
6171 8 @ISA=qw(Tie::StdScalar);
6172  
6173 364 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=1 SCAR=0.0
6174 364 return bless $_[1], $_[0];
6175 }
6176  
6177 8 1;
6178  
« XSH2/Completion.pm ↑ Summary XSH2/XPathToXML.pm »