use XML::Parser;

my $rpt = shift;
my $file = shift;

die "Usage is:\n\tperl scale.pl rptcnt file\n"
  unless ($rpt > 0 and -r $file);

my $p = new XML::Parser(ErrorContext => 2,
			Handlers => {Start => \&start,
				     End   => \&end,
				     Char  => \&char,
				     Proc  => \&copy,
				     Comment => \&copy,
				     CdataStart => \&cdstart,
				     CdataEnd   => \&cdend});


my $incd = 0;

$p->parsefile($file);

################
## End of main
################

sub start {
  my $xp = shift;
  my $el = shift;

  print "<$el";

  while (@_) {
    my $att = shift;
    print " $att=";
    my $val = $xp->xml_escape(shift, "'");
    print "'$val'";
  }

  print ">";
}

sub end {
  print "</$_[1]>";
}

sub copy {
  my ($xp) = @_;

  print $xp->recognized_string;
}

sub cdstart {
  $incd = 1;
  print '<![CDATA[';
}

sub cdend {
  $incd = 0;
  print ']]>';
}

sub char {
  my ($xp, $data) = @_;

  unless ($incd) {
    $data = $xp->xml_escape($data);
  }

  for (my $i = 0; $i < $rpt; $i++) {
    print $data;
  }
}
