FormMagick/Setup.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 191 CC 20 SCAR 38.4
CC
20
Cov
0%
clean_page_list
110.0
parse_xml
30.0
clean_field_list
20.0
CRAP 46.3
1 #!/usr/bin/perl -w
2 #
3 #
4 # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org>
5 # Copyright (c) 2000-2002 Mitel Networks Corporation
6 # This software is distributed under the same licenses as Perl itself;
7 # see the file COPYING for details.
8  
9 #
10 # $Id: Setup.pm,v 1.24 2003/02/05 17:18:36 anoncvs_gtkglext Exp $
11 #
12  
13 package CGI::FormMagick;
14  
15 use strict;
16 use Carp;
17 use File::Basename;
18  
19 =pod
20  
21 =head1 NAME
22  
23 CGI::FormMagick::Setup - setup/initialisation routines for FormMagick
24  
25 =head1 SYNOPSIS
26  
27 use CGI::FormMagick;
28  
29 =head1 DESCRIPTION
30  
31 =head2 default_xml_filename()
32  
33 default source filename to the same as the perl script, with .xml
34 extension
35  
36 =begin testing
37  
38 BEGIN: {
39 use vars qw( $fm );
40 use lib "./lib";
41 use CGI::FormMagick;
42 }
43  
44 ok($fm = CGI::FormMagick->new(type => 'file', source => "t/simple.xml"), "create fm object");
45 ok($fm2 = CGI::FormMagick->new(type => 'file', source => "t/simple.xml",
46 charset => 'ISO-8859-1'), "create fm object with charset");
47 $fm->parse_xml(); # suck in structure without display()ing
48 $fm2->parse_xml(); # suck in structure without display()ing
49  
50 =end testing
51  
52 =cut
53  
54 sub default_xml_filename {
55
56 my($scriptname, $scriptdir, $extension) =
57 File::Basename::fileparse($0, '\.[^\.]+');
58
59 return $scriptname . '.xml';
60 }
61  
62 =head2 parse_xml()
63  
64 Parses the XML input, setting $fm->{xml} to a usable hash of the form
65 elements, and $fm->{lexicon} to a hash of the l10n lexicon.
66  
67 The $fm->{xml} hash has the following form:
68  
69 {
70 'form' => {
71 'post-event' => 'submit_order',
72 'title' => 'FormMagick demo application'
73 },
74 'pages' => [
75 {
76 'post-event' => 'lookup_group_info',
77 'fields' => [
78 {
79 'type' => 'text',
80 'id' => 'firstname',
81 'validation' => 'nonblank',
82 'label' => 'first name'
83 },
84 {
85 'type' => 'text',
86 'id' => 'lastname',
87 'validation' => 'nonblank',
88 'label' => 'last name'
89 }
90 {
91 'type' => 'fragment',
92 'content' => 'This is a simple fragment'
93 }
94 {
95 'type' => 'subroutine',
96 'src' => 'fragment_subroutine_name()'
97 }
98 ],
99 'name' => 'Personal',
100 'title' => 'Personal details'
101 }
102 ]
103 };
104  
105 Note on lexicon files:
106 If FormMagick was given a charset argument, then the output will be encoded
107 in that character set. Otherwise, it will be in UTF-8.
108  
109 =for testing
110 is(ref($fm->{xml}), "HASH", "parse_xml gives us a hash");
111 is($fm->{xml}->{title}, "FormMagick demo application",
112 "Picked up form title");
113 is(ref($fm->{xml}->{pages}), "ARRAY",
114 "parse_xml gives us an array of pages");
115 is(ref($fm->{xml}->{pages}->[0]), "HASH",
116 "each page is a hashref");
117 is($fm->{xml}->{pages}->[0]->{name}, "Personal",
118 "Picked up first page's name");
119 is($fm->{xml}->{pages}->[0]->{title}, "Personal details",
120 "Picked up first page's title");
121 is(ref($fm->{xml}->{pages}->[0]->{fields}), "ARRAY",
122 "Page's fields are an array");
123 is(ref($fm->{xml}->{pages}->[0]->{fields}->[0]), "HASH",
124 "Field is a hashref");
125 is($fm->{xml}->{pages}->[0]->{fields}->[0]->{label}, "first name",
126 "Picked up field title");
127 is($fm->{xml}{pages}[0]{fields}[0]{description}, "description here",
128 "Picked up field description");
129 print "Charset parsing tests:\n";
130 is(ref($fm2->{xml}), "HASH", "parse_xml gives us a hash");
131 is($fm2->{xml}->{title}, "FormMagick demo application",
132 "Picked up form title");
133 is(ref($fm2->{xml}->{pages}), "ARRAY",
134 "parse_xml gives us an array of pages");
135 is(ref($fm2->{xml}->{pages}->[0]), "HASH",
136 "each page is a hashref");
137 is($fm2->{xml}->{pages}->[0]->{name}, "Personal",
138 "Picked up first page's name");
139 is($fm2->{xml}->{pages}->[0]->{title}, "Personal details",
140 "Picked up first page's title");
141 is(ref($fm2->{xml}->{pages}->[0]->{fields}), "ARRAY",
142 "Page's fields are an array");
143 is(ref($fm2->{xml}->{pages}->[0]->{fields}->[0]), "HASH",
144 "Field is a hashref");
145 is($fm2->{xml}->{pages}->[0]->{fields}->[0]->{label}, "first name",
146 "Picked up field title");
147 is($fm2->{xml}{pages}[0]{fields}[0]{description}, "description here",
148 "Picked up field description");
149  
150 =cut
151  
152 sub parse_xml {
153 my $self = shift;
154  
155 my $p;
156 if ($self->{charset})
157 {
158 $p = new XML::Parser (Style => 'Tree',
159 ProtocolEncoding => $self->{charset});
160 }
161 else
162 {
163 $p = new XML::Parser (Style => 'Tree');
164 }
165  
166 my $xml;
167  
168 if ($self->{inputtype} eq "file") {
169 $xml = $p->parsefile($self->{source} || default_xml_filename());
170 } elsif ($self->{inputtype} eq "string") {
171 # Catch errors in parse_xml and save the output for debugging.
172 my $result = eval { $xml = $p->parse($self->{source}) };
173 unless ($result)
174 {
175 open OUT, ">/tmp/FormMagick_XML_$$";
176 print OUT $self->{source};
177 print OUT "\n\n<!--\nERRORS:\n$@\n-->\n";
178 croak "Whoops, parse_xml() failed. The data and error messages were
179 saved to /tmp/FormMagick_XML_$$\n";
180 }
181 } else {
182 croak 'Invalid source type specified (should be "file" or "string")';
183 }
184  
185 my @dirty_form = @{$xml->[1]};
186  
187 my %form_attributes = %{$dirty_form[0]};
188  
189 my @form_elements = $self->clean_xml_array(@dirty_form[1..$#dirty_form]);
190  
191 my ($pages, $lexicon) =
192 $self->clean_page_list(\@form_elements, \%form_attributes);
193  
194 $self->{xml} = {
195 %form_attributes,
196 pages => $pages,
197 };
198  
199 $self->{lexicon} = $lexicon;
200  
201 }
202  
203 =head2 clean_field_list
204  
205 Given a messy field list (as seen as @page_fields in parse_xml()),
206 removes extraneous data and returns a clean list.
207  
208 =cut
209  
210 sub clean_field_list {
211 my $self = shift;
212 my @page_fields = @_;
213 my @fields;
214 field: foreach my $field (@page_fields) {
215 my $field_type = $field->[0];
216 my %field_attributes = %{$field->[1]};
217 my @this_field = @$field;
218 my @field_elements = @this_field[2..$#this_field];
219 @field_elements = $self->clean_xml_array(@field_elements);
220  
221 field_element: foreach my $field_element (@field_elements) {
222 if ($field_type eq 'html') {
223 $field_attributes{type} = 'html';
224 $field_attributes{content} = $field->[3];
225 } elsif ($field_type eq 'subroutine') {
226 $field_attributes{type} = 'subroutine';
227 } elsif ($field_element->{type}) {
228 $field_attributes{$field_element->{type}} =
229 $field_element->{content}->[2];
230 } else {
231 next field_element;
232 }
233 }
234  
235 push @fields, \%field_attributes;
236 }
237  
238 return @fields;
239 }
240  
241  
242 =head2 clean_page_list(\@form_elements, \%form_attributes)
243  
244 Given a messy list of form elements (as seen as @form_elements in
245 parse_xml()), removes extraneous data and returns: 1) a
246 clean list of pages in the form, and 2) a lexicon hash.
247  
248 =cut
249  
250 sub clean_page_list {
251 my $self = $_[0];
252 my @form_elements = @{$_[1]};
253 my %form_attributes = %{$_[2]};
254 my @form_pages;
255 my @lexicons;
256 form_element: foreach my $form_element (@form_elements) {
257 if (not $form_element->{type}) {
258 next form_element;
259 } elsif ($form_element->{type} eq 'page') {
260 push @form_pages, $form_element->{content};
261 } elsif ($form_element->{type} eq 'lexicon') {
262 push @lexicons, $form_element->{content};
263 } elsif ($form_element->{type}) {
264 $form_attributes{$form_element->{type}} =
265 $form_element->{content}->[2];
266 }
267 }
268  
269 my @pages;
270 page: foreach my $page (@form_pages) {
271 my %page_attributes = %{$page->[0]};
272 my @this_page = @$page;
273 my @page_elements = @this_page[1..$#this_page];
274 @page_elements = $self->clean_xml_array(@page_elements);
275  
276 my @page_fields;
277 #use Data::Dumper;
278 #print Dumper @page_elements;
279 page_element: foreach my $page_element (@page_elements) {
280 if (not $page_element->{type}) {
281 next page_element;
282 } elsif ($page_element->{type} eq 'field') {
283 push @page_fields,
284 ["field", @{$page_element->{content}}];
285 } elsif ($page_element->{type} eq 'html') {
286 push @page_fields,
287 ["html", @{$page_element->{content}}];
288 } elsif ($page_element->{type} eq 'subroutine') {
289 push @page_fields,
290 ["subroutine", @{$page_element->{content}}];
291 } elsif ($page_element->{type}) {
292 $page_attributes{$page_element->{type}} =
293 $page_element->{content}->[2];
294 }
295 }
296  
297 my @fields = $self->clean_field_list(@page_fields);
298  
299 push @pages, { %page_attributes, fields => \@fields };
300 }
301  
302 my %lexicon = $self->get_lexicon(@lexicons);
303  
304 return \@pages, \%lexicon;
305 }
306  
307 =head2 $self->clean_xml_array($xml)
308  
309 Cleans up XML by removing superfluous stuff. Given an array of XML,
310 returns a cleaner array.
311  
312 =cut
313  
314 sub clean_xml_array {
315 my ($self, @array) = @_;
316 my @clean_array;
317 for (my $i=0; $i <= @array; $i+=4) {
318 my ($type, $content) = @array[$i+2, $i+3];
319 push @clean_array, { type => $type, content => $content };
320 }
321 return @clean_array;
322 }
323  
324 =pod
325  
326 =head2 initialise_sessiondir($self)
327  
328 Figures out where the session tokens should be kept.
329  
330 =for testing
331 ok( CGI::FormMagick::initialise_sessiondir("abc"), "Initialise sessiondir with name");
332 ok( CGI::FormMagick::initialise_sessiondir(), "Initialise sessiondir with undef");
333  
334 =cut
335  
336 sub initialise_sessiondir {
337 my ($sessiondir) = @_;
338  
339 # use the user-defined session handling directory (or default to
340 # session-tokens) to store session tokens
341 if ($sessiondir) {
342 return $sessiondir;
343 } else {
344 return get_or_create_default_sessiondir();
345 }
346 }
347  
348 sub get_or_create_default_sessiondir {
349 # It's recommended that you use a more hidden directory than this.
350 # However, this is the best default we can think of:
351 my $scriptdir = (File::Basename::fileparse($0, '\.[^\.]+'))[1];
352 my $sessionid_dir_name = $scriptdir . "session-tokens/";
353  
354 ensure_dir_is_writable($sessionid_dir_name)
355 or warn "(Expect CGI::Persistent to complain)";
356  
357 return $sessionid_dir_name;
358 }
359  
360 sub ensure_dir_is_writable {
361 my ($dir_name) = @_;
362  
363 if (not -d $dir_name) {
364 mkdir($dir_name) or do {
365 warn "Can't create $dir_name";
366 return 0;
367 }
368 }
369  
370 if (not -w $dir_name) {
371 warn "Can't write to $dir_name";
372 return 0;
373 }
374  
375 return 1;
376 }
377  
378 return "FALSE"; # true value ;)
379  
380 =pod
381  
382 =head1 SEE ALSO
383  
384 CGI::FormMagick
385  
386 =cut