tools/xml_split/xml_split

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_pp/xml_pp ↑ Summary
1 #!/usr/bin/perl -w
2 22 ▶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 import xml_split::state::parser;
9 import xml_split::state::twig;
10  
11 undef $Getopt::Std::STANDARD_HELP_VERSION;
12 $Getopt::Std::STANDARD_HELP_VERSION=1; # to stop processing after --help or --version
13  
14 use vars qw( $VERSION $USAGE);
15  
16 $VERSION= "0.06";
17 $USAGE= "xml_split [-l <level> [-s <size> | -g <nb_grouped>] | -c <cond>] [-b <base>] [-n <nb>] [-e <ext>] [-p <plugin>] [-I <plugin_dir>] [-i] [-d] [-v] [-h] [-m] [-V] <files>\n";
18  
19 { # main block
20  
21 my $opt={};
22 getopts('l:c:b:g:n:e:p:is:dvhmV', $opt);
23  
24 # defaults
25 $opt->{n} ||= 2; # number of digits used for creating parts
26 $opt->{I} ||= ($ENV{HOME} || '') . "/.xml_split";
27  
28 if( $opt->{h}) { die $USAGE, "\n"; }
29 if( $opt->{m}) { exec "pod2text $RealBin/$RealScript"; }
30 if( $opt->{V}) { print "xml_split version $VERSION\n"; exit; }
31  
32 my %factor=( ' ' => 1, K => 1000, M => 1_000_000, G => 1_000_000_000);
33 if( $opt->{s}) { if( $opt->{c}) { die "cannot use -c and -s at the same time\n"; }
34  
35 if( $opt->{s}=~ m{^\s*(\d+)\s*(G[bo]?|M[bo]?|K[bo]?\s*)?$}i)
36 { my( $size, $unit)= ($1, uc substr( $2 || ' ', 0, 1));
37 $opt->{s}= $size * $factor{$unit};
38 }
39 else
40 { die "invalid size (should be in Kb, Mb or Gb): '$opt->{s}'\n"; }
41 }
42  
43 if( $opt->{g}) { die "cannot use -g and -s at the same time\n" if( $opt->{s});
44 die "cannot use -g and -c at the same time\n" if( $opt->{c});
45 $opt->{l} ||= 1;
46 }
47 elsif( $opt->{c}) { die "cannot use -l and -c at the same time\n" if( $opt->{l}); }
48 else { $opt->{l} ||= 1; $opt->{c}= "level( $opt->{l})"; }
49  
50  
51 my $options= { cond => $opt->{c},
52 base => $opt->{b}, nb_digits => $opt->{n}, ext => $opt->{e},
53 plugin => $opt->{p},
54 no_pi => $opt->{d},
55 verbose => $opt->{v},
56 xinclude => $opt->{i} ? 1 : 0,
57 };
58  
59  
60 my $state;
61 if( my $plugin= $opt->{p})
62 { if( $plugin!~ m{^[\w:.-]+$}) { die "wrong plugin name '$plugin' (only word characters are allowed in plugin names)\n"; }
63 push @INC, $opt->{I};
64 eval { require $plugin };
65 if( $@) { die "cannot find plugin '$plugin': $!"; }
66 import $plugin;
67 $state= $plugin->new( $options);
68 }
69
70  
71 if( $opt->{s})
72 { $state||= xml_split::state::parser->new( $options);
73 $state->{level} = $opt->{l};
74 $state->{size} = $opt->{s};
75 $state->{current_size}=0;
76 $state->{handlers}= { Start => \&parser_start_tag_size, End => \&parser_end_tag_size , Default => \&parser_default_size};
77 warn "using XML::Parser\n" if( $opt->{v});
78 split_with_parser( $state, @ARGV);
79 }
80 elsif( $opt->{g})
81 { $state||= xml_split::state::parser->new( $options);
82 $state->{level}= $opt->{l};
83 $state->{group}= $opt->{g};
84 $state->{handlers}= { Start => \&parser_start_tag_grouped, End => \&parser_end_tag_grouped , Default => \&parser_default_grouped};
85 warn "using XML::Parser\n" if( $opt->{v});
86 split_with_parser( $state, @ARGV);
87 }
88 elsif( $opt->{l})
89 { $state||= xml_split::state::parser->new( $options);
90 $state->{level}= $opt->{l};
91 $state->{handlers}= { Start => \&parser_start_tag_level, End => \&parser_end_tag_level , Default => \&parser_default_level};
92 warn "using XML::Parser\n" if( $opt->{v});
93 split_with_parser( $state, @ARGV);
94 }
95 else
96 { $state||= xml_split::state::twig->new( $options);
97 split_with_twig( $state, @ARGV);
98 }
99  
100 exit;
101 }
102  
103 sub split_with_twig
104 { my( $state, @files)= @_;
105 if( !@files)
106 { $state->{base} ||= 'out';
107 $state->{ext} ||= '.xml';
108 my $twig_options= twig_options( $state);
109 my $t= XML::Twig->new( %$twig_options, $state);
110 $state->{twig}= $t;
111 $t->parse( \*STDIN);
112 end_file( $t, $state);
113 }
114 else
115 { foreach my $file (@files)
116 {
117 unless( $state->{base}) { $state->{seq_nb}=0; }
118 my( $base, $ext)= ($file=~ m{^(.*?)(\.\w+)?$});
119 $state->{base} ||= $base;
120 $state->{ext} ||= $ext || '.xml';
121 my $twig_options= twig_options( $state);
122 my $t= XML::Twig->new( %$twig_options);
123 $state->{twig}= $t;
124 $t->parsefile( $file);
125 end_file( $t, $state);
126 }
127 }
128 }
129  
130 sub split_with_parser
131 { my( $state, @files)= @_;
132 if( !@files)
133 { $state->{base} ||= 'out';
134 $state->{ext} ||= '.xml';
135 my $parser_options= parser_options( $state);
136 my $p= XML::Parser->new( %$parser_options);
137 $state->{parser}= $p;
138 $p->parse( \*STDIN);
139 }
140 else
141 { foreach my $file (@files)
142 {
143 unless( $state->{base}) { $state->{seq_nb}=0; }
144 my( $base, $ext)= ($file=~ m{^(.*?)(\.\w+)?$});
145 $state->{base} ||= $base;
146 $state->{ext} ||= $ext || '.xml';
147 my $parser_options= parser_options( $state);
148 my $p= XML::Parser->new( %$parser_options);
149 $state->{parser}= $p;
150 $p->parsefile( $file);
151 }
152 }
153 }
154
155 sub parser_options
156 { my( $state)= @_;
157 # prepare output to the main document
158 unless( $state->{no_pi})
159 { my $file_name= $state->main_file_name(); # main file name
160 warn "generating main file $file_name\n" if( $state->{verbose});
161 open( my $out, '>', $file_name) or die "cannot create main file '$file_name': $!";
162 $state->{main_fh}= $out;
163 $state->{current_fh}= $out;
164 }
165 my $handlers= { Start => sub { $state->{handlers}->{Start}->( $state, shift( @_)); },
166 End => sub { $state->{handlers}->{End}->( $state, shift( @_)); },
167 Default => sub { $state->{handlers}->{Default}->( $state, shift( @_)); },
168 XMLDecl => sub { parser_declaration( $state, @_); },
169 };
170
171 return { Handlers => $handlers };
172 }
173  
174 ###################################################################################
175 # #
176 # handlers for the -l option #
177 # #
178 ###################################################################################
179  
180 sub parser_start_tag_level
181 { my( $state, $p)= @_;
182  
183 if( $p->depth == $state->{level})
184 { $state->{seq_nb}++;
185 my $file_name= $state->file_name;
186 # prepare chunk file
187 warn "generating $file_name\n" if( $state->{verbose});
188 open( my $out, '>', $file_name) or die "cannot create output file '$file_name': $!";
189 $state->{current_fh}= $out;
190 if( $state->{xml_declaration}) { print {$state->{current_fh}} $state->{xml_declaration}, "\n"; }
191 # output pi
192 unless( $state->{no_pi})
193 { print {$state->{main_fh}} $state->include( $file_name) ; }
194 }
195 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
196 }
197
198 sub parser_end_tag_level
199 { my( $state, $p)= @_;
200 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
201 if( $p->depth == $state->{level})
202 { unless( $state->{current_fh} == $state->{main_fh})
203 { close $state->{current_fh};
204 $state->{current_fh}= $state->{main_fh};
205 }
206 }
207 }
208  
209 sub parser_default_level
210 { my( $state, $p)= @_;
211 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
212 }
213  
214  
215 ###################################################################################
216 # #
217 # handlers for the -s option #
218 # #
219 ###################################################################################
220  
221 sub parser_start_tag_size
222 { my( $state, $p)= @_;
223 if( $p->depth == $state->{level} && !$state->{current_size})
224 {
225 $state->{seq_nb}++;
226 my $file_name= $state->file_name;
227 # prepare chunk file
228 warn "generating $file_name\n" if( $state->{verbose});
229 open( my $out, '>', $file_name) or die "cannot create output file '$file_name': $!";
230 $state->{current_fh}= $out;
231 print {$state->{current_fh}} qq{$state->{xml_declaration}\n} if $state->{xml_declaration};
232 print {$state->{current_fh}} qq{<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">\n};
233 # output pi
234 unless( $state->{no_pi})
235 { print {$state->{main_fh}} $state->include( $file_name) ; }
236 $state->{store_size}=1;
237 }
238 my $original_string= $p->original_string;
239 $state->{current_size} += length( $original_string) if( $state->{store_size});
240 print {$state->{current_fh}} $original_string if( $state->{current_fh});
241 }
242
243 sub parser_end_tag_size
244 { my( $state, $p)= @_;
245 my $original_string= $p->original_string;
246 $state->{current_size} += length( $original_string) if( $state->{store_size});
247 if( $p->depth == $state->{level} && $state->{current_size} > $state->{size})
248 { print {$state->{current_fh}} $original_string if( $state->{current_fh});
249 end_file_with_size( $state);
250 }
251 else
252 { if($p->depth < $state->{level}) { end_file_with_size( $state); }
253 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
254 }
255 }
256  
257 sub end_file_with_size
258 { my( $state)= @_;
259 unless( $state->{current_fh} == $state->{main_fh})
260 { print {$state->{current_fh}} qq{\n</xml_split:root>\n};
261 close $state->{current_fh};
262 $state->{current_size}=0;
263 $state->{store_size}=0;
264 $state->{current_fh}= $state->{main_fh};
265 }
266 }
267  
268 sub parser_default_size
269 { my( $state, $p)= @_;
270 my $string= $p->original_string;
271 if( $state->{store_size})
272 { $state->{current_size} += length( $string);
273 if( $p->depth < $state->{level}) { end_file_with_size( $state); }
274 }
275 print {$state->{current_fh}} $string if( $state->{current_fh});
276 }
277  
278 ###################################################################################
279 # #
280 # handlers for the -g option #
281 # #
282 ###################################################################################
283  
284 sub parser_start_tag_grouped
285 { my( $state, $p)= @_;
286 if( $p->depth == $state->{level})
287 { if( !$state->{current_nb})
288 { $state->{seq_nb}++;
289 my $file_name= $state->file_name;
290 # prepare chunk file
291 warn "generating $file_name\n" if( $state->{verbose});
292 open( my $out, '>', $file_name) or die "cannot create output file '$file_name': $!";
293 $state->{current_fh}= $out;
294 print {$state->{current_fh}} join( "\n", grep { $_ } ( $state->{xml_declaration},
295 qq{<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">\n }
296 )
297 );
298 # output pi
299 unless( $state->{no_pi})
300 { print {$state->{main_fh}} $state->include( $file_name) ; }
301 }
302 }
303 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
304 }
305
306 sub parser_end_tag_grouped
307 { my( $state, $p)= @_;
308 if( $p->depth == $state->{level})
309 { print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
310 $state->{current_nb}++;
311 if( $state->{current_nb} == $state->{group}) { end_file_grouped( $state); }
312 }
313 else
314 { if($p->depth < $state->{level}) { end_file_grouped( $state, { no_nl => 1 }); }
315 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
316 }
317 }
318  
319 sub end_file_grouped
320 { my( $state, $options)= @_;
321 print {$state->{current_fh}} qq{\n} unless( $options->{no_nl});
322 unless( $state->{current_fh} == $state->{main_fh})
323 { print {$state->{current_fh}} qq{</xml_split:root>\n};
324 close $state->{current_fh};
325 $state->{current_nb}=0;
326 $state->{current_fh}= $state->{main_fh};
327 }
328 }
329  
330 sub parser_default_grouped
331 { my( $state, $p)= @_;
332 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
333 }
334  
335 sub char_parser
336 { my( $state, $p)=( shift, shift);
337 print {$state->{current_fh}} $_[0] if( $state->{current_fh});
338 }
339  
340 sub parser_declaration
341 { my( $state, $p, $version, $encoding, $standalone)= @_;
342 $state->{xml_declaration}= $p->recognized_string || '';
343 print {$state->{main_fh}} $state->{xml_declaration};
344 # avoid calling original_string if not needed
345 #if( !$state->{xml_declaration} || $state->{xml_declaration}=~ m{encoding\s*=\s*["']utf-?8["']}i)
346 # { $state->{utf8_encoded}=1;
347 # $p->setHandlers( Char => \&char_parser);
348 # }
349 }
350  
351  
352 sub twig_options
353 { my( $state)= @_;
354  
355 # base options, ensures maximum fidelity to the original document
356 my $twig_options= { keep_encoding => 1, keep_spaces => 1 };
357  
358 # prepare output to the main document
359 unless( $state->{no_pi})
360 { my $file_name= $state->main_file_name(); # main file name
361 warn "generating main file $file_name\n" if( $state->{verbose});
362 open( my $out, '>', $file_name) or die "cannot create main file '$file_name': $!";
363 $state->{out}= $out;
364 $twig_options->{twig_print_outside_roots}= $out;
365 $twig_options->{start_tag_handlers}= { $state->{cond} => sub { $_->set_att( '#in_fragment' => 1); } };
366 }
367
368 $twig_options->{twig_roots}= { $state->{cond} => sub { dump_elt( @_, $state); } };
369 return $twig_options;
370 }
371  
372 sub dump_elt
373 { my( $t, $elt, $state)= @_;
374 $state->{seq_nb}++;
375 $state->{elt}= $elt;
376  
377 my $file_name= $state->file_name;
378 warn "generating $file_name\n" if( $state->{verbose});
379  
380 my $fragment= XML::Twig->new();
381 $fragment->{twig_xmldecl} = $t->{twig_xmldecl};
382 $fragment->{twig_doctype} = $t->{twig_doctype};
383 $fragment->{twig_dtd} = $t->{twig_dtd};
384
385 if( !$state->{no_pis})
386 { # if we are still within a fragment, just replace the element by the PI
387 # otherwise print it to the main document
388 my $include= $state->include( $file_name);
389  
390 $elt->del_att( '#in_fragment');
391
392 if( $elt->inherited_att( '#in_fragment'))
393 { $elt->parent( '*[@#in_fragment="1"]')->set_att( '#has_subdocs' => 1);
394 $include->replace( $elt);
395 }
396 else
397 { $elt->cut;
398 $include->print( $state->{out});
399 }
400 }
401 else
402 { $elt->cut; }
403
404 $fragment->set_root( $elt);
405 open( my $out, '>', $file_name) or die "cannot create output file '$file_name': $!";
406 #if( $state->{xml_declaration}) { warn "c1"; print {$out} $state->{xml_declaration}, "\n"; }
407 #if( $fragment->{xml_decl}) { warn "c2"; print {$out} $fragment->xml_decl, "\n"; }
408 $fragment->set_keep_encoding( 1);
409 $fragment->print( $out);
410 close $out;
411 }
412
413 sub end_file
414 { my( $t, $state)= @_;
415 unless( $state->{no_pi})
416 { close $state->{out}; }
417 }
418  
419
420 # for Getop::Std
421 sub HELP_MESSAGE { return $USAGE; }
422 sub VERSION_MESSAGE { return $VERSION; }
423  
424 package xml_split::state;
425  
426 sub new
427 { my( $ref, $options)= @_;
428 my $state= bless $options, $ref;
429 $state->{seq_nb}=0;
430 return $state;
431 }
432  
433 sub file_name
434 { my( $state)= @_;
435 my $nb= sprintf( "%0$state->{nb_digits}d", $state->{seq_nb});
436 my $file_name= "$state->{base}-$nb$state->{ext}";
437 $file_name =~ s{\\}{/}g;
438 return $file_name;
439 }
440  
441 sub main_file_name
442 { my( $state)= @_;
443 my $nb= sprintf( "%0$state->{nb_digits}d", 0);
444 my $file_name= "$state->{base}-$nb$state->{ext}";
445 return $file_name;
446 }
447 1;
448  
449 ###################################################################################
450 # #
451 # state when using XML::Parser #
452 # #
453 ###################################################################################
454  
455 package xml_split::state::parser;
456 import xml_split::state;
457 use base 'xml_split::state';
458  
459 sub include
460 { my( $state, $file_name)= @_;
461 if( $state->{xinclude})
462 { return qq{<xi:include href="$file_name" />}; }
463 else
464 { return qq{<?merge subdocs = 0 :$file_name?>}; }
465 }
466 1;
467  
468 ###################################################################################
469 # #
470 # state when using XML::Twig #
471 # #
472 ###################################################################################
473  
474 package xml_split::state::twig;
475 import xml_split::state;
476 use base 'xml_split::state';
477  
478 sub include
479 { my( $state, $file_name)= @_;
480 my $include;
481 my $subdocs= $state->{elt}->att( '#has_subdocs') || 0;
482 if( $state->{xinclude})
483 { $include= XML::Twig::Elt->new( 'xi:include', { href => $file_name });
484 if( $subdocs) { $include->set_att( subdocs => 1); }
485 }
486 else
487 {
488 $include= XML::Twig::Elt->new( '#PI')
489 ->set_pi( merge => " subdocs = $subdocs :$file_name");
490 }
491 return $include;
492 }
493  
494 1;
495  
496 package main;
497  
498 __END__
« tools/xml_pp/xml_pp ↑ Summary