This file is indexed.

/usr/share/perl5/Template/Plugin/XML/View.pm is in libtemplate-plugin-xml-perl 2.17-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#============================================================= -*-Perl-*-
#
# Template::Plugin::XML::View
#
# DESCRIPTION
#   Template Toolkit plugin to parse XML and generate a view by raising
#   events to a Template::View object for each element in the XML source.
#
#   -- UNDER CONSTRUCTION -- NOT INCLUDED IN THE MAIN DISTRIBUTION --
#
# AUTHOR
#   Andy Wardley   <abw@cpan.org>
#
# COPYRIGHT
#   Copyright (C) 2001-2004 Andy Wardley.  All Rights Reserved.
#
#   This module is free software; you can redistribute it and/or
#   modify it under the same terms as Perl itself.
#
# REVISION
#   $Id: View.pm,v 2.4 2003/03/17 22:29:16 abw Exp $
#
#============================================================================

package Template::Plugin::XML::View;

require 5.004;

use strict;
use Template::Plugin;
use XML::Parser;

use base qw( Template::Plugin );
use vars qw( $VERSION $DEBUG $XML_PARSER_ARGS $ELEMENT );

$VERSION = sprintf("%d.%02d", q$Revision: 2.4 $ =~ /(\d+)\.(\d+)/);
$DEBUG   = 0 unless defined $DEBUG;
$XML_PARSER_ARGS = {
    ErrorContext  => 4,
    Namespaces    => 1,
    ParseParamEnt => 1,
    NoExpand      => 1,
};

$ELEMENT = 'Template::Plugin::XML::View::Element';

#------------------------------------------------------------------------
# new($context, $file_or_text, \%config)
#------------------------------------------------------------------------

sub new {
    my $class   = shift;
    my $context = shift;
    my $args    = ref $_[-1] eq 'HASH' ? pop(@_) : { };
    my ($input, $about);

    # determine the input source from a positional parameter (may be a 
    # filename or XML text if it contains a '<' character) or by using
    # named parameters which may specify one of 'file', 'filename', 'text'
    # or 'xml'

    if ($input = shift) {
        if ($input =~ /\</) {
            $about  = 'xml text';
        }
        else {
            $about = "xml file $input";
            $input = $class->file_contents($input);
        }
    }
    elsif ($input = $args->{ text } || $args->{ xml }) {
        $about = 'xml text';
    }
    elsif ($input = $args->{ file } || $args->{ filename }) {
        $about = "xml file $input";
        $input = $class->file_contents($input);
    }
    else {
        $class->throw('no filename or xml text specified');
    }
    
    # munge input to protect entity refs
    $input =~ s/&/&amp;/g;
    
    my $xpargs = {
        map { exists $args->{$_} 
              ? ( $_, $args->{ $_ } )
                  : ( $_, $XML_PARSER_ARGS->{ $_ } ) }
        keys %$XML_PARSER_ARGS,
    };
    
    my $parser = XML::Parser->new(
        %$xpargs,
        Style    => 'Template::Plugin::XML::View::Parser',
        Handlers => {
            Init => sub {
                my $expat = shift;
                DEBUG("[Init]\n") if $DEBUG;
                $expat->{ _TT2_XVIEW_TEXT    }  = '';
                $expat->{ _TT2_XVIEW_RESULT  }  = '';
                $expat->{ _TT2_XVIEW_CONTEXT }  = $context;
                $expat->{ _TT2_XVIEW_STACK   }  = [ ];
            },
        },
    );
    my $result = $parser->parse($input);

    DEBUG("result: $result\n") if $DEBUG;
    return $result;
}


sub file_contents {
    my ($self, $file) = @_;
    my $text;
    local *FP;
    local $/ = undef;
    open(FP, $file) || $self->throw("cannot read XML file $file: $!");
    $text = <FP>;
    close(FP);
    return $text;
}
    

#------------------------------------------------------------------------
# _throw($errmsg)
#
# Raise a Template::Exception of type XML.View via die().
#------------------------------------------------------------------------

sub throw {
    my ($self, $error) = @_;
    die (Template::Exception->new('XML.View', $error));
}

sub DEBUG { print STDERR @_ };


#========================================================================
# Template::Plugin::XML::View::Parser
#
# Package defines subroutines which are called by the XML::Parser
# instance.  They manipulate a stack of T-::P-::XML::View::Element
# objects which each represent nested elements currently under parse
# at any time, with the innermost element object on top of the stack.
# These subs call the element() 
#========================================================================

package Template::Plugin::XML::View::Parser;
use vars qw( $DEBUG $ELEMENT );

*DEBUG   = \*Template::Plugin::XML::View::DEBUG;
$ELEMENT = 'Template::Plugin::XML::View::Element';


sub Start {
    my ($expat, $name, %attr) = @_;
    my $attr = \%attr;

    # flush any character content
    Text($expat) if length $expat->{ _TT2_XVIEW_TEXT };

    if ($DEBUG) {
        my $iattr = join(' ', map { "$_=\"$attr{$_}\"" } keys %attr);
        $attr = " $attr" if $attr;
        DEBUG("[Start] <$name$attr>\n");
    }

    my $stack = $expat->{ _TT2_XVIEW_STACK };

    my $element = $ELEMENT->new($name, \%attr)
        || $stack->[-1]->throw($ELEMENT->error());
    
    push(@$stack, $element);
}

sub End {
    my ($expat, $name) = @_;

    # flush any character content
    Text($expat) if length $expat->{ _TT2_XVIEW_TEXT };

    DEBUG("[End] </$name>\n") if $DEBUG;

    my $stack = $expat->{ _TT2_XVIEW_STACK };
    my $top = pop(@$stack);
    my $end = $top->end($expat, $name)
        || $top->throw($top->error());
    if (@$stack) {
        $stack->[-1]->child($expat, $name, $end);
    }
    else {
        DEBUG("popped last handler off stack\n") if $DEBUG;
#        die "corrupt stack\n";
        $expat->{ _TT2_XVIEW_RESULT } = $end;
    }
}

sub Char {
    my ($expat, $char) = @_;

    DEBUG("[Char] [$char]\n") if $DEBUG;

    # push character content onto buffer
    $expat->{ _TT2_XVIEW_TEXT } .= $char;

}


#------------------------------------------------------------------------
# Text()
#
# This is an extension subroutine which we're using to buffer chunks
# of Char input into complete text blocks.  These then get notified to 
# the parent in one happy bundle rather than several scraggly lumps.
#------------------------------------------------------------------------

sub Text {
    my $expat = shift;
    my $text  = $expat->{ _TT2_XVIEW_TEXT };

    if ($DEBUG) {
        my $dbgtext = $text;
        $dbgtext =~ s/\n/\\n/g;
        DEBUG("[Text] [$dbgtext]\n") if $DEBUG;
    }

    $expat->{ _TT2_XVIEW_STACK }->[-1]->text($expat, $text);
    $expat->{ _TT2_XVIEW_TEXT } = '';
}


sub Final {
    my $expat = shift;
    return $expat->{ _TT2_XVIEW_RESULT } || die "no result\n";

    my $stack = $expat->{ _TT2_XVIEW_STACK };
    my $top = pop(@$stack) || die "corrupt stack in Final";
    my $end = $top->end($expat)
        || $top->throw($top->error());
    my $r = $expat->{ _TT2_XVIEW_RESULT } || die "no result\n";# $end;
    DEBUG("[Final] => [$r]\n") if $DEBUG;
    return $r;
}



#========================================================================
# Template::Plugin::XML::View::Element
#
# Implements a parser handler for representing each element in the 
#========================================================================

package Template::Plugin::XML::View::Element;


sub new {
    my ($class, $name, $attr) = @_;
    bless {
        name    => $name,
        attr    => $attr,
        content => [ ],
    }, $class;
}

# called to receive character content
sub text {
    my $self = shift;
    my $expat = shift;
    push(@{ $self->{ content } }, @_);
}

# called to receive completed child element
sub child {
    my ($self, $expat, $name, $child) = @_;
    push(@{ $self->{ content } }, $child);    
}

# called at end of element
sub end {
    my ($self, $expat, $name) = @_;
    return $self;
}

# generate element as XML
sub xml {
    my $self = shift;
    my $name = $self->{ name };

    # generate XML representation of attributes
    my $attr = $self->{ attr };
    $attr = join(' ', map {
        "$_=\"$attr->{$_}\"";
    } keys %$attr);
    $attr = " $attr" if length $attr;

    # generate XML representation of content
    my $content = $self->{ content };
    $content = join(' ', map {
        ref $_ ? $_->xml() : $_;
    } @$content);

    # generate complete XML element
    return length $content 
        ? "<${name}${attr}>$content</$name>"
        : "<${name}${attr} />";
}


sub present {
    my ($self, $view) = @_;
    my $vars = {
        %$self,
        %{ $self->{ attr } },
        element => $self,
        content => sub { $self->content($view) },
    };
    $view->include($self->{ name }, $vars)
}

sub content {
    my ($self, $view) = @_;
    return $self->{ content } unless $view;
    my $output = '';
    foreach my $node (@{ $self->{ content } }) {
	$output .= ref $node ? $node->present($view) : $node;
    }
    return $output;
}


1;

__END__