r10461 - in /branches/upstream/libxml-filter-reindent-perl: ./ current/ current/Changes current/MANIFEST current/Makefile.PL current/README current/Reindent.pm current/bin/ current/bin/README.txt current/bin/pretty.pl current/test.pl

vdanjean at users.alioth.debian.org vdanjean at users.alioth.debian.org
Sat Dec 1 12:28:32 UTC 2007


Author: vdanjean
Date: Sat Dec  1 12:28:31 2007
New Revision: 10461

URL: http://svn.debian.org/wsvn/?sc=1&rev=10461
Log:
[svn-inject] Installing original source of libxml-filter-reindent-perl

Added:
    branches/upstream/libxml-filter-reindent-perl/
    branches/upstream/libxml-filter-reindent-perl/current/
    branches/upstream/libxml-filter-reindent-perl/current/Changes
    branches/upstream/libxml-filter-reindent-perl/current/MANIFEST
    branches/upstream/libxml-filter-reindent-perl/current/Makefile.PL
    branches/upstream/libxml-filter-reindent-perl/current/README
    branches/upstream/libxml-filter-reindent-perl/current/Reindent.pm
    branches/upstream/libxml-filter-reindent-perl/current/bin/
    branches/upstream/libxml-filter-reindent-perl/current/bin/README.txt
    branches/upstream/libxml-filter-reindent-perl/current/bin/pretty.pl   (with props)
    branches/upstream/libxml-filter-reindent-perl/current/test.pl

Added: branches/upstream/libxml-filter-reindent-perl/current/Changes
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-filter-reindent-perl/current/Changes?rev=10461&op=file
==============================================================================
--- branches/upstream/libxml-filter-reindent-perl/current/Changes (added)
+++ branches/upstream/libxml-filter-reindent-perl/current/Changes Sat Dec  1 12:28:31 2007
@@ -1,0 +1,12 @@
+Revision history for Perl extension XML::Filter::Reindent.
+
+0.03  Fri Apr 5th
+	- Added bin/pretty.pl from old XML-DOM distribution
+
+0.02  Fri Dec 14 17:27:57 2001
+	- fixed bug with end_document result code not being passed back
+		(Barrie Slaymaker contributed patch and test cases)
+
+0.01  Sun Aug 26 10:59:06 2001
+	- moved from XML-DOM to separate distribution
+

Added: branches/upstream/libxml-filter-reindent-perl/current/MANIFEST
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-filter-reindent-perl/current/MANIFEST?rev=10461&op=file
==============================================================================
--- branches/upstream/libxml-filter-reindent-perl/current/MANIFEST (added)
+++ branches/upstream/libxml-filter-reindent-perl/current/MANIFEST Sat Dec  1 12:28:31 2007
@@ -1,0 +1,5 @@
+Changes
+Makefile.PL
+MANIFEST
+README
+Reindent.pm

Added: branches/upstream/libxml-filter-reindent-perl/current/Makefile.PL
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-filter-reindent-perl/current/Makefile.PL?rev=10461&op=file
==============================================================================
--- branches/upstream/libxml-filter-reindent-perl/current/Makefile.PL (added)
+++ branches/upstream/libxml-filter-reindent-perl/current/Makefile.PL Sat Dec  1 12:28:31 2007
@@ -1,0 +1,8 @@
+use ExtUtils::MakeMaker;
+# See lib/ExtUtils/MakeMaker.pm for details of how to influence
+# the contents of the Makefile that is written.
+WriteMakefile(
+    'NAME'		=> 'XML::Filter::Reindent',
+    'VERSION_FROM'	=> 'Reindent.pm', # finds $VERSION
+    'PREREQ_PM'		=> {XML::Filter::DetectWS => 0},
+);

Added: branches/upstream/libxml-filter-reindent-perl/current/README
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-filter-reindent-perl/current/README?rev=10461&op=file
==============================================================================
--- branches/upstream/libxml-filter-reindent-perl/current/README (added)
+++ branches/upstream/libxml-filter-reindent-perl/current/README Sat Dec  1 12:28:31 2007
@@ -1,0 +1,32 @@
+XML::Filter::Reindent version 0.03
+==================================
+
+DESCRIPTION
+
+XML::Filter::Reindent is a sub class of XML::Filter::DetectWS. 
+
+XML::Filter::Reindent can be used as a PerlSAX filter to reformat an XML document before sending it to a PerlSAX
+handler that prints it (like XML::Handler::Composer.) 
+
+Like XML::Filter::DetectWS, it detects ignorable whitespace and blocks of whitespace characters in certain places. It
+uses this information and information supplied by the user to determine where whitespace may be modified, deleted or
+inserted. Based on the indent settings, it then modifies, inserts and deletes characters and ignorable_whitespace
+events accordingly. 
+
+This is just a first stab at the implementation. It may be buggy and may change completely! 
+
+INSTALLATION
+
+To install this module type the following:
+
+   perl Makefile.PL
+   make
+   make test
+   make install
+
+COPYRIGHT AND LICENCE
+
+Copyright (c) 1999,2000 Enno Derksen
+All rights reserved.
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.

Added: branches/upstream/libxml-filter-reindent-perl/current/Reindent.pm
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-filter-reindent-perl/current/Reindent.pm?rev=10461&op=file
==============================================================================
--- branches/upstream/libxml-filter-reindent-perl/current/Reindent.pm (added)
+++ branches/upstream/libxml-filter-reindent-perl/current/Reindent.pm Sat Dec  1 12:28:31 2007
@@ -1,0 +1,309 @@
+package XML::Filter::Reindent;
+use strict;
+use XML::Filter::DetectWS;
+
+use vars qw{ $VERSION @ISA };
+$VERSION = '0.03';
+ at ISA = qw{ XML::Filter::DetectWS };
+
+sub MAYBE (%) { 2 }
+
+sub new
+{
+    my $class = shift;
+    my $self = $class->SUPER::new (@_);
+
+    # Use one space per indent level (by default)
+    $self->{Tab} = " " unless defined $self->{Tab};
+
+    # Note that this is a PerlSAX filter so we use the XML newline ("\x0A"),
+    # not the Perl output newline ("\n"), by default.
+    $self->{Newline} = "\x0A" unless defined $self->{Newline};
+
+    $self;
+}
+
+# Indent the element if its parent element says so
+sub indent_element
+{
+    my ($self, $event, $parent_says_indent) = @_;
+    return $parent_says_indent;
+}
+
+# Always indent children unless element (or its ancestor) has 
+# xml:space="preserve" attribute
+sub indent_children
+{
+    my ($self, $event) = @_;
+    return $event->{PreserveWS} ? 0 : MAYBE;
+}
+
+sub start_element
+{
+    my ($self, $event) = @_;
+
+    my $parent = $self->{ParentStack}->[-1];
+    my $level = $self->{Level}++;
+    $self->SUPER::start_element ($event);
+
+    my $parent_says_indent = $parent->{IndentChildren} ? 1 : 0;
+    # init with 1 if parent says MAYBE
+    $event->{Indent} = $self->indent_element ($event, $parent_says_indent) ?
+			$level : undef;
+
+    $event->{IndentChildren} = $self->indent_children ($event);
+}
+
+sub end_element
+{
+    my ($self, $event) = @_;
+    my $start_element = $self->{ParentStack}->[-1];
+
+    if ($start_element->{IndentChildren} == MAYBE)
+    {
+	my $q = $self->{EventQ};
+	my $prev = $q->[-1];
+
+	if ($prev == $start_element)
+	{
+	    # End tag follows start tag: compress tag
+	    $start_element->{Compress} = 1;
+	    $event->{Compress} = 1;
+#?? could detect if it contains only ignorable_ws
+	}
+	elsif ($prev->{EventType} eq 'characters')
+	{
+	    if ($q->[-2] == $start_element)
+	    {
+		# Element has only one child, a text node.
+		# Print element as: <a>text here</a>
+		delete $prev->{Indent};
+		$start_element->{IndentChildren} = 0;
+	    }
+	}
+    }
+
+    my $level = --$self->{Level};
+    $event->{Indent} = $start_element->{IndentChildren} ? $level : undef;
+
+    my $compress = $start_element->{Compress};
+    if ($compress)
+    {
+	$event->{Compress} = $compress;
+	delete $event->{Indent};
+    }
+
+    $self->SUPER::end_element ($event);
+}
+
+sub end_document
+{
+    my ($self, $event) = @_;
+
+    $self->push_event ('end_document', $event || {});
+    $self->flush (0);	# send remaining events
+}
+
+sub push_event
+{
+    my ($self, $type, $event) = @_;
+
+    $event->{EventType} = $type;
+    if ($type =~ /^(characters|comment|processing_instruction|entity_reference|cdata)$/)
+    {
+	my $indent_kids = $self->{ParentStack}->[-1]->{IndentChildren} ? 1 : 0;
+	$event->{Indent} =  $indent_kids ? $self->{Level} : undef;
+    }
+
+    my $q = $self->{EventQ};
+    push @$q, $event;
+
+    $self->flush (4);	# keep 4 events on the stack (maybe 3 is enough)
+}
+
+sub flush
+{
+    my ($self, $keep) = @_;
+    my $q = $self->{EventQ};
+
+    my $result;
+
+    while (@$q > $keep)
+    {
+	my $head = $q->[0];
+#	print "head=" . $head->{EventType} . " indent=" . $head->{Indent} . "\n";
+
+	if ($head->{EventType} =~ /ws|ignorable/)
+	{
+	    my $next = $q->[1];
+	    my $indent = $next->{Indent};
+
+	    if (defined $indent)	# fix existing indent
+	    {
+		$head->{Data} = $self->{Newline} . ($self->{Tab} x $indent);
+		$result = $self->send (2);
+	    }
+	    else		# remove existing indent
+	    {
+		shift @$q;
+		$result = $self->send (1);
+	    }
+#?? remove keys: Indent, ...
+	}
+	else
+	{
+	    my $indent = $head->{Indent};
+
+	    if (defined $indent)	# insert indent
+	    {
+		unshift @$q, { EventType => 'ws', 
+			       Data => $self->{Newline} . ($self->{Tab} x $indent) };
+		$result = $self->send (2);
+	    }
+	    else		# no indent - leave as is
+	    {
+		$result = $self->send (1);
+	    }
+	}
+    }
+    return $result;
+}
+
+sub send
+{
+    my ($self, $i) = @_;
+    
+    my $q = $self->{EventQ};
+
+    my $result;
+    while ($i--)
+    {
+	my $event = shift @$q;
+	my $type = $event->{EventType};
+	delete $event->{EventType};
+
+#print "TYPE=$type " . join(",", map { "$_=" . $event->{$_} } keys %$event) . "\n";
+	$result = $self->{Callback}->{$type}->($event);
+    }
+    return $result;
+}
+
+1;	# package return code
+
+=head1 NAME
+
+XML::Filter::Reindent - Reformats whitespace for pretty printing XML
+
+=head1 SYNOPSIS
+
+ use XML::Handler::Composer;
+ use XML::Filter::Reindent;
+
+ my $composer = new XML::Handler::Composer (%OPTIONS);
+ my $indent = new XML::Filter::Reindent (Handler => $composer, %OPTIONS);
+
+=head1 DESCRIPTION
+
+XML::Filter::Reindent is a sub class of L<XML::Filter::DetectWS>.
+
+XML::Filter::Reindent can be used as a PerlSAX filter to reformat an
+XML document before sending it to a PerlSAX handler that prints it
+(like L<XML::Handler::Composer>.)
+
+Like L<XML::Filter::DetectWS>, it detects ignorable whitespace and blocks of
+whitespace characters in certain places. It uses this information and
+information supplied by the user to determine where whitespace may be
+modified, deleted or inserted. 
+Based on the indent settings, it then modifies, inserts and deletes characters
+and ignorable_whitespace events accordingly.
+
+This is just a first stab at the implementation.
+It may be buggy and may change completely!
+
+=head1 Constructor Options
+
+=over 4
+
+=item * Handler
+
+The PerlSAX handler (or filter) that will receive the PerlSAX events from this 
+filter.
+
+=item * Tab (Default: one space)
+
+The number of spaces per indent level for elements etc. in document content.
+
+=item * Newline (Default: "\x0A")
+
+The newline to use when re-indenting. 
+The default is the internal newline used by L<XML::Parser>, L<XML::DOM> etc.,
+and should be fine when used in combination with L<XML::Handler::Composer>.
+
+=back
+
+=head1 $self->indent_children ($start_element_event)
+
+This method determines whether children of a certain element
+may be reformatted. 
+The default implementation checks the PreserveWS parameter of the specified
+start_element event and returns 0 if it is set or MAYBE otherwise.
+The value MAYBE (2) indicates that further investigation is needed, e.g.
+by examining the element contents. A value of 1 means yes, indent the
+child nodes, no further investigation is needed.
+
+NOTE: the PreserveWS parameter is set by the parent class, 
+L<XML::Filter::DetectWS>, when the element or one of its ancestors has
+the attribute xml:space="preserve".
+
+Override this method to tweak the behavior of this class.
+
+=head1 $self->indent_element ($start_element_event, $parent_says_indent)
+
+This method determines whether a certain element may be re-indented. 
+The default implementation returns the value of the $parent_says_indent
+parameter, which was set to the value returned by indent_children for the
+parent element. In other words, the element will be re-indented if the
+parent element allows it.
+
+Override this method to tweak the behavior of this class.
+I'm not sure how useful this hook is. Please provide feedback!
+
+=head1 Current Implementation
+
+The current implementation puts all incoming Perl SAX events in a queue for
+further processing. When determining which nodes should be re-indented,
+it sometimes needs information from previous events, hence the use of the 
+queue.
+
+The parameter (Compress => 1) is added to 
+matching start_element and end_element events with no events in between
+This indicates to an XML printer that a compressed notation can be used, 
+e.g <foo/>.
+
+If an element allows reformatting of its contents (xml:space="preserve" was 
+not active and indent_children returned MAYBE), the element
+contents will be reformatted unless it only has one child node and that
+child is a regular text node (characters event.) 
+In that case, the element will be printed as <foo>text contents</foo>.
+
+If you want element nodes with just one text child to be reindented as well,
+simply override indent_children to return 1 instead of MAYBE (2.)
+
+This behavior may be changed or extended in the future.
+
+=head1 CAVEATS
+
+This code is highly experimental! 
+It has not been tested well and the API may change.
+
+The code that detects blocks of whitespace at potential indent positions
+may need some work.
+
+=head1 AUTHOR
+
+Original Author is Enno Derksen.
+
+Send bug reports, hints, tips, suggestions to T.J. Mather at
+<F<tjmather at tjmather.com>>. 
+
+=cut

Added: branches/upstream/libxml-filter-reindent-perl/current/bin/README.txt
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-filter-reindent-perl/current/bin/README.txt?rev=10461&op=file
==============================================================================
--- branches/upstream/libxml-filter-reindent-perl/current/bin/README.txt (added)
+++ branches/upstream/libxml-filter-reindent-perl/current/bin/README.txt Sat Dec  1 12:28:31 2007
@@ -1,0 +1,7 @@
+This directory contains some sample Perl scripts.
+
+- pretty.pl
+
+  Uses XML::Filter::Reindent and XML::Handler::Composer to 
+  pretty print your XML file.
+  These classes need some work so don't expect too much...

Added: branches/upstream/libxml-filter-reindent-perl/current/bin/pretty.pl
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-filter-reindent-perl/current/bin/pretty.pl?rev=10461&op=file
==============================================================================
--- branches/upstream/libxml-filter-reindent-perl/current/bin/pretty.pl (added)
+++ branches/upstream/libxml-filter-reindent-perl/current/bin/pretty.pl Sat Dec  1 12:28:31 2007
@@ -1,0 +1,22 @@
+#!/home1/enno/bin/perl -w
+#
+# Usage: perl pretty.pl URL > out.xml
+#
+
+use strict;
+
+my $url = shift;
+
+use XML::Parser::PerlSAX;
+use XML::Filter::Reindent;
+use XML::Handler::Composer;
+
+my $composer = new XML::Handler::Composer (Newline => "\n");
+
+my $reindent = new XML::Filter::Reindent (Handler => $composer);
+
+my $parser = new XML::Parser::PerlSAX (KeepCDATA => 1, 
+				       UseAttributeOrder => 1,
+				       Handler => $reindent);
+
+$parser->parse (Source => { SystemId => $url });

Propchange: branches/upstream/libxml-filter-reindent-perl/current/bin/pretty.pl
------------------------------------------------------------------------------
    svn:executable = 

Added: branches/upstream/libxml-filter-reindent-perl/current/test.pl
URL: http://svn.debian.org/wsvn/branches/upstream/libxml-filter-reindent-perl/current/test.pl?rev=10461&op=file
==============================================================================
--- branches/upstream/libxml-filter-reindent-perl/current/test.pl (added)
+++ branches/upstream/libxml-filter-reindent-perl/current/test.pl Sat Dec  1 12:28:31 2007
@@ -1,0 +1,51 @@
+# Before `make install' is performed this script should be runnable with
+# `make test'. After `make install' it should work as `perl test.pl'
+ 
+use Test;
+use XML::Filter::Reindent;
+use strict;
+ 
+my $expected = "result from end_document";
+ 
+my $r;
+ 
+my $output;
+ 
+sub start_document{ $output = ""                  }
+sub start_element { $output .= "<$_[1]->{Name}>"  }
+sub end_element   { $output .= "</$_[1]->{Name}>" }
+sub characters    { $output .= $_[1]->{Data}      }
+sub end_document  { return $expected              }
+ 
+my @tests = (
+sub { ok 1 },
+sub {
+    $r = XML::Filter::Reindent->new;
+    $r->start_document({});
+    ok ! defined $r->end_document({}), 1, "undefined result from end_document";
+},
+sub {
+    $output = "ouch";
+    $r = XML::Filter::Reindent->new( Handler => "main" );
+    $r->start_document({});
+    ok $r->end_document({}), $expected;
+},
+sub {
+    ok $output, "";
+},
+sub {
+    $r->start_document({});
+    $r->start_element({ Name => "foo" });
+    $r->start_element({ Name => "bar" });
+    $r->end_element({   Name => "bar" });
+    $r->end_element({   Name => "foo" });
+    ok $r->end_document({}), $expected;
+},
+sub {
+    ok $output =~ m{<foo>\n\s+<bar></bar>\n</foo>};
+},
+);
+ 
+plan tests => scalar @tests;
+ 
+$_->() for @tests;




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