blib/lib/XML/LibXML/Error.pm

filter: Statementstmt 93.0% 186 / 200 Branchbran 76.1% 32 / 42 Conditioncond 64.7% 11 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 90.5% 48 / 53 Podpod 50.0% 3 / 6 totaltotal 86.3% 284 / 329 CC 28 SCAR 15.8
CC
28
Cov
88%
as_string
15.9
_callback_error
8.9
domain
3.0
CRAP 4.8
« blib/lib/XML/LibXML.pm ↑ Summary blib/lib/XML/LibXML/XPathContext.pm »
1 # $Id: Error.pm,v 1.1.2.1 2004/04/20 20:09:48 pajas Exp $
2 #
3 # This is free software, you may use it and distribute it under the same terms as
4 # Perl itself.
5 #
6 # Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas
7 #
8 #
9 package XML::LibXML::Error;
10  
11 77 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 77 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # To avoid a "Deep recursion on subroutine as_string" warning
15 77 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 77 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 use overload
20 77 ▶ '""' => \&as_string,
Subroutine BEGIN: called CC=1 SCAR=0.1
21 0 ▶ 'eq' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 0 ("$_[0]" eq "$_[1]")
23 },
24 12 ▶ 'cmp' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 12 ("$_[0]" cmp "$_[1]")
26 },
27 77 fallback => 1;
28  
29 77 our $WARNINGS = 0; # 0: suppress, 1: report via warn, 2: report via die
30 77 our $VERSION = "2.0213"; # VERSION TEMPLATE: DO NOT CHANGE
31  
32 77 ▶use constant XML_ERR_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 77 ▶use constant XML_ERR_WARNING => 1; # A simple warning
Subroutine BEGIN: called CC=1 SCAR=0.0
34 77 ▶use constant XML_ERR_ERROR => 2; # A recoverable error
Subroutine BEGIN: called CC=1 SCAR=0.0
35 77 ▶use constant XML_ERR_FATAL => 3; # A fatal error
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 77 ▶use constant XML_ERR_FROM_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 77 ▶use constant XML_ERR_FROM_PARSER => 1; # The XML parser
Subroutine BEGIN: called CC=1 SCAR=0.0
39 77 ▶use constant XML_ERR_FROM_TREE => 2; # The tree module
Subroutine BEGIN: called CC=1 SCAR=0.0
40 77 ▶use constant XML_ERR_FROM_NAMESPACE => 3; # The XML Namespace module
Subroutine BEGIN: called CC=1 SCAR=0.0
41 77 ▶use constant XML_ERR_FROM_DTD => 4; # The XML DTD validation
Subroutine BEGIN: called CC=1 SCAR=0.0
42 77 ▶use constant XML_ERR_FROM_HTML => 5; # The HTML parser
Subroutine BEGIN: called CC=1 SCAR=0.0
43 77 ▶use constant XML_ERR_FROM_MEMORY => 6; # The memory allocator
Subroutine BEGIN: called CC=1 SCAR=0.0
44 77 ▶use constant XML_ERR_FROM_OUTPUT => 7; # The serialization code
Subroutine BEGIN: called CC=1 SCAR=0.0
45 77 ▶use constant XML_ERR_FROM_IO => 8; # The Input/Output stack
Subroutine BEGIN: called CC=1 SCAR=0.0
46 77 ▶use constant XML_ERR_FROM_FTP => 9; # The FTP module
Subroutine BEGIN: called CC=1 SCAR=0.0
47 77 ▶use constant XML_ERR_FROM_HTTP => 10; # The FTP module
Subroutine BEGIN: called CC=1 SCAR=0.0
48 77 ▶use constant XML_ERR_FROM_XINCLUDE => 11; # The XInclude processing
Subroutine BEGIN: called CC=1 SCAR=0.0
49 77 ▶use constant XML_ERR_FROM_XPATH => 12; # The XPath module
Subroutine BEGIN: called CC=1 SCAR=0.0
50 77 ▶use constant XML_ERR_FROM_XPOINTER => 13; # The XPointer module
Subroutine BEGIN: called CC=1 SCAR=0.0
51 77 ▶use constant XML_ERR_FROM_REGEXP => 14; # The regular expressions module
Subroutine BEGIN: called CC=1 SCAR=0.0
52 77 ▶use constant XML_ERR_FROM_DATATYPE => 15; # The W3C XML Schemas Datatype module
Subroutine BEGIN: called CC=1 SCAR=0.0
53 77 ▶use constant XML_ERR_FROM_SCHEMASP => 16; # The W3C XML Schemas parser module
Subroutine BEGIN: called CC=1 SCAR=0.0
54 77 ▶use constant XML_ERR_FROM_SCHEMASV => 17; # The W3C XML Schemas validation module
Subroutine BEGIN: called CC=1 SCAR=0.0
55 77 ▶use constant XML_ERR_FROM_RELAXNGP => 18; # The Relax-NG parser module
Subroutine BEGIN: called CC=1 SCAR=0.0
56 77 ▶use constant XML_ERR_FROM_RELAXNGV => 19; # The Relax-NG validator module
Subroutine BEGIN: called CC=1 SCAR=0.0
57 77 ▶use constant XML_ERR_FROM_CATALOG => 20; # The Catalog module
Subroutine BEGIN: called CC=1 SCAR=0.0
58 77 ▶use constant XML_ERR_FROM_C14N => 21; # The Canonicalization module
Subroutine BEGIN: called CC=1 SCAR=0.0
59 77 ▶use constant XML_ERR_FROM_XSLT => 22; # The XSLT engine from libxslt
Subroutine BEGIN: called CC=1 SCAR=0.0
60 77 ▶use constant XML_ERR_FROM_VALID => 23; # The DTD validation module with valid context
Subroutine BEGIN: called CC=1 SCAR=0.0
61 77 ▶use constant XML_ERR_FROM_CHECK => 24; # The error-checking module
Subroutine BEGIN: called CC=1 SCAR=0.0
62 77 ▶use constant XML_ERR_FROM_WRITER => 25; # The xmlwriter module
Subroutine BEGIN: called CC=1 SCAR=0.0
63 77 ▶use constant XML_ERR_FROM_MODULE => 26; # The dynamically-loaded module module
Subroutine BEGIN: called CC=1 SCAR=0.0
64 77 ▶use constant XML_ERR_FROM_I18N => 27; # The module handling character conversion
Subroutine BEGIN: called CC=1 SCAR=0.0
65 77 ▶use constant XML_ERR_FROM_SCHEMATRONV=> 28; # The Schematron validator module
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 77 our @error_domains = ("", "parser", "tree", "namespace", "validity",
68 "HTML parser", "memory", "output", "I/O", "ftp",
69 "http", "XInclude", "XPath", "xpointer", "regexp",
70 "Schemas datatype", "Schemas parser", "Schemas validity",
71 "Relax-NG parser", "Relax-NG validity",
72 "Catalog", "C14N", "XSLT", "validity", "error-checking",
73 "xmlwriter", "dynamic loading", "i18n",
74 "Schematron validity");
75  
76 77 my $MAX_ERROR_PREV_DEPTH = 100;
77  
78 77 for my $field (qw<code _prev level file line nodename message column context
79 str1 str2 str3 num1 num2 __prev_depth>) {
80 185 ▶ my $method = sub { $_[0]{$field} };
Subroutine __ANON__: called CC=1 SCAR=0.0
81 77 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1155 *$field = $method;
83 }
84  
85 {
86  
87 266 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
88 266 my ($class,$xE) = @_;
89 266 my $terr;
90 266 ▶ if (ref($xE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $xE) { } 265 1 266
91 265 my ($context,$column) = $xE->context_and_column();
92 265 ▶ $terr =bless {
Branch: 1 branch
BranchTrueFalseTotal
defined $context ? : 184 81 265
93 domain => $xE->domain(),
94 level => $xE->level(),
95 code => $xE->code(),
96 message => $xE->message(),
97 file => $xE->file(),
98 line => $xE->line(),
99 str1 => $xE->str1(),
100 str2 => $xE->str2(),
101 str3 => $xE->str3(),
102 num1 => $xE->num1(),
103 num2 => $xE->num2(),
104 __prev_depth => 0,
105 (defined($context) ?
106 (
107 context => $context,
108 column => $column,
109 ) : ()),
110 }, $class;
111 } else {
112 # !!!! problem : got a flat error
113 # warn("PROBLEM: GOT A FLAT ERROR $xE\n");
114 1 $terr =bless {
115 domain => 0,
116 level => 2,
117 code => -1,
118 message => $xE,
119 file => undef,
120 line => undef,
121 str1 => undef,
122 str2 => undef,
123 str3 => undef,
124 num1 => undef,
125 num2 => undef,
126 __prev_depth => 0,
127 }, $class;
128 }
129 266 return $terr;
130 }
131  
132 265 ▶ sub _callback_error {
Subroutine _callback_error: called CC=8 SCAR=21.9
133 #print "CALLBACK\n";
134 265 my ($xE,$prev) = @_;
135 265 my $terr;
136 265 $terr=XML::LibXML::Error->new($xE);
137 265 ▶ if ($terr->{level} == XML_ERR_WARNING and $WARNINGS!=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($terr->{"level"} == 1 and $XML::LibXML::Error::WARNINGS != 2) 8 257 265
Condition66%2/3
$terr->{"level"} == 1 and $XML::LibXML::Error::WARNINGS != 2
!ll&&!rl&&r
25708
MC/DC50%1/2
$terr->{"level"} == 1 and $XML::LibXML::Error::WARNINGS != 2
$terr->{"level"} == 1 $XML::LibXML::Error::WARNINGS != 2
Truth table66%2/3
A and B
A= $terr->{"level"} == 1B= $XML::LibXML::Error::WARNINGS != 2
ABresult
0X0
100
111
138 8 ▶ warn $terr if $WARNINGS;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::LibXML::Error::WARNINGS 0 8 8
139 8 return $prev;
140 }
141 #unless ( defined $terr->{file} and length $terr->{file} ) {
142 # this would make it easier to recognize parsed strings
143 # but it breaks old implementations
144 # [CG] $terr->{file} = 'string()';
145 #}
146 #warn "Saving the error ",$terr->dump;
147  
148 257 ▶ if (ref($prev))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prev) { } 89 168 257
149 {
150 89 ▶ if ($prev->__prev_depth() >= $MAX_ERROR_PREV_DEPTH)
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->__prev_depth >= $MAX_ERROR_PREV_DEPTH) 0 89 89
151 {
152 0 return $prev;
153 }
154 89 $terr->{_prev} = $prev;
155 89 $terr->{__prev_depth} = $prev->__prev_depth() + 1;
156 }
157 else
158 {
159 168 ▶ $terr->{_prev} = defined($prev) && length($prev) ? XML::LibXML::Error->new($prev) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $prev && length $prev ? : 0 168 168
Condition33%1/3
defined $prev and length $prev
!ll&&!rl&&r
16800
MC/DC0%0/2
defined $prev and length $prev
defined $prev length $prev
Truth table33%1/3
A and B
A= defined $prevB= length $prev
ABresult
0X0
100
111
160 }
161 257 return $terr;
162 }
163 0 ▶ sub _instant_error_callback {
Subroutine _instant_error_callback: not called CC=1 SCAR=6.9
164 0 my $xE = shift;
165 0 my $terr= XML::LibXML::Error->new($xE);
166 0 print "Reporting an instanteous error ",$terr->dump;
167 0 die $terr;
168 }
169 1 ▶ sub _report_warning {
Subroutine _report_warning: called CC=2 SCAR=7.1
170 1 my ($saved_error) = @_;
171 #print "CALLBACK WARN\n";
172 1 ▶ if ( defined $saved_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $saved_error) 1 0 1
173 #print "reporting a warning ",$saved_error->dump;
174 1 warn $saved_error;
175 }
176 }
177 166 ▶ sub _report_error {
Subroutine _report_error: called CC=2 SCAR=7.1
178 166 my ($saved_error) = @_;
179 #print "CALLBACK ERROR: $saved_error\n";
180 166 ▶ if ( defined $saved_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $saved_error) 166 0 166
181 166 die $saved_error;
182 }
183 }
184 }
185  
186  
187 # backward compatibility
188 0 ▶sub int1 { $_[0]->num1 }
Subroutine int1: not called CC=1 SCAR=1.2
Pod undocumented
189 0 ▶sub int2 { $_[0]->num2 }
Subroutine int2: not called CC=1 SCAR=6.9
Pod undocumented
190  
191 436 ▶sub domain {
Subroutine domain: called CC=3 SCAR=11.0
Pod documented
192 436 my ($self)=@_;
193 436 ▶ return undef unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 436 436
194 436 my $domain = $self->{domain};
195 # Newer versions of libxml2 might yield errors in domains that aren't
196 # listed above. Invent something reasonable in that case.
197 436 ▶ return $domain < @error_domains ? $error_domains[$domain] : "domain_$domain";
Branch: 1 branch
BranchTrueFalseTotal
$domain < @XML::LibXML::Error::error_domains ? : 434 2 436
198 }
199  
200 433 ▶sub as_string {
Subroutine as_string: called CC=15 SCAR=27.7
Pod documented
201 433 my ($self)=@_;
202 433 my $msg = "";
203 433 my $level;
204  
205 433 ▶ if (defined($self->{_prev})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_prev"}) 138 295 433
206 138 $msg = $self->{_prev}->as_string;
207 }
208  
209 433 ▶ if ($self->{level} == XML_ERR_NONE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'level'} == 0) { } 0 433 433
elsif ($self->{'level'} == 1) { } 0 433 433
elsif ($self->{'level'} == 2 or $self->{'level'} == 3) { } 433 0 433
210 0 $level = "";
211 } elsif ($self->{level} == XML_ERR_WARNING) {
212 0 $level = "warning";
213 ▶ } elsif ($self->{level} == XML_ERR_ERROR ||
Condition66%2/3
$self->{"level"} == 2 or $self->{"level"} == 3
l!l&&r!l&&!r
1412920
MC/DC0%0/2
$self->{"level"} == 2 or $self->{"level"} == 3
$self->{"level"} == 2 $self->{"level"} == 3
Truth table66%2/3
A or B
A= $self->{"level"} == 2B= $self->{"level"} == 3
ABresult
1X1
011
000
214 $self->{level} == XML_ERR_FATAL) {
215 433 $level = "error";
216 }
217 433 my $where="";
218 433 ▶ if (defined($self->{file})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'file'}) { } 337 96 433
elsif ($self->{'domain'} == 1 and $self->{'line'}) { } 32 128 160
219 337 $where="$self->{file}:$self->{line}";
220 ▶ } elsif (($self->{domain} == XML_ERR_FROM_PARSER)
Condition66%2/3
$self->{"domain"} == 1 and $self->{"line"}
!ll&&!rl&&r
64032
MC/DC50%1/2
$self->{"domain"} == 1 and $self->{"line"}
$self->{"domain"} == 1 $self->{"line"}
Truth table66%2/3
A and B
A= $self->{"domain"} == 1B= $self->{"line"}
ABresult
0X0
100
111
221 and
222 $self->{line}) {
223 32 $where="Entity: line $self->{line}";
224 }
225 433 ▶ if ($self->{nodename}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nodename"}) 0 433 433
226 0 $where.=": element ".$self->{nodename};
227 }
228 433 ▶ $msg.=$where.": " if $where ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $where ne "" 369 64 433
229 433 $msg.=$self->domain." ".$level." :";
230 433 ▶ my $str=$self->{message}||"";
Condition50%1/2
$self->{'message'} || ''
l!l
4330
MC/DC0%0/1
$self->{'message'} || ''
$self->{'message'}
Truth table50%1/2
A
A= $self->{'message'}
Aresult
11
00
231 433 chomp($str);
232 433 $msg.=" ".$str."\n";
233 433 ▶ if (($self->{domain} == XML_ERR_FROM_XPATH) and
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'domain'} == 12 and defined $self->{'str1'}) { } 20 413 433
elsif (defined $self->{'context'}) { } 337 76 413
Condition100%3/3
$self->{"domain"} == 12 and defined $self->{"str1"}
!ll&&!rl&&r
3833020
MC/DC100%2/2
$self->{"domain"} == 12 and defined $self->{"str1"}
$self->{"domain"} == 12 defined $self->{"str1"}
Truth table100%3/3
A and B
A= $self->{"domain"} == 12B= defined $self->{"str1"}
ABresult
0X0
100
111
234 defined($self->{str1})) {
235 20 $msg.=$self->{str1}."\n";
236 20 $msg.=(" " x $self->{num1})."^\n";
237 } elsif (defined $self->{context}) {
238 # If the error relates to character-encoding problems in the context,
239 # then doing textual operations on it will spew warnings that
240 # XML::LibXML can do nothing to fix. So just disable all such
241 # warnings. This has the pleasing benefit of making the test suite
242 # run warning-free.
243 77 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
244 337 my $context = Encode::encode('UTF-8', $self->{context});
245 337 $msg.=$context."\n";
246 337 $context = substr($context,0,$self->{column});
247 337 $context=~s/[^\t]/ /g;
248 337 $msg.=$context."^\n";
249 }
250 433 return $msg;
251 }
252  
253 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
254 0 my ($self)=@_;
255 0 require Data::Dumper;
256 0 return Data::Dumper->new([$self],['error'])->Dump;
257 }
258  
259 77 1;
« blib/lib/XML/LibXML.pm ↑ Summary blib/lib/XML/LibXML/XPathContext.pm »