home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / perl5 / XML / Grove / PerlSAX.pm < prev    next >
Encoding:
Text File  |  1999-10-23  |  6.7 KB  |  268 lines

  1. #
  2. # Copyright (C) 1998, 1999 Ken MacLeod
  3. # XML::Grove::PerlSAX is free software; you can redistribute it and/or
  4. # modify it under the same terms as Perl itself.
  5. #
  6. # $Id: PerlSAX.pm,v 1.3 1999/08/17 15:01:28 kmacleod Exp $
  7. #
  8.  
  9. use strict;
  10.  
  11. package XML::Grove::PerlSAX;
  12.  
  13. use UNIVERSAL;
  14. use Data::Grove::Visitor;
  15.  
  16. sub new {
  17.     my $type = shift;
  18.     my $self = ($#_ == 0) ? shift : { @_ };
  19.  
  20.     return bless $self, $type;
  21. }
  22.  
  23. sub parse {
  24.     my $self = shift;
  25.  
  26.     die "XML::Grove::PerlSAX: parser instance ($self) already parsing\n"
  27.     if (defined $self->{ParseOptions});
  28.  
  29.     # If there's one arg and it's a subclass of Data::Grove,
  30.     # then that's what we're parsing
  31.     my $args;
  32.     if (scalar (@_) == 1 && UNIVERSAL::isa($_[0], 'Data::Grove')) {
  33.     $args = { Source => { Grove => shift } };
  34.     } else {
  35.     $args = (scalar (@_) == 1) ? shift : { @_ };
  36.     }
  37.  
  38.     my $parse_options = { %$self, %$args };
  39.     $self->{ParseOptions} = $parse_options;
  40.  
  41.     # ensure that we have at least one source
  42.     if (!defined $parse_options->{Source}
  43.     || !(defined $parse_options->{Source}{Grove})) {
  44.     die "XML::Grove::PerlSAX: no source defined for parse\n";
  45.     }
  46.  
  47.     # assign default Handler to any undefined handlers
  48.     if (defined $parse_options->{Handler}) {
  49.     $parse_options->{DocumentHandler} = $parse_options->{Handler}
  50.         if (!defined $parse_options->{DocumentHandler});
  51.     }
  52.  
  53.     # ensure that we have a DocumentHandler
  54.     if (!defined $parse_options->{DocumentHandler}) {
  55.     die "XML::Grove::PerlSAX: no Handler or DocumentHandler defined for parse\n";
  56.     }
  57.  
  58.     # cache DocumentHandler in self for callbacks
  59.     $self->{DocumentHandler} = $parse_options->{DocumentHandler};
  60.  
  61.     if (ref($self->{Source}{Grove}) !~ /Document/) {
  62.     $self->{DocumentHandler}->start_document( { } );
  63.     $parse_options->{Source}{Grove}->accept($self);
  64.     return $self->{DocumentHandler}->end_document( { } );
  65.     } else {
  66.     $self->{Source}{Grove}->accept($self);
  67.     }
  68.  
  69.     # clean up parser instance
  70.     delete $self->{ParseOptions};
  71.     delete $self->{DocumentHandler};
  72. }
  73.  
  74. sub _parse_self {
  75.     my $grove = shift;
  76.     my $self = ($#_ == 0) ? shift : { @_ };
  77.  
  78.     bless $self, 'XML::Grove::PerlSAX';
  79.  
  80.     if (ref($grove) !~ /Document/) {
  81.     $self->{DocumentHandler}->start_document( { } );
  82.     $grove->accept($self);
  83.     return $self->{DocumentHandler}->end_document( { } );
  84.     } else {
  85.     return $grove->accept($self);
  86.     }
  87. }
  88.  
  89. sub visit_document {
  90.     my $self = shift; my $grove = shift;
  91.  
  92.     $self->{DocumentHandler}->start_document($grove);
  93.     $grove->children_accept($self);
  94.     return $self->{DocumentHandler}->end_document($grove);
  95. }
  96.  
  97. sub visit_element {
  98.     my $self = shift; my $element = shift;
  99.  
  100.     $self->{DocumentHandler}->start_element($element);
  101.     $element->children_accept($self);
  102.     return $self->{DocumentHandler}->end_element($element);
  103. }
  104.  
  105. sub visit_entity {
  106.     my $self = shift; my $entity = shift;
  107.  
  108.     return $self->{DocumentHandler}->int_entity($entity);
  109. }
  110.  
  111. sub visit_pi {
  112.     my $self = shift; my $pi = shift;
  113.  
  114.     return $self->{DocumentHandler}->processing_instruction($pi);
  115. }
  116.  
  117. sub visit_comment {
  118.     my $self = shift; my $comment = shift;
  119.  
  120.     return $self->{DocumentHandler}->comment($comment);
  121. }
  122.  
  123. sub visit_characters {
  124.     my $self = shift; my $characters = shift;
  125.  
  126.     return $self->{DocumentHandler}->characters($characters);
  127. }
  128.  
  129. package XML::Grove::Document;
  130.  
  131. sub parse {
  132.     goto &XML::Grove::PerlSAX::_parse_self;
  133. }
  134.  
  135. package XML::Grove::Element;
  136.  
  137. sub parse {
  138.     goto &XML::Grove::PerlSAX::_parse_self;
  139. }
  140.  
  141. 1;
  142.  
  143. __END__
  144.  
  145. =head1 NAME
  146.  
  147. XML::Grove::PerlSAX - an PerlSAX event interface for XML objects
  148.  
  149. =head1 SYNOPSIS
  150.  
  151.  use XML::Grove::PerlSAX;
  152.  
  153.  $parser = XML::Grove::PerlSAX->new( [OPTIONS] );
  154.  $result = $parser->parse( [OPTIONS] );
  155.  
  156.  # or
  157.  $result = $xml_object->parse( [OPTIONS] );
  158.  
  159. =head1 DESCRIPTION
  160.  
  161. C<XML::Grove::PerlSAX> is a PerlSAX parser that generates PerlSAX
  162. events from XML::Grove objects.  This man page summarizes the specific
  163. options, handlers, and properties supported by C<XML::Grove::PerlSAX>;
  164. please refer to the PerlSAX standard in `C<PerlSAX.pod>' for general
  165. usage information.
  166.  
  167. =head1 METHODS
  168.  
  169. =over 4
  170.  
  171. =item new
  172.  
  173. Creates a new parser object.  Default options for parsing, described
  174. below, are passed as key-value pairs or as a single hash.  Options may
  175. be changed directly in the parser object unless stated otherwise.
  176. Options passed to `C<parse()>' override the default options in the
  177. parser object for the duration of the parse.
  178.  
  179. =item parse
  180.  
  181. Parses a document.  Options, described below, are passed as key-value
  182. pairs or as a single hash.  Options passed to `C<parse()>' override
  183. default options in the parser object.
  184.  
  185. =back
  186.  
  187. =head1 OPTIONS
  188.  
  189. The following options are supported by C<XML::Grove::PerlSAX>:
  190.  
  191.  Handler          default handler to receive events
  192.  DocumentHandler  handler to receive document events
  193.  Source           hash containing the input source for parsing
  194.  
  195. If no handlers are provided then all events will be silently ignored.
  196.  
  197. If a single grove argument is passed to the `C<parse()>' method, it is
  198. treated as if a `C<Source>' option was given with a `C<Grove>'
  199. parameter.
  200.  
  201. The `C<Source>' hash may contain the following parameters:
  202.  
  203.  Grove            The grove object used to generate parse events..
  204.  
  205. =head1 HANDLERS
  206.  
  207. The following events are generated by C<XML::Grove::PerlSAX>.
  208. XML::Grove::PerlSAX passes the corresponding grove object as it's
  209. parameter so the properties passed to the handler are those that were
  210. used to create or were assigned to the grove.  Please see the docs for
  211. the parser used to create the grove for a list of properties that were
  212. provided.
  213.  
  214. =head2 DocumentHandler methods
  215.  
  216. =over 4
  217.  
  218. =item start_document
  219.  
  220. Receive notification of the beginning of a document.  This is called
  221. from the XML::Grove::Document object before processing any document
  222. content.
  223.  
  224. =item end_document
  225.  
  226. Receive notification of the end of a document.  This is called from
  227. the XML::Grove::Document object after processing all document content.
  228.  
  229. =item start_element
  230.  
  231. Receive notification of the beginning of an element.  This is called
  232. from the XML::Grove::Element object before processing any element
  233. content.
  234.  
  235. =item end_element
  236.  
  237. Receive notification of the end of an element.  This is called from
  238. the XML::Grove::Element object after processing all element content.
  239.  
  240. =item characters
  241.  
  242. Receive notification of character data.  This is called from the
  243. XML::Grove::Characters object.
  244.  
  245. =item processing_instruction
  246.  
  247. Receive notification of a processing instruction.  This is called from
  248. the XML::Grove::PI object.
  249.  
  250. =item comment
  251.  
  252. Receive notification of a comment.  This is called from the
  253. XML::Grove::Comment object.
  254.  
  255. =back
  256.  
  257. =head1 AUTHOR
  258.  
  259. Ken MacLeod, ken@bitsko.slc.ut.us
  260.  
  261. =head1 SEE ALSO
  262.  
  263. perl(1), XML::Grove(3)
  264.  
  265. Extensible Markup Language (XML) <http://www.w3c.org/XML>
  266.  
  267. =cut
  268.