File Coverage

blib/lib/Builder/XML.pm
Criterion Covered Total %
statement 87 89 97.7
branch 20 24 83.3
condition 6 7 85.7
subroutine 26 26 100.0
pod n/a
total 139 146 95.2


line stmt bran cond sub pod time code
1             package Builder::XML;
2 6     6   1021 use strict;
  6         11  
  6         228  
3 6     6   65 use warnings;
  6         12  
  6         179  
4 6     6   32 use Carp;
  6         10  
  6         452  
5 6     6   2758 use Builder::Utils;
  6         14  
  6         168  
6 6     6   3304 use Builder::XML::Utils;
  6         551  
  6         8142  
7             our $VERSION = '0.06';
8             our $AUTOLOAD;
9              
10              
11             sub __new__ {
12 9     9   17 my ( $class ) = shift;
13 9         30 my %args = Builder::XML::Utils::get_args( @_ );
14            
15             bless {
16             %args,
17             block_id => $args{ _block_id },
18             stack => $args{ _stack },
19 9         66 context => Builder::XML::Utils::build_context(),
20             }, $class;
21             }
22              
23             sub AUTOLOAD {
24 129     129   2273 my ( $self ) = shift;
25 129         221 my @args = @_;
26              
27 129 50       602 if ( $AUTOLOAD =~ /.*::(.*)/ ) {
28 129         274 my $elt = $1;
29 129         137 my $attr = undef;
30            
31             # sub args get resent as callback
32 129 100       247 if ( wantarray ) {
33 37     37   337 return sub { $self->$elt( @args ) };
  37         166  
34             }
35            
36             # if first arg is hasharray then its attributes!
37 92 100       204 $attr = shift @args if ref $args[0] eq 'HASH';
38            
39 92 100       190 if ( ref $args[0] eq 'CODE' ) {
40 34         82 $self->__element__( context => 'start', element => $elt, attr => $attr );
41 34         49 for my $inner ( @args ) {
42 56 100       133 if ( ref $inner eq 'CODE' ) { $inner->() }
  52         89  
43 4     4   18 else { $self->__push__( sub { $inner } ) }
  4         18  
44             }
45 34         96 $self->__element__( context => 'end', element => $elt );
46 34         144 return;
47             }
48            
49             # bog standard element
50 58         196 $self->__element__( element => $elt, attr => $attr, text => "@args" );
51             }
52            
53 58         160 $self;
54             }
55              
56              
57             ######################################################
58             # methods
59              
60             sub __render__ {
61 5     5   6 my $self = shift;
62 5         5 my $render;
63            
64             # render subs just for this block
65 5     30   14 my @this_block = Builder::Utils::yank { $_->[0] == $self->{block_id} } @{ $self->{stack} };
  30         88  
  5         16  
66 5         26 while ( my $block = shift @this_block ) {
67 8         11 my ( $block_id, $code ) = @$block;
68 8         18 $render.= $code->();
69             }
70 5         22 return $render;
71             }
72              
73             sub __element__ {
74 126     126   373 my ( $self, %param ) = @_;
75 126   100     413 $param{ text } ||= '';
76 126   100     376 $param{ context } ||= 'element';
77 126         447 $self->{ context }->{ $param{ context } }->( $self, \%param );
78 126         406 return;
79             }
80              
81             sub __cdata__ {
82 2     2   7 my $self = shift;
83 2 100       9 return $_[0] if $self->{ cdata } == 1;
84 1         2 return $self->__cdatax__( $_[0] );
85             }
86              
87             sub __cdatax__ {
88 3     3   4 my $self = shift;
89 3         10 return q{};
90             }
91              
92             sub __say__ {
93 1     1   6 my ( $self, @say ) = @_;
94 1     1   3 for my $said ( @say ) { $self->__push__( sub { $said } ) }
  1         5  
  1         5  
95 1         3 $self;
96             }
97              
98             sub __push__ {
99 131     131   362 my ( $self, $code ) = @_;
100            
101             # straight to output stream if provided
102 131 100       279 if ( $self->{ _output } ) {
103 21         106 print { $self->{ _output } } $code->();
  21         77  
104 21         266 return;
105             }
106            
107             # else add to stack
108 110         113 push @{ $self->{ stack } }, [ $self->{ block_id }, $code ];
  110         405  
109             }
110              
111 34     34   90 sub __inc__ { $_[0]->{ _inc }->() }
112              
113 34     34   92 sub __dec__ { $_[0]->{ _dec }->() }
114              
115 57     57   147 sub __level__ { $_[0]->{ _level }->() }
116              
117             sub __tab__ {
118 126     126   146 my $self = shift;
119 126         145 my $tab = q{};
120 126 100       259 $tab = q{ } x $self->{ pre_indent } if $self->{ pre_indent };
121 126 100       288 $tab.= q{ } x ( $self->{ indent } * $self->__level__ ) if $self->{ indent };
122 126         476 return $tab;
123             }
124              
125 34     34   80 sub __start_tab__ { $_[0]->__tab__ }
126              
127             sub __end_tab__ {
128 34     34   41 my $self = shift;
129 34 50 66     133 return q{} if $self->{ newline } && ! $self->{ open_newline };
130 34         68 $self->__tab__;
131             }
132              
133             sub __open_newline__ {
134 34     34   42 my $self = shift;
135 34 50       155 return $self->{cr} if $self->{ open_newline };
136 0         0 return q{};
137             }
138              
139             sub __close_newline__ {
140 92     92   103 my $self = shift;
141 92 50       632 return $self->{cr} if $self->{ close_newline };
142 0         0 return q{};
143             }
144              
145             sub DESTROY {
146 9     9   2781 my $self = shift;
147 9         611 $self = undef;
148             }
149              
150              
151             1;
152              
153              
154             __END__