XML/Template/Element/File/Load.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 63 SCAR 40.3
CC
7
Cov
0%
load
42.0
_init
6.0
BEGIN
2.0
CRAP 56.0
1 ###############################################################################
2 # XML::Template::Element::File::Load
3 #
4 # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu>
5 # All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 ###############################################################################
10 package XML::Template::Element::File::Load;
11 use base qw(XML::Template::Base);
12  
13 use strict;
14 use XML::Template::Document;
15  
16  
17 =pod
18  
19 =head1 NAME
20  
21 XML::Template::Element::File::Load - XML::Template loader module that
22 loads documents from files.
23  
24 =head1 SYNOPSIS
25  
26 This module implements an XML::Template document loader that loads
27 documents from files.
28  
29 =head1 CONSTRUCTOR
30  
31 A constructor method C<new> is provided by L<XML::Template::Base>. A list
32 of named configuration parameters may be passed to the constructor. The
33 constructor returns a reference to a new block loader object or under if
34 an error occurred. If undef is returned, you can use the method C<error>
35 to retrieve the error. For instance:
36  
37 my $parser = XML::Template::Element::File::Load->new (%config)
38 || die XML::Template::Element::File::Load->error;
39  
40 The following named configuration parameters may be passed to the
41 constructor:
42  
43 =over 4
44  
45 =item IncludePath
46  
47 This is a reference to an array containing paths where XML::Template
48 documents will be searched for when loading.
49  
50 =back
51  
52 =head1 PRIVATE METHODS
53  
54 =head2 _init
55  
56 This method is the internal initialization function called from
57 L<XML::Template::Base> when a new file loader object is created.
58  
59 =cut
60  
61 sub _init {
62 my $self = shift;
63 my %params = @_;
64  
65 print ref ($self) . "->_init\n" if $self->{_debug};
66  
67 $self->{_include_path} = $params{IncludePath}
68 || XML::Template::Config->include_path
69 || [''];
70  
71 $self->{_enabled} = 1;
72  
73 return 1;
74 }
75  
76 =pod
77  
78 =head2 load
79  
80 This method loads an XML document from a file located in one of the
81 directories listed in the IncludePath constructor parameter. It returns a
82 new L<XML::Template::Document> object.
83  
84 =cut
85  
86 sub load {
87 my $self = shift;
88 my $filename = shift;
89  
90 print ref ($self) . "->load\n" if $self->{_debug};
91  
92 undef $self->{_error};
93  
94 my $xml;
95  
96 my $include_path = $self->{_include_path};
97 # If file name is absolute, no need to search path.
98 $include_path = [''] if $filename =~ /^\//;
99  
100 my $filespec;
101 foreach my $path (@$include_path) {
102 # If path is empty, do not prepend anything.
103 $filespec = $path eq '' ? $filename : "$path/$filename";
104  
105 if (open (FILE, $filespec)) {
106 $xml = '';
107 while (my $line = <FILE>) {
108 $line =~ s/\$(?!{)/\\\$/g;
109 $line =~ s/&(?!amp)/&amp\;/g;
110 $xml .= $line;
111 }
112 close (FILE);
113 last;
114 }
115 }
116  
117 if (defined $xml) {
118 return XML::Template::Document->new (XML => $xml,
119 Source => "file:$filespec");
120 } else {
121 return undef;
122 }
123 }
124  
125 =pod
126  
127 =head1 AUTHOR
128  
129 Jonathan Waxman
130 <jowaxman@bbl.med.upenn.edu>
131  
132 =head1 COPYRIGHT
133  
134 Copyright (c) 2002-2003 Jonathan A. Waxman
135 All rights reserved.
136  
137 This program is free software; you can redistribute it and/or
138 modify it under the same terms as Perl itself.
139  
140 =cut
141  
142  
143 1;