Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / i486-linux-gnu-thread-multi / XML / LibXML / Error.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 use strict;
12 use vars qw($AUTOLOAD @error_domains $VERSION $WARNINGS);
13 use Carp;
14 use overload 
15   '""' => \&as_string,
16   'eq' => sub {
17     ("$_[0]" eq "$_[1]")
18   },
19   'cmp' => sub {
20     ("$_[0]" cmp "$_[1]")
21   },
22   fallback => 1;
23
24 $WARNINGS = 0; # 0: supress, 1: report via warn, 2: report via die
25 $VERSION = "1.70"; # VERSION TEMPLATE: DO NOT CHANGE
26
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
31
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
56
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");
63
64
65
66   sub new {
67     my ($class,$xE) = @_;
68     my $terr;
69     if (ref($xE)) {
70       my ($context,$column) = $xE->context_and_column();
71       $terr =bless {
72         domain  => $xE->domain(),
73         level   => $xE->level(),
74         code    => $xE->code(),
75         message => $xE->message(),
76         file    => $xE->file(),
77         line    => $xE->line(),
78         str1    => $xE->str1(),
79         str2    => $xE->str2(),
80         str3    => $xE->str3(),
81         num1    => $xE->num1(),
82         num2    => $xE->num2(),
83         (defined($context) ?
84            (
85              context => $context,
86              column => $column,
87             ) : ()),
88       }, $class;
89     } else {
90       # !!!! problem : got a flat error
91       # warn("PROBLEM: GOT A FLAT ERROR $xE\n");
92       $terr =bless {
93         domain  => 0,
94         level   => 2,
95         code    => -1,
96         message => $xE,
97         file    => undef,
98         line    => undef,
99         str1    => undef,
100         str2    => undef,
101         str3    => undef,
102         num1    => undef,
103         num2    => undef,
104       }, $class;
105     }
106     return $terr;
107   }
108
109     sub _callback_error {
110       #print "CALLBACK\n";
111       my ($xE,$prev) = @_;
112       my $terr;
113       $terr=XML::LibXML::Error->new($xE);
114       if ($terr->{level} == XML_ERR_WARNING and $WARNINGS!=2) {
115         warn $terr if $WARNINGS;
116         return $prev;
117       }
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()';
122       #}
123       #warn "Saving the error ",$terr->dump;
124       $terr->{_prev} = ref($prev) ? $prev :
125         defined($prev) && length($prev) ? XML::LibXML::Error->new($prev) : undef;
126       return $terr;
127     }
128     sub _instant_error_callback {
129       my $xE = shift;
130       my $terr= XML::LibXML::Error->new($xE);
131       print "Reporting an instanteous error ",$terr->dump;
132       die $terr;
133     }
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;
139         warn $saved_error;
140       }
141     }
142     sub _report_error {
143       my ($saved_error) = @_;
144       #print "CALLBACK ERROR: $saved_error\n";
145       if ( defined $saved_error ) {
146         die $saved_error;
147       }
148     }
149 }
150
151
152 sub AUTOLOAD {
153   my $self=shift;
154   return undef unless ref($self);
155   my $sub = $AUTOLOAD;
156   $sub =~ s/.*:://;
157   if ($sub=~/^(?:code|_prev|level|file|line|domain|nodename|message|column|context|str[123]|num[12])$/) {
158     return $self->{$sub};
159   } else {
160     croak("Unknown error field $sub");
161   }
162 }
163
164 # backward compatibility
165 sub int1 { $_[0]->num1 }
166 sub int2 { $_[0]->num2 }
167
168 sub DESTROY {}
169
170 sub domain {
171     my ($self)=@_;
172     return undef unless ref($self);
173     return $error_domains[$self->{domain}];
174 }
175
176 sub as_string {
177     my ($self)=@_;
178     my $msg = "";
179     my $level;
180
181     if (defined($self->{_prev})) {
182         $msg = $self->{_prev}->as_string;
183     }
184
185     if ($self->{level} == XML_ERR_NONE) {
186         $level = "";
187     } elsif ($self->{level} == XML_ERR_WARNING) {
188         $level = "warning";
189     } elsif ($self->{level} == XML_ERR_ERROR ||
190              $self->{level} == XML_ERR_FATAL) {
191         $level = "error";
192     }
193     my $where="";
194     if (defined($self->{file})) {
195         $where="$self->{file}:$self->{line}";
196     } elsif (($self->{domain} == XML_ERR_FROM_PARSER)
197              and
198              $self->{line})  {
199         $where="Entity: line $self->{line}";
200     }
201     if ($self->{nodename}) {
202         $where.=": element ".$self->{nodename};
203     }
204     $msg.=$where.": " if $where ne "";
205     $msg.=$error_domains[$self->{domain}]." ".$level." :";
206     my $str=$self->{message}||"";
207     chomp($str);
208     $msg.=" ".$str."\n";
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};
215       $msg.=$context."\n";
216       $context = substr($context,0,$self->{column});
217       $context=~s/[^\t]/ /g;
218       $msg.=$context."^\n";
219     }
220     return $msg;
221 }
222
223 sub dump {
224   my ($self)=@_;
225   use Data::Dumper;
226   return Data::Dumper->new([$self],['error'])->Dump;
227 }
228
229 1;