source: trunk/gsdl3/bin/script/convert_coll_from_gs2.pl@ 13917

Last change on this file since 13917 was 13917, checked in by kjdon, 14 years ago

levels now go into collectionconfig as well as build config, have displayItems, and names and shortnames likeindexes

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 22.2 KB
Line 
1#!/usr/bin/perl -w
2
3BEGIN {
4 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
5 die "GSDL3HOME not set\n" unless defined $ENV{'GSDL3HOME'};
6 die "GSDL3SRCHOME not set\n" unless defined $ENV{'GSDL3SRCHOME'};
7 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
8 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
9 unshift (@INC, "$ENV{'GSDL3SRCHOME'}/lib/perl/cpan");
10}
11
12use colcfg;
13use util;
14use parsargv;
15use FileHandle;
16use XML::Writer;
17#can't get this to work on windows
18#use GDBM_File;
19
20use strict;
21
22&main();
23sub print_usage() {
24 print STDOUT "Usage: convert_coll_from_gs2.pl [options] coll-name\n";
25 print STDOUT "options:\n";
26
27 print STDOUT " -collectdir Directory where collection lives.\n";
28 print STDOUT " -verbosity Controls the amount of output.\n";
29 print STDOUT " -defaultlang The language that is considered the default (for display text etc). defaults to 'en'\n\n";
30}
31
32
33sub main {
34
35 my ($defaultlang, $verbosity, $collectdir);
36 # note that no defaults are passed for most options as they're set
37 # later (after we check the collect.cfg file)
38 if (!&parsargv::parse(\@ARGV,
39 'verbosity/\d+/', \$verbosity,
40 'collectdir/.*/', \$collectdir,
41 'defaultlang/.*/', \$defaultlang)) {
42 &print_usage();
43 die "\n";
44 }
45
46 # get and check the collection name
47 my ($collection) = @ARGV;
48 if (!defined($collection) || $collection eq "") {
49 die "No collection specified\n";
50 }
51 if ($collection eq "gs2model") {
52 die "You cant convert the model collection\n";
53 }
54
55 if (!defined $collectdir || $collectdir eq "") {
56 $collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect");
57 }
58
59 if (!defined $defaultlang || $defaultlang eq "") {
60 $defaultlang = 'en';
61 }
62 # add on the coll name
63 $collectdir = &util::filename_cat ($collectdir, $collection);
64
65 my $collconfigfilename = &util::filename_cat ($collectdir, "etc", "collect.cfg");
66 print STDOUT "coll config=$collconfigfilename\n";
67 my $collectcfg;
68 if (-e $collconfigfilename) {
69 $collectcfg = &colcfg::read_collect_cfg ($collconfigfilename);
70
71 } else {
72 print STDERR "collect.cfg not found!!";
73 die "\n";
74 }
75
76
77 my $buildconfigfilename = &util::filename_cat ($collectdir, "index", "build.cfg");
78 my $buildcfg;
79 if (-e $buildconfigfilename) {
80 $buildcfg = &colcfg::read_build_cfg ($buildconfigfilename);
81
82 } else {
83 print STDERR "build.cfg not found!!";
84 die "\n";
85 }
86
87
88
89 my $colloutfile = &util::filename_cat ($collectdir, "etc", "collectionConfig.xml");
90 if (-e $colloutfile) {
91 print STDOUT "collectionConfig file already exists! overwriting it!\n";
92
93 }
94
95 my $buildoutfile = &util::filename_cat ($collectdir, "index", "buildConfig.xml");
96 if (-e $buildoutfile) {
97 print STDOUT "buildConfig file already exists! overwriting it!\n";
98
99 }
100
101# my $db_file = &util::filename_cat ($collectdir, "index", "text", "$collection.ldb");
102 my $database;
103# if (-e $db_file) {
104# $database = &open_database($db_file);
105# } else {
106# print STDERR "gdbm database file $db_file not found!!";
107# die "\n";
108# }
109
110 my $buildtype;
111 if (defined $buildcfg->{'buildtype'}) {
112 $buildtype = $buildcfg->{'buildtype'};
113 } else {
114 $buildtype = 'mg';
115 }
116
117 my $indexstem = undef;
118 if (defined $buildcfg->{'indexstem'}) {
119 $indexstem = $buildcfg->{'indexstem'};
120 }
121
122 my $buildoutput = new IO::File(">$buildoutfile");
123 my $buildwriter = new XML::Writer(OUTPUT => $buildoutput, NEWLINES => 1);
124
125 $buildwriter->startTag('buildConfig', 'xmlns:gsf'=>"http://www.greenstone.org/greenstone3/schema/ConfigFormat");
126
127 my $colloutput = new IO::File(">$colloutfile");
128 my $collwriter = new XML::Writer(OUTPUT => $colloutput, NEWLINES => 1);
129
130 $collwriter->startTag('collectionConfig', 'xmlns:gsf'=>"http://www.greenstone.org/greenstone3/schema/ConfigFormat", 'xmlns:xsl'=>'http://www.w3.org/1999/XSL/Transform');
131
132 #output the collection metadata to the collectionConfig file
133 $collwriter->startTag('metadataList');
134
135 my $creator = $collectcfg->{'creator'};
136 &output_metadata($collwriter, 'creator', $creator);
137 my $public =$collectcfg->{'public'};
138 &output_metadata($collwriter, 'public', $public);
139 $collwriter->endTag('metadataList');
140
141 #output the display collectionmeta to collectionConfig.xml
142
143 my $collectionmeta = $collectcfg->{'collectionmeta'};
144 if (defined $collectionmeta) {
145 my %name_map = ('collectionname', 'name',
146 'collectionextra', 'description',
147 'iconcollection', 'icon',
148 'iconcollectionsmall', 'smallicon');
149
150 $collwriter->startTag('displayItemList');
151 foreach my $entry ( keys %$collectionmeta) {
152 if (defined $name_map{$entry}) {
153 my $name= $name_map{$entry};
154 foreach my $lang (keys %{$collectionmeta->{$entry}}) {
155 my $value = $collectionmeta->{$entry}->{$lang};
156 if ($entry =~ /^icon/) {
157 $value = format_icon_value($value);
158 } else {
159 $value = tidy_up_display_item($value);
160 }
161 &output_display($collwriter, $name, $lang, $value);
162 }
163 }
164 }
165 $collwriter->endTag('displayItemList');
166 }
167
168 # output building metadata to build config file
169 my $numdocs = $buildcfg->{'numdocs'};
170 $buildwriter->startTag('metadataList');
171 &output_metadata($buildwriter, 'numDocs', $numdocs);
172 &output_metadata($buildwriter, 'buildType', $buildtype);
173 $buildwriter->endTag('metadataList');
174
175
176 #indexes
177 # maps index name to shortname
178 my $indexmap = {};
179 # keeps the order for indexes
180 my @indexlist = ();
181 my $defaultindex = "";
182 my $first = 1;
183 my $maptype = "indexfieldmap";
184 if ($buildtype eq "mg") {
185 $maptype = "indexmap";
186 }
187 if (defined $buildcfg->{$maptype}) {
188 my $indexmap_t = $buildcfg->{$maptype};
189 foreach my $i (@$indexmap_t) {
190 my ($k, $v) = $i =~ /^(.*)\-\>(.*)$/;
191 $indexmap->{$k} = $v;
192 push @indexlist, $k;
193 if ($first) {
194 $defaultindex = $v;
195 $first = 0;
196 }
197 }
198 } else {
199 print STDERR "$maptype not defined";
200 }
201 # we use the shortname for default index
202 if (defined $collectcfg->{'defaultindex'}) {
203 $defaultindex = $collectcfg->{'defaultindex'};
204 $defaultindex = $indexmap->{$defaultindex};
205 }
206
207 # levels
208 my $levelmap = {};
209 my @levellist = ();
210 my $default_search_level = "";
211 my $default_retrieve_level = "Sec";
212 $first = 1;
213 if ($buildtype eq "mgpp" || $buildtype eq "lucene") {
214 if (defined $buildcfg->{'levelmap'}) {
215 my $levelmap_t = $buildcfg->{'levelmap'};
216 foreach my $l (@$levelmap_t) {
217 my ($k, $v) = $l =~ /^(.*)\-\>(.*)$/;
218 $levelmap->{$k} = $v;
219 push @levellist, $k;
220 if ($first) {
221 $default_search_level = $v;
222 $first = 0;
223 }
224 }
225 }
226
227 if (defined $collectcfg->{'defaultlevel'}) {
228 $default_search_level = $collectcfg->{'defaultlevel'};
229 $default_search_level = $levelmap->{$default_search_level};
230 }
231 if (defined $buildcfg->{'textlevel'}) {
232 $default_retrieve_level = $buildcfg->{'textlevel'};
233 }
234 }
235 # format stuff
236 my $format = $collectcfg->{'format'};
237
238 #output the search stuff to coll cfg
239 $collwriter->startTag('search');
240 foreach my $i (keys %$indexmap) {
241 $collwriter->startTag('index', 'name'=>$i);
242 #find the coll meta stuff
243 my $indexdisplay = ".$i";
244 foreach my $lang (keys %{$collectionmeta->{$indexdisplay}}) {
245 my $value = $collectionmeta->{$indexdisplay}->{$lang};
246 output_display($collwriter, 'name', $lang, $value);
247 }
248 $collwriter->endTag('index');
249 }
250
251 # level stuff for mgpp/lucene
252 if ($buildtype eq 'mgpp' || $buildtype eq 'lucene'){
253 foreach my $l (keys %$levelmap) {
254 $collwriter->startTag('level', 'name'=>$l);
255 #find the coll meta stuff
256 my $leveldisplay = ".$l";
257 foreach my $lang (keys %{$collectionmeta->{$leveldisplay}}) {
258 my $value = $collectionmeta->{$leveldisplay}->{$lang};
259 output_display($collwriter, 'name', $lang, $value);
260 }
261 $collwriter->endTag('level');
262 }
263 }
264 # add in the format stuff
265 if (defined $format->{'SearchVList'}) {
266
267 $collwriter->startTag('format');
268 write_format($collwriter, $format->{'SearchVList'}, "document");
269 $collwriter->endTag('format');
270 }
271 elsif (defined $format->{'VList'}) {
272 $collwriter->startTag('format');
273 write_format($collwriter, $format->{'VList'}, "document");
274 $collwriter->endTag('format');
275 }
276
277 $collwriter->endTag('search');
278
279 $buildwriter->startTag('serviceRackList');
280
281 my @levels = ();
282 my $defaultlevel;
283 my $service_type = "MG";
284 if ($buildtype eq 'mgpp') {
285 $service_type = "MGPP";
286 } elsif ($buildtype eq "lucene") {
287 $service_type = "Lucene";
288 }
289
290 #do the retrieve service
291 $buildwriter->startTag('serviceRack', 'name'=>"GS2".$service_type."Retrieve");
292 if ($buildtype eq 'mgpp' || $buildtype eq 'lucene') {
293 $buildwriter->emptyTag('defaultLevel', 'shortname'=>$default_retrieve_level);
294 } elsif ($buildtype eq "mg") {
295 $buildwriter->emptyTag('defaultIndex', 'shortname'=>$defaultindex);
296
297 }
298 if (defined $indexstem) {
299 $buildwriter->emptyTag('indexStem', 'name'=>$indexstem);
300 }
301 # close off the Retrieve service
302 $buildwriter->endTag('serviceRack');
303
304 # add in the classifiers if needed
305
306 my $count = 1;
307 my $phind = 0;
308 my $started_classifiers = 0;
309 if (defined $collectcfg->{'classify'}) {
310 $collwriter->startTag('browse');
311 # add in default format if necessary
312 if (defined $format->{"VList"} || defined $format->{"HList"}) {
313 # global formats
314 $collwriter->startTag('format');
315 if (defined $format->{"VList"}) {
316 # VLIst applies to both classifier and doc nodes
317 write_format($collwriter, $format->{"VList"}, "document");
318 write_format($collwriter, $format->{"VList"}, "classifier");
319 }
320 if (defined $format->{"HList"}) {
321 # hlist is only for classifier nodes
322 write_format($collwriter, $format->{"HList"}, "horizontal");
323 }
324 $collwriter->endTag('format');
325 }
326 my $classifiers = $collectcfg->{'classify'};
327 foreach my $cl (@$classifiers) {
328 my $name = "CL$count";
329 $count++;
330 my ($classname) = @$cl[0];
331 if ($classname =~ /^phind$/i) {
332 $phind=1;
333 #should add it into coll config classifiers
334 next;
335 }
336
337 my $horizontalAtTop = &isHorizontalClassifier($database, $name);
338 if (not $started_classifiers) {
339 $buildwriter->startTag('serviceRack', 'name'=>'GS2Browse');
340 if (defined $indexstem) {
341 $buildwriter->emptyTag('indexStem', 'name'=>$indexstem);
342 }
343
344 $buildwriter->startTag('classifierList');
345 $started_classifiers = 1;
346 }
347 my $content = ''; #use buttonname first, then metadata
348 if ($classname eq "DateList") {
349 $content = "Date";
350 } else {
351 for (my $i=0; $i<scalar(@$cl); $i++) {
352 my $arg = @$cl[$i];
353 if ($arg eq "-buttonname"){
354 $content = @$cl[$i+1];
355 last;
356 } elsif ($arg eq "-metadata") {
357 $content = @$cl[$i+1];
358 }
359
360 }
361 }
362 if ($horizontalAtTop) {
363 $buildwriter->emptyTag('classifier', 'name'=>$name, 'content'=>$content, 'horizontalAtTop'=>'true');
364 } else {
365 $buildwriter->emptyTag('classifier', 'name'=>$name, 'content'=>$content);
366 }
367
368
369 $collwriter->startTag('classifier', 'name'=>$name);
370 my $vlist = $name."VList";
371 my $hlist = $name."HList";
372 my $dlist = "";
373 if ($classname eq "DateList") {
374 $dlist = "DateList";
375 }
376 # need to work out how to split into classifier and document
377 if (defined $format->{$vlist} || defined $format->{$hlist} || defined $format->{$dlist}) {
378 $collwriter->startTag('format');
379 if (defined $format->{$vlist}) {
380 write_format($collwriter, $format->{$vlist}, "document");
381 write_format($collwriter, $format->{$vlist}, "classifier");
382 }
383 if (defined $format->{$hlist}) {
384 write_format($collwriter, $format->{$hlist}, "horizontal");
385 }
386
387 if (defined $format->{$dlist}) {
388 write_format($collwriter, $format->{$dlist}, "document");
389 }
390 $collwriter->endTag('format');
391 }
392 $collwriter->endTag('classifier');
393 } #foreach classifier
394 if ($started_classifiers) {
395 # end the classifiers
396 $buildwriter->endTag('classifierList');
397 # close off the Browse service
398 $buildwriter->endTag('serviceRack');
399 }
400
401 $collwriter->endTag('browse');
402 }
403
404
405 # the phind classifier is a separate service
406 if ($phind) {
407 # if phind classifier
408 $buildwriter->emptyTag('serviceRack', 'name'=>'PhindPhraseBrowse');
409 }
410
411 # do the search service
412 $buildwriter->startTag('serviceRack', 'name'=>'GS2'.$service_type.'Search');
413 $buildwriter->emptyTag('defaultIndex', 'shortname'=>$defaultindex);
414 $buildwriter->startTag('indexList');
415 #for each index
416 foreach my $i (@indexlist) {
417 my $index = $indexmap->{$i};
418 $buildwriter->emptyTag('index', 'name'=>$i, 'shortname'=>$index);
419 }
420 $buildwriter->endTag('indexList');
421 if (defined $indexstem) {
422 $buildwriter->emptyTag('indexStem', 'name'=>$indexstem);
423 }
424
425 # index options
426 if ($buildtype eq 'mg' || $buildtype eq 'mgpp') {
427 $buildwriter->startTag('indexOptionList');
428 my $stemindexes = 3; # default is stem and casefold
429 if (defined $buildcfg->{'stemindexes'} && $buildcfg->{'stemindexes'} =~ /^\d+$/ ) {
430 $stemindexes = $buildcfg->{'stemindexes'};
431 }
432 $buildwriter->emptyTag('indexOption', 'name'=>'stemIndexes', 'value'=>$stemindexes);
433
434 my $maxnumeric = 4; # default
435 if (defined $buildcfg->{'maxnumeric'} && $buildcfg->{'maxnumeric'} =~ /^\d+$/) {
436 $maxnumeric = $buildcfg->{'maxnumeric'};
437 }
438 $buildwriter->emptyTag('indexOption', 'name'=>'maxnumeric', 'value'=>$maxnumeric);
439
440 $buildwriter->endTag('indexOptionList');
441 }
442
443 if ($buildtype eq 'mgpp' || $buildtype eq 'lucene') {
444
445 # level info
446 $buildwriter->emptyTag('defaultLevel', 'shortname'=>$default_search_level);
447 $buildwriter->emptyTag('defaultGDBMLevel', 'shortname'=>$default_retrieve_level);
448 $buildwriter->startTag('levelList');
449 foreach my $l (@levellist) {
450 my $level = $levelmap->{$l};
451 $buildwriter->emptyTag('level', 'name'=>$l, 'shortname'=>$level);
452 }
453 $buildwriter->endTag('levelList');
454
455 # do the search types if there
456 if (defined $collectcfg->{'searchtype'}) {
457 $buildwriter->startTag('searchTypeList');
458 foreach my $st (@{$collectcfg->{'searchtype'}}) {
459 $buildwriter->emptyTag('searchType', 'name'=>$st);
460 }
461 $buildwriter->endTag('searchTypeList');
462 } elsif (defined $format->{'SearchTypes'}) {
463 #check format statement
464 my $searchtype = $format->{'SearchTypes'};
465 $buildwriter->startTag('searchTypeList');
466 if ($searchtype =~ /form/) {
467 $buildwriter->emptyTag('searchType', 'name'=>'form');
468 }
469 if ($searchtype =~ /plain/) {
470 $buildwriter->emptyTag('searchType', 'name'=>'plain');
471 }
472 $buildwriter->endTag('searchTypeList');
473 }
474 }
475
476
477
478 $buildwriter->endTag('serviceRack');
479
480 $buildwriter->endTag('serviceRackList');
481 $buildwriter->endTag('buildConfig');
482
483 # we add in the default replace list just in case we have macros in the
484 # collection
485 $collwriter->emptyTag('replaceListRef', 'id'=>'gs2-standard');
486 $collwriter->endTag('collectionConfig');
487 $collwriter->end();
488 $buildwriter->end();
489 $buildoutput->close();
490 $colloutput->close();
491 &close_database($database);
492}
493
494
495sub output_metadata {
496 my ($writer, $metaname, $metavalue) = @_;
497 $writer->startTag('metadata', 'name'=>$metaname);
498 $writer->characters($metavalue);
499 $writer->endTag('metadata');
500}
501
502sub output_display {
503 my ($writer, $name, $lang, $value) = @_;
504 $lang = 'en' if $lang eq 'default';
505 if ($lang =~ /^\[/) {
506 ($lang) = $lang =~ /\[l=(.*)\]/;
507 }
508 $writer->startTag('displayItem', 'name'=>$name, 'lang'=>$lang);
509 $writer->characters($value);
510 $writer->endTag('displayItem');
511}
512sub format_icon_value {
513 my ($value) = @_;
514 if ($value =~ /^_/) {
515 my ($newvalue) = $value =~ /images\/(.*)$/;
516 if ($newvalue) {
517 return $newvalue;
518 }
519 }
520 return $value;
521}
522
523sub tidy_up_display_item {
524 my ($value) = @_;
525 # remove \n
526 $value =~ s/\\n//g;
527 # replace \' with '
528 $value =~ s/\\\'/\'/g;
529 # replace \" with "
530 $value =~ s/\\\"/\"/g;
531 # replace _httpprefix_ with _httpsite_
532 $value =~ s/_httpprefix_/_httpsite_/g;
533 $value =~ s/_gwcgi_//g;
534 $value =~ s/[a-z][a-z]?=_cgiarg[a-z][a-z]?_&?//g;
535 $value =~ s/&p=/&sa=/g;
536 return $value;
537}
538
539sub write_format {
540 my ($writer, $old_format, $node_type) = @_;
541 # replace \' with '
542 $old_format =~ s/\\\'/\'/g;
543 # replace \" with "
544 $old_format =~ s/\\\"/\"/g;
545 #convert [] to <gsf:...>
546 # assume no nesting {If} or {Or} for now
547 $old_format =~ s/\{If\}\{([^\}]*)\}/&format_if($1, $node_type)/eg;
548 $old_format =~ s/\{Or\}\{([^\}]*)\}/&format_or($1)/eg;
549 $old_format =~ s/\[Text\]/\<gsf:text\/\>/g;
550 $old_format =~ s/\[num\]/\<gsf:num\/\>/g;
551 $old_format =~ s/\[link\]/\<gsf:link type=\'$node_type\'\>/g;
552 $old_format =~ s/\[\/link\]/\<\/gsf:link\>/g;
553 $old_format =~ s/\[srclink\]/\<gsf:link type=\'source\'\>/g;
554 $old_format =~ s/\[\/srclink\]/\<\/gsf:link\>/g;
555 $old_format =~ s/\[icon\]/\<gsf:icon type=\'$node_type\'\/\>/g;
556 $old_format =~ s/\[srcicon\]/\<gsf:icon type=\'source\'\/\>/g;
557
558 # what to do with hightlight??
559 $old_format =~ s/\[\/?highlight\]//g;
560
561 #now do the rest of the [] which are assumed to be metadata
562 $old_format =~ s/\[([^\]]*)\]/&format_metadata($1)/eg;
563
564 # some html tidy
565 #turn <br> into <br />
566 $old_format =~ s/\<br\>/\<br \/\>/g;
567 #turn <p> into <p />
568 $old_format =~ s/\<p\>/\<p \/\>/g;
569
570 #put quotes around any atts
571 $old_format =~ s/=([a-z]+)([> ])/=\'$1\'$2/g;
572
573 if ($node_type eq "document") {
574 $writer->startTag('gsf:template', 'match'=>'documentNode');
575 $writer->charactersXML($old_format);
576 $writer->endTag('gsf:template');
577 } elsif ($node_type eq "classifier") {
578 $writer->startTag('gsf:template', 'match'=>'classifierNode');
579 $writer->charactersXML($old_format);
580 $writer->endTag('gsf:template');
581 } elsif ($node_type eq "horizontal") {
582 $writer->startTag('gsf:template', 'match'=>'classifierNode', 'mode'=>'horizontal');
583 $writer->charactersXML($old_format);
584 $writer->endTag('gsf:template');
585
586 }
587}
588
589sub format_metadata {
590 my ($metadata_string) = @_;
591
592 #print STDERR "original meta = $metadata_string\n";
593
594 # what shall we do with cgisafe??
595 my $cgisafe = $metadata_string =~ s/^cgisafe://;
596
597 my ($select) = $metadata_string =~ /^(parent|sibling)/;
598 $metadata_string =~ s/^(parent|sibling)//;
599 my ($scope, $delim);
600
601 if ($select) {
602 ($scope) = $metadata_string =~ /^\((Top|All)/;
603 $metadata_string =~ s/^\((Top|All)\)?//;
604 if ($scope) {
605 ($delim) = $metadata_string =~ /^\'([^\']*)\'\)/;
606 $metadata_string =~ s/^\'([^\']*)\'\)//;
607 }
608 }
609 $metadata_string =~ s/^://;
610 # remove ex.
611 $metadata_string =~ s/^ex\.//;
612
613 #print STDERR "select=$select, scope=$scope, delim=|$delim|, meta = $metadata_string\n";
614
615 my $new_format = "<gsf:metadata name='$metadata_string' ";
616 if (defined $select) {
617 if ($select eq "sibling") {
618 $new_format .= "multiple='true' ";
619 if (defined $delim) {
620 $new_format .= "separator='$delim' ";
621 }
622 } elsif ($select eq "parent"){
623 if (defined $scope) {
624 if ($scope eq "Top") {
625 $new_format .= "select='root' ";
626 } elsif ($scope eq "All") {
627 $new_format .= "select='ancestors' ";
628 if (defined $delim) {
629 $new_format .= "separator='$delim' ";
630 }
631 }
632 } else {
633 $new_format .= "select='parent' ";
634 }
635 }
636 }
637 $new_format .= "/>";
638 #print STDERR "$new_format\n";
639 return $new_format;
640
641}
642
643sub format_if {
644
645 my ($if_string, $node_type) = @_;
646 #print STDERR "if string = $if_string\n";
647
648 my @parts = split /,/, $if_string;
649 my $test = $parts[0];
650 my $true_option = $parts[1];
651 my $false_option;
652 if (scalar (@parts) == 3) {
653 $false_option = $parts[2];
654 }
655 $test =~ s/^\s*//;
656 $test =~ s/\s*$//;
657 my ($test_meta, $test_type, $test_value);
658 if ($test =~ /^(\[.+\])$/) {
659 $test_meta = $1;
660 $test_type = 'exists';
661 } else {
662 my ($lhs, $exp, $rhs) = $test =~ /^(.+)\s+(eq|ne|lt|gt|le|ge|sw|ew)\s+(.+)$/;
663 #print STDERR "lhs, exp, rhs = $lhs, $exp, $rhs\n";
664 if ($exp eq "eq") {
665 $test_type = "equals";
666 } elsif ($exp eq "sw") {
667 $test_type = "startsWith";
668 } elsif ($exp eq "ew") {
669 $test_type = "endsWith";
670 } elsif ($exp eq "ne") {
671 $test_type = "notEquals";
672 } elsif ($exp eq "lt") {
673 $test_type = "lessThan";
674 }elsif ($exp eq "gt") {
675 $test_type = "greaterThan";
676 }elsif ($exp eq "le") {
677 $test_type = "lessThanOrEquals";
678 }elsif ($exp eq "ge") {
679 $test_type = "greaterThanOrEquals";
680 }
681 if ($lhs =~ /^\[.+\]$/) {
682 $test_meta = $lhs;
683 $test_value = $rhs;
684 } else {
685 # assume rhs has meta
686 $test_meta = $rhs;
687 $test_value = $lhs;
688 }
689
690 #remove beginning and end quotes
691 $test_value =~ s/^[\'\"]//;
692 $test_value =~ s/[\'\"]$//;
693 }
694 my $test_atts = "test='$test_type' ";
695 if (defined $test_value) {
696 $test_atts .= "test-value='$test_value' ";
697 }
698 #print STDERR "test, true, false = $test, $true_option, $false_option\n";
699 my $new_format = "<gsf:switch>$test_meta";
700 $new_format .= "<gsf:when $test_atts>$true_option</gsf:when>";
701 if (defined $false_option) {
702 $new_format .="<gsf:otherwise>$false_option</gsf:otherwise>";
703 }
704 $new_format .= "</gsf:switch>";
705
706 #print STDERR "new format = $new_format\n";
707 return $new_format;
708}
709
710sub format_or {
711 my ($or_string) = @_;
712 my @meta_list = split (',', $or_string);
713 return "" unless scalar (@meta_list);
714 my $new_format = "<gsf:choose-metadata>";
715 foreach my $m (@meta_list) {
716 if ($m =~ /^\[(.*)\]$/) {
717 $new_format .= &format_metadata($1);
718 } else {
719 # a default value
720 $new_format .= "<gsf:default>$m</gsf:default>";
721 last;
722 }
723 }
724 $new_format .= "</gsf:choose-metadata>";
725 return $new_format;
726}
727
728sub open_database {
729 my ($db_file) = @_;
730
731 my $database = ();
732# tie (%$database, 'GDBM_File', $db_file, GDBM_READER, 0400) ||
733# die "Couldn't open database $db_file\n";
734
735 return $database;
736}
737
738sub close_database {
739 my ($database) = @_;
740 untie %$database;
741}
742sub isHorizontalClassifier {
743 my ($database, $name) = @_;
744
745 return 0; # can't get this to work for windows
746 my $record = $database->{$name};
747 my ($childtype) = $record =~ /<childtype>(\w*)/;
748 if ($childtype eq "HList") {
749 return 1;
750 }
751 return 0;
752}
753#$writer->startTag('');
754#$writer->endTag('');
755#$writer->characters();
756#$writer->emptyTag('');
757
7581;
Note: See TracBrowser for help on using the repository browser.