XML-Filter-Reindent-0.03/ 0040775 0000765 0000765 00000000000 07453467205 015337 5 ustar tjmather tjmather XML-Filter-Reindent-0.03/bin/ 0040755 0000765 0000765 00000000000 07314130275 016073 5 ustar tjmather tjmather XML-Filter-Reindent-0.03/bin/README.txt 0100644 0000765 0000765 00000000325 07314130411 017556 0 ustar tjmather tjmather 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...
XML-Filter-Reindent-0.03/bin/pretty.pl 0100755 0000765 0000765 00000000745 07053067234 017771 0 ustar tjmather tjmather #!/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 });
XML-Filter-Reindent-0.03/Changes 0100664 0000765 0000765 00000000552 07453466775 016644 0 ustar tjmather tjmather 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
XML-Filter-Reindent-0.03/Makefile.PL 0100664 0000765 0000765 00000000453 07342207566 017307 0 ustar tjmather tjmather 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},
);
XML-Filter-Reindent-0.03/MANIFEST 0100664 0000765 0000765 00000000060 07342207656 016460 0 ustar tjmather tjmather Changes
Makefile.PL
MANIFEST
README
Reindent.pm
XML-Filter-Reindent-0.03/README 0100664 0000765 0000765 00000002101 07453467153 016210 0 ustar tjmather tjmather 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.
XML-Filter-Reindent-0.03/test.pl 0100664 0000765 0000765 00000002346 07406563012 016644 0 ustar tjmather tjmather # 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{\n\s+\n};
},
);
plan tests => scalar @tests;
$_->() for @tests;
XML-Filter-Reindent-0.03/Reindent.pm 0100644 0000765 0000765 00000020141 07453467166 017444 0 ustar tjmather tjmather package XML::Filter::Reindent;
use strict;
use XML::Filter::DetectWS;
use vars qw{ $VERSION @ISA };
$VERSION = '0.03';
@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: text here
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::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.)
Like L, 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, L etc.,
and should be fine when used in combination with L.
=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, 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 .
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 text contents.
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
>.
=cut