[23774] | 1 | # $Id: Stream.pm,v 1.1 2003/07/27 16:07:49 matt Exp $
|
---|
| 2 |
|
---|
| 3 | package XML::Parser::Style::Stream;
|
---|
| 4 | use strict;
|
---|
| 5 |
|
---|
| 6 | # This style invented by Tim Bray <[email protected]>
|
---|
| 7 |
|
---|
| 8 | sub Init {
|
---|
| 9 | no strict 'refs';
|
---|
| 10 | my $expat = shift;
|
---|
| 11 | $expat->{Text} = '';
|
---|
| 12 | my $sub = $expat->{Pkg} ."::StartDocument";
|
---|
| 13 | &$sub($expat)
|
---|
| 14 | if defined(&$sub);
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | sub Start {
|
---|
| 18 | no strict 'refs';
|
---|
| 19 | my $expat = shift;
|
---|
| 20 | my $type = shift;
|
---|
| 21 |
|
---|
| 22 | doText($expat);
|
---|
| 23 | $_ = "<$type";
|
---|
| 24 |
|
---|
| 25 | %_ = @_;
|
---|
| 26 | while (@_) {
|
---|
| 27 | $_ .= ' ' . shift() . '="' . shift() . '"';
|
---|
| 28 | }
|
---|
| 29 | $_ .= '>';
|
---|
| 30 |
|
---|
| 31 | my $sub = $expat->{Pkg} . "::StartTag";
|
---|
| 32 | if (defined(&$sub)) {
|
---|
| 33 | &$sub($expat, $type);
|
---|
| 34 | } else {
|
---|
| 35 | print;
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | sub End {
|
---|
| 40 | no strict 'refs';
|
---|
| 41 | my $expat = shift;
|
---|
| 42 | my $type = shift;
|
---|
| 43 |
|
---|
| 44 | # Set right context for Text handler
|
---|
| 45 | push(@{$expat->{Context}}, $type);
|
---|
| 46 | doText($expat);
|
---|
| 47 | pop(@{$expat->{Context}});
|
---|
| 48 |
|
---|
| 49 | $_ = "</$type>";
|
---|
| 50 |
|
---|
| 51 | my $sub = $expat->{Pkg} . "::EndTag";
|
---|
| 52 | if (defined(&$sub)) {
|
---|
| 53 | &$sub($expat, $type);
|
---|
| 54 | } else {
|
---|
| 55 | print;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | sub Char {
|
---|
| 60 | my $expat = shift;
|
---|
| 61 | $expat->{Text} .= shift;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | sub Proc {
|
---|
| 65 | no strict 'refs';
|
---|
| 66 | my $expat = shift;
|
---|
| 67 | my $target = shift;
|
---|
| 68 | my $text = shift;
|
---|
| 69 |
|
---|
| 70 | doText($expat);
|
---|
| 71 |
|
---|
| 72 | $_ = "<?$target $text?>";
|
---|
| 73 |
|
---|
| 74 | my $sub = $expat->{Pkg} . "::PI";
|
---|
| 75 | if (defined(&$sub)) {
|
---|
| 76 | &$sub($expat, $target, $text);
|
---|
| 77 | } else {
|
---|
| 78 | print;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | sub Final {
|
---|
| 83 | no strict 'refs';
|
---|
| 84 | my $expat = shift;
|
---|
| 85 | my $sub = $expat->{Pkg} . "::EndDocument";
|
---|
| 86 | &$sub($expat)
|
---|
| 87 | if defined(&$sub);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | sub doText {
|
---|
| 91 | no strict 'refs';
|
---|
| 92 | my $expat = shift;
|
---|
| 93 | $_ = $expat->{Text};
|
---|
| 94 |
|
---|
| 95 | if (length($_)) {
|
---|
| 96 | my $sub = $expat->{Pkg} . "::Text";
|
---|
| 97 | if (defined(&$sub)) {
|
---|
| 98 | &$sub($expat);
|
---|
| 99 | } else {
|
---|
| 100 | print;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | $expat->{Text} = '';
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | 1;
|
---|
| 108 | __END__
|
---|
| 109 |
|
---|
| 110 | =head1 NAME
|
---|
| 111 |
|
---|
| 112 | XML::Parser::Style::Stream - Stream style for XML::Parser
|
---|
| 113 |
|
---|
| 114 | =head1 SYNOPSIS
|
---|
| 115 |
|
---|
| 116 | use XML::Parser;
|
---|
| 117 | my $p = XML::Parser->new(Style => 'Stream', Pkg => 'MySubs');
|
---|
| 118 | $p->parsefile('foo.xml');
|
---|
| 119 |
|
---|
| 120 | {
|
---|
| 121 | package MySubs;
|
---|
| 122 |
|
---|
| 123 | sub StartTag {
|
---|
| 124 | my ($e, $name) = @_;
|
---|
| 125 | # do something with start tags
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | sub EndTag {
|
---|
| 129 | my ($e, $name) = @_;
|
---|
| 130 | # do something with end tags
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | sub Characters {
|
---|
| 134 | my ($e, $data) = @_;
|
---|
| 135 | # do something with text nodes
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | =head1 DESCRIPTION
|
---|
| 140 |
|
---|
| 141 | This style uses the Pkg option to find subs in a given package to call for each event.
|
---|
| 142 | If none of the subs that this
|
---|
| 143 | style looks for is there, then the effect of parsing with this style is
|
---|
| 144 | to print a canonical copy of the document without comments or declarations.
|
---|
| 145 | All the subs receive as their 1st parameter the Expat instance for the
|
---|
| 146 | document they're parsing.
|
---|
| 147 |
|
---|
| 148 | It looks for the following routines:
|
---|
| 149 |
|
---|
| 150 | =over 4
|
---|
| 151 |
|
---|
| 152 | =item * StartDocument
|
---|
| 153 |
|
---|
| 154 | Called at the start of the parse .
|
---|
| 155 |
|
---|
| 156 | =item * StartTag
|
---|
| 157 |
|
---|
| 158 | Called for every start tag with a second parameter of the element type. The $_
|
---|
| 159 | variable will contain a copy of the tag and the %_ variable will contain
|
---|
| 160 | attribute values supplied for that element.
|
---|
| 161 |
|
---|
| 162 | =item * EndTag
|
---|
| 163 |
|
---|
| 164 | Called for every end tag with a second parameter of the element type. The $_
|
---|
| 165 | variable will contain a copy of the end tag.
|
---|
| 166 |
|
---|
| 167 | =item * Text
|
---|
| 168 |
|
---|
| 169 | Called just before start or end tags with accumulated non-markup text in
|
---|
| 170 | the $_ variable.
|
---|
| 171 |
|
---|
| 172 | =item * PI
|
---|
| 173 |
|
---|
| 174 | Called for processing instructions. The $_ variable will contain a copy of
|
---|
| 175 | the PI and the target and data are sent as 2nd and 3rd parameters
|
---|
| 176 | respectively.
|
---|
| 177 |
|
---|
| 178 | =item * EndDocument
|
---|
| 179 |
|
---|
| 180 | Called at conclusion of the parse.
|
---|
| 181 |
|
---|
| 182 | =back
|
---|
| 183 |
|
---|
| 184 | =cut |
---|