| 1 |
#!/usr/bin/perl |
| 2 |
#----------------------------------------------------------------------------- |
| 3 |
# $Id : odffindbasic 0.2 2008-05-04 JMG$ |
| 4 |
#----------------------------------------------------------------------------- |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
odffindbasic - Basic macro removal from OpenOffice.org files |
| 9 |
|
| 10 |
=head1 USAGE |
| 11 |
|
| 12 |
odffindbasic [options] <sourcefile> [<targetfile>] |
| 13 |
|
| 14 |
=head1 DESCRIPTION |
| 15 |
|
| 16 |
A simple command that allows the user to detect, export or remove the |
| 17 |
Basic modules from a regular OpenOffice.org file. |
| 18 |
|
| 19 |
Without option, the program displays the number of Basic modules found |
| 20 |
in the file. |
| 21 |
|
| 22 |
The 2nd filename is used in combination with the --delete option. |
| 23 |
|
| 24 |
=head1 OPTIONS |
| 25 |
|
| 26 |
--nocount |
| 27 |
Prevents the program from displaying the number of Basic |
| 28 |
modules found. |
| 29 |
|
| 30 |
--delete -d |
| 31 |
If this option is set, the Basic modules are physically |
| 32 |
deleted. The file manifest is updated accordingly. |
| 33 |
However, the document content remains unchanged, even if |
| 34 |
it contains some references to the deleted macros. The |
| 35 |
code is removed (so the macros are no longer executable). |
| 36 |
If a target file name is provided as a 2nd argument, the |
| 37 |
changes are saved in it and the source file remains |
| 38 |
unchanged. |
| 39 |
|
| 40 |
--export -e |
| 41 |
Exports the macros. Every Basic module is extracted and |
| 42 |
converted to a flat Basic source file in the current |
| 43 |
directory. The name of each created file is constructed |
| 44 |
according to the corresponding path in the ODF file, and |
| 45 |
its suffix is "bas". |
| 46 |
|
| 47 |
--list -l |
| 48 |
The Basic modules are listed through the standard |
| 49 |
output. |
| 50 |
|
| 51 |
--verbose |
| 52 |
Some information messages are printed. |
| 53 |
|
| 54 |
=cut |
| 55 |
|
| 56 |
use OpenOffice::OODoc 2.101; |
| 57 |
use Getopt::Long; |
| 58 |
|
| 59 |
my $show_count = 1; |
| 60 |
GetOptions |
| 61 |
( |
| 62 |
'nocount' => sub { $show_count = 0; }, |
| 63 |
'list' => \(my $list = undef), |
| 64 |
'delete' => \(my $delete = undef), |
| 65 |
'export' => \(my $export = undef), |
| 66 |
'verbose' => \(my $verbose = undef) |
| 67 |
); |
| 68 |
|
| 69 |
exit unless $ARGV[0]; |
| 70 |
my $input = $ARGV[0] |
| 71 |
or die "Usage: oofindbasic <source_odf> [<target_odf>]\n"; |
| 72 |
my $output = $ARGV[1] || $input; |
| 73 |
print "Loading $input\n" if $verbose; |
| 74 |
my $archive = odfFile($input) or die "Unavailable file\n"; |
| 75 |
my $manifest = undef; |
| 76 |
if ($delete) |
| 77 |
{ |
| 78 |
print "Extracting the manifest\n" if $verbose; |
| 79 |
$manifest = odfManifest(file => $archive); |
| 80 |
} |
| 81 |
my $deleted = 0; |
| 82 |
my $count = 0; |
| 83 |
foreach my $m (@{$archive->{members}}) |
| 84 |
{ |
| 85 |
next unless $m =~ /^Basic/; |
| 86 |
if ($delete) |
| 87 |
{ |
| 88 |
push @to_be_deleted, $m; |
| 89 |
$deleted++; |
| 90 |
} |
| 91 |
next if $m =~ /\/script\-[A-Za-z]*\.xml$/; |
| 92 |
print "$m\n" if $list; |
| 93 |
$count++; |
| 94 |
if ($export) |
| 95 |
{ |
| 96 |
my $module = odfDocument |
| 97 |
( |
| 98 |
container => $archive, |
| 99 |
part => $m, |
| 100 |
read_only => 'true', |
| 101 |
element => 'script:module' |
| 102 |
); |
| 103 |
my $filename = $m; |
| 104 |
$filename =~ s/[\/\\]/_/g; |
| 105 |
$filename =~ s/\.xml$/.bas/i; |
| 106 |
print "Exporting $filename\n" if $verbose; |
| 107 |
open BASIC, ">", $filename; |
| 108 |
print BASIC $module->{xpath}->root->string_value(); |
| 109 |
close BASIC; |
| 110 |
$module->dispose; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if ($show_count) |
| 115 |
{ |
| 116 |
if ($verbose) |
| 117 |
{ |
| 118 |
print "Number of BASIC modules in the file: "; |
| 119 |
} |
| 120 |
print "$count\n"; |
| 121 |
} |
| 122 |
|
| 123 |
if ($deleted) |
| 124 |
{ |
| 125 |
foreach my $member (@to_be_deleted) |
| 126 |
{ |
| 127 |
print "Deleting $member\n" if $verbose; |
| 128 |
$archive->raw_delete($member); |
| 129 |
$manifest->removeEntry($member); |
| 130 |
} |
| 131 |
print "Writing the changes to $output\n" if $verbose; |
| 132 |
$archive->save($output); |
| 133 |
} |
| 134 |
exit; |