source: main/trunk/greenstone2/perllib/collConfigxml.pm@ 24460

Last change on this file since 24460 was 24055, checked in by sjm84, 13 years ago

Making the use of <store_metadata_coverage value="true"/> possible in collectionConfig.xml files

  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
1###########################################################################
2#
3# collConfigxml.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# reads in configuration files of xml form
27
28package collConfigxml;
29use strict;
30no strict 'refs';
31no strict 'subs';
32
33use XMLParser;
34
35# A mapping hash to resolve name discrepancy between gs2 and gs3.
36my $nameMap = {"key" => "value",
37 "creator" => "creator",
38 "maintainer" => "maintainer",
39 "public" => "public",
40 "infodb" => "infodbtype",
41 "defaultIndex" => "defaultindex",
42 "defaultLevel" => "defaultlevel",
43 "name" => "collectionname",
44 "description" => "collectionextra",
45 "smallicon" => "iconcollectionsmall",
46 "icon" => "iconcollection",
47 "level" => "levels",
48 "classifier" => "classify",
49 "indexSubcollection" => "indexsubcollections",
50 "indexLanguage" => "languages",
51 "defaultIndexLanguage" => "defaultlanguage",
52 "index" => "indexes",
53 "plugin" => "plugin",
54 "plugout" => "plugout",
55 "indexOption" => "indexoptions",
56 "searchType" => "searchtype",
57 "languageMetadata" => "languagemetadata",
58 "buildType" => "buildtype",
59 "buildOption" => "buildOption"
60 };
61# A hash structure which is returned by sub read_cfg_file.
62my $data = {};
63
64my $repeatedBlock = q/^(browse|pluginList)$/; #|buildOptionList)$/;
65
66# use those unique attribute values to locate the text within the elements
67# creator, public, maintainer and within a displayItem.
68my $currentLocation = "";
69my $stringexp = q/^(creator|maintainer|public|buildType)$/;
70my $displayItemNames = q/^(name|description)$/;
71
72# For storing the attributes during the StartTag subroutine, so that
73# we can use it later in Text (or EndTag) subroutines
74my $currentAttrRef = undef;
75
76my $currentLevel = "";
77
78# Count the elements with same name within the same block
79# ("plugin", "option")
80my $currentIndex = 0;
81my $arrayexp = q/^(index|level|indexSubcollection|indexLanguage)$/;
82my $arrayarrayexp= q/^(plugin|classifier)$/; #|buildOption)$/;
83my $hashexp = q/^(subcollection)$/; # add other element names that should be represented by hash expressions here
84my $hashhashexp = q/^(displayItem)$/; # add other (collectionmeta) element names that should be represented by hashes of hashes here.
85
86my $defaults = q/^(defaultIndex|defaultLevel|defaultIndexLanguage|languageMetadata)$/;
87
88# Reads in the model collection configuration file, collectionConfig.xml,
89# into a structure which complies with the one used by gs2 (i.e. one read
90# in by &cfgread::read_cfg_file).
91sub read_cfg_file {
92 my ($filename) = @_;
93 $data = {};
94 if ($filename !~ /collectionConfig\.xml$/ || !-f $filename) {
95 return undef;
96 }
97
98 # Removed ProtocolEncoding (see MetadataXMLPlugin for details)
99
100 # create XML::Parser object for parsing metadata.xml files
101 my $parser = new XML::Parser('Style' => 'Stream',
102 'Pkg' => 'collConfigxml',
103 'Handlers' => {'Char' => \&Char,
104 'Doctype' => \&Doctype
105 });
106 if (!open (COLCFG, $filename)) {
107 print STDERR "cfgread::read_cfg_file couldn't read the cfg file $filename\n";
108 } else {
109
110 $parser->parsefile ($filename);# (COLCFG);
111 close (COLCFG);
112 }
113
114 #&Display;
115 return $data;
116}
117
118sub StartTag {
119# Those marked with #@ will not be executed at the same time when this sub is being called
120# so that if/elsif is used to avoid unnecessary tests
121 my ($expat, $element) = @_;
122
123 # See http://search.cpan.org/~msergeant/XML-Parser-2.36/Parser.pm#Stream
124 # %_ is a hash of all the attributes of this element, we want to store them so we can use the attributes
125 # when the textnode contents of the element are parsed in the subroutine Text (that's the handler for Text).
126 $currentAttrRef = \%_;
127
128 my $name = $_{'name'};
129 my $value = $_{'value'};
130 my $type = $_{'type'};
131
132 # for subcollections
133 my $filter = $_{'filter'};
134
135 # was this just a flax thing??
136 my $assigned = $_{'assigned'};
137
138 #@ Marking repeated block
139 if ($element =~ /$repeatedBlock/) {
140 $currentIndex = 0;
141 }
142
143 #@ handling block metadataList
144 elsif (defined $name and $name =~ /$stringexp/){
145 $currentLocation = $name;
146 }
147 #@ handling default search index/level/indexLanguage and languageMetadata
148 elsif ($element =~ /$defaults/) {
149 if (defined $name and $name =~ /\w/) {
150 $data->{$nameMap->{$element}} = $name;
151 }
152 }
153
154 #@ handling the displayItems name and description (known as collectionname and collectionextra in GS2)
155 elsif($element eq "displayItemList") {
156 $currentLevel = "displayItemList"; # storing the parent if it is displayItemList
157 }
158 elsif($element =~ /$hashhashexp/) { # can expand on this to check for other collectionmeta elements
159 if((!defined $assigned) || (defined $assigned and $assigned =~ /\w/ and $assigned eq "true")) {
160 # either when there is no "assigned" attribute, or when assigned=true (for displayItems):
161 $currentLocation = $name;
162 }
163 }
164
165 #@ Handling database type: gdbm or gdbm-txtgz, later jdbm.
166 elsif ($element eq "infodb") {
167 $data->{'infodbtype'} = $type;
168 }
169
170 #@ Handling indexer: mgpp/mg/lucene; stringexp
171 elsif ($element eq "search") {
172 $data->{'buildtype'} = $type;
173 }
174
175 elsif ($element eq "store_metadata_coverage")
176 {
177 print STDERR "*&*&*&*&*& HERE &*&*&*&*&*&*";
178 $data->{'store_metadata_coverage'} = $value;
179 }
180
181 #@ Handling searchtype: plain,form; arrayexp
182 #elsif ($element eq "format" and defined $name and $name =~ /searchType/) {
183 #@ Handling searchtype: plain, form
184 #$currentLocation = $name;
185 #}
186
187 #@ Handle index|level|indexSubcollection|indexLanguage
188 elsif ($element =~ /$arrayexp/) {
189 my $key = $nameMap->{$element};
190 if (!defined $data->{$key}) {
191 $data->{$key} = [];
192 }
193
194 push (@{$data->{$key}},$name);
195 }
196
197 #*****************************************
198 elsif ($element eq "buildOption") {
199 print STDERR "**** BUILD OPTION PAIR $name $value\n";
200 $data->{$name} = $value;
201 }
202
203
204 #@ indexoptions: accentfold/casefold/stem; arrayexp
205 elsif ($element eq "indexOption") {
206 $currentLevel = "indexOption";
207 }
208 if ($currentLevel eq "indexOption" and $element eq "option") {
209 my $key = $nameMap->{$currentLevel};
210 if (!defined $data->{$key}) {
211 $data->{$key} = [];
212 }
213 push (@{$data->{$key}},$name);
214 }
215 #@ plugout options
216 elsif ($element eq "plugout") {
217 $currentLevel = "plugout";
218 my $key = $nameMap->{$currentLevel};
219 if (!defined $data->{$key}) {
220 $data->{$key} = [];
221 }
222 if(defined $name and $name ne ""){
223 push (@{$data->{$key}},$name);
224 }
225 else{
226 push (@{$data->{$key}},"GreenstoneXMLPlugout");
227 }
228 }
229 if ($currentLevel eq "plugout" and $element eq "option") {
230 my $key = $nameMap->{$currentLevel};
231 if (defined $name and $name ne ""){
232 push (@{$data->{$key}},$name);
233 }
234 if (defined $value and $value ne ""){
235 push (@{$data->{$key}},$value);
236 }
237 }
238
239 #@ use hash of hash of strings: hashexp
240 elsif ($element =~ /$hashexp/) {
241 if (!defined $data->{$element}) {
242 $data->{$element} = {};
243 }
244 if (defined $name and $name =~ /\w/) {
245 if (defined $filter and $filter =~ /\w/) {
246 $data->{$element}->{$name} = $filter;
247
248 }
249 }
250 }
251
252 #@ Handling each classifier/plugin element
253 elsif ($element =~ /$arrayarrayexp/) {
254 # find the gs2 mapping name
255 $currentLevel = $element;
256 my $key = $nameMap->{$element};
257
258 # define an array of array of strings foreach $k (@{$data->{$key}}) {
259 if (!defined $data->{$key}) {
260 $data->{$key} = [];
261 }
262 # Push classifier/plugin name (e.g. AZList) into $data as the first string
263 push (@{$data->{$key}->[$currentIndex]},$name);
264 if (defined $value and $value =~ /\w/) {
265 push (@{$data->{$key}->[$currentIndex]}, $value);
266 print "$value\n";
267 }
268 #print $currentIndex."indexup\n";
269 }
270
271 #@ Handling the option elements in each classifier/plugin element (as the following strings)
272 elsif ($currentLevel =~ /$arrayarrayexp/ and $element eq "option") {
273 # find the gs2 mapping name for classifier and plugin
274 my $key = $nameMap->{$currentLevel};
275
276 if (defined $name and $name =~ /\w/) {
277 push (@{$data->{$key}->[$currentIndex]}, $name);
278 }
279 if (defined $value and $value =~ /\w/) {
280 push (@{$data->{$key}->[$currentIndex]}, $value);
281 }
282
283 }
284
285}
286
287sub EndTag {
288 my ($expat, $element) = @_;
289 my $endTags = q/^(browse|pluginList|displayItemList)$/; #|buildOptionList)$/;
290 if ($element =~ /$endTags/) {
291 $currentIndex = 0;
292 $currentLevel = "";
293 }
294 # $arrayarrayexp contains classifier|plugin
295 elsif($element =~ /$arrayarrayexp/ ){
296 $currentIndex = $currentIndex + 1;
297 }
298}
299
300sub Text {
301 if (defined $currentLocation) {
302 #@ Handling block metadataList(creator, maintainer, public)
303 if($currentLocation =~ /$stringexp/){
304 #print $currentLocation;
305 my $key = $nameMap->{$currentLocation};
306 $data->{$key} = $_;
307 undef $currentLocation;
308 }
309
310 #@ Handling displayItem metadata that are children of displayItemList
311 # that means we will be getting the collection's name and possibly description ('collectionextra' in GS2).
312 elsif($currentLevel eq "displayItemList" && $currentLocation =~ /$displayItemNames/) {
313 my $lang = $currentAttrRef->{'lang'};
314 my $name = $currentAttrRef->{'name'};
315
316 # this is how data->collectionmeta's language is set in Greenstone 2.
317 # Need to be consistent, since export.pl accesses these values all in the same way
318 if(!defined $lang) {
319 $lang = 'default';
320 } else {
321 $lang = "[l=$lang]";
322 }
323
324 if(defined $name and $name =~ /$displayItemNames/) { # attribute name = 'name' || 'description'
325 # using $nameMap->$name resolves to 'collectionname' if $name='name' and 'collectionextra' if $name='description'
326 $data->{'collectionmeta'}->{$nameMap->{$name}}->{$lang} = $_; # the value is the Text parsed
327 #print STDERR "***Found: $nameMap->{$name} collectionmeta, lang is $lang. Value: $data->{'collectionmeta'}->{$nameMap->{$name}}->{$lang}\n";
328 }
329 undef $currentLocation;
330 }
331
332 #@ Handling searchtype: plain,form; arrayexp
333 elsif (defined $currentLocation and $currentLocation =~ /searchType/) {
334 # map 'searchType' into 'searchtype'
335 my $key = $nameMap->{$currentLocation};
336 # split it by ','
337 my ($plain, $form) = split (",", $_);
338
339 if (!defined $data->{$key}) {
340 $data->{$key} = [];
341 }
342 if (defined $plain and $plain =~ /\w/) {
343 push @{ $data->{$key} }, $plain;
344 }
345 if (defined $form and $form =~ /\w/) {
346 push @{ $data->{$key} }, $form;
347 }
348 }
349 }
350}
351
352# This sub is for debugging purposes
353sub Display {
354 # metadataList
355 foreach my $k (keys %{$data}) {
356 print STDERR "*** metadatalist key $k\n";
357 }
358
359 print STDERR "*** creator: ".$data->{'creator'}."\n" if (defined $data->{'creator'});
360 print STDERR "*** maintainer: ".$data->{"maintainer"}."\n" if (defined $data->{"maintainer"});
361 print STDERR "*** public: ".$data->{"public"}."\n" if (defined $data->{"public"});
362 print STDERR "*** default index: ".$data->{"defaultindex"}."\n" if (defined $data->{"defaultindex"});
363 print STDERR "*** default level: ".$data->{"defaultlevel"}."\n" if (defined $data->{"defaultlevel"});
364 print STDERR "*** build type: ".$data->{"buildtype"}."\n" if (defined $data->{"buildtype"});
365 print STDERR "*** search types: \n";
366 print STDERR join(",",@{$data->{"searchtype"}})."\n" if (defined $data->{"searchtype"});
367 print STDERR "*** levels: \n";
368 print STDERR join(",",@{$data->{'levels'}})."\n" if (defined $data->{'levels'});
369 print STDERR "*** index subcollections: \n";
370 print STDERR join(",",@{$data->{'indexsubcollections'}})."\n" if (defined $data->{'indexsubcollections'});
371 print STDERR "*** indexes: \n";
372 print STDERR join(",",@{$data->{'indexes'}})."\n" if (defined $data->{'indexes'});
373 print STDERR "*** index options: \n";
374 print STDERR join(",",@{$data->{'indexoptions'}})."\n" if (defined $data->{'indexoptions'});
375 print STDERR "*** languages: \n";
376 print STDERR join(",",@{$data->{'languages'}})."\n" if (defined $data->{'languages'});
377 print STDERR "*** language metadata: \n";
378 print STDERR join(",",@{$data->{'languagemetadata'}})."\n" if (defined $data->{'languagemetadata'});
379
380 print STDERR "*** Plugins: \n";
381 if (defined $data->{'plugin'}) {
382 foreach $a (@{$data->{'plugin'}}) {
383 print join(",",@$a);
384 print "\n";
385 }
386 }
387
388 #print STDERR "*** Build options: \n";
389 #if (defined $data->{'store_metadata_coverage'}) {
390 #foreach $a (@{$data->{'store_metadata_coverage'}}) {
391 # print join(",",@$a,@$_);
392 # print "\n";
393 #}
394 #}
395
396 if (defined $data->{'classify'}) {
397 print STDERR "*** Classifiers: \n";
398 map { print join(",",@$_)."\n"; } @{$data->{'classify'}};
399 }
400
401 if (defined $data->{'subcollection'}) {
402 foreach my $key (keys %{$data->{'subcollection'}}) {
403 print "subcollection ".$key." ".$data->{'subcollection'}->{$key}."\n";
404 }
405 }
406}
407# is this actually used??
408sub Doctype {
409 my ($expat, $name, $sysid, $pubid, $internal) = @_;
410
411 die if ($name !~ /^CollectionConfig$/);
412}
413
414# This Char function overrides the one in XML::Parser::Stream to overcome a
415# problem where $expat->{Text} is treated as the return value, slowing
416# things down significantly in some cases.
417sub Char {
418 if ($]<5.008) {
419 use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+ and Perl 5.6
420 }
421 $_[0]->{'Text'} .= $_[1];
422 return undef;
423}
424
425
426
427
428#########################################################
429
4301;
Note: See TracBrowser for help on using the repository browser.