Mercurial > pub > Yours
changeset 4:8001dc056331
fixed problems with sax parser
fixed handling of empty repos
added log autoflush
author | sergey |
---|---|
date | Wed, 23 Oct 2013 11:49:16 +0400 |
parents | ae61af01bfa5 |
children | 45a84be3ebb1 |
files | _test/repo.pl _test/test_xml.pl lib/Yours/Parsers/PMDParser.pm lib/Yours/Parsers/SaxParser.pm lib/Yours/SyncRepository.pm |
diffstat | 5 files changed, 44 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/_test/repo.pl Wed Oct 23 01:13:19 2013 +0400 +++ b/_test/repo.pl Wed Oct 23 11:49:16 2013 +0400 @@ -23,6 +23,11 @@ name => 'nvidia', dir => 'nvidia', location => 'ftp://download.nvidia.com/opensuse/12.3/' + }, + { + name => 'broken', + dir => 'broken', + location => 'http://mirror.yandex.ru/opensuse/repositories/KDE:/Extra/KDE_Release_410_openSUSE_12.3/' } );
--- a/_test/test_xml.pl Wed Oct 23 01:13:19 2013 +0400 +++ b/_test/test_xml.pl Wed Oct 23 11:49:16 2013 +0400 @@ -2,16 +2,20 @@ package MyParser; use IMPL::declare { + require => { + XMLReader => 'XML::LibXML::Reader' + }, base => [ 'Yours::Parsers::SaxParser' => undef ] }; + sub ProcessRootNode { my ($this,$node) = @_; my $handler; - my $level = 0; + my $level = 1; $handler = sub { my ($me,$node) = @_; print ' ' x $level, $node->depth, " ", $node->name,"\n"; @@ -25,16 +29,27 @@ } my $text = '<?xml version="1.0"?> -<root> - <n1></n1> +<root xmlns:ns="asd"> + <n0/> + <ns:n1 a="hi"></ns:n1> <n2> <w1>asd</w1> </n2> - <n3></n3> + <n3 /> </root> '; -MyParser->new()->Parse({ +my $reader = XMLReader->new({ string => $text }); +while ($reader->read) { + print " " x $reader->depth, "[", $reader->nodeType,"] ", $reader->name, "\n" +} + +print "\n"; + +MyParser->new()->Parse({ + string => $text +}); +
--- a/lib/Yours/Parsers/PMDParser.pm Wed Oct 23 01:13:19 2013 +0400 +++ b/lib/Yours/Parsers/PMDParser.pm Wed Oct 23 11:49:16 2013 +0400 @@ -22,6 +22,9 @@ $this->ReadChildren(sub { my ($me,$reader) = @_; + + return unless $reader->localName eq 'package'; + my $type = $me->attribute('type'); my $package = $me->ReadComplexNode({
--- a/lib/Yours/Parsers/SaxParser.pm Wed Oct 23 01:13:19 2013 +0400 +++ b/lib/Yours/Parsers/SaxParser.pm Wed Oct 23 11:49:16 2013 +0400 @@ -21,9 +21,9 @@ my $reader = $this->_reader( XMLReader->new($options) ); - $reader->read(); - $this->ProcessRootNode($reader); - $reader->read(); + if ( $reader->read() > 0) { + $this->ProcessRootNode($reader); + } } sub ProcessRootNode { @@ -34,19 +34,18 @@ my ( $this, $handler ) = @_; my $reader = $this->_reader; - - return if $reader->isEmptyElement; - - my $currentLevel = $reader->depth+1; - - while ( - $reader->read - ) - { - print "---\n" and return if $reader->depth <= $currentLevel && $reader->nodeType == XML_READER_TYPE_END_ELEMENT; - #if ($reader->nodeType != XML_READER_TYPE_END_ELEMENT) { - $this->$handler($reader) if $handler; - #} + + # содержимое можеть быть только у не пустых элементов + if($reader->nodeType == XML_READER_TYPE_ELEMENT && !$reader->isEmptyElement) { + # нужно прочитать все, что ниже, для этого запоминаем текущий уровень + my $currentLevel = $reader->depth; + + # при чтении и проверке данного условия "съедается" закрывающий теэг текущего узла + while($reader->read && $reader->depth > $currentLevel) { + # при обходе дочерних узлов нужно пропустить закрывающие узлы + $this->$handler($reader) + if $handler and $reader->nodeType != XML_READER_TYPE_END_ELEMENT; + } } }