/[cvs]/nfo/perl/libs/Data/Rap/Metadata.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Rap/Metadata.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations)
Sun Jun 20 23:00:23 2004 UTC (20 years ago) by joko
Branch: MAIN
Changes since 1.4: +12 -1 lines
minor fix: now creates cache-file in same directory where rap.xml is

1 ## ----------------------------------------------------------------------
2 ## $Id: Metadata.pm,v 1.4 2004/06/20 16:12:24 joko Exp $
3 ## ----------------------------------------------------------------------
4 ## $Log: Metadata.pm,v $
5 ## Revision 1.4 2004/06/20 16:12:24 joko
6 ## + sub indexTargets
7 ## modified getTargetDetails to use cached target details
8 ##
9 ## Revision 1.3 2003/03/29 07:13:19 joko
10 ## better exception handling if target was not found in xml
11 ##
12 ## Revision 1.2 2003/02/20 20:04:35 joko
13 ## renamed methods
14 ## - refactored xml-specific code to Data::Storage::Handler::XML
15 ##
16 ## Revision 1.1 2003/02/18 15:35:49 joko
17 ## + initial commit
18 ##
19 ## ----------------------------------------------------------------------
20
21
22 package Data::Rap::Metadata;
23
24 use strict;
25 use warnings;
26
27
28 use Data::Dumper;
29 use DesignPattern::Object;
30 use Storable;
31 use File::Basename qw( dirname );
32
33
34 sub accessMetadata {
35 my $self = shift;
36
37 # create instance of storage object once
38 if (!$self->{__rap}->{metadbengine}) {
39 $self->{__rap}->{metadbengine} =
40 DesignPattern::Object->fromPackage('Data::Storage::Handler::XML', filename => $self->{__rap}->{filename} );
41 }
42
43 return $self->{__rap}->{metadbengine};
44
45 }
46
47 sub getTargetList {
48 my $self = shift;
49
50 $self->log( "Reading database of targets (dot) from XML.", 'notice' );
51
52 # prepare access to metadata (assure instantiated storage handle)
53 my $mdbe = $self->accessMetadata();
54
55 $mdbe->sendQuery("*/target");
56 $mdbe->circumflex('result');
57
58 # checks
59 if ($mdbe->isEmpty) {
60 $self->log("XML metadata was empty.", 'critical');
61 return;
62 }
63
64 # trace
65 #print Dumper($self->{buffer});
66 #exit;
67
68 # behaviour
69 #$self->xml2simplehash();
70 my $tree = $mdbe->toEasyTree();
71 #return $result;
72
73 # trace
74 #print Dumper($tree);
75 #exit;
76
77 # build result
78 my @targets;
79 foreach my $entry (@{$tree->{content}}) {
80 my $description = $entry->{content}->[0]->{content}->[0]->{content};
81 push @targets, { name => $entry->{attrib}->{name}, description => $description };
82 }
83
84 return \@targets;
85
86 }
87
88
89 sub getTargetDetails {
90 my $self = shift;
91 my $target = shift;
92 my $options = shift;
93
94 # check cache and return result from there
95 if (my $result = $self->{cache}->{targets}->{$target} and not $options->{force}) {
96 return $result;
97 }
98
99 # get metadata for single task from storage
100 my $mdbe = $self->accessMetadata();
101 $mdbe->sendQuery("*/target[\@name=\"$target\"]");
102
103 # FIXME: this is wrong behaviour! upper statement should return a proper
104 # result, which itself (already) has the method 'isEmpty' or similar...
105 if ($mdbe->isEmpty()) {
106 $self->log( "Target '$target' not found.", 'notice' );
107 return;
108 }
109
110 my $tree = $mdbe->toEasyTree();
111
112 # trace
113 #print Dumper($tree);
114 #exit;
115
116 return $tree;
117 }
118
119 sub indexTargets {
120 my $self = shift;
121 my $options = shift;
122
123 # determine filename for cache-file
124 my $filename = 'rap.xml.cache';
125 if (my $rapfile = $self->{__rap}->{filename}) {
126 if (my $path = dirname($rapfile)) {
127 $filename = $path . '/' . $filename;
128 }
129 }
130
131 if ($options->{build}) {
132 $self->log("Building index of Targets", 'notice');
133 # clear cache
134 delete $self->{cache}->{targets};
135 my $list = $self->getTargetList();
136 foreach my $target (@$list) {
137 $self->log("Reading target: $target->{name}", 'info');
138 my $details = $self->getTargetDetails($target->{name}, { force => 1 });
139 $self->{cache}->{targets}->{$target->{name}} = $details;
140 }
141 store $self->{cache}->{targets}, $filename;
142
143 } elsif ($options->{load}) {
144 if (not -e $filename) {
145 return;
146 }
147 if ($self->{cache}->{targets} = retrieve($filename)) {
148 return 1;
149 }
150
151 } else {
152 $self->log("Please call with options build or load", 'warning');
153
154 }
155 }
156
157 1;
158 __END__

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed