Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.8 KiB

  1. package HTML::PullParser;
  2. # $Id: PullParser.pm,v 2.6 2001/04/02 23:26:18 gisle Exp $
  3. require HTML::Parser;
  4. @ISA=qw(HTML::Parser);
  5. $VERSION = sprintf("%d.%02d", q$Revision: 2.6 $ =~ /(\d+)\.(\d+)/);
  6. use strict;
  7. use Carp ();
  8. sub new
  9. {
  10. my($class, %cnf) = @_;
  11. # Construct argspecs for the various events
  12. my %argspec;
  13. for (qw(start end text declaration comment process default)) {
  14. my $tmp = delete $cnf{$_};
  15. next unless defined $tmp;
  16. $argspec{$_} = $tmp;
  17. }
  18. Carp::croak("Info not collected for any events")
  19. unless %argspec;
  20. my $file = delete $cnf{file};
  21. my $doc = delete $cnf{doc};
  22. Carp::croak("Can't parse from both 'doc' and 'file' at the same time")
  23. if defined($file) && defined($doc);
  24. Carp::croak("No 'doc' or 'file' given to parse from")
  25. unless defined($file) || defined($doc);
  26. # Create object
  27. $cnf{api_version} = 3;
  28. my $self = $class->SUPER::new(%cnf);
  29. my $accum = $self->{pullparser_accum} = [];
  30. while (my($event, $argspec) = each %argspec) {
  31. $self->SUPER::handler($event => $accum, $argspec);
  32. }
  33. if (defined $doc) {
  34. $self->{pullparser_str_ref} = ref($doc) ? $doc : \$doc;
  35. $self->{pullparser_str_pos} = 0;
  36. }
  37. else {
  38. if (!ref($file) && ref(\$file) ne "GLOB") {
  39. require IO::File;
  40. $file = IO::File->new($file, "r") || return;
  41. }
  42. $self->{pullparser_file} = $file;
  43. }
  44. $self;
  45. }
  46. sub handler
  47. {
  48. Carp::croak("Can't set handlers for HTML::PullParser");
  49. }
  50. sub get_token
  51. {
  52. my $self = shift;
  53. while (!@{$self->{pullparser_accum}} && !$self->{pullparser_eof}) {
  54. if (my $f = $self->{pullparser_file}) {
  55. # must try to parse more from the file
  56. my $buf;
  57. if (read($f, $buf, 512)) {
  58. $self->parse($buf);
  59. } else {
  60. $self->eof;
  61. $self->{pullparser_eof}++;
  62. delete $self->{pullparser_file};
  63. }
  64. }
  65. elsif (my $sref = $self->{pullparser_str_ref}) {
  66. # must try to parse more from the scalar
  67. my $pos = $self->{pullparser_str_pos};
  68. my $chunk = substr($$sref, $pos, 512);
  69. $self->parse($chunk);
  70. $pos += length($chunk);
  71. if ($pos < length($$sref)) {
  72. $self->{pullparser_str_pos} = $pos;
  73. }
  74. else {
  75. $self->eof;
  76. $self->{pullparser_eof}++;
  77. delete $self->{pullparser_str_ref};
  78. delete $self->{pullparser_str_pos};
  79. }
  80. }
  81. else {
  82. die;
  83. }
  84. }
  85. shift @{$self->{pullparser_accum}};
  86. }
  87. sub unget_token
  88. {
  89. my $self = shift;
  90. unshift @{$self->{pullparser_accum}}, @_;
  91. $self;
  92. }
  93. 1;
  94. __END__
  95. =head1 NAME
  96. HTML::PullParser - Alternative HTML::Parser interface
  97. =head1 SYNOPSIS
  98. use HTML::PullParser;
  99. $p = HTML::PullParser->new(file => "index.html",
  100. start => 'event, tagname, @attr',
  101. end => 'event, tagname',
  102. ignore_elements => [qw(script style)],
  103. ) || die "Can't open: $!";
  104. while (my $token = $p->get_token) {
  105. #...do something with $token
  106. }
  107. =head1 DESCRIPTION
  108. The HTML::PullParser is an alternative interface to the HTML::Parser class.
  109. It basically turns the HTML::Parser inside out. You associate a file
  110. (or any IO::Handle object or string) with the parser at construction time and
  111. then repeatedly call $parser->get_token to obtain the tags and text
  112. found in the parsed document.
  113. The following methods are provided:
  114. =over 4
  115. =item $p = HTML::PullParser->new( file => $file, %options )
  116. =item $p = HTML::PullParser->new( doc => \$doc, %options )
  117. A C<HTML::PullParser> can be made to parse from either a file or a
  118. literal document based on whether the C<file> or C<doc> option is
  119. passed to the parser's constructor.
  120. The C<file> passed in can either be a file name or a file handle
  121. object. If a file name is passed, and it can't be opened for reading,
  122. then the constructor will return an undefined value and $! will tell
  123. you why it failed. Otherwise the argument is taken to be some object
  124. that the C<HTML::PullParser> can read() from when it needs more data.
  125. The stream will be read() until EOF, but not closed.
  126. A C<doc> can be passed plain or as a reference
  127. to a scalar. If a reference is passed then the value of this scalar
  128. should not be changed before all tokens have been extracted.
  129. Next the information to be returned for the different token types must
  130. be set up. This is done by simply assosiating an argspec (as defined
  131. in L<HTML::Parser>) with the events you have an interrest in. For
  132. instance, if you want C<start> tokens to be reported as the string
  133. C<'S'> followed by the tagname and the attributes you might pass an
  134. C<start>-option like this:
  135. $p = HTML::Parser-New( doc => $doc_to_parse,
  136. start => '"S", tagname, @attr',
  137. end => '"E", tagname',
  138. );
  139. At last other C<HTML::Parser> options, like C<ignore_tags>, and
  140. C<unbroken_text>, can be passed in. Note that you should not use the
  141. I<event>_h options to set up parser handlers.
  142. =item $token = $p->get_token
  143. This method will return the next I<token> found in the HTML document,
  144. or C<undef> at the end of the document. The token is usually returned
  145. as an array reference. The content of this array match the argspec
  146. set up during C<HTML::PullParser> construction.
  147. =item $p->unget_token($token,...)
  148. If you find out you have read too many tokens you can push them back,
  149. so that they are returned again the next time $p->get_token is called.
  150. =head1 EXAMPLES
  151. The 'eg/hform' script shows how we might parse the form section of
  152. HTML::Documents using HTML::PullParser.
  153. =head1 SEE ALSO
  154. L<HTML::Parser>, L<HTML::TokeParser>
  155. =head1 COPYRIGHT
  156. Copyright 1998-2001 Gisle Aas.
  157. This library is free software; you can redistribute it and/or
  158. modify it under the same terms as Perl itself.
  159. =cut