#!/usr/bin/perl

# $Id: xupdate,v 1.13 2002/11/07 09:46:00 pajas Exp $

use FindBin;
use lib ("$FindBin::RealBin", "$FindBin::RealBin/../lib",
         "$FindBin::Bin","$FindBin::Bin/../lib",
	 "$FindBin::Bin/lib", "$FindBin::RealBin/lib",
	 "$FindBin::Bin/../lib/site_perl"
	);

use strict;
use Getopt::Long;
use Pod::Usage;

use vars qw($VERSION $REVISION $keep_ws $print_version $help $usage $indent $extra_indent);

use XML::LibXML;
use XML::XUpdate::LibXML;
use XML::Normalize::LibXML qw(xml_strip_whitespace);

BEGIN {
  GetOptions('-help|h' => \$help,
	     '-version|V' => \$print_version,
	     '-keep-ws|k' => \$keep_ws,
	     '-indent|i' => \$indent,
	     '-extra-indent|j' => \$extra_indent,
	     '-usage|u' => \$usage,
	    );
  $VERSION='0.3';
  $REVISION='$Revision: 1.13 $';
}

if ($print_version) {
  print "Current version is $VERSION ($REVISION)\n";
  print "Current version of XML::XUpdate::LibXML is $XML::XUpdate::LibXML::VERSION\n";
  exit 1;
}
pod2usage(-exitstatus => 0, -verbose => 2) if $help;
pod2usage() if ($usage or @ARGV<2);

my $parser  = XML::LibXML->new();
my $xupdate = XML::XUpdate::LibXML->new();

if ($indent||$extra_indent) {
  $parser->keep_blanks(0);
} else {
  $parser->keep_blanks(1);
}
my $dom     = $parser->parse_file($ARGV[1]);

if ($keep_ws) {
  $parser->keep_blanks(1);
} else {
  $parser->keep_blanks(0);
}
my $actions = $parser->parse_file($ARGV[0]);



xml_strip_whitespace($actions) unless $keep_ws;
$xupdate->process($dom->getDocumentElement(), $actions);
print $dom->toString(0+($indent||$extra_indent)+$extra_indent);

1;

__END__


=head1 xupdate

xupdate - Process XUpdate commands over an XML document

=head1 SYNOPSIS

xupdate [options] <xupdate-file> <input-file>

 Options:
   -u | --usage        print brief help on usage
   -h | --help         print documentation
   -k | --keep-ws      preserve whitespace in XUpdate file
   -V | --version      print current version and revision
   -i | --indent       indent the output XML
   -j | --extra-indent like -i, but also adds a leading and a trailing
                       linebreak to every text node.

 but also put an extra newline after
 every start-tag and before every end-tag

=head1 OPTIONS

=over 8

=item B<--usage>

Print a brief help message on usage and exits.

=item B<--help>

Prints the manual page and exits.

=item B<--keep-ws>

Preserves whitespace in XUpdate file. The default behaviour is to
strip any whitespace on the beginning or end of a text node in XUpdate
file.

=item B<--version>

Print version and revision number of B<This program> command and
version number of XML::XUpdate library used.

=item B<--indent>

Indent the resulting document on output.

=item B<--extra-indent>

Indent the resulting document on output as --indent, but also add a
leading and a trailing linebreak to every text node.

=back


=head1 DESCRIPTION

B<This program> will parse the given XUpdate file and the input file
and print the input file updated accordingly. XUpdate file format is
described in XUpdate Working Draft from 2000-09-14
(http://www.xmldb.org/xupdate/xupdate-wd.html).

=cut
