Reformats whitespace for pretty printing xml
use XML::Handler::Composer; use XML::Filter::Reindent; my $composer = new XML::Handler::Composer (%OPTIONS); my $indent = new XML::Filter::Reindent (Handler => $composer, %OPTIONS);
XML::Filter::Reindent is a sub class of XML::Filter::DetectWS.
XML::Filter::Reindent can be used as a PerlSAX filter to reformat an \s-1XML\s0 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!
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: \*(L"\x0A\*(R") The newline to use when re-indenting. The default is the internal newline used by XML::Parser, \s-1XML::DOM\s0 etc., and should be fine when used in combination with XML::Handler::Composer. 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 \s-1MAYBE\s0 otherwise. The value \s-1MAYBE\s0 (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.
\s-1NOTE:\s0 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=\*(L"preserve\*(R".
Override this method to tweak the behavior of this class. 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!
The current implementation puts all incoming Perl \s-1SAX\s0 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 \s-1XML\s0 printer that a compressed notation can be used, e.g <foo/>.
If an element allows reformatting of its contents (xml:space=\*(L"preserve\*(R" was not active and indent_children returned \s-1MAYBE\s0), 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 \s-1MAYBE\s0 (2.)
This behavior may be changed or extended in the future.
This code is highly experimental! It has not been tested well and the \s-1API\s0 may change.
The code that detects blocks of whitespace at potential indent positions may need some work.
Original Author is Enno Derksen.
Send bug reports, hints, tips, suggestions to T.J. Mather at <[email protected]>.