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