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   361965 use strict;
  29         72  
  29         888  
3 29     29   104 use warnings;
  29         57  
  29         1118  
4 29     29   115 use Carp ();
  29         43  
  29         329  
5 29     29   2158 use Pod::Simple ();
  29         42  
  29         2041  
6             our $VERSION = '3.48';
7             BEGIN {
8 29     29   446 our @ISA = ('Pod::Simple');
9 29 50       20186 *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 525968 my $self = shift;
21 469         1486 my $new = $self->SUPER::new(@_);
22 469   50     2402 $new->{'output_fh'} ||= *STDOUT{IO};
23 469         1321 $new->keep_encoding_directive(1);
24             #$new->accept_codes('VerbatimFormatted');
25 469         957 return $new;
26             }
27              
28             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29              
30             sub _handle_element_start {
31             # ($self, $element_name, $attr_hash_r)
32 1870     1870   2479 my $fh = $_[0]{'output_fh'};
33 1870         2200 my($key, $value);
34 1870         1828 DEBUG and print STDERR "++ $_[1]\n";
35 1870         5007 print $fh "<", $_[1];
36 1870 100       2644 if($SORT_ATTRS) {
37 1160         1194 foreach my $key (sort keys %{$_[2]}) {
  1160         2997  
38 2043 100       3370 unless($key =~ m/^~/s) {
39 1602 50 66     3826 next if $key eq 'start_line' and $_[0]{'hide_line_numbers'};
40 695         1452 _xml_escape($value = $_[2]{$key});
41 695         1160 print $fh $ATTR_PAD, $key, '="', $value, '"';
42             }
43             }
44             } else { # faster
45 710         758 while(($key,$value) = each %{$_[2]}) {
  1544         3595  
46 834 100       1509 unless($key =~ m/^~/s) {
47 764 100 100     2066 next if $key eq 'start_line' and $_[0]{'hide_line_numbers'};
48 197         446 _xml_escape($value);
49 197         387 print $fh $ATTR_PAD, $key, '="', $value, '"';
50             }
51             }
52             }
53 1870         3236 print $fh ">";
54 1870         3138 return;
55             }
56              
57             sub _handle_text {
58 1278     1278   1287 DEBUG and print STDERR "== \"$_[1]\"\n";
59 1278 100       1969 if(length $_[1]) {
60 1267         1429 my $text = $_[1];
61 1267         2097 _xml_escape($text);
62 1267         1297 print {$_[0]{'output_fh'}} $text;
  1267         2384  
63             }
64 1278         2643 return;
65             }
66              
67             sub _handle_element_end {
68 1870     1870   1774 DEBUG and print STDERR "-- $_[1]\n";
69 1870         1819 print {$_[0]{'output_fh'}} "";
  1870         3312  
70 1870         2592 return;
71             }
72              
73             # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
74             #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
75              
76             sub _xml_escape {
77 2159     2159   2452 foreach my $x (@_) {
78             # Escape things very cautiously:
79 2159 50       6135 if ("$]" >= 5.007_003) {
80 2159         4443 $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/'&#'.(utf8::native_to_unicode(ord($1))).';'/eg;
  862         2164  
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         2345 return;
88             }
89              
90             #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
91             1;
92              
93             __END__