/[webpac2]/trunk/lib/WebPAC/Validate.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Validate.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 671 by dpavlin, Mon Sep 11 15:59:35 2006 UTC revision 837 by dpavlin, Thu May 24 12:44:45 2007 UTC
# Line 18  WebPAC::Validate - provide simple valida Line 18  WebPAC::Validate - provide simple valida
18    
19  =head1 VERSION  =head1 VERSION
20    
21  Version 0.08  Version 0.11
22    
23  =cut  =cut
24    
25  our $VERSION = '0.08';  our $VERSION = '0.11';
26    
27  =head1 SYNOPSIS  =head1 SYNOPSIS
28    
# Line 41  configuration file in following format: Line 41  configuration file in following format:
41    205! a    205! a
42    # while 210 can have a c or d    # while 210 can have a c or d
43    210 a c d    210 a c d
44      # field which is ignored in validation
45      999-
46    
47  =head1 FUNCTIONS  =head1 FUNCTIONS
48    
# Line 50  Create new validation object Line 52  Create new validation object
52    
53    my $validate = new WebPAC::Validate(    my $validate = new WebPAC::Validate(
54          path => 'conf/validate/file',          path => 'conf/validate/file',
55            delimiters => [ ' : ', ' / ', ' ; ', ' , ' ],
56            delimiters_path => 'conf/validate/delimiters/file',
57    );    );
58    
59    Optional parametar C<delimiters> will turn on validating of delimiters. Be
60    careful here, those delimiters are just stuck into regex, so they can
61    contain L<perlre> regexpes.
62    
63  =cut  =cut
64    
65  sub new {  sub new {
# Line 59  sub new { Line 67  sub new {
67          my $self = {@_};          my $self = {@_};
68          bless($self, $class);          bless($self, $class);
69    
70    warn dump( @_ );
71    
72          my $log = $self->_get_logger();          my $log = $self->_get_logger();
73    
74          foreach my $p (qw/path/) {          if ( $self->{path} ) {
                 $log->logconfess("need $p") unless ($self->{$p});  
         }  
75    
76          my $v_file = read_file( $self->{path} ) ||                  my $v_file = read_file( $self->{path} ) ||
77                  $log->logdie("can't open validate path $self->{path}: $!");                          $log->logdie("can't open validate path $self->{path}: $!");
78    
79          my $v;                  my $v;
80          my $curr_line = 1;                  my $curr_line = 1;
81    
82          foreach my $l (split(/[\n\r]+/, $v_file)) {                  foreach my $l (split(/[\n\r]+/, $v_file)) {
83                  $curr_line++;                          $curr_line++;
84    
85                  # skip comments and whitespaces                          # skip comments and whitespaces
86                  next if ($l =~ /^#/ || $l =~ /^\s*$/);                          next if ($l =~ /^#/ || $l =~ /^\s*$/);
87    
88                  $l =~ s/^\s+//;                          $l =~ s/^\s+//;
89                  $l =~ s/\s+$//;                          $l =~ s/\s+$//;
90    
91                  my @d = split(/\s+/, $l);                          my @d = split(/\s+/, $l);
92    
93                  my $fld = shift @d;                          my $fld = shift @d;
94    
95                  if ($fld =~ s/!$//) {                          if ($fld =~ s/!$//) {
96                          $self->{must_exist}->{$fld}++;                                  $self->{must_exist}->{$fld}++;
97                  }                          } elsif ($fld =~ s/-$//) {
98                                    $self->{dont_validate}->{$fld}++;
99                            }
100    
101                  $log->logdie("need field name in line $curr_line: $l") unless (defined($fld));                          $log->logdie("need field name in line $curr_line: $l") unless (defined($fld));
102    
103                            if (@d) {
104                                    $v->{$fld} = [ map {
105                                            my $sf = $_;
106                                            if ( $sf =~ s/!(\*)?$/$1/ ) {
107                                                    $self->{must_exist_sf}->{ $fld }->{ $sf }++;
108                                            };
109                                            $sf;
110                                    } @d ];
111                            } else {
112                                    $v->{$fld} = 1;
113                            }
114    
                 if (@d) {  
                         $v->{$fld} = [ map {  
                                 my $sf = $_;  
                                 if ( $sf =~ s/!(\*)?$/$1/ ) {  
                                         $self->{must_exist_sf}->{ $fld }->{ $sf }++;  
                                 };  
                                 $sf;  
                         } @d ];  
                 } else {  
                         $v->{$fld} = 1;  
115                  }                  }
116    
117          }                  $log->debug("current validation rules: ", dump($v));
118    
119          $log->debug("current validation rules: ", dump($v));                  $self->{rules} = $v;
120    
121          $self->{rules} = $v;                  $log->info("validation uses rules from $self->{path}");
122            }
123    
124          $log->info("validation uses rules from $self->{path}");          if ( $self->{delimiters} ) {
125                    $self->{delimiters_regex} = '(\^[a-z0-9]|' . join('|', @{ $self->{delimiters} }) . ')';
126                    $log->info("validation check delimiters with regex $self->{delimiters_regex}");
127            }
128    
129            if ( my $path = $self->{delimiters_path} ) {
130                    if ( -e $path ) {
131                            $log->info("using delimiter validation rules from $path");
132                            open(my $d, $path) || $log->fatal("can't open $path: $!");
133                            while(<$d>) {
134                                    chomp($d);
135                                    if (/^\s*(#*)\s*(\d+)\t+(\d+)\t+(.*)$/) {
136                                            my ($comment,$field,$count,$template) = ($1,$2,$3,$4);
137                                            $self->{_validate_delimiters_templates}->{$field}->{$template} = $count unless ($comment);
138                                    } else {
139                                            warn "## ignored $d\n";
140                                    }
141                            }
142                            close($d);
143                            warn "_validate_delimiters_templates = ",dump( $self->{_validate_delimiters_templates} );
144                    } else {
145                            $log->warn("delimiters path $path doesn't exist, it will be created after this run");
146                    }
147            }
148    
149          $self ? return $self : return undef;          $self ? return $self : return undef;
150  }  }
151    
152  =head2 validate_errors  =head2 validate_rec
153    
154  Validate record and return errors  Validate record and return errors
155    
156    my @errors = $validate->validate_errors( $rec, $rec_dump );    my @errors = $validate->validate_rec( $rec, $rec_dump );
157    
158  =cut  =cut
159    
160  sub validate_errors {  sub validate_rec {
161          my $self = shift;          my $self = shift;
162    
163          my $log = $self->_get_logger();          my $log = $self->_get_logger();
164    
165          my $rec = shift || $log->logdie("validate_errors need record");          my $rec = shift || $log->logdie("validate_rec need record");
166          my $rec_dump = shift;          my $rec_dump = shift;
167    
168          $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH');          $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH');
169          $log->logdie("can't find validation rules") unless (my $r = $self->{rules});  #       $log->logdie("can't find validation rules") unless (my $r = $self->{rules});
170            my $r = $self->{rules};
171    
172          my $errors;          my $errors;
173    
# Line 142  sub validate_errors { Line 179  sub validate_errors {
179    
180                  next if (!defined($f) || $f eq '' || $f eq '000');                  next if (!defined($f) || $f eq '' || $f eq '000');
181    
182                    # first check delimiters
183                    if ( my $regex = $self->{delimiters_regex} ) {
184    
185                            foreach my $v (@{ $rec->{$f} }) {
186                                    my $l = _pack_subfields_hash( $v, 1 );
187                                    my $subfield_dump = $l;
188                                    my $template = '';
189                                    $l =~ s/$regex/$template.=$1/eg;
190                                    #warn "## template: $template\n";
191    
192                                    if ( $template ) {
193                                            $self->{_delimiters_templates}->{$f}->{$template}++;
194    
195                                            if ( my $v = $self->{_validate_delimiters_templates} ) {
196                                                    if ( ! defined( $v->{$template} ) ) {
197                                                            $errors->{$f}->{invalid_delimiters_combination} = $template;
198                                                            $errors->{$f}->{dump} = $subfield_dump;
199                                                    } else {
200                                                            warn "## $f $template ok\n";
201                                                    }
202                                            }
203                                    }
204                            }
205                    }
206    
207                    next unless ( $r );     # skip validation of no rules are specified
208    
209                    next if (defined( $self->{dont_validate}->{$f} ));
210    
211                    # track field usage
212                  $fields->{$f}++;                  $fields->{$f}++;
213    
214                  if ( ! defined($r->{$f}) ) {                  if ( ! defined($r->{$f}) ) {
# Line 215  sub validate_errors { Line 282  sub validate_errors {
282                  }                  }
283          }          }
284    
285            $log->debug("_delimiters_templates = ", dump( $self->{_delimiters_templates} ) );
286    
287          foreach my $must (sort keys %{ $self->{must_exist} }) {          foreach my $must (sort keys %{ $self->{must_exist} }) {
288                  next if ($fields->{$must});                  next if ($fields->{$must});
289                  $errors->{$must}->{missing}++;                  $errors->{$must}->{missing}++;
# Line 294  sub report_error { Line 363  sub report_error {
363    
364                          if ($k eq 'dump') {                          if ($k eq 'dump') {
365                                  $dump = $tree->{dump};                                  $dump = $tree->{dump};
366  #                               warn "## dump: ",dump($dump),"\n";                                  #warn "## dump ",dump($dump),"\n";
367                                  next;                                  next;
368                          }                          }
369    
# Line 328  sub report_error { Line 397  sub report_error {
397          sub _reformat {          sub _reformat {
398                  my $l = shift;                  my $l = shift;
399                  $l =~ s/\t/ /g;                  $l =~ s/\t/ /g;
400                  $l =~ s/_/ /;                  $l =~ s/_/ /g;
401                  return $l;                  return $l;
402          }          }
403    
# Line 373  sub report { Line 442  sub report {
442    
443  }  }
444    
445    =head2 delimiters_templates
446    
447    Generate report of delimiter tamplates
448    
449      my $report = $validate->delimiter_teplates(
450            report => 1,
451      );
452    
453    Options:
454    
455    =over 4
456    
457    =item report
458    
459    Generate humanly readable report with single fields
460    
461    =back
462    
463    =cut
464    
465    sub delimiters_templates {
466            my $self = shift;
467    
468            my $args = {@_};
469    
470            my $t = $self->{_delimiters_templates};
471    
472            my $log = $self->_get_logger;
473    
474            unless ($t) {
475                    $log->error("called without delimiters");
476                    return;
477            }
478    
479            my $out;
480    
481            foreach my $f (sort { $a <=> $b } keys %$t) {
482                    $out .= "$f\n" if ( $args->{report} );
483                    foreach my $template (sort { $a cmp $b } keys %{ $t->{$f} }) {
484                            my $count = $t->{$f}->{$template};
485                            $out .=
486                                    ( $count ? "" : "# " ) .
487                                    ( $args->{report} ? "" : "$f" ) .
488                                    "\t$count\t$template\n";
489                    }
490            }
491    
492            return $out;
493    }
494    
495    =head2 save_delimiters_templates
496    
497    =cut
498    
499    sub save_delimiters_templates {
500            my $self = shift;
501    
502            my $path = $self->{delimiters_path};
503    
504            return unless ( $path );
505    
506            my $log = $self->_get_logger;
507    
508            open(my $d, '>', $path) || $log->fatal("can't open $path: $!");
509            print $d $self->delimiters_templates;
510            close($d);
511    
512            $log->info("new delimiters templates saved to $path");
513    }
514    
515  =head1 AUTHOR  =head1 AUTHOR
516    
517  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

Legend:
Removed from v.671  
changed lines
  Added in v.837

  ViewVC Help
Powered by ViewVC 1.1.26