The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl
#-----------------------------------------------------------------------------
#	$Id : odfsearch 0.3 2010-01-11 JMG$
#-----------------------------------------------------------------------------

=head1	NAME

odfsearch - Text selection and copy from one document to another using callback

=head1	SYNOPSIS

This sample script extracts the content of every text element (paragraph,
header, list item) in a source document matching a given pattern (string or
regex), and appends it as a new paragraph to a target document. The target
document must exist.

The target document must exist (it can be an empty document or a template).

usage : odfsearch target_file source_file "search_string"

=cut

use OpenOffice::OODoc	2.101;

our $count	= 0;

#-----------------------------------------------------------------------------
# callback function executed each time a matching element is found while
# the selectElementsByContent method is running.
# the last argument is the selected element; it's preceded by
# application-specific arguments

sub	append_text
	{
	my ($dst, $stylename, $element) = @_;
	$count++;
		# convert the given element to flat text
		# we use getText as a class method; here we don't need/know
		# the context of the source document
	my $text	= OpenOffice::OODoc::Text->getText($element);
		# append as a new paragraph to target document
	$dst->appendParagraph(text => $text, style => $stylename);
	return undef;
	}

#-----------------------------------------------------------------------------
# main program

	# get the command line arguments
my $target_file		= $ARGV[0];
my $source_file		= $ARGV[1];
my $search_string	= $ARGV[2];
my $stylename		= $ARGV[3] || 'Text body';

die "Usage : odfsearch target_file source_file search_string\n"
	unless ($target_file && $source_file && $search_string);

	# create the 2 Document instances
print "Opening the target file $target_file...\n";
my $target_doc	= odfDocument(file => $target_file)
	or die "$target_file is not available\n";
print "Opening the source file $source_file...\n";
my $source_doc	= odfDocument(file => $source_file)
	or die "$source_file is not available\n";

	# the main processing takes place here :
	# the search method invocation, with search string and reference to
	# the procedure to be executed to process each selected element.
	# The selected element is automatically passed as an argument to the
	# callback, following explicit (optional) arguments provided by
	# the application (here $source_doc, $target_doc, $stylename)
print "Selecting the content...\n";
$source_doc->selectElementsByContent
			(
			$search_string,		# filter expression
			\&append_text,		# callback function reference
			$target_doc, $stylename	# callback function arguments
			);

	# append a conclusion
my $report	=	"$count text elements from $source_file "	.
			"have been selected, converted in paragraphs " .
			"and appended to $target_file. Good reading.";
$target_doc->appendParagraph(text => $report, style => $stylename);
		
	# save the changes and leave
print "Saving the target document...\n";
$target_doc->save;
print "Job complete\n";
exit;

#-----------------------------------------------------------------------------