| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# $Id: xupdate,v 1.18 2003/12/03 12:01:54 pajas Exp $ |
| 4 |
|
| 5 |
use FindBin; |
| 6 |
use lib ("$FindBin::RealBin", "$FindBin::RealBin/../lib", |
| 7 |
"$FindBin::Bin","$FindBin::Bin/../lib", |
| 8 |
"$FindBin::Bin/lib", "$FindBin::RealBin/lib", |
| 9 |
"$FindBin::Bin/../lib/site_perl" |
| 10 |
); |
| 11 |
|
| 12 |
use strict; |
| 13 |
use Getopt::Long; |
| 14 |
use Pod::Usage; |
| 15 |
|
| 16 |
use vars qw($VERSION $REVISION $keep_ws $strip_ws $print_version $help $usage $indent $extra_indent @prefixmap); |
| 17 |
|
| 18 |
|
| 19 |
use XML::LibXML; |
| 20 |
use XML::XUpdate::LibXML; |
| 21 |
use XML::Normalize::LibXML qw(xml_strip_element); |
| 22 |
|
| 23 |
BEGIN { |
| 24 |
Getopt::Long::Configure ("bundling"); |
| 25 |
GetOptions('-help|h' => \$help, |
| 26 |
'-version|V' => \$print_version, |
| 27 |
'-keep-ws|k' => \$keep_ws, |
| 28 |
'-strip-ws|s' => \$strip_ws, |
| 29 |
'-indent|i' => \$indent, |
| 30 |
'-extra-indent|j' => \$extra_indent, |
| 31 |
'-namespace|n=s' => \@prefixmap, |
| 32 |
'-usage|u' => \$usage, |
| 33 |
'-debug|D' => \$XML::XUpdate::LibXML::debug, |
| 34 |
) || die "Wrong command-line arguments\n"; |
| 35 |
$VERSION='0.4'; |
| 36 |
$REVISION='$Revision: 1.18 $'; |
| 37 |
} |
| 38 |
|
| 39 |
if ($print_version) { |
| 40 |
print "Current version is $VERSION ($REVISION)\n"; |
| 41 |
print "Current version of XML::XUpdate::LibXML is $XML::XUpdate::LibXML::VERSION\n"; |
| 42 |
exit 1; |
| 43 |
} |
| 44 |
pod2usage(-exitstatus => 0, -verbose => 2) if $help; |
| 45 |
pod2usage() if ($usage or @ARGV<2); |
| 46 |
|
| 47 |
my $parser = XML::LibXML->new(); |
| 48 |
my $xupdate = XML::XUpdate::LibXML->new(); |
| 49 |
|
| 50 |
foreach (@prefixmap) { |
| 51 |
my ($prefix,$uri)= split /\s*=\s*/,$_; |
| 52 |
if ($prefix ne '' and $uri ne '') { |
| 53 |
$xupdate->registerNs($prefix,$uri); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if ($extra_indent||$strip_ws) { |
| 58 |
$parser->keep_blanks(0); |
| 59 |
} else { |
| 60 |
$parser->keep_blanks(1); |
| 61 |
} |
| 62 |
my $dom = $parser->parse_file($ARGV[1]); |
| 63 |
|
| 64 |
if ($keep_ws) { |
| 65 |
$parser->keep_blanks(1); |
| 66 |
} else { |
| 67 |
$parser->keep_blanks(0); |
| 68 |
} |
| 69 |
my $actions = $parser->parse_file($ARGV[0]); |
| 70 |
|
| 71 |
unless ($keep_ws) { |
| 72 |
my $command=$actions->getDocumentElement->firstChild; |
| 73 |
while ($command) { |
| 74 |
xml_strip_element($command); |
| 75 |
$command = $command->nextSibling(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$xupdate->process($dom->getDocumentElement(), $actions); |
| 80 |
|
| 81 |
print $dom->toString(0+($indent||$extra_indent)+$extra_indent); |
| 82 |
|
| 83 |
1; |
| 84 |
|
| 85 |
__END__ |
| 86 |
|
| 87 |
|
| 88 |
=head1 xupdate |
| 89 |
|
| 90 |
xupdate - Process XUpdate commands over an XML document |
| 91 |
|
| 92 |
=head1 SYNOPSIS |
| 93 |
|
| 94 |
xupdate [options] <xupdate-file> <input-file> |
| 95 |
|
| 96 |
Options: |
| 97 |
-u | --usage print brief help on usage |
| 98 |
-h | --help print documentation |
| 99 |
|
| 100 |
-n | --namespace prefix=namespace-uri |
| 101 |
associate a namespace with a prefix for use |
| 102 |
in XPath selections in XUpdate file |
| 103 |
(this option may occur several times) |
| 104 |
|
| 105 |
-k | --keep-ws preserve whitespace in the XUpdate file |
| 106 |
-s | --strip-ws strip ignorable whitespace from the input file |
| 107 |
-V | --version print current version and revision |
| 108 |
-i | --indent indent the output XML |
| 109 |
-j | --extra-indent like -i, but also adds a leading and a trailing |
| 110 |
linebreak to every text node. |
| 111 |
|
| 112 |
but also put an extra newline after |
| 113 |
every start-tag and before every end-tag |
| 114 |
|
| 115 |
=head1 OPTIONS |
| 116 |
|
| 117 |
=over 8 |
| 118 |
|
| 119 |
=item B<--usage> |
| 120 |
|
| 121 |
Print a brief help message on usage and exits. |
| 122 |
|
| 123 |
=item B<--help> |
| 124 |
|
| 125 |
Prints the manual page and exits. |
| 126 |
|
| 127 |
=item B<--namespace> prefix=namespace-uri |
| 128 |
|
| 129 |
Associate a namespace with a prefix. The prefix may be used in the |
| 130 |
XPath selections in the XUpdate file to address nodes of the source |
| 131 |
document that belong to the given namespace. This is especially |
| 132 |
useful for mapping the default namespace to a prefix because XPath by |
| 133 |
definition doesn't honour default namespaces. This option may occur |
| 134 |
several times. |
| 135 |
|
| 136 |
=item B<--keep-ws> |
| 137 |
|
| 138 |
Preserves any whitespace in XUpdate file. The default behaviour is to |
| 139 |
remove all ignorable whitespace and any leading or trailing whitespace |
| 140 |
in all XUpdate command elements in the XUpdate file. |
| 141 |
|
| 142 |
=item B<--strip-ws> |
| 143 |
|
| 144 |
Remove "ignorable" whitespace from the input file. The default |
| 145 |
behaviour is to keep any whitespace unless the --extra-indent (-j) |
| 146 |
option is used. Note that the whitespace being present or not may |
| 147 |
affect results returned by some XPath expressions (such as |
| 148 |
/foo/bar/text()[2]). |
| 149 |
|
| 150 |
=item B<--version> |
| 151 |
|
| 152 |
Print version and revision number of B<This program> command and |
| 153 |
version number of XML::XUpdate library used. |
| 154 |
|
| 155 |
=item B<--indent> |
| 156 |
|
| 157 |
Indent the resulting document on output. |
| 158 |
|
| 159 |
=item B<--extra-indent> |
| 160 |
|
| 161 |
Indent the resulting document on output as --indent, but also add a |
| 162 |
leading and a trailing linebreak to every text node. |
| 163 |
|
| 164 |
=item B<--debug> |
| 165 |
|
| 166 |
Print some debugging information about commands being applied. |
| 167 |
|
| 168 |
=back |
| 169 |
|
| 170 |
|
| 171 |
=head1 DESCRIPTION |
| 172 |
|
| 173 |
B<This program> will parse the given XUpdate file and the input file |
| 174 |
and print the input file updated accordingly. XUpdate file format is |
| 175 |
described in XUpdate Working Draft from 2000-09-14 |
| 176 |
(http://www.xmldb.org/xupdate/xupdate-wd.html). |
| 177 |
|
| 178 |
=cut |
| 179 |
|
| 180 |
=head1 AUTHOR |
| 181 |
|
| 182 |
Petr Pajas, [email protected] |
| 183 |
|
| 184 |
=head1 COPYRIGHT |
| 185 |
|
| 186 |
Copyright 2002-2003 Petr Pajas, All rights reserved. |
| 187 |
|
| 188 |
This program is free software; you can redistribute it and/or modify it |
| 189 |
under the same terms as Perl itself. |