File Coverage

blib/lib/BW/XML/Out.pm
Criterion Covered Total %
statement 12 71 16.9
branch 0 34 0.0
condition 0 7 0.0
subroutine 4 12 33.3
pod 4 6 66.6
total 20 130 15.3


line stmt bran cond sub pod time code
1             # BW::XML::Out.pm
2             # Simple XML output
3             #
4             # by Bill Weinman - http://bw.org/
5             # Copyright (c) 1995-2010 The BearHeart Group, LLC
6             #
7             # See POD for History
8             #
9             package BW::XML::Out;
10 1     1   797 use strict;
  1         3  
  1         32  
11 1     1   6 use warnings;
  1         1  
  1         24  
12              
13 1     1   6 use BW::Constants;
  1         4  
  1         64  
14 1     1   7 use base qw( BW::Base );
  1         2  
  1         1368  
15              
16             our $VERSION = "1.4";
17 0     0 0   sub version { $VERSION }
18              
19             sub _init
20             {
21 0     0     my $self = shift;
22 0           $self->SUPER::_init(@_);
23              
24 0 0         $self->indent_mul(2) unless $self->{indent_mul}; # default to 2 spaces of indentation
25              
26 0           return SUCCESS;
27             }
28              
29             # _setter_getter entry points
30 0     0 1   sub indent_mul { BW::Base::_setter_getter(@_); }
31 0     0 1   sub indent_level { BW::Base::_setter_getter(@_); }
32              
33             sub element
34             {
35 0     0 1   my $sn = 'element';
36 0           my ( $self, $element, $content, $options ) = @_;
37 0           my $a = '';
38 0           my $attribs = '';
39 0           my $flags = {};
40 0   0       my $indent_level = $self->indent_level() || 0;
41              
42 0 0         $options = {} unless $options;
43              
44 0 0         $content = '' unless defined $content;
45 0 0 0       $content = $self->xml_escape($content) if ( $content and not $options->{xmlContent} );
46              
47 0 0         if ($options) {
48 0 0         if ( $options->{indentLevel} ) {
49 0           $indent_level = $options->{indentLevel};
50             }
51 0 0         if ( $options->{indent} ) {
52 0           ++$indent_level;
53             }
54 0 0         if ( $options->{startTag} ) {
    0          
    0          
55 0           $flags->{start} = TRUE;
56             } elsif ( $options->{endTag} ) {
57 0           $flags->{end} = TRUE;
58 0           --$indent_level;
59             } elsif ( $options->{emptyTag} ) {
60 0           $flags->{empty} = TRUE;
61             }
62              
63 0 0         if ( $options->{attribs} ) {
64 0           my @ats = @{ $options->{attribs} };
  0            
65 0           foreach my $att (@ats) {
66 0           my @att = %$att;
67 0           my $a_lh = $self->xml_escape( $att[0] );
68 0           my $a_rh = $self->xml_escape( $att[1] );
69 0           $attribs .= qq{ ${a_lh}="${a_rh}"};
70             }
71             }
72             }
73              
74             # create the indent
75 0           $a .= ' ' x ( $indent_level * $self->indent_mul() );
76              
77 0 0         if ( $flags->{empty} ) {
    0          
    0          
78 0           $a .= qq{<${element}${attribs} />};
79             } elsif ( $flags->{start} ) {
80 0           $a .= qq{<${element}${attribs}>};
81             } elsif ( $flags->{end} ) {
82 0           $a .= qq{};
83             } else {
84 0           $a .= qq{<${element}${attribs}>${content}};
85             }
86              
87             # terminate the element with a newline
88 0 0         $a .= "\n" unless $options->{noNewline};
89              
90 0 0         if ( $options->{startTag} ) {
91 0           ++$indent_level;
92             }
93              
94             # keep the indent level
95 0           $self->indent_level($indent_level);
96              
97 0           return $a;
98             }
99              
100             sub xml_escape
101             {
102 0     0 0   my ( $self, $c ) = @_;
103 0 0         return '' unless defined $c;
104 0           $c =~ s/&/&/gsm;
105 0           $c =~ s/
106 0           $c =~ s/>/>/gsm;
107 0           $c =~ s/"/"/g;
108              
109 0           return $c;
110             }
111              
112             # set the error string and return FAILURE
113             sub _error
114             {
115 0     0     my $self = shift;
116 0   0       $self->{error} = "$self->{me}: " . ( shift || 'unknown error' );
117 0           return FAILURE;
118             }
119              
120             # get and clear error string
121             sub error
122             {
123 0     0 1   my $self = shift;
124 0           my $errstr = $self->{error};
125 0           $self->{error} = VOID;
126 0           return $errstr;
127             }
128              
129             1;
130              
131             __END__