/usr/share/perl5/XML/DOM/ValParser.pm is in libxml-checker-perl 0.13-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | #
# Use XML::DOM::ValParser instead of XML::DOM::Parser and it will
# use XML::Checker to validate XML at parse time.
#
package XML::DOM::ValParser;
use strict;
use XML::DOM;
use XML::Checker::Parser;
use vars qw( @ISA @SupportedHandlers );
@ISA = qw( XML::Checker::Parser );
# These XML::Parser handlers are currently supported by XML::DOM
@SupportedHandlers = qw( Init Final Char Start End Default Doctype
CdataStart CdataEnd XMLDecl Entity Notation Proc
Default Comment Attlist Element Unparsed );
sub new
{
my ($class, %args) = @_;
my %handlers = ();
for (@SupportedHandlers)
{
my $domHandler = "XML::Parser::Dom::$_";
$handlers{$_} = \&$domHandler;
}
$args{Handlers} = \%handlers;
$class->SUPER::new (%args);
}
sub parse
{
# Do what XML::DOM::Parser normally does.
# Temporarily override his @ISA, so that he thinks he's a
# XML::DOM::ValParser and calls the right SUPER::parse(),
# (otherwise he thinks he's an XML::DOM::Parser and you see runtime
# error: Can't call method "Init" on unblessed reference ...)
local @XML::DOM::Parser::ISA = @ISA;
local $XML::Checker::Parser::_skipInsignifWS = $_[0]->{SkipInsignifWS};
XML::DOM::Parser::parse (@_);
}
1; # package return code
__END__
=head1 NAME
XML::DOM::ValParser - an XML::DOM::Parser that validates at parse time
=head1 SYNOPSIS
use XML::DOM::ValParser;
my %expat_options = (KeepCDATA => 1,
Handlers => [ Unparsed => \&my_Unparsed_handler ]);
my $parser = new XML::DOM::ValParser (%expat_options);
eval {
local $XML::Checker::FAIL = \&my_fail;
my $doc = $parser->parsefile ("fail.xml");
... XML::DOM::Document was created successfully ...
};
if ($@) {
# Either XML::Parser (expat) threw an exception or my_fail() died.
... your error handling code here ...
# Note that the XML::DOM::Document is automatically disposed off and
# will be garbage collected
}
# Throws an exception (with die) when an error is encountered, this
# will stop the parsing process.
# Don't die if a warning or info message is encountered, just print a message.
sub my_fail {
my $code = shift;
die XML::Checker::error_string ($code, @_) if $code < 200;
XML::Checker::print_error ($code, @_);
}
=head1 DESCRIPTION
Use XML::DOM::ValParser wherever you would use L<XML::DOM::Parser> and
your XML will be checked using L<XML::Checker> at parse time.
See L<XML::DOM> for details on XML::DOM::Parser options.
See L<XML::Checker> for details on setting the fail handler (my_fail.)
The following handlers are currently supported, just like XML::DOM::Parser:
Init, Final, Char, Start, End, Default, Doctype, CdataStart, CdataEnd,
XMLDecl, Entity, Notation, Proc, Default, Comment, Attlist, Element, Unparsed.
=head1 XML::DOM::ValParser
XML::DOM::ValParser extends from L<XML::Checker::Parser>. It creates an
L<XML::Checker> object and routes all event handlers through the checker,
before processing the events to create the XML::DOM::Document.
Just like L<XML::Checker::Parser>, the checker object can be retrieved with
the getChecker() method and can be reused later on (provided that the DOCTYPE
section of the XML::DOM::Document did not change in the mean time.)
You can control which errors are fatal (and therefore should stop creation
of the XML::DOM::Document) by filtering the appropriate error codes in
the global $XML::Checker::FAIL handler
(see L<XML::Checker/ERROR_HANDLING>) and
calling I<die> or I<croak> appropriately.
Just like XML::Checker::Parser, XML::DOM::ValParser supports the
SkipExternalDTD and SkipInsignifWS options. See L<XML::Checker::Parser>
for details.
=head1 AUTHOR
Enno Derksen is the original author.
Send bug reports, hints, tips, suggestions to T.J. Mather at
<F<tjmather@tjmather.com>>.
=head1 SEE ALSO
L<XML::DOM>, L<XML::Checker> (L<XML::Checker/SEE_ALSO>)
|