XML/Template/Element/File.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran n/a Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 56 SCAR 6.9
CC
1
Cov
0%
include
2.0
list
2.0
loopinit
2.0
CRAP 2.0
1 ###############################################################################
2 # XML::Template::Element::File
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;
11 use base qw(XML::Template::Element XML::Template::Element::Iterator);
12  
13 use strict;
14 use XML::Template::Element;
15  
16  
17 =pod
18  
19 =head1 NAME
20  
21 XML::Template::Element::File - XML::Template plugin module for the file
22 namespace.
23  
24 =head1 SYNOPSIS
25  
26 This XML::Template plugin module implements the file namespace tagset.
27 The block namespace includes tags that handle XML files as well as
28 arbitrary files.
29  
30 =head1 CONSTRUCTOR
31  
32 XML::Template::Element::Block inherits its constructor method, C<new>,
33 from L<XML::Template::Element>.
34  
35 =head1 FILE TAGSET METHODS
36  
37 =head2 include
38  
39 This method implements the include element. It reads a file and if
40 requested parses it. The following attributes are used:
41  
42 =over 4
43  
44 =item name
45  
46 The name of the file to include. It can be an absolute file spec or a
47 path relative to the include directory set for
48 L<XML::Template::Element::File::Load>.
49  
50 =item parse
51  
52 If set to C<true>, the document will be parsed. If set to C<false>, the
53 raw text of the document will be read in unparsed. The default value is
54 C<true>.
55  
56 =back
57  
58 =cut
59  
60 sub include {
61 my $self = shift;
62 my ($code, $attribs) = @_;
63  
64 my $name = $self->get_attrib ($attribs, 'name') || 'undef';
65 my $parse = $self->get_attrib ($attribs, 'parse') || "'true'";
66  
67 # Create attribute param code;
68 my $attribs_named_params = $self->generate_named_params ($attribs);
69  
70 my $outcode = qq!
71 do {
72 use XML::Template::Element::File::Load;
73  
74 \$vars->create_context ();
75  
76 my \%attribs = ($attribs_named_params);
77  
78 if ($parse =~ /^false\$/i) {
79 open (FILE, $name)
80 || die "Could not open " . $name . ": \$\!";
81 while (<FILE>) {
82 print \$_;
83 }
84 close (FILE);
85
86 } else {
87 # my \@old_load = \$process->load (XML::Template::Element::File::Load->new ());
88  
89 my \%loaded = \$process->get_load ();
90 my \%tloaded;
91 while (my (\$module, \$loaded) = each \%loaded) {
92 if (\$module =~ /Cache/) {
93 \$tloaded{\$module} = \$loaded;
94 } else {
95 \$tloaded{\$module} = 0;
96 }
97 }
98 \$tloaded{'XML::Template::Element::File::Load'} = 1;
99 \$process->set_load (\%tloaded);
100 \$process->process ($name, \\\%attribs) || die \$process->error;
101 \$process->set_load (\%loaded);
102  
103 # \$process->load (\@old_load);
104 }
105  
106 \$vars->delete_context ();
107 };
108 !;
109  
110 return $outcode;
111 }
112  
113 =pod
114  
115 =head2 list
116  
117 This method implements the list element, which displays a file listing.
118 The following attributes are used:
119  
120 =over 4
121  
122 =item src
123  
124 The name of the directory in which to display a file listing.
125  
126 =item cols
127  
128 The number of columns in the file listing.
129  
130 =back
131  
132 =cut
133  
134 sub list {
135 my $self = shift;
136 my ($code, $attribs) = @_;
137  
138 my $cols = $self->get_attrib ($attribs, 'cols') || 'undef';
139 my $src = $self->get_attrib ($attribs, 'src') || 'undef';
140  
141 # Create attribute param code;
142 my $attribs_named_params = $self->generate_named_params ($attribs);
143  
144 my $outcode = qq{
145 do {
146 \$vars->create_context ();
147  
148 \$process->print (qq{<table border="0" cellspacing="5">\n});
149 opendir (DIR, $src)
150 || die XML::Template::Exception->new ('File', "Could not open directory '" . $src . "': \$!");
151 while (my \$file = readdir (DIR)) {
152 \$process->print (" <tr>\n");
153 for (my \$i = 0; \$i < int ($cols); \$i++) {
154 \$process->print (" <td>\$file</td>\n");
155 \$file = readdir (DIR) || last;
156 }
157 \$process->print (" </tr>\n");
158 }
159 closedir (DIR);
160 \$process->print ("</table>\n");
161  
162 \$vars->delete_context ();
163 };
164 };
165  
166 return $outcode;
167 }
168  
169 =head2 foreach
170  
171 XML::Template::Element::File is a subclass of
172 L<XML::Template::Element::Iterator>, so it inherits the C<foreach> method,
173 which in conjunction with the iterator methods defined in this module,
174 implements iteration through a list of files.
175  
176 The following attributes are used:
177  
178 =over 4
179  
180 =item src
181  
182 The name of the directory containing files to iterate through.
183  
184 =item var
185  
186 The name of the variable, available in the content of this element, that
187 contains the current file name.
188  
189 =back
190  
191 For instance,
192  
193 <file:foreach xmlns:file="http://syrme.net/xml-template/file/v1"
194 src="/home/jowaxman" var="file">
195 file ${file}
196 </file:foreach>
197  
198 iterates through the files in the directory C</home/jowaxman> and prints
199 each one.
200  
201 =cut
202  
203 sub loopinit {
204 my $self = shift;
205 my $attribs = shift;
206  
207 my $src = $self->get_attrib ($attribs, 'src') || 'undef';
208 my $var = $self->get_attrib ($attribs, 'var') || 'undef';
209  
210 my $outcode = qq!
211 my \$__var = $var;
212 if (-d $src) {
213 opendir (DIR, $src)
214 || die XML::Template::Exception->new ('File', \$\!);
215 \@__array = readdir (DIR);
216 closedir (DIR);
217 } else {
218 \@__array = ($src);
219 }
220 !;
221  
222 return $outcode;
223 }
224  
225 sub set_loopvar {
226 my $self = shift;
227 my $attribs = shift;
228  
229 my $outcode = qq!
230 \$vars->set (\$__var => \$__value);
231 !;
232  
233 return $outcode;
234 }
235  
236 =pod
237  
238 =head1 AUTHOR
239  
240 Jonathan Waxman
241 <jowaxman@bbl.med.upenn.edu>
242  
243 =head1 COPYRIGHT
244  
245 Copyright (c) 2002-2003 Jonathan A. Waxman
246 All rights reserved.
247  
248 This program is free software; you can redistribute it and/or
249 modify it under the same terms as Perl itself.
250  
251 =cut
252  
253  
254 1;