tools/xml_merge/xml_merge

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/XML/Twig/XPath.pm ↑ Summary tools/xml_pp/xml_pp »
1 #!/usr/bin/perl -w
2 # $Id: /xmltwig/trunk/tools/xml_merge/xml_merge 12 2007-04-22T06:04:54.627880Z mrodrigu $
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 21 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use FindBin qw( $RealBin $RealScript);
7 use Getopt::Std;
8  
9 $Getopt::Std::STANDARD_HELP_VERSION=1; # to stop processing after --help or --version
10  
11 use vars qw( $VERSION $USAGE);
12  
13 $VERSION= "0.02";
14 $USAGE= "xml_merge [-o <output_file>] [-i] [-v] [-h] [-m] [-V] [file]\n";
15  
16 { # main block
17  
18 my $opt={};
19 getopts('o:ivhmV', $opt);
20  
21 if( $opt->{h}) { die $USAGE, "\n"; }
22 if( $opt->{m}) { exec "pod2text $RealBin/$RealScript"; }
23 if( $opt->{V}) { print "xml_merge version $VERSION\n"; exit; }
24  
25 if( $opt->{o})
26 { open( my $out, '>', $opt->{o}) or die "cannot create $opt->{o}: $!";
27 $opt->{fh}= $out; # used to set twig_print_outside_roots
28 }
29 else
30 { $opt->{fh}= 1; } # this way twig_print_outside_roots outputs to STDOUT
31  
32 $opt->{subdocs} = 1;
33 $opt->{file} = $ARGV[0];
34  
35 $opt->{twig_roots}= $opt->{i} ? { 'xi:include' => sub { $opt->{file}= $_->att( 'href');
36 if( $_->att( 'subdocs')) { merge( $opt); }
37 else { spit( $opt); }
38 },
39 }
40 : { '?merge' => sub { $opt= parse( $_->data, $opt);
41 if( $opt->{subdocs}) { merge( $opt); }
42 else { spit( $opt); }
43 },
44 }
45  
46 ;
47  
48 merge( $opt);
49  
50 if( $opt->{v}) { warn "done\n"; }
51  
52 }
53  
54 sub merge
55 { my( $opt)= @_;
56 my $t= XML::Twig->new( keep_encoding => 1, keep_spaces => 1,
57 twig_roots => $opt->{twig_roots},
58 twig_print_outside_roots => $opt->{fh},
59 );
60 if( $opt->{v} && $opt->{file}) { warn "merging $opt->{file} (parsing)\n"; }
61 if( $opt->{file}) { $t->parsefile( $opt->{file}); } else { $t->parse( \*STDIN); }
62 }
63  
64 sub spit
65 { my( $opt)= @_;
66 if( $opt->{v} && $opt->{file}) { warn "merging $opt->{file} (no parsing)\n"; }
67 open( my $in, '<', $opt->{file}) or die "cannot open sub document '$opt->{file}': $!";
68 while( <$in>)
69 { next if( m{^\Q<?xml version} || m{^\s*</?xml_split:root});
70 if( $opt->{o}) { print {$opt->{fh}} $_; } else { print $_; }
71 }
72 close $in;
73 }
74  
75 # data is the pi data,
76 # (ugly) format is keyword1 = val1 : keyword2 = val2 ... : filename
77 # ex: subdoc = 1 : file-01.xml
78  
79 sub parse
80 { my( $data, $opt)= @_;
81 while( $data=~ s{^\s*(\S+)\s*=\s*(\S+)\s*:\s*}{}) { $opt->{$1}= $2; }
82 $opt->{file}= $data;
83 return $opt;
84 }
85
86
87 # for Getop::Std
88 sub HELP_MESSAGE { return $USAGE; }
89 sub VERSION_MESSAGE { return $VERSION; }
90  
91 __END__
« blib/lib/XML/Twig/XPath.pm ↑ Summary tools/xml_pp/xml_pp »