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