File: mab2_convert.pl

package info (click to toggle)
libcatmandu-mab2-perl 0.24-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 372 kB
  • sloc: perl: 815; xml: 23; makefile: 7; sh: 1
file content (113 lines) | stat: -rw-r--r-- 2,550 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl

# PODNAME: mab2_convert.pl
our $VERSION = '0.11';

use utf8;
use warnings;

use Getopt::Long;
use MAB2::Parser::Disk;
use MAB2::Parser::RAW;
use MAB2::Parser::XML;
use MAB2::Writer::Disk;
use MAB2::Writer::RAW;
use MAB2::Writer::XML;

my ($in, $out, $from, $to);

my $options_ok = GetOptions (
    'i=s'     => \$in,
    'o=s'     => \$out,
    'f=s'     => \$from,
    't=s'     => \$to,
    'help'    => sub { cli_usage() },
    'usage'   => sub { cli_usage() },
);

cli_usage() if !$options_ok or !$in or !$out or !$from or !$to;

my $formats = qr{^(Disk|RAW|XML)$};
die "Not a valid input format \"$from\"" if $from !~ m/$formats/;
die "Not a valid output format \"$to\"" if $to !~ m/$formats/;

convert($in, $out, $from, $to);

sub cli_usage {
    
    my $usage = <<END;

Usage: mab2_convert.pl -i mab2raw.dat -o mab2xml.xml -f RAW -t XML

Description: Convert records from one MAB2 format to an other.

Options:
    -i        Specify input file.
    -o        Specify output file.
    -f        Specify input format (Disk|RAW|XML).
    -t        Specify output format (Disk|RAW|XML).
    
    --help    Print this documentation
END
    
    print "Unknown option: @_\n" if ( @_ );
    print "$usage\n";
    exit;
}

sub convert {
    my ($in, $out, $from, $to) = @_;
    print "START\n";
    my $parser = "MAB2::Parser::$from"->new( $in );
    my $writer;
    if ($to eq 'XML') {
        $writer = MAB2::Writer::XML->new( file => $out, xml_declaration => 1, collection => 1 );
        $writer->start();
    }
    else{
        $writer = "MAB2::Writer::$to"->new( file => $out );
    
    }
    while ( my $record = $parser->next() ) {
        $writer->write($record);        
    }
    $writer->end() if $to eq 'XML';
    print "DONE\n";
}

__END__

=pod

=encoding UTF-8

=head1 NAME

mab2_convert.pl - converter for MAB2 formats

=head1 SYNOPSIS

    Usage: mab2_convert.pl -i mab2raw.dat -o mab2xml.xml -f RAW -t XML

    Description: Convert records from one MAB2 format to an other.

    Options:
        -i        Specify input file.
        -o        Specify output file.
        -f        Specify input format (Disk|RAW|XML).
        -t        Specify output format (Disk|RAW|XML).
        
        --help    Print this documentation

=head1 AUTHOR

Johann Rolschewski <jorol@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Johann Rolschewski.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut