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