#!/usr/bin/perl -w

=head1	NAME

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


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 OpenOffice.org document must exist (it can be an empty document
or a template).

usage : search target_file source_file "search_string"


use OpenOffice::OODoc;

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) = @_;
		# 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 : search target_file source_file search_string\n"
	unless ($target_file && $source_file && $search_string);

	# create the 2 OO-Text objects
print "Opening the target file $target_file...\n";
my $target_doc	= ooText(file => $target_file)
	or die "$target_file is not available\n";
print "Opening the source file $source_file...\n";
my $source_doc	= ooText(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";
			$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";
print "Job complete\n";