tools/xml_pp/xml_pp

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
« tools/xml_merge/xml_merge ↑ Summary tools/xml_split/xml_split »
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 File::Temp qw/tempfile/;
6 use File::Basename qw/dirname/;
7  
8 my @styles= XML::Twig->_pretty_print_styles; # from XML::Twig
9 my $styles= join '|', @styles; # for usage
10 my %styles= map { $_ => 1} @styles; # to check option
11  
12 my $DEFAULT_STYLE= 'indented';
13  
14 my $USAGE= "usage: $0 [-v] [-i<extension>] [-s ($styles)] [-p <tag(s)>] [-e <encoding>] [-l] [-f <file>] [<files>]";
15  
16 # because of the -i.bak option I don't think I can use one of the core
17 # option processing modules, so it's custom handling and no clusterization :--(
18  
19  
20 my %opt= process_options(); # changes @ARGV
21  
22 my @twig_options=( pretty_print => $opt{style},
23 error_context => 1,
24 );
25 if( $opt{preserve_space_in})
26 { push @twig_options, keep_spaces_in => $opt{preserve_space_in};}
27  
28 if( $opt{encoding})
29 { push @twig_options, output_encoding => $opt{encoding};
30 }
31 else
32 { push @twig_options, keep_encoding => 1; }
33  
34 # in normal (ie not -l) mode tags are output as soon as possible
35 push @twig_options, twig_handlers => { _all_ => sub { $_[0]->flush } }
36 unless( $opt{load});
37  
38 if( @ARGV)
39 { foreach my $file (@ARGV)
40 { print STDERR "$file\n" if( $opt{verbose});
41  
42 my $t= XML::Twig->new( @twig_options);
43  
44 my $tempfile;
45 if( $opt{in_place})
46 { (undef, $tempfile)= tempfile( DIR => dirname( $file)) or die "cannot create tempfile for $file: $!\n" ;
47 open( PP_OUTPUT, '>', $tempfile) or die "cannot create tempfile $tempfile: $!";
48 select PP_OUTPUT;
49 }
50 $t= $t->safe_parsefile( $file);
51  
52 if( $t)
53 { if( $opt{load}) { $t->print; }
54  
55 select STDOUT;
56  
57 if( $opt{in_place})
58 { close PP_OUTPUT;
59 my $mode= mode( $file);
60 if( $opt{backup})
61 { my $backup= backup( $file, $opt{backup});
62 rename( $file, $backup) or die "cannot create backup file $backup: $!";
63 }
64 rename( $tempfile, $file) or die "cannot overwrite file $file: $!";
65 if( $mode ne mode( $file)) { chmod $mode, $file or die "cannot set $file mode to $mode: $!"; }
66 }
67  
68 }
69 else
70 { if( defined $tempfile)
71 { unlink $tempfile or die "cannot unlink temp file $tempfile: $!"; }
72 die $@;
73 }
74 }
75 }
76 else
77 { my $t= XML::Twig->new( @twig_options);
78 $t->parse( \*STDIN);
79 if( $opt{load}) { $t->print; }
80 }
81  
82
83 sub mode
84 { my( $file)= @_;
85 return (stat($file))[2];
86 }
87
88 sub process_options
89 { my %opt;
90 while( @ARGV && ($ARGV[0]=~ m{^-}) )
91 { my $opt= shift @ARGV;
92 if( ($opt eq '-v') || ($opt eq '--verbose') )
93 { die $USAGE if( $opt{verbose});
94 $opt{verbose}= 1;
95 }
96 elsif( ($opt eq '-s') || ($opt eq '--style') )
97 { die $USAGE if( $opt{style});
98 $opt{style}= shift @ARGV;
99 die $USAGE unless( $styles{$opt{style}});
100 }
101 elsif( ($opt=~ m{^-i(.*)$}) || ($opt=~ m{^--in_place(.*)$}) )
102 { die $USAGE if( $opt{in_place});
103 $opt{in_place}= 1;
104 $opt{backup}= $1 ||'';
105 }
106 elsif( ($opt eq '-p') || ($opt eq '--preserve') )
107 { my $tags= shift @ARGV;
108 my @tags= split /\s+/, $tags;
109 $opt{preserve_space_in} ||= [];
110 push @{$opt{preserve_space_in}}, @tags;
111 }
112 elsif( ($opt eq '-e') || ($opt eq '--encoding') )
113 { die $USAGE if( $opt{encoding});
114 $opt{encoding}= shift @ARGV;
115 }
116 elsif( ($opt eq '-l') || ($opt eq '--load'))
117 { die $USAGE if( $opt{load});
118 $opt{load}=1;
119 }
120 elsif( ($opt eq '-f') || ($opt eq '--files') )
121 { my $file= shift @ARGV;
122 push @ARGV, files_from( $file);
123 }
124 elsif( ($opt eq '-h') || ($opt eq '--help'))
125 { system "pod2text", $0; exit; }
126 elsif( $opt eq '--')
127 { last; }
128 else
129 { die $USAGE; }
130 }
131  
132 $opt{style} ||= $DEFAULT_STYLE;
133  
134 return %opt;
135 }
136  
137 # get the list of files (one per line) from a file
138 sub files_from
139 { my $file= shift;
140 open( FILES, '<', $file) or die "cannot open file $file: $!";
141 my @files;
142 while( <FILES>) { chomp; push @files, $_; }
143 close FILES;
144 return @files;
145 }
146  
147 sub backup
148 { my( $file, $extension)= @_;
149 my $backup;
150 if( $extension=~ m{\*})
151 { ($backup= $extension)=~ s{\*}{$file}g; }
152 else
153 { $backup= $file.$extension; }
154 return $backup;
155 }
156
157 __END__
« tools/xml_merge/xml_merge ↑ Summary tools/xml_split/xml_split »