Debian Codegraph @

lib/XML/LibXML/Error.pm

raw
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 use strict;
12 use warnings;
13
14 # To avoid a "Deep recursion on subroutine as_string" warning
15 no warnings 'recursion';
16
17 use Encode ();
18
19 use vars qw(@error_domains $VERSION $WARNINGS);
20 use Carp;
21 use overload
22 '""' => \&as_string,
23 'eq' => sub {
24 ("$_[0]" eq "$_[1]")
25 },
26 'cmp' => sub {
27 ("$_[0]" cmp "$_[1]")
28 },
29 fallback => 1;
30
31 $WARNINGS = 0; # 0: suppress, 1: report via warn, 2: report via die
32 $VERSION = "2.0134"; # VERSION TEMPLATE: DO NOT CHANGE
33
34 use constant XML_ERR_NONE => 0;
35 use constant XML_ERR_WARNING => 1; # A simple warning
36 use constant XML_ERR_ERROR => 2; # A recoverable error
37 use constant XML_ERR_FATAL => 3; # A fatal error
38
39 use constant XML_ERR_FROM_NONE => 0;
40 use constant XML_ERR_FROM_PARSER => 1; # The XML parser
41 use constant XML_ERR_FROM_TREE => 2; # The tree module
42 use constant XML_ERR_FROM_NAMESPACE => 3; # The XML Namespace module
43 use constant XML_ERR_FROM_DTD => 4; # The XML DTD validation
44 use constant XML_ERR_FROM_HTML => 5; # The HTML parser
45 use constant XML_ERR_FROM_MEMORY => 6; # The memory allocator
46 use constant XML_ERR_FROM_OUTPUT => 7; # The serialization code
47 use constant XML_ERR_FROM_IO => 8; # The Input/Output stack
48 use constant XML_ERR_FROM_FTP => 9; # The FTP module
49 use constant XML_ERR_FROM_HTTP => 10; # The FTP module
50 use constant XML_ERR_FROM_XINCLUDE => 11; # The XInclude processing
51 use constant XML_ERR_FROM_XPATH => 12; # The XPath module
52 use constant XML_ERR_FROM_XPOINTER => 13; # The XPointer module
53 use constant XML_ERR_FROM_REGEXP => 14; # The regular expressions module
54 use constant XML_ERR_FROM_DATATYPE => 15; # The W3C XML Schemas Datatype module
55 use constant XML_ERR_FROM_SCHEMASP => 16; # The W3C XML Schemas parser module
56 use constant XML_ERR_FROM_SCHEMASV => 17; # The W3C XML Schemas validation module
57 use constant XML_ERR_FROM_RELAXNGP => 18; # The Relax-NG parser module
58 use constant XML_ERR_FROM_RELAXNGV => 19; # The Relax-NG validator module
59 use constant XML_ERR_FROM_CATALOG => 20; # The Catalog module
60 use constant XML_ERR_FROM_C14N => 21; # The Canonicalization module
61 use constant XML_ERR_FROM_XSLT => 22; # The XSLT engine from libxslt
62 use constant XML_ERR_FROM_VALID => 23; # The DTD validation module with valid context
63 use constant XML_ERR_FROM_CHECK => 24; # The error-checking module
64 use constant XML_ERR_FROM_WRITER => 25; # The xmlwriter module
65 use constant XML_ERR_FROM_MODULE => 26; # The dynamically-loaded module module
66 use constant XML_ERR_FROM_I18N => 27; # The module handling character conversion
67 use constant XML_ERR_FROM_SCHEMATRONV=> 28; # The Schematron validator module
68
69 @error_domains = ("", "parser", "tree", "namespace", "validity",
70 "HTML parser", "memory", "output", "I/O", "ftp",
71 "http", "XInclude", "XPath", "xpointer", "regexp",
72 "Schemas datatype", "Schemas parser", "Schemas validity",
73 "Relax-NG parser", "Relax-NG validity",
74 "Catalog", "C14N", "XSLT", "validity", "error-checking",
75 "xmlwriter", "dynamic loading", "i18n",
76 "Schematron validity");
77
78 my $MAX_ERROR_PREV_DEPTH = 100;
79
80 for my $field (qw<code _prev level file line nodename message column context
81 str1 str2 str3 num1 num2 __prev_depth>) {
82 my $method = sub { $_[0]{$field} };
83 no strict 'refs';
84 *$field = $method;
85 }
86
87 {
88
89 sub new {
90 my ($class,$xE) = @_;
91 my $terr;
92 if (ref($xE)) {
93 my ($context,$column) = $xE->context_and_column();
94 $terr =bless {
95 domain => $xE->domain(),
96 level => $xE->level(),
97 code => $xE->code(),
98 message => $xE->message(),
99 file => $xE->file(),
100 line => $xE->line(),
101 str1 => $xE->str1(),
102 str2 => $xE->str2(),
103 str3 => $xE->str3(),
104 num1 => $xE->num1(),
105 num2 => $xE->num2(),
106 __prev_depth => 0,
107 (defined($context) ?
108 (
109 context => $context,
110 column => $column,
111 ) : ()),
112 }, $class;
113 } else {
114 # !!!! problem : got a flat error
115 # warn("PROBLEM: GOT A FLAT ERROR $xE\n");
116 $terr =bless {
117 domain => 0,
118 level => 2,
119 code => -1,
120 message => $xE,
121 file => undef,
122 line => undef,
123 str1 => undef,
124 str2 => undef,
125 str3 => undef,
126 num1 => undef,
127 num2 => undef,
128 __prev_depth => 0,
129 }, $class;
130 }
131 return $terr;
132 }
133
134 sub _callback_error {
135 #print "CALLBACK\n";
136 my ($xE,$prev) = @_;
137 my $terr;
138 $terr=XML::LibXML::Error->new($xE);
139 if ($terr->{level} == XML_ERR_WARNING and $WARNINGS!=2) {
140 warn $terr if $WARNINGS;
141 return $prev;
142 }
143 #unless ( defined $terr->{file} and length $terr->{file} ) {
144 # this would make it easier to recognize parsed strings
145 # but it breaks old implementations
146 # [CG] $terr->{file} = 'string()';
147 #}
148 #warn "Saving the error ",$terr->dump;
149
150 if (ref($prev))
151 {
152 if ($prev->__prev_depth() >= $MAX_ERROR_PREV_DEPTH)
153 {
154 return $prev;
155 }
156 $terr->{_prev} = $prev;
157 $terr->{__prev_depth} = $prev->__prev_depth() + 1;
158 }
159 else
160 {
161 $terr->{_prev} = defined($prev) && length($prev) ? XML::LibXML::Error->new($prev) : undef;
162 }
163 return $terr;
164 }
165 sub _instant_error_callback {
166 my $xE = shift;
167 my $terr= XML::LibXML::Error->new($xE);
168 print "Reporting an instanteous error ",$terr->dump;
169 die $terr;
170 }
171 sub _report_warning {
172 my ($saved_error) = @_;
173 #print "CALLBACK WARN\n";
174 if ( defined $saved_error ) {
175 #print "reporting a warning ",$saved_error->dump;
176 warn $saved_error;
177 }
178 }
179 sub _report_error {
180 my ($saved_error) = @_;
181 #print "CALLBACK ERROR: $saved_error\n";
182 if ( defined $saved_error ) {
183 die $saved_error;
184 }
185 }
186 }
187
188
189 # backward compatibility
190 sub int1 { $_[0]->num1 }
191 sub int2 { $_[0]->num2 }
192
193 sub domain {
194 my ($self)=@_;
195 return undef unless ref($self);
196 my $domain = $self->{domain};
197 # Newer versions of libxml2 might yield errors in domains that aren't
198 # listed above. Invent something reasonable in that case.
199 return $domain < @error_domains ? $error_domains[$domain] : "domain_$domain";
200 }
201
202 sub as_string {
203 my ($self)=@_;
204 my $msg = "";
205 my $level;
206
207 if (defined($self->{_prev})) {
208 $msg = $self->{_prev}->as_string;
209 }
210
211 if ($self->{level} == XML_ERR_NONE) {
212 $level = "";
213 } elsif ($self->{level} == XML_ERR_WARNING) {
214 $level = "warning";
215 } elsif ($self->{level} == XML_ERR_ERROR ||
216 $self->{level} == XML_ERR_FATAL) {
217 $level = "error";
218 }
219 my $where="";
220 if (defined($self->{file})) {
221 $where="$self->{file}:$self->{line}";
222 } elsif (($self->{domain} == XML_ERR_FROM_PARSER)
223 and
224 $self->{line}) {
225 $where="Entity: line $self->{line}";
226 }
227 if ($self->{nodename}) {
228 $where.=": element ".$self->{nodename};
229 }
230 $msg.=$where.": " if $where ne "";
231 $msg.=$self->domain." ".$level." :";
232 my $str=$self->{message}||"";
233 chomp($str);
234 $msg.=" ".$str."\n";
235 if (($self->{domain} == XML_ERR_FROM_XPATH) and
236 defined($self->{str1})) {
237 $msg.=$self->{str1}."\n";
238 $msg.=(" " x $self->{num1})."^\n";
239 } elsif (defined $self->{context}) {
240 # If the error relates to character-encoding problems in the context,
241 # then doing textual operations on it will spew warnings that
242 # XML::LibXML can do nothing to fix. So just disable all such
243 # warnings. This has the pleasing benefit of making the test suite
244 # run warning-free.
245 no warnings 'utf8';
246 my $context = Encode::encode('utf8', $self->{context}, Encode::FB_DEFAULT);
247 $msg.=$context."\n";
248 $context = substr($context,0,$self->{column});
249 $context=~s/[^\t]/ /g;
250 $msg.=$context."^\n";
251 }
252 return $msg;
253 }
254
255 sub dump {
256 my ($self)=@_;
257 use Data::Dumper;
258 return Data::Dumper->new([$self],['error'])->Dump;
259 }
260
261 1;