| 1 | # $Id: XMLParser.pm,v 1.16 2008/06/03 17:31:15 cmungall Exp $ | ||
| 2 | # | ||
| 3 | # Copyright (C) 2002 Chris Mungall <cjm@fruitfly.org> | ||
| 4 | # | ||
| 5 | # See also - http://stag.sourceforge.net | ||
| 6 | # | ||
| 7 | # This module is free software. | ||
| 8 | # You may distribute this module under the same terms as perl itself | ||
| 9 | |||
| 10 | package Data::Stag::XMLParser; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | XMLParser.pm - parses XML files into stag events | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | |||
| 23 | =head1 AUTHOR | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | use Exporter; | ||
| 28 | use Carp; | ||
| 29 | use FileHandle; | ||
| 30 | use strict; | ||
| 31 | use XML::Parser::PerlSAX; | ||
| 32 | use Data::Stag::Util qw(rearrange); | ||
| 33 | use base qw(Data::Stag::BaseGenerator Exporter); | ||
| 34 | |||
| 35 | use vars qw($VERSION); | ||
| 36 | $VERSION="0.14"; | ||
| 37 | |||
| 38 | sub fmtstr { | ||
| 39 | return 'xml'; | ||
| 40 | } | ||
| 41 | |||
| 42 | # OVERRIDE | ||
| 43 | sub parse { | ||
| 44 | my $self = shift; | ||
| 45 | my ($file, $str, $fh) = | ||
| 46 | rearrange([qw(file str fh)], @_); | ||
| 47 | if ($str) { | ||
| 48 | # problem with IO::String in perl5.6.1 | ||
| 49 | |||
| 50 | my $parser = XML::Parser::PerlSAX->new(); | ||
| 51 | my $source = {String => $str}; | ||
| 52 | my %parser_args = (Source => $source, | ||
| 53 | Handler => $self->handler); | ||
| 54 | |||
| 55 | $parser->parse(%parser_args); | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | $self->SUPER::parse(-file=>$file, -str=>$str, -fh=>$fh); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub parse_fh { | ||
| 63 | my $self = shift; | ||
| 64 | my $fh = shift; | ||
| 65 | my $parser = XML::Parser::PerlSAX->new(); | ||
| 66 | my $source = {ByteStream => $fh}; | ||
| 67 | my %parser_args = (Source => $source, | ||
| 68 | Handler => $self->handler); | ||
| 69 | $parser->parse(%parser_args); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | 1; |