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