1 # $Id: Error.pm,v 1.1.2.1 2004/04/20 20:09:48 pajas Exp $
3 # This is free software, you may use it and distribute it under the same terms as
6 # Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas
9 package XML::LibXML::Error;
12 use vars qw($AUTOLOAD @error_domains $VERSION $WARNINGS);
24 $WARNINGS = 0; # 0: supress, 1: report via warn, 2: report via die
25 $VERSION = "1.70"; # VERSION TEMPLATE: DO NOT CHANGE
27 use constant XML_ERR_NONE => 0;
28 use constant XML_ERR_WARNING => 1; # A simple warning
29 use constant XML_ERR_ERROR => 2; # A recoverable error
30 use constant XML_ERR_FATAL => 3; # A fatal error
32 use constant XML_ERR_FROM_NONE => 0;
33 use constant XML_ERR_FROM_PARSER => 1; # The XML parser
34 use constant XML_ERR_FROM_TREE => 2; # The tree module
35 use constant XML_ERR_FROM_NAMESPACE => 3; # The XML Namespace module
36 use constant XML_ERR_FROM_DTD => 4; # The XML DTD validation
37 use constant XML_ERR_FROM_HTML => 5; # The HTML parser
38 use constant XML_ERR_FROM_MEMORY => 6; # The memory allocator
39 use constant XML_ERR_FROM_OUTPUT => 7; # The serialization code
40 use constant XML_ERR_FROM_IO => 8; # The Input/Output stack
41 use constant XML_ERR_FROM_FTP => 9; # The FTP module
42 use constant XML_ERR_FROM_HTTP => 10; # The FTP module
43 use constant XML_ERR_FROM_XINCLUDE => 11; # The XInclude processing
44 use constant XML_ERR_FROM_XPATH => 12; # The XPath module
45 use constant XML_ERR_FROM_XPOINTER => 13; # The XPointer module
46 use constant XML_ERR_FROM_REGEXP => 14; # The regular expressions module
47 use constant XML_ERR_FROM_DATATYPE => 15; # The W3C XML Schemas Datatype module
48 use constant XML_ERR_FROM_SCHEMASP => 16; # The W3C XML Schemas parser module
49 use constant XML_ERR_FROM_SCHEMASV => 17; # The W3C XML Schemas validation module
50 use constant XML_ERR_FROM_RELAXNGP => 18; # The Relax-NG parser module
51 use constant XML_ERR_FROM_RELAXNGV => 19; # The Relax-NG validator module
52 use constant XML_ERR_FROM_CATALOG => 20; # The Catalog module
53 use constant XML_ERR_FROM_C14N => 21; # The Canonicalization module
54 use constant XML_ERR_FROM_XSLT => 22; # The XSLT engine from libxslt
55 use constant XML_ERR_FROM_VALID => 23; # The validaton module
57 @error_domains = ("", "parser", "tree", "namespace", "validity",
58 "HTML parser", "memory", "output", "I/O", "ftp",
59 "http", "XInclude", "XPath", "xpointer", "regexp",
60 "Schemas datatype", "Schemas parser", "Schemas validity",
61 "Relax-NG parser", "Relax-NG validity",
62 "Catalog", "C14N", "XSLT", "validity");
70 my ($context,$column) = $xE->context_and_column();
72 domain => $xE->domain(),
73 level => $xE->level(),
75 message => $xE->message(),
90 # !!!! problem : got a flat error
91 # warn("PROBLEM: GOT A FLAT ERROR $xE\n");
109 sub _callback_error {
113 $terr=XML::LibXML::Error->new($xE);
114 if ($terr->{level} == XML_ERR_WARNING and $WARNINGS!=2) {
115 warn $terr if $WARNINGS;
118 #unless ( defined $terr->{file} and length $terr->{file} ) {
119 # this would make it easier to recognize parsed strings
120 # but it breaks old implementations
121 # [CG] $terr->{file} = 'string()';
123 #warn "Saving the error ",$terr->dump;
124 $terr->{_prev} = ref($prev) ? $prev :
125 defined($prev) && length($prev) ? XML::LibXML::Error->new($prev) : undef;
128 sub _instant_error_callback {
130 my $terr= XML::LibXML::Error->new($xE);
131 print "Reporting an instanteous error ",$terr->dump;
134 sub _report_warning {
135 my ($saved_error) = @_;
136 #print "CALLBACK WARN\n";
137 if ( defined $saved_error ) {
138 #print "reporting a warning ",$saved_error->dump;
143 my ($saved_error) = @_;
144 #print "CALLBACK ERROR: $saved_error\n";
145 if ( defined $saved_error ) {
154 return undef unless ref($self);
157 if ($sub=~/^(?:code|_prev|level|file|line|domain|nodename|message|column|context|str[123]|num[12])$/) {
158 return $self->{$sub};
160 croak("Unknown error field $sub");
164 # backward compatibility
165 sub int1 { $_[0]->num1 }
166 sub int2 { $_[0]->num2 }
172 return undef unless ref($self);
173 return $error_domains[$self->{domain}];
181 if (defined($self->{_prev})) {
182 $msg = $self->{_prev}->as_string;
185 if ($self->{level} == XML_ERR_NONE) {
187 } elsif ($self->{level} == XML_ERR_WARNING) {
189 } elsif ($self->{level} == XML_ERR_ERROR ||
190 $self->{level} == XML_ERR_FATAL) {
194 if (defined($self->{file})) {
195 $where="$self->{file}:$self->{line}";
196 } elsif (($self->{domain} == XML_ERR_FROM_PARSER)
199 $where="Entity: line $self->{line}";
201 if ($self->{nodename}) {
202 $where.=": element ".$self->{nodename};
204 $msg.=$where.": " if $where ne "";
205 $msg.=$error_domains[$self->{domain}]." ".$level." :";
206 my $str=$self->{message}||"";
209 if (($self->{domain} == XML_ERR_FROM_XPATH) and
210 defined($self->{str1})) {
211 $msg.=$self->{str1}."\n";
212 $msg.=(" " x $self->{num1})."^\n";
213 } elsif (defined $self->{context}) {
214 my $context = $self->{context};
216 $context = substr($context,0,$self->{column});
217 $context=~s/[^\t]/ /g;
218 $msg.=$context."^\n";
226 return Data::Dumper->new([$self],['error'])->Dump;