NAME

XML::Filter::Reindent - Reformats whitespace for pretty printing XML

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);

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!

Constructor Options

  • Handler

    The PerlSAX handler (or filter) that will receive the PerlSAX events from this filter.

  • Tab (Default: one space)

    The number of spaces per indent level for elements etc. in document content.

  • Newline (Default: "\x0A")

    The newline to use when re-indenting. The default is the internal newline used by XML::Parser, XML::DOM etc., and should be fine when used in combination with XML::Handler::Composer.

$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, 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.

$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!

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.

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.

AUTHOR

Send bug reports, hints, tips, suggestions to Enno Derksen at <enno@att.com>.