File Coverage

blib/lib/Pod/Simple/XMLOutStream.pm
Criterion Covered Total %
statement 54 56 96.4
branch 13 16 81.2
condition 6 8 75.0
subroutine 10 10 100.0
pod 1 1 100.0
total 84 91 92.3


line stmt bran cond sub pod time code
1             package Pod::Simple::XMLOutStream;
2 29     29   525760 use strict;
  29         67  
  29         1747  
3 29     29   145 use warnings;
  29         64  
  29         1498  
4 29     29   164 use Carp ();
  29         59  
  29         499  
5 29     29   2605 use Pod::Simple ();
  29         62  
  29         2525  
6             our $VERSION = '3.47';
7             BEGIN {
8 29     29   516 our @ISA = ('Pod::Simple');
9 29 50       27366 *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG;
10             }
11              
12             our $ATTR_PAD;
13             $ATTR_PAD = "\n" unless defined $ATTR_PAD;
14             # Don't mess with this unless you know what you're doing.
15              
16             our $SORT_ATTRS;
17             $SORT_ATTRS = 0 unless defined $SORT_ATTRS;
18              
19             sub new {
20 469     469 1 588303 my $self = shift;
21 469         1950 my $new = $self->SUPER::new(@_);
22 469   50     3386 $new->{'output_fh'} ||= *STDOUT{IO};
23 469         1927 $new->keep_encoding_directive(1);
24             #$new->accept_codes('VerbatimFormatted');
25 469         1218 return $new;
26             }
27              
28             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29              
30             sub _handle_element_start {
31             # ($self, $element_name, $attr_hash_r)
32 1870     1870   3706 my $fh = $_[0]{'output_fh'};
33 1870         3187 my($key, $value);
34 1870         2692 DEBUG and print STDERR "++ $_[1]\n";
35 1870         8083 print $fh "<", $_[1];
36 1870 100       3938 if($SORT_ATTRS) {
37 1160         1602 foreach my $key (sort keys %{$_[2]}) {
  1160         4711  
38 2043 100       5175 unless($key =~ m/^~/s) {
39 1602 50 66     6393 next if $key eq 'start_line' and $_[0]{'hide_line_numbers'};
40 695         2329 _xml_escape($value = $_[2]{$key});
41 695         2014 print $fh $ATTR_PAD, $key, '="', $value, '"';
42             }
43             }
44             } else { # faster
45 710         1097 while(($key,$value) = each %{$_[2]}) {
  1544         5390  
46 834 100       2426 unless($key =~ m/^~/s) {
47 764 100 100     3274 next if $key eq 'start_line' and $_[0]{'hide_line_numbers'};
48 197         593 _xml_escape($value);
49 197         639 print $fh $ATTR_PAD, $key, '="', $value, '"';
50             }
51             }
52             }
53 1870         6045 print $fh ">";
54 1870         4775 return;
55             }
56              
57             sub _handle_text {
58 1278     1278   1873 DEBUG and print STDERR "== \"$_[1]\"\n";
59 1278 100       3395 if(length $_[1]) {
60 1267         2201 my $text = $_[1];
61 1267         3258 _xml_escape($text);
62 1267         1948 print {$_[0]{'output_fh'}} $text;
  1267         4310  
63             }
64 1278         4072 return;
65             }
66              
67             sub _handle_element_end {
68 1870     1870   2586 DEBUG and print STDERR "-- $_[1]\n";
69 1870         2618 print {$_[0]{'output_fh'}} "";
  1870         5400  
70 1870         3876 return;
71             }
72              
73             # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
74             #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
75              
76             sub _xml_escape {
77 2159     2159   4319 foreach my $x (@_) {
78             # Escape things very cautiously:
79 2159 50       10565 if ("$]" >= 5.007_003) {
80 2159         7191 $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/'&#'.(utf8::native_to_unicode(ord($1))).';'/eg;
  862         3525  
81             } else { # Is broken for non-ASCII platforms on early perls
82 0         0 $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/'&#'.(ord($1)).';'/eg;
  0         0  
83             }
84             # Yes, stipulate the list without a range, so that this can work right on
85             # all charsets that this module happens to run under.
86             }
87 2159         7402 return;
88             }
89              
90             #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
91             1;
92              
93             __END__