| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | # vim:ts=8 sw=4 sts=4 ai | ||||||||||
| 3 | 1 | require v5.6.1; | |||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | xml2fv - convert an XML file into Tie::FieldVals data. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 VERSION | ||||||||||
| 12 | |||||||||||
| 13 | This describes version B<0.6203> of xml2fv. | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 1 | our $VERSION = '0.6203'; | |||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | xml2fv --help | --manpage | --version | ||||||||||
| 22 | |||||||||||
| 23 | xml2fv I<xmlfile> [ I<outfile> ] | ||||||||||
| 24 | |||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||
| 26 | |||||||||||
| 27 | This script converts an XML file into Tie::FieldVals data. | ||||||||||
| 28 | |||||||||||
| 29 | =head1 OPTIONS | ||||||||||
| 30 | |||||||||||
| 31 | =over | ||||||||||
| 32 | |||||||||||
| 33 | =item --help | ||||||||||
| 34 | |||||||||||
| 35 | Print help message and exit. | ||||||||||
| 36 | |||||||||||
| 37 | =item --manpage | ||||||||||
| 38 | |||||||||||
| 39 | Print the full help documentation (manual page) and exit. | ||||||||||
| 40 | |||||||||||
| 41 | =item --verbose | ||||||||||
| 42 | |||||||||||
| 43 | Print informational messages. | ||||||||||
| 44 | |||||||||||
| 45 | =item --version | ||||||||||
| 46 | |||||||||||
| 47 | Print version information and exit. | ||||||||||
| 48 | |||||||||||
| 49 | =back | ||||||||||
| 50 | |||||||||||
| 51 | =head1 FILE FORMATS | ||||||||||
| 52 | |||||||||||
| 53 | =head2 XML FORMAT | ||||||||||
| 54 | |||||||||||
| 55 | The format of the input XML file is as follows: | ||||||||||
| 56 | |||||||||||
| 57 | <fv_data> | ||||||||||
| 58 | <record> | ||||||||||
| 59 | <Field>Value</Field> | ||||||||||
| 60 | <AnotherField>AnotherValue</AnotherField> | ||||||||||
| 61 | ... | ||||||||||
| 62 | </record> | ||||||||||
| 63 | ... | ||||||||||
| 64 | </fv_data> | ||||||||||
| 65 | |||||||||||
| 66 | Each field name is given its own tag, and the element contains | ||||||||||
| 67 | the value for that field. | ||||||||||
| 68 | |||||||||||
| 69 | =head2 FieldVals Format | ||||||||||
| 70 | |||||||||||
| 71 | The output data file is in the form of Field:Value pairs, with each | ||||||||||
| 72 | record separated by a line with '=' on it. | ||||||||||
| 73 | |||||||||||
| 74 | See L<Tie::FieldVals/FILE FORMAT> for more information. | ||||||||||
| 75 | |||||||||||
| 76 | =head1 REQUIRES | ||||||||||
| 77 | |||||||||||
| 78 | Getopt::Long | ||||||||||
| 79 | Pod::Usage | ||||||||||
| 80 | Getopt::ArgvFile | ||||||||||
| 81 | Data::Dumper | ||||||||||
| 82 | Tie::FieldVals::Row | ||||||||||
| 83 | |||||||||||
| 84 | =head1 SEE ALSO | ||||||||||
| 85 | |||||||||||
| 86 | perl(1) | ||||||||||
| 87 | Getopt::Long | ||||||||||
| 88 | Getopt::ArgvFile | ||||||||||
| 89 | Pod::Usage | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | 1 | ▶ | use Getopt::Long 2.34; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 94 | 1 | ▶ | use Getopt::ArgvFile qw(argvFile); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 1 | ▶ | use Pod::Usage; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 96 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 97 | 1 | ▶ | use Tie::FieldVals::Row; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 98 | |||||||||||
| 99 | #======================================================== | ||||||||||
| 100 | # Subroutines | ||||||||||
| 101 | 1 | ▶ | sub init_data ($) { | ||||||||
|
Subroutine
init_data: called CC=1 SCAR=0.0
| |||||||||||
| 102 | 1 | my $data_ref = shift; | |||||||||
| 103 | |||||||||||
| 104 | # options | ||||||||||
| 105 | 1 | my %default_conf = (); | |||||||||
| 106 | 1 | $default_conf{debug} = 0; | |||||||||
| 107 | 1 | $default_conf{manpage} = 0; | |||||||||
| 108 | 1 | $default_conf{version} = 0; | |||||||||
| 109 | 1 | $default_conf{verbose} = 0; | |||||||||
| 110 | 1 | $default_conf{outfile} = '-'; | |||||||||
| 111 | 1 | $default_conf{xmlfile} = ''; | |||||||||
| 112 | 1 | $data_ref->{options} = \%default_conf; | |||||||||
| 113 | } # init_data | ||||||||||
| 114 | |||||||||||
| 115 | 1 | ▶ | sub process_args ($) { | ||||||||
|
Subroutine
process_args: called CC=4 SCAR=14.6
| |||||||||||
| 116 | 1 | my $data_ref = shift; | |||||||||
| 117 | |||||||||||
| 118 | 1 | my $ok = 1; | |||||||||
| 119 | |||||||||||
| 120 | 1 | argvFile(home=>1,current=>1,startupFilename=>'.xml2fvrc'); | |||||||||
| 121 | |||||||||||
| 122 | 1 | ▶ | pod2usage(2) unless @ARGV; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | |||||||||||
| 124 | 1 | my $op = new Getopt::Long::Parser; | |||||||||
| 125 | 1 | $op->configure(qw(auto_version auto_help)); | |||||||||
| 126 | $op->getoptions($data_ref->{options}, | ||||||||||
| 127 | 1 | ▶ | 'verbose!', | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 'manpage', | ||||||||||
| 129 | 'debug!', | ||||||||||
| 130 | 'xmlfile=s', | ||||||||||
| 131 | 'outfile=s', | ||||||||||
| 132 | ) or pod2usage(2); | ||||||||||
| 133 | |||||||||||
| 134 | 1 | ▶ | if ($data_ref->{options}->{'manpage'}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | { | ||||||||||
| 136 | 0 | pod2usage({ -message => "$0 version $VERSION", | |||||||||
| 137 | -exitval => 0, | ||||||||||
| 138 | -verbose => 2, | ||||||||||
| 139 | }); | ||||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | } # process_args | ||||||||||
| 143 | |||||||||||
| 144 | 1 | ▶ | sub convert_file ($) { | ||||||||
|
Subroutine
convert_file: called CC=14 SCAR=27.0
| |||||||||||
| 145 | 1 | my $data_ref = shift; | |||||||||
| 146 | |||||||||||
| 147 | 1 | my $outfile = $data_ref->{options}->{outfile}; | |||||||||
| 148 | |||||||||||
| 149 | print STDERR "xmlfile: ", $data_ref->{options}->{xmlfile}, | ||||||||||
| 150 | " outfile: ", $outfile, | ||||||||||
| 151 | 1 | ▶ | "\n" if ($data_ref->{options}->{verbose}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 1 | ▶ | if ($data_ref->{options}->{debug}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | { | ||||||||||
| 154 | 0 | print STDERR Data::Dumper->Dump([$data_ref], [qw(xml2fv)]); | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | 1 | my $outhandle = \*STDOUT; | |||||||||
| 158 | 1 | ▶ | if ($outfile ne '-') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | { | ||||||||||
| 160 | 1 | ▶ | open(OUTFILE, ">$outfile") || die "Can't open '$outfile' for writing."; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 1 | $outhandle = \*OUTFILE; | |||||||||
| 162 | } | ||||||||||
| 163 | # open the XML file and go through it, with </record> | ||||||||||
| 164 | # as the record separator | ||||||||||
| 165 | 1 | my $recsep = '</record>'; | |||||||||
| 166 | 1 | my $count = 0; | |||||||||
| 167 | 1 | my @field_names = (); | |||||||||
| 168 | { | ||||||||||
| 169 | 1 | local $/ = $recsep; | |||||||||
| 170 | open(FILE, $data_ref->{options}->{xmlfile}) | ||||||||||
| 171 | 1 | ▶ | or die "cannot open ", $data_ref->{options}->{xmlfile}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 1 | ▶ | while (my $rec = <FILE>) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | { | ||||||||||
| 174 | 120 | ▶ | warn $rec, "\n" if $data_ref->{options}->{debug}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 175 | 120 | ▶ | if ($rec =~ /<record>/) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | { | ||||||||||
| 177 | 119 | my %row = (); | |||||||||
| 178 | 119 | my $row_obj; | |||||||||
| 179 | 119 | ▶ | if (@field_names) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | { | ||||||||||
| 181 | 118 | $row_obj = tie %row, 'Tie::FieldVals::Row', | |||||||||
| 182 | fields=>\@field_names; | ||||||||||
| 183 | 118 | $row_obj->set_from_xml_string($rec); | |||||||||
| 184 | } | ||||||||||
| 185 | else # get the field names from the first record | ||||||||||
| 186 | { | ||||||||||
| 187 | 1 | $row_obj = tie %row, 'Tie::FieldVals::Row', | |||||||||
| 188 | fields=>[qw(dummy)]; | ||||||||||
| 189 | 1 | $row_obj->set_from_xml_string($rec, | |||||||||
| 190 | override_keys=>1); | ||||||||||
| 191 | 1 | @field_names = @{$row_obj->field_names()}; | |||||||||
| 192 | # print the initial empty record | ||||||||||
| 193 | 1 | foreach my $fn (@field_names) | |||||||||
| 194 | { | ||||||||||
| 195 | 11 | print $outhandle "$fn:\n"; | |||||||||
| 196 | } | ||||||||||
| 197 | 1 | print $outhandle "=\n"; | |||||||||
| 198 | } | ||||||||||
| 199 | # print the current record | ||||||||||
| 200 | 119 | print $outhandle $row_obj->get_as_string(); | |||||||||
| 201 | 119 | print $outhandle "\n=\n"; | |||||||||
| 202 | 119 | $count++; | |||||||||
| 203 | } | ||||||||||
| 204 | } | ||||||||||
| 205 | 1 | close(FILE); | |||||||||
| 206 | } | ||||||||||
| 207 | |||||||||||
| 208 | 1 | ▶ | print STDERR "$count records processed\n" if ($data_ref->{options}->{verbose}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 209 | |||||||||||
| 210 | 1 | ▶ | if ($outfile ne '-') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 211 | { | ||||||||||
| 212 | 1 | close(OUTFILE); | |||||||||
| 213 | } | ||||||||||
| 214 | 1 | ▶ | print STDERR "done!\n" if ($data_ref->{options}->{verbose}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 215 | } # convert_file | ||||||||||
| 216 | |||||||||||
| 217 | #======================================================== | ||||||||||
| 218 | # Main | ||||||||||
| 219 | |||||||||||
| 220 | MAIN: { | ||||||||||
| 221 | 1 | my %data = (); | |||||||||
| 222 | |||||||||||
| 223 | 1 | init_data(\%data); | |||||||||
| 224 | 1 | process_args(\%data); | |||||||||
| 225 | |||||||||||
| 226 | # first argument is the xmlfile | ||||||||||
| 227 | 1 | ▶ | if (@ARGV) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 228 | { | ||||||||||
| 229 | 1 | $data{options}->{xmlfile} = shift @ARGV; | |||||||||
| 230 | } | ||||||||||
| 231 | 1 | ▶ | if (!$data{options}->{xmlfile}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 232 | { | ||||||||||
| 233 | 0 | print STDERR "$0 no xmlfile\n"; | |||||||||
| 234 | 0 | return 1; | |||||||||
| 235 | } | ||||||||||
| 236 | 1 | ▶ | if (!-e $data{options}->{xmlfile}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 237 | { | ||||||||||
| 238 | 0 | print STDERR "$0 xmlfile not found\n"; | |||||||||
| 239 | 0 | return 1; | |||||||||
| 240 | } | ||||||||||
| 241 | |||||||||||
| 242 | # remaining argument is the output file | ||||||||||
| 243 | 1 | ▶ | if (@ARGV) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 244 | { | ||||||||||
| 245 | 1 | $data{options}->{outfile} = shift @ARGV; | |||||||||
| 246 | } | ||||||||||
| 247 | 1 | convert_file(\%data); | |||||||||
| 248 | } | ||||||||||
| 249 | |||||||||||
| 250 | =head1 BUGS | ||||||||||
| 251 | |||||||||||
| 252 | Please report any bugs or feature requests to the author. | ||||||||||
| 253 | |||||||||||
| 254 | =head1 AUTHOR | ||||||||||
| 255 | |||||||||||
| 256 | Kathryn Andersen (RUBYKAT) | ||||||||||
| 257 | perlkat AT katspace dot com | ||||||||||
| 258 | http://www.katspace.com | ||||||||||
| 259 | |||||||||||
| 260 | =head1 COPYRIGHT AND LICENCE | ||||||||||
| 261 | |||||||||||
| 262 | Copyright (c) 2004 by Kathryn Andersen | ||||||||||
| 263 | |||||||||||
| 264 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 265 | under the same terms as Perl itself. | ||||||||||
| 266 | |||||||||||
| 267 | |||||||||||
| 268 | =cut | ||||||||||
| 269 | |||||||||||
| 270 | __END__ | ||||||||||