XML-Parser-Lite-Tree-0.14/0000755000000000000000000000000011572311137013617 5ustar rootrootXML-Parser-Lite-Tree-0.14/t/0000755000000000000000000000000011572311137014062 5ustar rootrootXML-Parser-Lite-Tree-0.14/t/03_comments.t0000644000000000000000000000171611572310041016374 0ustar rootrootuse Test::More tests => 9; use XML::Parser::Lite::Tree; # # test comment nodes # my $parser = new XML::Parser::Lite::Tree(skip_white => 1); my $tree = $parser->parse(q~ ~); is(&get_node($tree, '0' )->{type}, 'element'); is(&get_node($tree, '0/0')->{type}, 'element'); is(&get_node($tree, '0/1')->{type}, 'comment'); is(&get_node($tree, '0/2')->{type}, 'element'); is(&get_node($tree, '0' )->{name}, 'foo'); is(&get_node($tree, '0/0')->{name}, 'woo'); is(&get_node($tree, '0/1')->{name}, undef); is(&get_node($tree, '0/2')->{name}, 'hoopla'); is(&get_node($tree, '0/1')->{content}, ' yay '); # # a super-simple xpath-like function for finding a single given child # sub get_node { my ($tree, $path) = @_; my $node = $tree; if (length $path){ my @refs = split /\//, $path; for my $ref (@refs){ $node = $node->{children}->[$ref]; } } return $node; } XML-Parser-Lite-Tree-0.14/t/05_doctypes.t0000644000000000000000000000155711572310041016406 0ustar rootrootuse Test::More tests => 5; use XML::Parser::Lite::Tree; # # test processing instructions # my $parser = new XML::Parser::Lite::Tree(skip_white => 1); my $tree = $parser->parse(q~ ~); is(&get_node($tree, '0' )->{type}, 'dtd'); is(&get_node($tree, '1' )->{type}, 'element'); is(&get_node($tree, '1/0')->{type}, 'element'); is(&get_node($tree, '0' )->{name}, 'html'); like(&get_node($tree, '0')->{content}, qr/^PUBLIC/); # # a super-simple xpath-like function for finding a single given child # sub get_node { my ($tree, $path) = @_; my $node = $tree; if (length $path){ my @refs = split /\//, $path; for my $ref (@refs){ $node = $node->{children}->[$ref]; } } return $node; } XML-Parser-Lite-Tree-0.14/t/02_options.t0000644000000000000000000000573011572310041016241 0ustar rootrootuse Test::More tests => 36; use XML::Parser::Lite::Tree; # # test the whitespace folding # my $parser = new XML::Parser::Lite::Tree(skip_white => 1); my $tree = $parser->parse(" woo "); is(scalar @{&get_node($tree, '' )->{children}}, 1, "one child of the root node"); is(scalar @{&get_node($tree, '0' )->{children}}, 1, "one child, level 2"); is(scalar @{&get_node($tree, '0/0' )->{children}}, 1, "one child, level 3"); is(scalar @{&get_node($tree, '0/0/0')->{children}}, 1, "one child, level 4"); is(&get_node($tree, '0' )->{type}, 'element'); is(&get_node($tree, '0/0' )->{type}, 'element'); is(&get_node($tree, '0/0/0' )->{type}, 'element'); is(&get_node($tree, '0/0/0/0')->{type}, 'text'); is(&get_node($tree, '0' )->{name}, 'foo'); is(&get_node($tree, '0/0' )->{name}, 'bar'); is(&get_node($tree, '0/0/0' )->{name}, 'baz'); is(&get_node($tree, '0/0/0/0')->{content}, 'woo'); # # test the namespace parsing # my $xml = q~ ~; $parser = new XML::Parser::Lite::Tree(process_ns => 1, skip_white => 1); $tree = $parser->parse($xml); is(&get_node($tree, '0' )->{ns}, 'urn:default'); is(&get_node($tree, '0/0' )->{ns}, 'urn:default'); is(&get_node($tree, '0/1' )->{ns}, 'urn:foo'); is(&get_node($tree, '0/1/0')->{ns}, 'urn:override'); is(&get_node($tree, '0' )->{name}, 'aaa'); is(&get_node($tree, '0/0' )->{name}, 'bbb'); is(&get_node($tree, '0/1' )->{name}, 'foo:ccc'); is(&get_node($tree, '0/1/0')->{name}, 'ddd'); is(&get_node($tree, '0' )->{local_name}, 'aaa'); is(&get_node($tree, '0/0' )->{local_name}, 'bbb'); is(&get_node($tree, '0/1' )->{local_name}, 'ccc'); is(&get_node($tree, '0/1/0')->{local_name}, 'ddd'); is(&get_node($tree, '0' )->{namespaces}->{__default__}, 'urn:default'); is(&get_node($tree, '0/0' )->{namespaces}->{__default__}, 'urn:default'); is(&get_node($tree, '0/1' )->{namespaces}->{__default__}, 'urn:override'); is(&get_node($tree, '0/1/0')->{namespaces}->{__default__}, 'urn:override'); is(&get_node($tree, '0' )->{namespaces}->{foo}, 'urn:foo'); is(&get_node($tree, '0/0' )->{namespaces}->{foo}, 'urn:foo'); is(&get_node($tree, '0/1' )->{namespaces}->{foo}, 'urn:foo'); is(&get_node($tree, '0/1/0')->{namespaces}->{foo}, 'urn:foo'); is(&get_node($tree, '0 ')->{namespaces}->{bar}, undef); is(&get_node($tree, '0/0 ')->{namespaces}->{bar}, undef); is(&get_node($tree, '0/1 ')->{namespaces}->{bar}, undef); is(&get_node($tree, '0/1/0')->{namespaces}->{bar}, 'urn:bar'); # # a super-simple xpath-like function for finding a single given child # sub get_node { my ($tree, $path) = @_; my $node = $tree; if (length $path){ my @refs = split /\//, $path; for my $ref (@refs){ $node = $node->{children}->[$ref]; } } return $node; } XML-Parser-Lite-Tree-0.14/t/06_attributes.t0000644000000000000000000000236211572310041016736 0ustar rootrootuse Test::More tests => 11; # # this tests a bug present in 0.10 on perl 5.8.9 only which # caused attributes to be copied from one node into the next # use XML::Parser::Lite::Tree; my $x = XML::Parser::Lite::Tree->instance(); my $tree = $x->parse(''); # has a root element called 'foo' with 3 children is($tree->{children}->[0]->{name}, "foo"); is(scalar @{$tree->{children}->[0]->{children}}, 3); # children are called bar, bar, baz is($tree->{children}->[0]->{children}->[0]->{name}, "bar"); is($tree->{children}->[0]->{children}->[1]->{name}, "bar"); is($tree->{children}->[0]->{children}->[2]->{name}, "baz"); # first child has a single attribute (id="a") is(scalar keys %{$tree->{children}->[0]->{children}->[0]->{attributes}}, 1); is($tree->{children}->[0]->{children}->[0]->{attributes}->{id}, "a"); # second child has 2 attributes (a=1, b=2) is(scalar keys %{$tree->{children}->[0]->{children}->[1]->{attributes}}, 2); is($tree->{children}->[0]->{children}->[1]->{attributes}->{a}, "1"); is($tree->{children}->[0]->{children}->[1]->{attributes}->{b}, "2"); # third child has no attributes is(scalar keys %{$tree->{children}->[0]->{children}->[2]->{attributes}}, 0); XML-Parser-Lite-Tree-0.14/t/01_basic.t0000644000000000000000000000250111572310041015617 0ustar rootrootuse Test::More tests => 15; use XML::Parser::Lite::Tree; my $x = XML::Parser::Lite::Tree->instance(); ok( defined($x), "instance() returns something" ); ok( ref $x eq 'XML::Parser::Lite::Tree', "instance returns the right object" ); my $tree = $x->parse('woohoopla'); ok( defined($tree), "parse() returns something" ); ok( scalar @{$tree->{children}} == 1, "tree root contains a single root node" ); my $root_node = pop @{$tree->{children}}; ok( $root_node->{type} eq 'element', "root node is an element" ); ok( $root_node->{name} eq 'foo', "root node has correct name" ); ok( scalar keys %{$root_node->{attributes}} == 1, "correct attribute count" ); ok( $root_node->{attributes}->{bar} eq 'baz', "correct attribute name and value" ); ok( scalar @{$root_node->{children}} == 3, "correct child count" ); ok( $root_node->{children}->[0]->{type} eq 'text', "child 1 type correct" ); ok( $root_node->{children}->[0]->{content} eq 'woo', "child 1 content correct" ); ok( $root_node->{children}->[1]->{type} eq 'element', "child 2 type correct" ); ok( $root_node->{children}->[1]->{name} eq 'yay', "child 2 name correct" ); ok( $root_node->{children}->[2]->{type} eq 'text', "child 3 type correct" ); ok( $root_node->{children}->[2]->{content} eq 'hoopla', "child 3 content correct" ); XML-Parser-Lite-Tree-0.14/t/08_cdata.t0000644000000000000000000000124211572310041015622 0ustar rootrootuse Test::More tests => 4; use XML::Parser::Lite::Tree; my $x = XML::Parser::Lite::Tree->instance(); my $tree = $x->parse(''); is(&get_node($tree, '0')->{type}, 'element'); is(&get_node($tree, '0')->{name}, 'foo'); is(&get_node($tree, '0/0')->{type}, 'cdata'); is(&get_node($tree, '0/0')->{content}, 'MethodUndefined'); # # a super-simple xpath-like function for finding a single given child # sub get_node { my ($tree, $path) = @_; my $node = $tree; if (length $path){ my @refs = split /\//, $path; for my $ref (@refs){ $node = $node->{children}->[$ref]; } } return $node; } XML-Parser-Lite-Tree-0.14/t/00_xmlparserlite.t0000644000000000000000000001141011572310041017427 0ustar rootrootuse Test::More tests => 81; use XML::Parser::LiteCopy; use Data::Dumper; my($s, $c, $e, $a); # # start, char, end # ($s, $c, $e) = (0) x 3; my $p1 = XML::Parser::LiteCopy->new(); $p1->setHandlers( Start => sub { $s++; }, Char => sub { $c++; }, End => sub { $e++; }, ); $p1->parse('Hello World!'); is($s, 1); is($c, 1); is($e, 1); # # attributes from start event # ($s, $c, $e) = (0) x 3; my %foo; my $p2 = new XML::Parser::LiteCopy Handlers => { Start => sub { shift; $s++; %foo = @_[1..$#_] if $_[0] eq 'foo'; }, Char => sub { $c++; }, End => sub { $e++; }, } ; $p2->parse('Hello cruel World!'); is($s, 3); is($c, 4); is($e, 3); is($foo{id}, 'me'); ok(defined $foo{root}); is($foo{root}, '0'); ok(defined $foo{empty}); is($foo{empty}, ''); # # Char & CDATA # sub test_chars { my @chars; my $p = new XML::Parser::LiteCopy Handlers => { Char => sub { push @chars, $_[1]; }, CData => sub { push @chars, 'CDATA:'.$_[1]; }, } ; my $in = shift; $p->parse($in); is(scalar @chars, scalar @_); is_deeply(\@chars, \@_); } &test_chars('', ()); &test_chars('', ()); &test_chars('hey', ('hey')); &test_chars('hey<', ('hey<')); &test_chars('&hey', ('&hey')); &test_chars('', ('CDATA:yo')); &test_chars('', ('CDATA: yo ')); &test_chars('', ('CDATA:foo]bar')); &test_chars('', ('CDATA:foo]]bar')); &test_chars('bar]]>', ('CDATA:foo]>bar')); &test_chars('', ('CDATA:foo]')); &test_chars('wooyay', ('woo','CDATA:foo','CDATA:bar','yay')); # # comments # sub test_comments { my @comments; my $p = new XML::Parser::LiteCopy Handlers => { Comment => sub { push @comments, $_[1]; }, } ; my $in = shift; $p->parse($in); is(scalar @comments, scalar @_); is_deeply(\@comments, \@_); } # >>> A note about comments: # An XML comment opens with a "" delimiter. An explicitly stated exception is that a double # hyphen is not permitted within the body of a comment. This rule ensures that unterminated # comments are detected if a new comment opening delimiter is encountered. There is an # additional restriction that comments cannot be terminated with the "--->" sequence, that is, # that the body of the comment cannot terminate with a hyphen &test_comments('', ()); &test_comments('', ('a')); &test_comments('', (' b ')); &test_comments('', (' c-d ')); &test_comments('', (' e- ')); &test_comments('', (' - ')); &test_comments('', ('fg','h')); &test_comments('', ('i-j')); # # processing instructions (PI) # sub test_pi { my @instructions; my $p = new XML::Parser::LiteCopy Handlers => { PI => sub { push @instructions, $_[1]; }, } ; my $in = shift; $p->parse($in); is(scalar @instructions, scalar @_); is_deeply(\@instructions, \@_); } &test_pi('', ()); &test_pi('', ('name pidata')); &test_pi('', ('xml version="1.0"? encoding="UTF-8"')); &test_pi(qq||, (qq|php\nexit;\n|)); &test_pi('', ('yay woo?')); # technically allowed... # # error conditions # sub test_error { my @errors; my $p = new XML::Parser::LiteCopy Handlers => { Error => sub { push @errors, $_[1]; }, }, ReturnErrors => 1 ; my $in = shift; $p->parse($in); # first test method gets a list of errors from the Error() event handler is(scalar @errors, scalar @_); for my $i(0..scalar @_-1){ like($errors[$i], $_[$i]); } # and then we check it dies correctly my $p2 = new XML::Parser::LiteCopy; eval { $p2->parse($in); }; like($@, $_[0]); } &test_error('fooHello World!', qr/^junk .+ before/); &test_error('Hello World!bar', qr/^junk .+ after/); &test_error('Hello World!', qr/^not properly closed tag 'foo'/); &test_error('Hello World!', qr/^mismatched tag 'foo'/, qr/^mismatched tag 'bar'/); &test_error('Hello World!', qr/^multiple roots, wrong element 'bar'/, qr/^unexpected closing tag 'bar'/); &test_error(' ', qr/^no element found/); # TODO tests # check for unclosed PI: $p2->parse(''); # check for unclosed CDATA # check for bad doctype # check for bad comments (various kinds) XML-Parser-Lite-Tree-0.14/t/07_stack.t0000644000000000000000000000101611572310041015651 0ustar rootrootuse Test::More tests => 2; # # this tests a bug present in 0.09 on perl 5.10 only which # cuased the parse stack to become undefined during the # parse. # use XML::Parser::Lite::Tree; my $x = XML::Parser::Lite::Tree->instance(); my $tree = $x->parse(''); # has a root element called 'aaa' with 5 children is($tree->{children}->[0]->{name}, "aaa"); is(scalar @{$tree->{children}->[0]->{children}}, 5); XML-Parser-Lite-Tree-0.14/t/04_processing_instructions.t0000644000000000000000000000204011572310041021537 0ustar rootrootuse Test::More tests => 9; use XML::Parser::Lite::Tree; # # test processing instructions # my $parser = new XML::Parser::Lite::Tree(skip_white => 1); my $tree = $parser->parse(q~ ~); is(&get_node($tree, '0' )->{type}, 'pi'); is(&get_node($tree, '1' )->{type}, 'element'); is(&get_node($tree, '1/0')->{type}, 'element'); is(&get_node($tree, '1/1')->{type}, 'element'); is(&get_node($tree, '1/2')->{type}, 'pi'); is(&get_node($tree, '0' )->{target}, 'xml'); is(&get_node($tree, '1/2')->{target}, 'php'); like(&get_node($tree, '0' )->{content}, qr/^version/); like(&get_node($tree, '1/2')->{content}, qr/^echo/); # # a super-simple xpath-like function for finding a single given child # sub get_node { my ($tree, $path) = @_; my $node = $tree; if (length $path){ my @refs = split /\//, $path; for my $ref (@refs){ $node = $node->{children}->[$ref]; } } return $node; } XML-Parser-Lite-Tree-0.14/lib/0000755000000000000000000000000011572311137014365 5ustar rootrootXML-Parser-Lite-Tree-0.14/lib/XML/0000755000000000000000000000000011572311137015025 5ustar rootrootXML-Parser-Lite-Tree-0.14/lib/XML/Parser/0000755000000000000000000000000011572311137016261 5ustar rootrootXML-Parser-Lite-Tree-0.14/lib/XML/Parser/LiteCopy.pm0000644000000000000000000002470211572310160020347 0ustar rootroot# NOTE: This module originally came from SOAP::Lite, which you probably # don't have. It was first repackaged here just to avoid the huge # dependancy tree, but this version has several features (CDATA # support, better PI and Comment support) that have been added. # # Copyright (C) 2000-2007 Paul Kulchenko (paulclinger@yahoo.com) # Copyright (C) 2008 Martin Kutter (martin.kutter@fen-net.de) # Copyright (C) 2009-2011 Cal Henderson (cal@iamcal.com) # # SOAP::Lite is free software; you can redistribute it # and/or modify it under the same terms as Perl itself. # package XML::Parser::LiteCopy; use strict; use vars qw($VERSION); $VERSION = '0.720.00'; my $ReturnErrors = 0; sub new { my $class = shift; return $class if ref $class; my $self = bless {} => $class; my %parameters = @_; $self->setHandlers(); # clear first $self->setHandlers(%{$parameters{Handlers} || {}}); $ReturnErrors = $parameters{ReturnErrors} || 0; return $self; } sub setHandlers { my $self = shift; # allow symbolic refs, avoid "subroutine redefined" warnings no strict 'refs'; local $^W; # clear all handlers if called without parameters if (not @_) { for (qw(Start End Char Final Init CData Comment Doctype PI Error)) { *$_ = sub {} } } # we could use each here, too... while (@_) { my($name, $func) = splice(@_, 0, 2); *$name = defined $func ? $func : sub {} } return $self; } sub _regexp { my $patch = shift || ''; my $package = __PACKAGE__; # This parser is based on "shallow parser" http://www.cs.sfu.ca/~cameron/REX.html # Robert D. Cameron "REX: XML Shallow Parsing with Regular Expressions", # Technical Report TR 1998-17, School of Computing Science, Simon Fraser University, November, 1998. # Copyright (c) 1998, Robert D. Cameron. # The following code may be freely used and distributed provided that # this copyright and citation notice remains intact and that modifications # or additions are clearly identified. use re 'eval'; my $TextSE = "[^<]+"; # the following backrefs have been added: # 1 : TextSE # 2 : MarkupSPE / DeclCE / CommentCE # 3 : MarkupSPE / DeclCE / CDATA_CE # 4 : MarkupSPE / DeclCE / DocTypeCE # 5 : MarkupSPE / PI_CE # 6 : MarkupSPE / EndTagCE # 7+: MarkupSPE / ElemTagCE my $Until2Hyphens = "(?:[^-]*)-(?:[^-]+-)*-"; my $CommentCE = "($Until2Hyphens)(?{${package}::comment(\$2)})>?"; my $UntilRSBs = "[^\\]]*](?:[^\\]]+])*]+"; my $CDATA_CE = "($UntilRSBs(?:[^\\]>]$UntilRSBs)*)(?{${package}::cdata(\$3)})>"; my $S = "[ \\n\\t\\r]+"; my $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]"; my $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]"; my $Name = "(?:$NameStrt)(?:$NameChar)*"; my $QuoteSE = "\"[^\"]*\"|'[^']*'"; my $DT_IdentSE = "$Name(?:$S(?:$Name|$QuoteSE))*"; my $MarkupDeclCE = "(?:[^\\]\"'><]+|$QuoteSE)*>"; my $S1 = "[\\n\\r\\t ]"; my $UntilQMs = "[^?]*\\?+"; my $PI_Tail = "\\?|$S1$UntilQMs(?:[^>?]$UntilQMs)*"; my $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail>))|%$Name;|$S"; my $DocTypeCE = "$S($DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?)>(?{${package}::_doctype(\$4)})"; my $DeclCE = "--(?:$CommentCE)?|\\[CDATA\\[(?:$CDATA_CE)?|DOCTYPE(?:$DocTypeCE)?"; my $PI_CE = "($Name(?:$PI_Tail))>(?{${package}::_pi(\$5); undef})"; # these expressions were modified for backtracking and events my $EndTagCE = "($Name)(?{${package}::_end(\$6); undef})(?:$S)?>"; my $AttValSE = "\"([^<\"]*)\"|'([^<']*)'"; my $ElemTagCE = "($Name)" . "(?:$S($Name)(?:$S)?=(?:$S)?(?:$AttValSE)" . "(?{[\@{\$^R||[]},\$8=>defined\$9?\$9:\$10]}))*(?:$S)?(/)?>" . "(?{${package}::_start(\$7,\@{\$^R||[]}),\$^R=[]})(?{\$11 and ${package}::_end(\$7); undef})"; my $MarkupSPE = "<(?:!(?:$DeclCE)?|\\?(?:$PI_CE)?|/(?:$EndTagCE)?|(?:$ElemTagCE)?)"; # Next expression is under "black magic". # Ideally it should be '($TextSE)(?{${package}::char(\$1)})|$MarkupSPE', # but it doesn't work under Perl 5.005 and only magic with # (?:....)?? solved the problem. # I would appreciate if someone let me know what is the right thing to do # and what's the reason for all this magic. # Seems like a problem related to (?:....)? rather than to ?{} feature. # Tests are in t/31-xmlparserlite.t if you decide to play with it. #"(?{[]})(?:($TextSE)(?{${package}::_char(\$1)}))$patch|$MarkupSPE"; "(?:($TextSE)(?{${package}::_char(\$1)}))$patch|$MarkupSPE"; } setHandlers(); # Try 5.6 and 5.10 regex first my $REGEXP = _regexp('??'); sub _parse_re { use re "eval"; undef $^R; 1 while $_[0] =~ m{$REGEXP}go }; # fixup regex if it does not work... { if (not eval { _parse_re('bar'); 1; } ) { $REGEXP = _regexp(); local $^W; *_parse_re = sub { use re "eval"; undef $^R; 1 while $_[0] =~ m{$REGEXP}go }; } } sub parse { _init(); _parse_re($_[1]); _final(); } my(@stack, $level); sub _init { @stack = (); $level = 0; Init(__PACKAGE__, @_); } sub _final { return _error("not properly closed tag '$stack[-1]'") if @stack; return _error("no element found") unless $level; Final(__PACKAGE__, @_) } sub _start { return _error("multiple roots, wrong element '$_[0]'") if $level++ && !@stack; push(@stack, $_[0]); Start(__PACKAGE__, @_); } sub _char { Char(__PACKAGE__, $_[0]), return if @stack; # check for junk before or after element # can't use split or regexp due to limitations in ?{} implementation, # will iterate with loop, but we'll do it no more than two times, so # it shouldn't affect performance for (my $i=0; $i < length $_[0]; $i++) { return _error("junk '$_[0]' @{[$level ? 'after' : 'before']} XML element") if index("\n\r\t ", substr($_[0],$i,1)) < 0; # or should '< $[' be there } } sub _end { return _error("unexpected closing tag '$_[0]'") if !@stack; pop(@stack) eq $_[0] or return _error("mismatched tag '$_[0]'"); End(__PACKAGE__, $_[0]); } sub comment { Comment(__PACKAGE__, substr $_[0], 0, -2); } sub end { pop(@stack) eq $_[0] or return _error("mismatched tag '$_[0]'"); End(__PACKAGE__, $_[0]); } sub cdata { return _error("CDATA outside of tag stack") unless @stack; CData(__PACKAGE__, substr $_[0], 0, -2); } sub _doctype { Doctype(__PACKAGE__, $_[0]); } sub _pi { PI(__PACKAGE__, substr $_[0], 0, -1); } sub _error { if ($ReturnErrors){ Error(__PACKAGE__, $_[0]); return; } die "$_[0]\n"; } # ====================================================================== 1; __END__ =head1 NAME XML::Parser::LiteCopy - Lightweight regexp-based XML parser =head1 SYNOPSIS use XML::Parser::LiteCopy; $p1 = new XML::Parser::LiteCopy; $p1->setHandlers( Start => sub { shift; print "start: @_\n" }, Char => sub { shift; print "char: @_\n" }, End => sub { shift; print "end: @_\n" }, ); $p1->parse('Hello World!'); $p2 = new XML::Parser::LiteCopy Handlers => { Start => sub { shift; print "start: @_\n" }, Char => sub { shift; print "char: @_\n" }, End => sub { shift; print "end: @_\n" }, } ; $p2->parse('Hello cruel World!'); =head1 DESCRIPTION This Perl implements an XML parser with a interface similar to XML::Parser. Though not all callbacks are supported, you should be able to use it in the same way you use XML::Parser. Due to using experimantal regexp features it'll work only on Perl 5.6 and above and may behave differently on different platforms. Note that you cannot use regular expressions or split in callbacks. This is due to a limitation of perl's regular expression implementation (which is not re-entrant). =head1 SUBROUTINES/METHODS =head2 new Constructor. As (almost) all SOAP::Lite constructors, new() returns the object called on when called as object method. This means that the following effectifely is a no-op if $obj is a object: $obj = $obj->new(); New accepts a single named parameter, C with a hash ref as value: my $parser = XML::Parser::Lite->new( Handlers => { Start => sub { shift; print "start: @_\n" }, Char => sub { shift; print "char: @_\n" }, End => sub { shift; print "end: @_\n" }, } ); The handlers given will be passed to setHandlers. =head2 setHandlers Sets (or resets) the parsing handlers. Accepts a hash with the handler names and handler code references as parameters. Passing C instead of a code reference replaces the handler by a no-op. The following handlers can be set: Init Start Char End Final CData Doctype Comment PI All other handlers are ignored. Calling setHandlers without parameters resets all handlers to no-ops. =head2 parse Parses the XML given. In contrast to L's parse method, parse() only parses strings. =head1 Handler methods =head2 Init Called before parsing starts. You should perform any necessary initializations in Init. =head2 Start Called at the start of each XML node. See L for details. =head2 Char Called for each character sequence. May be called multiple times for the characters contained in an XML node (even for every single character). Your implementation has to make sure that it captures all characters. =head2 End Called at the end of each XML node. See L for details =head2 Comment See L for details =head2 PI See XMLDecl in L for details, but also includes other processing instructions =head2 Doctype See L for details =head2 Final Called at the end of the parsing process. You should perform any neccessary cleanup here. =head1 SEE ALSO XML::Parser =head1 COPYRIGHT Copyright (C) 2000-2007 Paul Kulchenko. All rights reserved. Copyright (C) 2008 Martin Kutter. All rights reserved. Copyright (C) 2009 Cal Henderson. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This parser is based on "shallow parser" http://www.cs.sfu.ca/~cameron/REX.html Copyright (c) 1998, Robert D. Cameron. =head1 AUTHOR Paul Kulchenko (paulclinger@yahoo.com) Martin Kutter (martin.kutter@fen-net.de) Additional handlers supplied by Adam Leggett. Further modifications by Cal Henderson. =cut XML-Parser-Lite-Tree-0.14/lib/XML/Parser/Lite/0000755000000000000000000000000011572311137017156 5ustar rootrootXML-Parser-Lite-Tree-0.14/lib/XML/Parser/Lite/Tree.pm0000644000000000000000000001675211572311070020422 0ustar rootrootpackage XML::Parser::Lite::Tree; use 5.006; use strict; use warnings; use XML::Parser::LiteCopy; our $VERSION = '0.14'; use vars qw( $parser ); sub instance { return $parser if $parser; $parser = __PACKAGE__->new; } sub new { my $class = shift; my $self = bless {}, $class; my %opts = (ref $_[0]) ? ((ref $_[0] eq 'HASH') ? %{$_[0]} : () ) : @_; $self->{opts} = \%opts; $self->{__parser} = new XML::Parser::LiteCopy Handlers => { Start => sub { $self->_start_tag(@_); }, Char => sub { $self->_do_char(@_); }, CData => sub { $self->_do_cdata(@_); }, End => sub { $self->_end_tag(@_); }, Comment => sub { $self->_do_comment(@_); }, PI => sub { $self->_do_pi(@_); }, Doctype => sub { $self->_do_doctype(@_); }, }; $self->{process_ns} = $self->{opts}->{process_ns} || 0; $self->{skip_white} = $self->{opts}->{skip_white} || 0; return $self; } sub parse { my ($self, $content) = @_; my $root = { 'type' => 'root', 'children' => [], }; $self->{tag_stack} = [$root]; $self->{__parser}->parse($content); $self->cleanup($root); if ($self->{skip_white}){ $self->strip_white($root); } if ($self->{process_ns}){ $self->{ns_stack} = {}; $self->mark_namespaces($root); } return $root; } sub _start_tag { my $self = shift; shift; my $new_tag = { 'type' => 'element', 'name' => shift, 'attributes' => {}, 'children' => [], }; while (my $a_name = shift @_){ my $a_value = shift @_; $new_tag->{attributes}->{$a_name} = $a_value; } push @{$self->{tag_stack}->[-1]->{children}}, $new_tag; push @{$self->{tag_stack}}, $new_tag; 1; } sub _do_char { my $self = shift; shift; for my $content(@_){ my $new_tag = { 'type' => 'text', 'content' => $content, }; push @{$self->{tag_stack}->[-1]->{children}}, $new_tag; } 1; } sub _do_cdata { my $self = shift; shift; for my $content(@_){ my $new_tag = { 'type' => 'cdata', 'content' => $content, }; push @{$self->{tag_stack}->[-1]->{children}}, $new_tag; } 1; } sub _end_tag { my $self = shift; pop @{$self->{tag_stack}}; 1; } sub _do_comment { my $self = shift; shift; for my $content(@_){ my $new_tag = { 'type' => 'comment', 'content' => $content, }; push @{$self->{tag_stack}->[-1]->{children}}, $new_tag; } 1; } sub _do_pi { my $self = shift; shift; push @{$self->{tag_stack}->[-1]->{children}}, { 'type' => 'pi', 'content' => shift, }; 1; } sub _do_doctype { my $self = shift; shift; push @{$self->{tag_stack}->[-1]->{children}}, { 'type' => 'dtd', 'content' => shift, }; 1; } sub mark_namespaces { my ($self, $obj) = @_; my @ns_keys; # # mark # if ($obj->{type} eq 'element'){ # # first, add any new NS's to the stack # my @keys = keys %{$obj->{attributes}}; for my $k(@keys){ if ($k =~ /^xmlns:(.*)$/){ push @{$self->{ns_stack}->{$1}}, $obj->{attributes}->{$k}; push @ns_keys, $1; delete $obj->{attributes}->{$k}; } if ($k eq 'xmlns'){ push @{$self->{ns_stack}->{__default__}}, $obj->{attributes}->{$k}; push @ns_keys, '__default__'; delete $obj->{attributes}->{$k}; } } # # now - does this tag have a NS? # if ($obj->{name} =~ /^(.*?):(.*)$/){ $obj->{local_name} = $2; $obj->{ns_key} = $1; $obj->{ns} = $self->{ns_stack}->{$1}->[-1]; }else{ $obj->{local_name} = $obj->{name}; $obj->{ns} = $self->{ns_stack}->{__default__}->[-1]; } # # finally, add xpath-style namespace nodes # $obj->{namespaces} = {}; for my $key (keys %{$self->{ns_stack}}){ if (scalar @{$self->{ns_stack}->{$key}}){ my $uri = $self->{ns_stack}->{$key}->[-1]; $obj->{namespaces}->{$key} = $uri; } } } # # descend # if ($obj->{type} eq 'root' || $obj->{type} eq 'element'){ for my $child (@{$obj->{children}}){ $self->mark_namespaces($child); } } # # pop from stack # for my $k (@ns_keys){ pop @{$self->{ns_stack}->{$k}}; } } sub strip_white { my ($self, $obj) = @_; if ($obj->{type} eq 'root' || $obj->{type} eq 'element'){ my $new_kids = []; for my $child (@{$obj->{children}}){ if ($child->{type} eq 'text'){ if ($child->{content} =~ m/\S/){ push @{$new_kids}, $child; } }elsif ($child->{type} eq 'element'){ $self->strip_white($child); push @{$new_kids}, $child; }else{ push @{$new_kids}, $child; } } $obj->{children} = $new_kids; } } sub cleanup { my ($self, $obj) = @_; # # cleanup PIs # if ($obj->{type} eq 'pi'){ my ($x, $y) = split /\s+/, $obj->{content}, 2; $obj->{target} = $x; $obj->{content} = $y; } # # cleanup DTDs # if ($obj->{type} eq 'dtd'){ my ($x, $y) = split /\s+/, $obj->{content}, 2; $obj->{name} = $x; $obj->{content} = $y; } # # recurse # if ($obj->{type} eq 'root' || $obj->{type} eq 'element'){ for my $child (@{$obj->{children}}){ $self->cleanup($child); } } } 1; __END__ =head1 NAME XML::Parser::Lite::Tree - Lightweight XML tree builder =head1 SYNOPSIS use XML::Parser::Lite::Tree; my $tree_parser = XML::Parser::Lite::Tree::instance(); my $tree = $tree_parser->parse($xml_data); OR my $tree = XML::Parser::Lite::Tree::instance()->parse($xml_data); =head1 DESCRIPTION This is a singleton class for parsing XML into a tree structure. How does this differ from other XML tree generators? By using XML::Parser::Lite, which is a pure perl XML parser. Using this module you can tree-ify simple XML without having to compile any C. For example, the following XML: hoopla Parses into the following tree: 'children' => [ { 'children' => [ { 'children' => [], 'attributes' => { 'a' => 'b', 'c' => 'd' }, 'type' => 'element', 'name' => 'bar' }, { 'content' => 'hoopla', 'type' => 'text' } ], 'attributes' => { 'woo' => 'yay' }, 'type' => 'element', 'name' => 'foo' } ], 'type' => 'root' }; Each node contains a C key, one of C, C and C. C is the document root, and only contains an array ref C. C represents a normal tag, and contains an array ref C, a hash ref C and a string C. C nodes contain only a C string. =head1 METHODS =over 4 =item C Returns an instance of the tree parser. =item C Creates a new parser. Valid options include C to process namespaces. =item C Parses the xml in C<$xml> and returns the tree as a hash ref. =back =head1 AUTHOR Copyright (C) 2004-2008, Cal Henderson, Ecal@iamcal.comE =head1 SEE ALSO L. =cut XML-Parser-Lite-Tree-0.14/MANIFEST0000644000000000000000000000047611572311137014757 0ustar rootrootMakefile.PL MANIFEST README t/00_xmlparserlite.t t/01_basic.t t/02_options.t t/03_comments.t t/04_processing_instructions.t t/05_doctypes.t t/06_attributes.t t/07_stack.t t/08_cdata.t lib/XML/Parser/LiteCopy.pm lib/XML/Parser/Lite/Tree.pm META.yml Module meta-data (added by MakeMaker) XML-Parser-Lite-Tree-0.14/Makefile.PL0000644000000000000000000000027211572310041015563 0ustar rootrootuse ExtUtils::MakeMaker; WriteMakefile( 'NAME' => 'XML::Parser::Lite::Tree', 'VERSION_FROM' => 'lib/XML/Parser/Lite/Tree.pm', 'PREREQ_PM' => { 'Test::More' => 0, }, ); XML-Parser-Lite-Tree-0.14/README0000644000000000000000000000130111572310041014463 0ustar rootrootXML::Parser::Lite::Tree ======================= Lightweight XML tree builder INSTALLATION To install this module type the following: perl Makefile.PL make make test make install DEPENDENCIES This module requires these other modules and libraries: Test::More COPYRIGHT AND LICENCE Copyright (C) 2004-2009 Cal Henderson License: Perl Artistic License 2.0 Contains XML::Parser::Lite: Copyright (C) 2000-2007 Paul Kulchenko Copyright (C) 2008 Martin Kutter Copyright (C) 2009 Cal Henderson SOAP::Lite is free software; you can redistribute it and/or modify it under the same terms as Perl itself. XML-Parser-Lite-Tree-0.14/META.yml0000644000000000000000000000075511572311137015077 0ustar rootroot--- #YAML:1.0 name: XML-Parser-Lite-Tree version: 0.14 abstract: ~ author: [] license: unknown distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: Test::More: 0 no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4