#!/usr/local/bin/perl -w
#
# Demo program using XML::Parser -- parse XML doc and output the
# same thing, with long lines of CDATA wrapped. Bug: currently
# converts <tag/> to <tag></tag>.
#
use strict;
use XML::Parser;

my $file = shift;
die "Can't find file \"$file\"" unless -f $file;

my $count=0;
my $parser = new XML::Parser(ErrorContext => 2);
$parser->setHandlers(Char    => \&char_handler,
                     Default => \&default_handler,
                     Start   => \&start_handler,
                     End     => \&end_handler );

$parser->parsefile($file);

exit(0);

#############################
sub char_handler
{
  my ($p, $data) = @_;
  while ($data =~ s/^(.{65,}?\S*?\s+)//) {	#wrap long lines
    print "$1\n"; 
  }
  print "$data";
}


sub default_handler
{
  my ($p, $data) = @_;
  print "$data" unless $data =~ m/^\s*$/;
}


sub start_handler
{
  my ($p, $el) = (shift, shift);
  my ($att, $val);
  print "<$el";
  while (@_) {
    my $att = shift;
    my $val=shift;
    print "$att=\"$val\"";
  }
  print "\n>";
}


sub end_handler
{
  my ($p, $data) = @_;
  print "</$data\n>" unless $data =~ m/^\s*$/;
}
