r10996 - in /branches/upstream/libxml-parser-lite-tree-perl: ./ current/ current/MANIFEST current/Makefile.PL current/README current/Tree.pm current/t/ current/t/01.t

ghostbar-guest at users.alioth.debian.org ghostbar-guest at users.alioth.debian.org
Fri Dec 7 03:46:53 UTC 2007


Author: ghostbar-guest
Date: Fri Dec  7 03:46:53 2007
New Revision: 10996

URL: http://svn.debian.org/wsvn/?sc=1&rev=10996
Log:
[svn-inject] Installing original source of libxml-parser-lite-tree-perl

Added:
    branches/upstream/libxml-parser-lite-tree-perl/
    branches/upstream/libxml-parser-lite-tree-perl/current/
    branches/upstream/libxml-parser-lite-tree-perl/current/MANIFEST
    branches/upstream/libxml-parser-lite-tree-perl/current/Makefile.PL
    branches/upstream/libxml-parser-lite-tree-perl/current/README
    branches/upstream/libxml-parser-lite-tree-perl/current/Tree.pm
    branches/upstream/libxml-parser-lite-tree-perl/current/t/
    branches/upstream/libxml-parser-lite-tree-perl/current/t/01.t

Added: branches/upstream/libxml-parser-lite-tree-perl/current/MANIFEST
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-parser-lite-tree-perl/current/MANIFEST?rev=10996&op=file
==============================================================================
--- branches/upstream/libxml-parser-lite-tree-perl/current/MANIFEST (added)
+++ branches/upstream/libxml-parser-lite-tree-perl/current/MANIFEST Fri Dec  7 03:46:53 2007
@@ -1,0 +1,5 @@
+Makefile.PL
+MANIFEST
+README
+t/01.t
+Tree.pm

Added: branches/upstream/libxml-parser-lite-tree-perl/current/Makefile.PL
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-parser-lite-tree-perl/current/Makefile.PL?rev=10996&op=file
==============================================================================
--- branches/upstream/libxml-parser-lite-tree-perl/current/Makefile.PL (added)
+++ branches/upstream/libxml-parser-lite-tree-perl/current/Makefile.PL Fri Dec  7 03:46:53 2007
@@ -1,0 +1,10 @@
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+	'NAME'		=> 'XML::Parser::Lite::Tree',
+	'VERSION_FROM'	=> 'Tree.pm',
+	'PREREQ_PM'	=> {
+				'XML::Parser::Lite'	=> 0,
+				'Test::Simple'		=> 0,
+			},
+);

Added: branches/upstream/libxml-parser-lite-tree-perl/current/README
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-parser-lite-tree-perl/current/README?rev=10996&op=file
==============================================================================
--- branches/upstream/libxml-parser-lite-tree-perl/current/README (added)
+++ branches/upstream/libxml-parser-lite-tree-perl/current/README Fri Dec  7 03:46:53 2007
@@ -1,0 +1,28 @@
+XML::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:
+
+  XML::Parser::Lite
+  Test::Simple
+
+
+COPYRIGHT AND LICENCE
+
+Copyright (C) 2004 Cal Henderson <cal at iamcal.com>
+License: Perl Artistic License

Added: branches/upstream/libxml-parser-lite-tree-perl/current/Tree.pm
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-parser-lite-tree-perl/current/Tree.pm?rev=10996&op=file
==============================================================================
--- branches/upstream/libxml-parser-lite-tree-perl/current/Tree.pm (added)
+++ branches/upstream/libxml-parser-lite-tree-perl/current/Tree.pm Fri Dec  7 03:46:53 2007
@@ -1,0 +1,177 @@
+package XML::Parser::Lite::Tree;
+
+use 5.006;
+use strict;
+use warnings;
+use XML::Parser::Lite;
+
+our $VERSION = '0.03';
+
+use vars qw( $parser );
+
+my $next_tag;
+my @tag_stack;
+
+sub instance {
+	return $parser if $parser;
+	$parser = __PACKAGE__->new;
+}
+
+sub new {
+	my $parser = bless { __parser => undef }, $_[0];
+	$parser->init;
+	$parser;
+}
+
+sub init {
+	my $parser = shift;
+	$parser->{__parser} = new XML::Parser::Lite
+		Handlers => {
+			Start => \&_start_tag,
+			Char => \&_do_char,
+			End => \&_end_tag,
+		};
+}
+
+sub parse {
+	my ($parser, $content) = @_;
+
+	$next_tag = {
+		'type' => 'root',
+		'children' => [],
+	};
+	@tag_stack = ($next_tag);
+
+	$parser->{__parser}->parse($content);
+
+	return $next_tag;
+}
+
+sub _start_tag {
+	shift;
+
+	my $new_tag = {
+		'type' => 'tag',
+		'name' => shift,
+		'attributes' => {},
+		'children' => [],
+	};
+	while(my $a_name = shift @_){
+		my $a_value = shift @_;
+		$new_tag->{attributes}->{$a_name} = $a_value;
+	}
+
+	push @{$next_tag->{children}}, $new_tag;
+
+	push @tag_stack, $new_tag;
+	$next_tag = $new_tag;
+}
+
+sub _do_char {
+	shift;
+	for my $content(@_){
+		my $new_tag = {
+			'type' => 'data',
+			'content' => $content,
+		};
+		push @{$next_tag->{children}}, $new_tag;
+	}
+}
+
+sub _end_tag {
+	pop @tag_stack;
+	$next_tag = $tag_stack[$#tag_stack];
+}
+
+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:
+
+  <foo woo="yay"><bar a="b" c="d" />hoopla</foo>
+
+
+Parses into the following tree:
+
+          'children' => [
+                          {
+                            'children' => [
+                                            {
+                                              'children' => [],
+                                              'attributes' => {
+                                                                'a' => 'b',
+                                                                'c' => 'd'
+                                                              },
+                                              'type' => 'tag',
+                                              'name' => 'bar'
+                                            },
+                                            {
+                                              'content' => 'hoopla',
+                                              'type' => 'data'
+                                            }
+                                          ],
+                            'attributes' => {
+                                              'woo' => 'yay'
+                                            },
+                            'type' => 'tag',
+                            'name' => 'foo'
+                          }
+                        ],
+          'type' => 'root'
+        };
+
+
+Each node contains a C<type> key, one of C<root>, C<tag> and C<data>. C<root> is the 
+document root, and only contains an array ref C<children>. C<tag> represents a normal
+tag, and contains an array ref C<children>, a hash ref C<attributes> and a string C<name>.
+C<data> nodes contain only a C<content> string.
+
+
+=head1 METHODS
+
+=over 4
+
+=item C<instance()>
+
+Returns an instance of the tree parser.
+
+=item C<parse($xml)>
+
+Parses the xml in C<$xml> and returns the tree as a hash ref.
+
+=back
+
+
+=head1 AUTHOR
+
+Copyright (C) 2004, Cal Henderson, E<lt>cal at iamcal.comE<gt>
+
+
+=head1 SEE ALSO
+
+L<XML::Parser::Lite>.
+
+=cut

Added: branches/upstream/libxml-parser-lite-tree-perl/current/t/01.t
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-parser-lite-tree-perl/current/t/01.t?rev=10996&op=file
==============================================================================
--- branches/upstream/libxml-parser-lite-tree-perl/current/t/01.t (added)
+++ branches/upstream/libxml-parser-lite-tree-perl/current/t/01.t Fri Dec  7 03:46:53 2007
@@ -1,0 +1,28 @@
+use Test::Simple 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('<foo bar="baz">woo<yay />hoopla</foo>');
+
+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 'tag', "root node is a tag" );
+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 'data', "child 1 type correct" );
+ok( $root_node->{children}->[0]->{content} eq 'woo', "child 1 content correct" );
+
+ok( $root_node->{children}->[1]->{type} eq 'tag', "child 2 type correct" );
+ok( $root_node->{children}->[1]->{name} eq 'yay', "child 2 name correct" );
+
+ok( $root_node->{children}->[2]->{type} eq 'data', "child 3 type correct" );
+ok( $root_node->{children}->[2]->{content} eq 'hoopla', "child 3 content correct" );




More information about the Pkg-perl-cvs-commits mailing list