Alvis-Convert

 view release on metacpan or  search on metacpan

bin/alvisXMLjoin  view on Meta::CPAN

#!/usr/bin/perl -w

use strict;

use Getopt::Long;
use Pod::Usage;
use Encode;

use Alvis::Utils qw(open_file);

use encoding 'utf8';
use open ':utf8';

my $xml_header = '<?xml version="1.0" encoding="UTF-8"?>';
my $doc_col = '<documentCollection xmlns="http://alvis.info/enriched/" version="1.1">';
my $doc_col_end = '</documentCollection>';

################################################################################
# main

join_files(read_params());

################################################################################
sub join_files {

	my $files = shift;

	print $xml_header, "\n", $doc_col, "\n";
	for my $file ( @{$files} ) {
		my $FP = open_file($file);
		for my $str (<$FP>) {
			if ( $str =~ /^<\?xml version/ ) {
				next;
			}
			elsif ( $str =~ /^\s*<documentCollection/ ) {
				next;
			}
			elsif ( $str =~ /^\s*<\/documentCollection/ ) {
				next;
			}
			else {
				print $str;
			}
		}
		close $FP;
	}
	print $doc_col_end, "\n";
}

################################################################################
sub read_params 
{
	my @files;
	GetOptions(
		'<>' => sub { push @files, @_ },    
		'h|help' => sub { pod2usage(1) }
	);

	pod2usage( -message => "ERROR: list of files is not specified" )
	  if ( !defined( $files[0] ) );

	# TODO: this is the case then directory is emty
	return \() if ($files[0] =~ /\/\*/ && not(-e $files[0]));  
	for my $file (@files) {
		pod2usage( -message => "ERROR: file does not exist '$file'" )
		  unless (-f $file);
	}  
	return \@files;
}
################################################################################

__END__



( run in 0.573 second using v1.01-cache-2.11-cpan-140bd7fdf52 )