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

Last change on this file since 27803 was 27803, checked in by kjdon, 11 years ago

code for sort and facet lists

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