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