| 1 | use 5.008; # utf8 | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | package IO::Async::XMLStream::SAXReader; | ||
| 7 | $IO::Async::XMLStream::SAXReader::VERSION = '0.001000'; | ||
| 8 | # ABSTRACT: Dispatch SAX events from an XML stream. | ||
| 9 | |||
| 10 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | ||
| 11 | |||
| 12 | use parent 'IO::Async::Stream'; | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | use XML::LibXML::SAX::ChunkParser 0.00007; # Buggy Finish | ||
| 67 | use IO::Async::XMLStream::SAXReader::DuckHandler; | ||
| 68 | |||
| 69 | ## no critic (NamingConventions) | ||
| 70 | sub _SAXReader { | ||
| 71 | my ($self) = @_; | ||
| 72 | my $key = 'SAXReader'; | ||
| 73 | return $self->{$key} if exists $self->{$key}; | ||
| 74 | $self->{$key} = {}; | ||
| 75 | $self->{$key}->{Parser} = XML::LibXML::SAX::ChunkParser->new( Handler => $self->{sax_handler} ); | ||
| 76 | return $self->{$key}; | ||
| 77 | } | ||
| 78 | ## use critic | ||
| 79 | |||
| 80 | my @XML_METHODS = qw( | ||
| 81 | attlist_decl | ||
| 82 | attribute_decl | ||
| 83 | characters | ||
| 84 | comment | ||
| 85 | doctype_decl | ||
| 86 | element_decl | ||
| 87 | end_cdata | ||
| 88 | end_document | ||
| 89 | end_dtd | ||
| 90 | end_element | ||
| 91 | end_entity | ||
| 92 | end_prefix_mapping | ||
| 93 | entity_decl | ||
| 94 | entity_reference | ||
| 95 | error | ||
| 96 | external_entity_decl | ||
| 97 | fatal_error | ||
| 98 | ignorable_whitespace | ||
| 99 | internal_entity_decl | ||
| 100 | notation_decl | ||
| 101 | processing_instruction | ||
| 102 | resolve_entity | ||
| 103 | set_document_locator | ||
| 104 | skipped_entity | ||
| 105 | start_cdata | ||
| 106 | start_document | ||
| 107 | start_dtd | ||
| 108 | start_element | ||
| 109 | start_entity | ||
| 110 | start_prefix_mapping | ||
| 111 | unparsed_entity_decl | ||
| 112 | warning | ||
| 113 | xml_decl | ||
| 114 | ); | ||
| 115 | |||
| 116 | sub configure { | ||
| 117 | my ( $self, %params ) = @_; | ||
| 118 | |||
| 119 | for my $method ('sax_handler') { | ||
| 120 | next unless exists $params{$method}; | ||
| 121 | $self->{$method} = delete $params{$method}; | ||
| 122 | } | ||
| 123 | |||
| 124 | if ( not $self->{'sax_handler'} ) { | ||
| 125 | $self->{'sax_handler'} = IO::Async::XMLStream::SAXReader::DuckHandler->new( { SAXReader => $self, }, ); | ||
| 126 | for my $method (@XML_METHODS) { | ||
| 127 | next unless exists $params{ 'on_' . $method }; | ||
| 128 | $self->{ 'on_' . $method } = delete $params{ 'on_' . $method }; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | $self->_SAXReader; | ||
| 132 | return $self->SUPER::configure(%params); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub on_read { | ||
| 136 | my ( $self, $buffref, $eof ) = @_; | ||
| 137 | my $text = substr ${$buffref}, 0, length ${$buffref}, q[]; | ||
| 138 | |||
| 139 | $self->_SAXReader->{Parser}->parse_chunk($text) if length $text; | ||
| 140 | if ($eof) { | ||
| 141 | $self->_SAXReader->{Parser}->finish; | ||
| 142 | return 0; | ||
| 143 | } | ||
| 144 | return 1; | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | |||
| 149 | __END__ |