source: trunk/gsdl/perllib/classify/HFileHierarchy.pm@ 10218

Last change on this file since 10218 was 10218, checked in by kjdon, 19 years ago

Jeffrey's new parsing modifications, committed approx 6 July, 15.16

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1###########################################################################
2#
3# HFileHierarchy.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# classifier plugin for generating hierarchical classifications
27
28
29package HFileHierarchy;
30
31use BasClas;
32use util;
33use cfgread;
34use sorttools;
35
36sub BEGIN {
37 @HFileHierarchy::ISA = ('BasClas');
38}
39
40my $arguments =
41 [ { 'name' => "metadata",
42 'desc' => "{AZCompactList.metadata}",
43 'type' => "metadata",
44 'reqd' => "yes" },
45 { 'name' => "firstvalueonly",
46 'desc' => "{AZCompactList.firstvalueonly}",
47 'type' => "flag",
48 'reqd' => "no" },
49 { 'name' => "allvalues",
50 'desc' => "{AZCompactList.allvalues}",
51 'type' => "flag",
52 'reqd' => "no" },
53 { 'name' => "hfile",
54 'desc' => "{Hierarchy.hfile}",
55 'type' => "string",
56 'deft' => "",
57 'reqd' => "no" },
58 { 'name' => "buttonname",
59 'desc' => "{BasClas.buttonname}",
60 'type' => "string",
61 'deft' => "{BasClas.metadata.deft}",
62 'reqd' => "no" },
63 { 'name' => "sort",
64 'desc' => "{Hierarchy.sort}",
65 'type' => "string",
66# 'deft' => "{BasClas.metadata.deft}",
67 'reqd' => "no" },
68 { 'name' => "reverse_sort",
69 'desc' => "{Hierarchy.reverse_sort}",
70 'type' => "flag",
71 'reqd' => "no" },
72 { 'name' => "hlist_at_top",
73 'desc' => "{Hierarchy.hlist_at_top}",
74 'type' => "flag",
75 'reqd' => "no" } ];
76
77my $options =
78{ 'name' => "HFileHierarchy",
79 'desc' => "{HFileHierarchy.desc}",
80 'abstract' => "yes",
81 'inherits' => "yes",
82 'args' => $arguments };
83
84
85sub new {
86 my ($class) = shift (@_);
87 my ($classifierslist,$inputargs,$hashArgOptLists) = @_;
88 push(@$classifierslist, $class);
89
90 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
91 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
92
93 my $self = (defined $hashArgOptLists)? new BasClas($classifierslist,$inputargs,$hashArgOptLists): new BasClas($classifierslist,$inputargs);
94
95 my $metadata = $self->{'metadata'};
96 if (!$metadata) {
97 print STDERR "$class Error: required option -metadata not supplied\n";
98 $self->print_txt_usage(""); # Use default resource bundle
99
100 die "$class Error: required option -metadata not supplied\n";
101 }
102
103 $self->{'buttonname'} = $self->generate_title_from_metadata($metadata) unless ($self->{'buttonname'});
104
105 #$self->{'metaname'} = $metadata;
106 my @meta_list = split(/,/, $metadata);
107 $self->{'meta_list'} = \@meta_list;
108
109 print STDERR "sort = $self->{'sort'}\n";
110 # sort = undef in this case is the same as sort=nosort
111 if ($self->{'sort'} eq "nosort") {
112 $self->{'sort'} = undef;
113 }
114
115 # sortname is handled a bit differently - kjdon
116 # undef means to sort, but use the metadata value from -metadata
117 # because there is no one metadata value to get for sorting when
118 # we have a list of possible metadata.
119 # To get no sorting, set sortname = 'nosort'
120 # we don't need to set it to undef if its not defined do we???
121
122# if (!$self->{'sort'}) {
123# if (defined ($metadata)) {
124# $sortname = undef;
125# } else {
126# $sortname = "nosort";
127# }
128# }
129 # $self->{'sortname'} = $sortname;
130
131 #if (defined $self->{'sort'} && $reverse_sort) {
132# $self->{'reverse_sort'} = 1;
133 # }
134
135 if ($self->{'hfile'}) {
136 my $hfile = $self->{'hfile'};
137 my $subjectfile;
138 $subjectfile = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"etc", $hfile);
139 if (!-e $subjectfile) {
140 my $collfile = $subjectfile;
141 $subjectfile = &util::filename_cat($ENV{'GSDLHOME'},"etc", $hfile);
142 if (!-e $subjectfile) {
143 my $outhandle = $self->{'outhandle'};
144 print STDERR "\nHFileHierarchy Error: Can't locate subject file $hfile\n";
145 print STDERR "This file should be in $collfile or $subjectfile\n";
146 $self->print_txt_usage(""); # Use default resource bundle
147 print STDERR "\nHFileHierarchy Error: Can't locate subject file $hfile\n";
148 print STDERR "This file should be in $collfile or $subjectfile\n";
149 die "\n";
150 }
151 }
152 $self->{'descriptorlist'} = {}; # first field in subject file
153 $self->{'locatorlist'} = {}; # second field in subject file
154 $self->{'subjectfile'} = $subjectfile;
155 }
156
157
158 # $self->{'firstvalueonly'} = $firstvalueonly;
159 # $self->{'allvalues'} = $allvalues;
160
161 #$self->{'hlist_at_top'} = $hlist_at_top;
162
163 # Clean out the unused keys
164 delete $self->{'metadata'};
165 delete $self->{'hfile'};
166
167 return bless $self, $class;
168}
169
170sub init {
171 my $self = shift (@_);
172
173 my $subjectfile = $self->{'subjectfile'};
174 if (defined $subjectfile) {
175 # read in the subject file
176 my $list = &cfgread::read_cfg_file ($self->{'subjectfile'}, undef, '^[^#]?\S');
177 # $list is a hash that is indexed by the descriptor. The contents of this
178 # hash is a list of two items. The first item is the OID and the second item
179 # is the title
180 foreach $descriptor (keys (%$list)) {
181 $self->{'descriptorlist'}->{$descriptor} = $list->{$descriptor}->[0];
182 unless (defined $self->{'locatorlist'}->{$list->{$descriptor}->[0]}) {
183 $self->{'locatorlist'}->{$list->{$descriptor}->[0]}->{'title'} = $list->{$descriptor}->[1];
184 $self->{'locatorlist'}->{$list->{$descriptor}->[0]}->{'contents'} = [];
185 }
186 }
187 }
188}
189
190sub hfile_classify
191{
192 my $self = shift (@_);
193 my ($doc_obj,$sortmeta,$metavalues) = @_;
194
195 my $doc_OID = $doc_obj->get_OID();
196
197 foreach my $metaelement (@$metavalues) {
198 if ((defined $self->{'descriptorlist'}->{$metaelement}) &&
199 (defined $self->{'locatorlist'}->{$self->{'descriptorlist'}->{$metaelement}})) {
200 push (@{$self->{'locatorlist'}->{$self->{'descriptorlist'}->{$metaelement}}->{'contents'}},
201 [$doc_OID, $sortmeta]);
202 my $localid = $self->{'descriptorlist'}->{$metaelement};
203 my $classid = $self->get_number();
204
205 $doc_obj->add_metadata($doc_obj->get_top_section(), "memberof", "CL$classid.$localid");
206 }
207 }
208}
209
210
211
212
213sub hfile_get_classify_info {
214 my $self = shift (@_);
215
216 my ($classifyinfo) = @_;
217
218 my $list = $self->{'locatorlist'};
219
220 # sorted the keys - otherwise funny things happen - kjdon 03/01/03
221 foreach $OID (sort keys (%$list)) {
222 my $tempinfo = $self->get_OID_entry ($OID, $classifyinfo, $list->{$OID}->{'title'}, "VList");
223
224 if (defined $self->{'sort'}) {
225 if ($self->{'reverse_sort'}) {
226 foreach $subOID (sort {$b->[1] cmp $a->[1];} @{$list->{$OID}->{'contents'}}) {
227 push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID->[0]});
228 }
229 }
230 else {
231 foreach $subOID (sort {$a->[1] cmp $b->[1];} @{$list->{$OID}->{'contents'}}) {
232 push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID->[0]});
233 }
234 }
235 }
236 else {
237 foreach $subOID (@{$list->{$OID}->{'contents'}}) {
238 push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID->[0]});
239 }
240 }
241 }
242
243 return $classifyinfo;
244}
245
246
247sub supports_memberof {
248 my $self = shift(@_);
249
250 return "true";
251}
252
253sub get_OID_entry {
254 my $self = shift (@_);
255 my ($OID, $classifyinfo, $title, $classifytype) = @_;
256
257 $OID = "" unless defined $OID;
258 $OID =~ s/^\.+//;
259
260 my ($headOID, $tailOID) = $OID =~ /^(\d+)(.*)$/;
261 $tailOID = "" unless defined $tailOID;
262
263 if (!defined $headOID) {
264 $classifyinfo->{'Title'} = $title;
265 $classifyinfo->{'classifytype'} = $classifytype;
266 return $classifyinfo;
267 }
268
269 $classifyinfo->{'contains'} = [] unless defined $classifyinfo->{'contains'};
270 my $offset = 0;
271 foreach my $thing (@{$classifyinfo->{'contains'}}) {
272 $offset ++ if defined $thing->{'OID'};
273 }
274
275 while (scalar(@{$classifyinfo->{'contains'}}) < ($headOID+$offset)) {
276 push (@{$classifyinfo->{'contains'}}, $self->get_entry("", $classifytype));
277 }
278
279 return $self->get_OID_entry ($tailOID, $classifyinfo->{'contains'}->[($headOID+$offset-1)], $title, $classifytype);
280}
281
282sub get_entry {
283 my $self = shift (@_);
284 my ($title, $childtype, $thistype) = @_;
285 my $memberof = &supports_memberof();
286
287 # organise into classification structure
288 my %classifyinfo = ('childtype'=>$childtype,
289 'Title'=>$title,
290 'supportsmemberof'=>$memberof,
291 'contains'=>[]);
292 $classifyinfo{'thistype'} = $thistype
293 if defined $thistype && $thistype =~ /\w/;
294
295 return \%classifyinfo;
296}
297
298
2991;
Note: See TracBrowser for help on using the repository browser.