MetaGenerator/Formula.pm untested

filter: Statementstmt 0.0% 0 / 346 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 48 Podpod 0.0% 0 / 44 totaltotal 0.0% 0 / 570 CC 50 SCAR 47.2
CC
50
Cov
0%
handle_start
306.0
_parse
306.0
handle_end
42.0
CRAP 112.7
1 package XML::MetaGenerator::Formula;
2  
3 use strict;
4  
5 BEGIN {
6 $XML::MetaGenerator::Formula::VERSION = '0.03';
7 @XML::MetaGenerator::Formula::ISA = qw();
8 }
9  
10 sub new {
11 my ($proto) = shift;
12 my ($class) = ref $proto || $proto;
13 my ($valids) = {};
14 my ($missings) = [];
15 my ($invalids) = [];
16 my ($handlers) = [
17 Init => \&{__PACKAGE__.'::handle_init'},
18 Start => \&{__PACKAGE__.'::handle_start'},
19 End => \&{__PACKAGE__.'::handle_end'},
20 Char => \&{__PACKAGE__.'::handle_char'}
21 ];
22 bless {
23 contest => [],
24 valids => $valids,
25 missings => $missings,
26 invalids => $invalids,
27 handlers => $handlers
28 }, $class;
29 }
30  
31 sub getHandlers {
32 my ($self) = shift;
33 return $self->{handlers};
34 }
35 ######################################################################
36 # Expat Handlers
37 ######################################################################
38  
39 sub handle_init {
40 my ($expat) = shift;
41 my ($wow) = XML::MetaGenerator->get_instance;
42 contest_push(contest_new($wow->{formula_key}, 'formula'))
43 }
44  
45 sub handle_start {
46 my ($expat) = shift;
47 my ($element) = shift;
48 my %attr = @_;
49  
50 my $wow=XML::MetaGenerator->get_instance;
51 my $self=$wow->{validator};
52 my $invalids = $self->{invalids};
53 my $missings = $self->{missings};
54  
55 if ($element eq 'element') {
56 # --- first of all change contest, then apply any global filter
57 contest_push(contest_new($attr{name}, 'element'));
58 my @filters = filters_get();
59 foreach (@filters) {
60 # #apply this filter ($_) to $wow->{form}->{$attr{name}}
61 my $filter = $_;
62 my $filtersub = 'filter_'.$filter;
63 {
64 no strict qw(subs refs);
65 $wow->{form}->{$attr{name}} = $filtersub->($wow->{form}->{$attr{name}});
66 }
67 }
68  
69 # if we have type constraints, check if they're ok
70 if ($attr{type}) {
71 my $type = $attr{type};
72 if ($type eq 'date') {
73 push @{$invalids}, $attr{name} unless ($wow->{form}->{$element} =~ m|\d+\/\d+\/\d+|);
74 }
75 }
76 # if we have a size limit, check this too
77 if ($attr{size}) {
78 push @{$invalids}, $attr{name} unless (length($wow->{form}->{$attr{name}}) <= $attr{size});
79 }
80 # then check the FLAGS
81 if (defined($attr{required}) && (lc($attr{required}) ne 'no')) {
82 push @{$missings}, $attr{name} unless (defined($wow->{form}->{$attr{name}}) && $wow->{form}->{$attr{name}} ne '');
83 }
84 }
85  
86 # it's not an input element, let's try with other tags...
87 elsif ($element eq 'deps') {
88 # empty for now...
89  
90 } elsif ($element eq 'check') {
91 # constraint. find out which type of 'check is it.
92 warn "check type not defined. \n" unless (defined $attr{type} && $attr{type} ne '');
93 my $type = $attr{type};
94 # pushing old sp onto the stack and initialize our env
95 my $contest = contest_get();
96 # should add the type to an array in order to resolve it later...
97 push @{$contest->{stack}}, $contest->{sp}, $type;
98 $contest->{sp} = 0;
99  
100 } elsif ($element eq 'filter') {
101 # filter to be applied to input. type attribute is the key
102 my $type = $attr{type};
103 my $contest = contest_get();
104 if ($contest->{type} eq 'element') {
105 my $filtersub = 'filter_'.$type ;
106 {
107 no strict qw(subs refs);
108 $wow->{form}->{$contest->{key}} = $filtersub->($wow->{form}->{$contest->{key}});
109 }
110 } else {
111 filters_add($type);
112 }
113 } elsif ($element eq 'param') {
114 # param of a check (or filter). should put into the stack the param id, if any
115 my $contest = contest_get();
116 if (defined($attr{id})) {
117 push @{$contest->{stack}}, $attr{id};
118 } else {
119 my $label = "_param".$contest->{sp};
120 push @{$contest->{stack}}, $label;
121 }
122 $contest->{sp}++;
123  
124  
125 } elsif ($element eq 'ref') {
126 # reference to another element. label parameter is the key
127 my $contest = contest_get();
128 $contest->{buffer} .= $wow->{form}->{$attr{label}};
129 }
130 }
131  
132 sub handle_char {
133 my ($expat) = shift;
134 my ($string) = shift;
135 # add $string to the loco buffer for future processing
136 my $contest = contest_get();
137 $contest->{buffer} .= $string;
138 }
139  
140 sub handle_end {
141 my ($expat) = shift;
142 my ($element) = shift;
143  
144 my $wow = XML::MetaGenerator->get_instance;
145 my $invalids = $wow->{validator}->{invalids};
146 my $valids = $wow->{validator}->{valids};
147 my $missings = $wow->{validator}->{missings};
148  
149 # in case of a filter or a check, should resolve it.
150 # in case of an element, should do garbage collection and restore contest
151 if ($element eq 'element') {
152 # restore previous contest
153 my $contest = contest_pop();
154 # return value
155 # XXXX Heavy work in progress here!
156 ${$valids}{$contest->{key}} = $wow->{form}->{$contest->{key}} unless (grep (/^$contest->{key}$/, @{$invalids}, @{$missings}));
157 }
158  
159 elsif ($element eq 'param') {
160 my $contest = contest_get();
161 $contest->{buffer} =~ s/^[\s\t\r\n]+//;
162 $contest->{buffer} =~ s/[\s\t\r\n]+$//;
163  
164 push @{$contest->{stack}}, $contest->{buffer} unless (!defined($contest->{buffer}));
165 $contest->{buffer} = '';
166 }
167  
168 elsif ($element eq 'check') {
169 #do the actual check
170 my $contest = contest_get();
171 my @args;
172 while ($contest->{sp}--) {
173 my $val = pop @{$contest->{stack}};
174 my $key = pop @{$contest->{stack}};
175 push @args, $key, $val;
176 }
177 # extract one more item from the stack, it holds the check type XXX
178 my ($checksub) = "check_". pop @{$contest->{stack}};
179 {
180 no strict qw(subs refs);
181 $checksub->($contest->{key}, @args);
182 }
183 }
184  
185 }
186  
187 ######################################################################
188 # Contest subsystem
189 ######################################################################
190  
191 sub contest_new {
192 my ($key) = shift;
193 my ($type) = shift;
194 my (@filters) = undef;
195 my (@deps) = undef;
196 my (@stack) = undef;
197 my ($buffer) = 0;
198 return {
199 key => $key,
200 type => $type,
201 sp => 0,
202 buffer => undef,
203 stack => [],
204 filters => [],
205 deps => [],
206 };
207 }
208  
209 sub contest_push {
210 my ($c) = shift;
211  
212 my $wow = XML::MetaGenerator->get_instance;
213 my $contest = $wow->{validator}->{contest};
214 push @{$contest}, $c;
215 }
216  
217 sub contest_pop {
218 my $wow = XML::MetaGenerator->get_instance;
219 my $contest = $wow->{validator}->{contest};
220 return pop @{$contest};
221 }
222  
223 sub contest_key {
224 my $wow = XML::MetaGenerator->get_instance;
225 my @contest = @{$wow->{validator}->{contest}};
226  
227 return $contest[$#contest]->{key};
228 }
229  
230 sub contest_get {
231 my $wow = XML::MetaGenerator->get_instance;
232 my @contest = @{$wow->{validator}->{contest}};
233  
234 return $contest[$#contest];
235 }
236  
237 ######################################################################
238 # Filters
239 ######################################################################
240  
241 sub filter_trim {
242 my ($string) = shift;
243 $string =~ s/^[\s\t]+//g;
244 $string =~ s/[\s\t]+$//g;
245 return $string;
246 }
247  
248 sub filter_strip {
249 my ($string) = shift;
250 $string =~ s/\s+/ /g;
251 return $string;
252 }
253  
254 sub filter_uc {
255 my ($string) = shift;
256 return uc($string);
257 }
258  
259 sub filter_lc {
260 my ($string) = shift;
261 return lc($string);
262 }
263  
264 sub filter_ucfirst {
265 my ($string) = shift;
266 return ucfirst($string);
267 }
268  
269 sub filter_money {
270 my $value = shift;
271 $value =~ tr/,/./;
272 $value =~ tr/0-9.+-//dc;
273 ($value) =~ m/(\d+\.?\d?\d?)/;
274 return $value;
275 }
276  
277 sub filter_phone {
278 my $value = shift;
279 $value =~ tr/0-9,().#-\+ //dc;
280 return $value;
281 }
282  
283 sub filter_digit {
284 my $value = shift;
285 $value =~ s/\D//g;
286  
287 return $value;
288 }
289  
290 sub filter_alphanum {
291 my $value = shift;
292 $value =~ s/\W//g;
293 return $value;
294 }
295  
296 sub filter_integer {
297 my $value = shift;
298 $value =~ tr/0-9+-//dc;
299 ($value) =~ m/([-+]?\d+)/;
300 return $value;
301 }
302  
303 sub filter_pos_integer {
304 my $value = shift;
305 $value =~ tr/0-9+//dc;
306 ($value) =~ m/(\+?\d+)/;
307 return $value;
308 }
309  
310 sub filter_neg_integer {
311 my $value = shift;
312 $value =~ tr/0-9-//dc;
313 ($value) =~ m/(-\d+)/;
314 return $value;
315 }
316  
317 sub filter_decimal {
318 my $value = shift;
319 # This is a localization problem, but anyhow...
320 $value =~ tr/,/./;
321 $value =~ tr/0-9.+-//dc;
322 ($value) =~ m/([-+]?\d+\.?\d*)/;
323 return $value;
324 }
325  
326 sub filter_pos_decimal {
327 my $value = shift;
328 # This is a localization problem, but anyhow...
329 $value =~ tr/,/./;
330 $value =~ tr/0-9.+//dc;
331 ($value) =~ m/(\+?\d+\.?\d*)/;
332 return $value;
333 }
334  
335 sub filter_neg_decimal {
336 my $value = shift;
337 # This is a localization problem, but anyhow...
338 $value =~ tr/,/./;
339 $value =~ tr/0-9.-//dc;
340 ($value) =~ m/(-\d+\.?\d*)/;
341 return $value;
342 }
343  
344 sub filter_quotemeta {
345 quotemeta $_[0];
346 }
347  
348 # Filter to "XMLify" html code in order to make sablotron and co. happier. This code is ugly by now,
349 # should rewrite this someday oneday.
350 sub _parse {
351 my ($string) = shift;
352 my ($state) = shift;
353 my $index='';
354 my $tok = '';
355 if ($state== 0) {
356 # CHECK FOR START TAG
357 if (($index =index $string, '<') != -1) {
358 $tok = substr $string, 0, $index+1, '';
359 $tok .= _parse ($string, 1);
360 return $tok;
361 } else {
362 return $string;
363 }
364 } elsif ($state == 1){
365 if ($string =~ s/^[\s\n\r]*([^\s^>]+[\s\n\r]*)//) {
366 $tok = lc($1);
367 }
368 if ($tok =~ m/(area|basefont|base|br|hr|img|input|isindex|link|map|meta|nobr|param|wbr)/) {
369 $tok .= _parse ($string, 4);
370 return $tok;
371 } elsif (($index = index $string, '=') != -1) {
372 $tok .= substr $string, 0, $index+1, '';
373 $tok .= _parse ($string, 3);
374 return $tok;
375 }elsif (($index = index $string, '"') != -1) {
376 $tok .= substr $string, 0, $index+1, '';
377 $tok .= _parse ($string, 2);
378 return $tok;
379 } elsif (($index = index $string,'>') != -1) {
380 $tok .= substr $string, 0, $index+1, '';
381 $tok .= _parse ($string, 0);
382 return $tok;
383 } else {
384 return $string;
385 }
386 } elsif ($state == 2) {
387 $index = index $string, '"';
388 if (($index = index $string, '"') != -1) {
389 $tok = substr $string, 0, $index+1,'';
390 $tok .= _parse ($string,1);
391 return $tok;
392 }else {
393 return $string;
394 }
395 } elsif ($state == 3) {
396 if ($string =~ s/^[\s\n\r]*([^\"\s\>]+)[\s\n\r]*//) {
397 $tok = '"'.$1.'" ';
398 print STDERR "\n\tTOK: $tok; STRING: $string\n";
399 $tok .= _parse ($string, 1);
400 print STDERR "\n\tTOK: $tok\n";
401 return $tok;
402 } else {
403 $tok .= _parse ($string, 1);
404 return $tok;
405 }
406  
407 } elsif ($state == 4) {
408 if (($index = index $string, '"') != -1) {
409 $tok = substr $string, 0, $index+1, '';
410 #print lc($tok);
411 $tok .= _parse ($string, 2);
412 return $tok;
413 } elsif (($index = index $string, '=') != -1) {
414 $tok = substr $string, 0, $index+1, '';
415 #print lc($tok);
416 $tok.= _parse ($string, 3);
417 return $tok;
418 } elsif (($index = index $string,'>') != -1) {
419 $tok = substr $string, 0, $index, '';
420 # print lc($tok);
421 $tok.="/>";
422 substr $string, 0, 1, '';
423 $tok .= _parse ($string, 0);
424 return $tok;
425 } else {
426 return $string;
427 }
428 }
429 }
430  
431 sub filter_to_xml {
432 my ($string) = shift;
433 print "Filter to_xml Called with argument: '$string'\n";
434 my ($res) = _parse($string, 0);
435 return $res;
436 }
437  
438 sub filter_regex {
439 my ($string)= shift;
440 my %params = @_;
441 my $sub = eval 'sub { $_[0] =~ '. $params{regex} . '}';
442 die "Error compiling regular expression ".$params{regex}.": $@" if $@;
443 return $sub;
444 }
445 ################################################################################
446 # Filter Utils Subs
447 ################################################################################
448  
449 sub filters_get {
450 my ($wow) = XML::MetaGenerator->get_instance;
451 my @contest = @{$wow->{validator}->{contest}};
452 my @list;
453 my ($this_contest);
454 foreach $this_contest (@contest) {
455 foreach (@{$this_contest->{filters}}) {
456 push @list, $_;
457 }
458 }
459 return @list;
460 }
461  
462 sub filters_add {
463 my ($wow) = XML::MetaGenerator->get_instance;
464 my $contest = contest_get;
465 my $filter = shift;
466 push @{$contest->{filters}}, $filter;
467 }
468  
469 ######################################################################
470 # Checks
471 ######################################################################
472  
473 sub check_state_province {
474 my ($element) = shift;
475 my (%args) = @_;
476 my ($wow) = XML::MetaGenerator->get_instance;
477 my $states = {
478 default => [qw(ag al an ao ap aq ar at av ba bg bi bl bn bo br bs bz ca cb ce ch cl cn co cr cs ct cz en fe fg fi fo fr ge go gr im is kr lc le li lo lt lu mc me mi mn mo ms mt na no nu or pa pc pd pe pg pi pn po pr ps pt pv pz ra rc re rg ri rm rn ro sa si so sp sr ss sv ta te tn to tp tr ts tv ud va vb vc ve vi vr vt vv)],
479 usa => [qw(al ak az ar ca co ct de fl ga hi id il in ia ks ky la me md
480 ma mi mn ms mo mt ne nv nh nj nm ny nc nd oh ok or pa pr ri
481 sc sd tn tx ut vt va wa wv wi wy dc ap fp fpo apo gu vi)],
482 canada => [qw(ab bc mb nb nf ns nt on pe qc sk yt yk)]
483 };
484 my $country = ((defined($args{country})) && ($args{country} ne '') && (exists $states->{$args{country}}))?$args{country}:'default';
485 my $found = 0;
486 foreach (@{$states->{$country}}) {
487 $found = 1 unless (lc($wow->{form}->{$element}) ne $_);
488 }
489  
490 push @{$wow->{validator}->{invalids}}, $element unless ($found);
491 }
492  
493 sub check_eq {
494 my ($element) = shift;
495 my ($dummy, $string) = @_;
496 my ($wow) = XML::MetaGenerator->get_instance;
497 if ($string eq $wow->{form}->{$element}) {
498 }
499 else {
500 push @{$wow->{validator}->{invalids}}, $element;
501 }
502 }
503  
504 sub check_cap_zip {
505 return check_zip(@_) || check_postcode(@_);
506 }
507  
508 sub check_postcode {
509 my ($element) = @_;
510 my ($wow) = XML::MetaGenerator->get_instance;
511 $wow->{form}->{$element} =~ s/[_\W]+//g;
512 push @{$wow->{validator}->{invalids}}, $element unless ($wow->{form}->{$element} =~ /^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]\d[A-Za-z][- ]?\d[A-Za-z]\d$/);
513 }
514  
515 sub check_zip {
516 my ($element) = @_;
517 my ($wow) = XML::MetaGenerator->get_instance;
518 push @{$wow->{validator}->{invalids}}, $element unless ($wow->{form}->{$element} =~ /^\s*\d{5}(?:[-]\d{4})?\s*$/);
519 }
520  
521 sub check_min_length {
522 my ($element, $dummy, $length) = @_;
523 my $string = $element;
524 my ($wow) = XML::MetaGenerator->get_instance;
525 if (length($string) >= $length) {
526 } else {
527 push @{$wow->{validator}->{invalids}}, $element;
528 }
529 }
530  
531 sub check_email {
532 my ($element) = shift;
533 my ($wow) = XML::MetaGenerator->get_instance;
534 push @{$wow->{validator}->{invalids}}, $element unless ($wow->{form}->{$element} =~ /[\040-\176]+\@[-A-Za-z0-9.]+\.[A-Za-z]+/);
535 }
536  
537 # simple check for italian like fiscal code. UGLY code
538 sub check_cfisc {
539 my ($element) = shift;
540 my ($wow) = XML::MetaGenerator->get_instance;
541 my $sommaind = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
542 my @sommapari = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
543 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 );
544 my @sommadisp = (1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 1, 0, 5, 7, 9, 13, 15, 17, 19,
545 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23 );
546  
547 my $str = $wow->{form}->{$element};
548 # check cfisc length (must be 16)
549 if (length($str) != 16) {
550 push @{$wow->{validator}->{invalids}}, $element;
551 return;
552 }
553 my $somma = 0;
554 my $i = 1;
555 while ($i<15) {
556 my $x = index ($sommaind, uc(substr($str,$i,1)));
557 $somma = $somma + $sommapari[$x];
558 $i+=2;
559 }
560 $i = 0;
561 while ($i<15) {
562 my $x = index ($sommaind, uc(substr($str, $i, 1)));
563 $somma = $somma + $sommadisp[$x];
564 $i+=2;
565 }
566 my $ris = $somma%26;
567 my $CIN = substr($sommaind, $ris+10, 1);
568 # print STDERR "CIN: $CIN\n";
569 push @{$wow->{validator}->{invalids}}, $element unless (uc($wow->{form}->{$element}) =~ m/$CIN$/);
570 }
571  
572 sub check_cc_no {}
573  
574 sub check_cc_exp {}
575  
576 sub check_cc_type {
577 my ($element)= shift;
578 my $wow = XML::MetaGenerator->get_instance;
579 push @{$wow->{validator}->{invalids}}, $element unless ($wow->{form}->{$element} =~ /^[MVAD]/i);
580 }
581  
582  
583 1;