/[A3C]/lib/A3C/AAIEduHr.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 /lib/A3C/AAIEduHr.pm

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

revision 88 by dpavlin, Tue Apr 29 19:54:52 2008 UTC revision 91 by dpavlin, Wed Apr 30 16:15:25 2008 UTC
# Line 12  AAAIEduHr Line 12  AAAIEduHr
12  Pull varios stuff from XML at L<http://shema.aaiedu.hr/> and cache it as  Pull varios stuff from XML at L<http://shema.aaiedu.hr/> and cache it as
13  JSON on disk  JSON on disk
14    
15    =head1 METHODS
16    
17  =cut  =cut
18    
19  #use base 'Jifty::Object';  #use base 'Jifty::Object';
20    
21  use XML::Simple;  use XML::Rules;
22  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
23  use File::Slurp;  use File::Slurp;
24  use JSON::XS;  use JSON::XS;
25    
26  sub available {  =head2 vocabulary
27    
28      valid are A3C::AAIEduHr->vocabulary('hrEdu001'),
29    
30    =cut
31    
32    sub vocabulary {
33          my $self = shift;          my $self = shift;
34    
35          my $name = shift;          my $name = shift;
# Line 53  sub available { Line 61  sub available {
61          my $data;          my $data;
62    
63          if ( ! -e $data_path ) {          if ( ! -e $data_path ) {
64                  my $xs = XML::Simple->new;                  my $rules = XML::Rules->new(
65                  $data = $xs->XMLin($xml_path,                  stripspaces => 8,
66  #                       KeyAttr => { vocabularyentry => 'key' },                  rules => [
67                          ContentKey => '-content',                          _default => sub {
68                  ) || die "can't parse $xml_path: $!";                                  my ($tag_name, $tag_hash, $context, $parent_data) = @_;
69                                    warn "_default $tag_name ",dump( $tag_hash );
70                                    return;
71                            },
72                            vocabulary => 'no content',
73                            vocabularyentry => sub {
74                                    die "no key attribute?" unless defined( $_[1]->{key} );
75                                    my $v = {
76                                            display =>      $_[1]->{_content},
77                                            value   =>      $_[1]->{key},
78                                    };
79                                    # if same, just store key
80                                    $v = $_[1]->{key} if ( $v->{display} eq $v->{value} );
81                                    '@' . $_[0] => $v;
82                            },
83                    ]);
84                    $data = $rules->parsefile( $xml_path )
85                            || die "can't parse $xml_path: $!";
86    
87                    $data = $data->{vocabulary} || die "no vocabulary in data: ", dump( $data );
88    
89                  write_file( $data_path, encode_json( $data )) || die "can't save into $data_path: $!";                  write_file( $data_path, encode_json( $data )) || die "can't save into $data_path: $!";
90                  Jifty->log->info("saved dump into $data_path");                  Jifty->log->info("saved dump into $data_path");
91          } else {          } else {
92                  $data = decode_json( read_file( $data_path ) ) || die "can't read $data_path: $!";                  $data = decode_json( read_file( $data_path ) ) || die "can't read $data_path: $!";
93                    Jifty->log->info("loaded dump from $data_path");
94          }          }
95    
96          warn '## ',ref($data),' = ',dump($data);  #       warn '## ',ref($data),' = ',dump($data);
97    
98          die "data not for for $name" unless $data->{name} eq $name;          die "data not for for $name" unless $data->{name} eq $name;
99            warn "no vocabularyentry for $name from $data_path" unless defined( $data->{vocabularyentry} );
100    
101          return $data;          return $data->{vocabularyentry};
102  }  }
103    
104  warn "# AAUEduHr loaded\n";  warn "# AAUEduHr loaded\n";

Legend:
Removed from v.88  
changed lines
  Added in v.91

  ViewVC Help
Powered by ViewVC 1.1.26