| 1 | # $Id: Config.pm,v 1.40 2009-04-08 12:58:11 mike Exp $ | ||
| 2 | |||
| 3 | package Net::Z3950::DBIServer::Config; | ||
| 4 | use IO::File; | ||
| 5 | use Carp; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Net::Z3950::DBIServer::Config - Configuration file parser for DBIServer | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Net::Z3950::DBIServer::Config; | ||
| 16 | $config = new Net::Z3950::DBIServer::Config($configFile); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | This module parses configuration files for the | ||
| 21 | C<Net::Z3950::DBIServer> module. I can't begin to imagine why anyone | ||
| 22 | who's not doing DBIServer development would be remotely interested in | ||
| 23 | the technical details of how that's done, and those people will be | ||
| 24 | reading the source rather than the documentation, so I don't propose | ||
| 25 | to write much. | ||
| 26 | |||
| 27 | The parser's a simple one-token-lookahead recursive descent parser, | ||
| 28 | similar to the one that's explained in some detail in pretty much the | ||
| 29 | first chapter of Aho, Sethi & Ullman's ``New Dragon Book'', | ||
| 30 | I<Compilers: Principles, Techniques and Tools>. If you want to write | ||
| 31 | parsers of any substance, you I<have> to have this book. It is the | ||
| 32 | absolute bible of compilers. | ||
| 33 | |||
| 34 | It was a pleasure writing the parser in Perl - it took about a tenth | ||
| 35 | as long as similar ones that I've written in C in the past (i.e. a | ||
| 36 | couple of hours) and that includes the lexer which, to my delight, | ||
| 37 | turned out to be thirty (count 'em!) lines of code. | ||
| 38 | |||
| 39 | I'm not going to insult your intelligence by documenting the public | ||
| 40 | methods, since the only one is the constructor, whose synopsis above | ||
| 41 | tells you all you need to know except that the C<$configFile> | ||
| 42 | parameter is a fileI<name>, not a fileI<handle>. The object that it | ||
| 43 | returns is pretty much self-documenting. Print it with | ||
| 44 | C<Data::Dumper> if you don't believe me. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | |||
| 49 | sub STRING { 500 } | ||
| 50 | sub NUMBER { 501 } | ||
| 51 | sub SYMBOL { 502 } | ||
| 52 | sub DATABASE { 503 } | ||
| 53 | sub SEARCH { 504 } | ||
| 54 | sub ACCESS { 505 } | ||
| 55 | sub ATTR { 506 } | ||
| 56 | sub ATTRSET { 507 } | ||
| 57 | sub DATA { 508 } | ||
| 58 | sub COLUMN { 509 } | ||
| 59 | sub FORMAT { 510 } | ||
| 60 | sub GRS1 { 511 } | ||
| 61 | sub XML { 512 } | ||
| 62 | sub RECORD { 513 } | ||
| 63 | sub DATASOURCE { 514 } | ||
| 64 | sub CACHESIZE { 515 } | ||
| 65 | sub TABLE { 516 } | ||
| 66 | sub FIELD { 517 } | ||
| 67 | sub AUXILIARY { 518 } | ||
| 68 | sub WITH { 519 } | ||
| 69 | sub USERNAME { 520 } | ||
| 70 | sub PASSWORD { 521 } | ||
| 71 | sub ATTRS { 522 } | ||
| 72 | sub RESTRICTION { 523 } | ||
| 73 | sub TRANSFORM { 524 } | ||
| 74 | sub MARC { 525 } | ||
| 75 | sub DEFAULTATTRS { 526 } | ||
| 76 | sub FULLTEXT { 527 } | ||
| 77 | sub SUTRS { 528 } | ||
| 78 | sub SCHEMA { 529 } | ||
| 79 | sub JOIN { 530 } | ||
| 80 | sub UPPERCASE { 531 } | ||
| 81 | sub OPTION { 532 } | ||
| 82 | |||
| 83 | |||
| 84 | sub new { | ||
| 85 | my $class = shift(); | ||
| 86 | my($filename) = @_; | ||
| 87 | |||
| 88 | if (!defined $filename) { | ||
| 89 | print STDERR "$class: configuration file not specified\n"; | ||
| 90 | return undef; | ||
| 91 | } | ||
| 92 | |||
| 93 | my @s = stat($filename) or die "can't stat '$filename': $!"; | ||
| 94 | my $mtime = $s[9]; | ||
| 95 | my $fh = new IO::File("<$filename"); | ||
| 96 | if (!defined $fh) { | ||
| 97 | print STDERR "$class: can't open '$filename': $!\n"; | ||
| 98 | return undef; | ||
| 99 | } | ||
| 100 | |||
| 101 | ### Should probably be a class with accessor methods | ||
| 102 | my $state = { class => $class, | ||
| 103 | filename => $filename, | ||
| 104 | fh => $fh, | ||
| 105 | buffer => '', | ||
| 106 | line => 0, | ||
| 107 | token => undef, | ||
| 108 | lexeme => undef }; | ||
| 109 | lex($state); | ||
| 110 | my $config = _parse_config($state); | ||
| 111 | $config->{"*timeStamp"} = $mtime; | ||
| 112 | return $config; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | # PRIVATE to the new() method | ||
| 117 | sub _parse_config { | ||
| 118 | my($state) = @_; | ||
| 119 | |||
| 120 | my $config = []; | ||
| 121 | my($dataSource, $userName, $passWord, %options); | ||
| 122 | |||
| 123 | while (defined $state->{token}) { | ||
| 124 | if ($state->{token} == DATASOURCE) { | ||
| 125 | fatal($state, "multiple dataSources specified") | ||
| 126 | if defined $dataSource; | ||
| 127 | match($state, DATASOURCE); | ||
| 128 | match($state, ord("=")); | ||
| 129 | $dataSource = parse_string($state); | ||
| 130 | } elsif ($state->{token} == USERNAME) { | ||
| 131 | fatal($state, "multiple userNames specified") | ||
| 132 | if defined $userName; | ||
| 133 | match($state, USERNAME); | ||
| 134 | match($state, ord("=")); | ||
| 135 | $userName = parse_string($state) | ||
| 136 | } elsif ($state->{token} == PASSWORD) { | ||
| 137 | fatal($state, "multiple passWords specified") | ||
| 138 | if defined $passWord; | ||
| 139 | match($state, PASSWORD); | ||
| 140 | match($state, ord("=")); | ||
| 141 | $passWord = parse_string($state); | ||
| 142 | } elsif ($state->{token} == OPTION) { | ||
| 143 | match($state, OPTION); | ||
| 144 | my $name = parse_string($state); | ||
| 145 | match($state, ord("=")); | ||
| 146 | my $value; | ||
| 147 | if ($state->{token} == NUMBER) { | ||
| 148 | $value = $state->{lexeme}; | ||
| 149 | match($state, NUMBER); | ||
| 150 | } else { | ||
| 151 | $value = parse_string($state); | ||
| 152 | } | ||
| 153 | $options{$name} = $value; | ||
| 154 | } else { | ||
| 155 | push @$config, parse_perDB($state); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | my $hashref = { map { $_->{dbname}, $_ } @$config }; | ||
| 160 | |||
| 161 | # Treat dataSource, userName and passWord as special database names | ||
| 162 | $hashref->{"*dataSource"} = $dataSource | ||
| 163 | or fatal($state, "no dataSource specified"); | ||
| 164 | $hashref->{"*userName"} = $userName | ||
| 165 | if defined $userName; | ||
| 166 | $hashref->{"*passWord"} = $passWord | ||
| 167 | if defined $passWord; | ||
| 168 | $hashref->{"*options"} = \%options | ||
| 169 | if %options; | ||
| 170 | |||
| 171 | return bless $hashref, $state->{class}; | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | sub parse_perDB { | ||
| 176 | my($state) = @_; | ||
| 177 | |||
| 178 | match($state, DATABASE); | ||
| 179 | my $dbname = parse_string($state); | ||
| 180 | match($state, ord("{")); | ||
| 181 | |||
| 182 | my($tableName, @auxiliary, $restriction, $join, $searchSpec, $dataSpec); | ||
| 183 | while ($state->{token} != ord("}")) { | ||
| 184 | if ($state->{token} == TABLE) { | ||
| 185 | fatal($state, "multiple table specifications") | ||
| 186 | if defined $tableName; | ||
| 187 | match($state, TABLE); | ||
| 188 | match($state, ord("=")); | ||
| 189 | $tableName = parse_string($state); | ||
| 190 | } elsif ($state->{token} == AUXILIARY) { | ||
| 191 | match($state, AUXILIARY); | ||
| 192 | my $linkName = parse_string($state); | ||
| 193 | match($state, WITH); | ||
| 194 | my $cond = parse_string($state); | ||
| 195 | push @auxiliary, bless({ tableName => $linkName, cond => $cond }, | ||
| 196 | $state->{class} . '::AuxSpec'); | ||
| 197 | } elsif ($state->{token} == RESTRICTION) { | ||
| 198 | fatal($state, "multiple restriction specifications") | ||
| 199 | if defined $restriction; | ||
| 200 | match($state, RESTRICTION); | ||
| 201 | match($state, ord("=")); | ||
| 202 | $restriction = parse_string($state); | ||
| 203 | } elsif ($state->{token} == JOIN) { | ||
| 204 | fatal($state, "multiple join specifications") | ||
| 205 | if defined $join; | ||
| 206 | match($state, JOIN); | ||
| 207 | match($state, ord("=")); | ||
| 208 | $join = parse_string($state); | ||
| 209 | fatal($state, "unrecognised join specification '$join'") | ||
| 210 | if !grep { $join eq $_ } qw(implicit outer); | ||
| 211 | } elsif ($state->{token} == SEARCH) { | ||
| 212 | fatal($state, "multiple search sections") if defined $searchSpec; | ||
| 213 | $searchSpec = parse_searchSpec($state); | ||
| 214 | } elsif ($state->{token} == DATA) { | ||
| 215 | fatal($state, "multiple data sections") if defined $dataSpec; | ||
| 216 | $dataSpec = parse_dataSpec($state); | ||
| 217 | } else { | ||
| 218 | # We know this will fail: match(0) always will. | ||
| 219 | match($state, 0, "SEARCH or DATA section"); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | match($state, ord("}")); | ||
| 223 | |||
| 224 | return bless { | ||
| 225 | dbname => $dbname, | ||
| 226 | tablename => $tableName, | ||
| 227 | auxiliary => \@auxiliary, | ||
| 228 | restriction => $restriction, | ||
| 229 | join => $join, | ||
| 230 | searchSpec => $searchSpec, | ||
| 231 | dataSpec => $dataSpec, | ||
| 232 | }, $state->{class} . '::PerDB'; | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | sub parse_searchSpec { | ||
| 237 | my($state) = @_; | ||
| 238 | |||
| 239 | match($state, SEARCH); | ||
| 240 | my $attrsets = []; | ||
| 241 | if ($state->{token} == ord("{")) { | ||
| 242 | # Multiple attribute-set specifications | ||
| 243 | match($state, ord("{")); | ||
| 244 | while ($state->{token} != ord("}")) { | ||
| 245 | push @$attrsets, parse_attrsetSpecOrDefaultAttrs($state); | ||
| 246 | } | ||
| 247 | match($state, ord("}")); | ||
| 248 | } else { | ||
| 249 | # Single attribute-set specification | ||
| 250 | push @$attrsets, parse_attrsetSpecOrDefaultAttrs($state); | ||
| 251 | } | ||
| 252 | |||
| 253 | # Now turn the list of lists into a hash keyed on attrset/type | ||
| 254 | my $hashref; | ||
| 255 | foreach my $attrset (@$attrsets) { | ||
| 256 | if ($attrset->isa($state->{class} . '::DefaultAttrs')) { | ||
| 257 | $hashref->{"*defaultattrs"} = $attrset; | ||
| 258 | next; | ||
| 259 | } | ||
| 260 | my $oid = $attrset->{attrset}; | ||
| 261 | foreach my $ap (@{ $attrset->{specs} }) { | ||
| 262 | $ap->{attrset} = $oid; | ||
| 263 | $hashref->{"$oid:" . $ap->{accesspoint}} = $ap; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | return bless $hashref, $state->{class} . '::SearchSpec'; | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | sub parse_attrsetSpecOrDefaultAttrs { | ||
| 272 | my($state) = @_; | ||
| 273 | |||
| 274 | if ($state->{token} == DEFAULTATTRS) { | ||
| 275 | return parse_defaultAttrs($state); | ||
| 276 | } else { | ||
| 277 | return parse_attrsetSpec($state); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | sub parse_defaultAttrs { | ||
| 283 | my($state) = @_; | ||
| 284 | |||
| 285 | match($state, DEFAULTATTRS); | ||
| 286 | my %attrs; | ||
| 287 | |||
| 288 | while (1) { | ||
| 289 | my $type = $state->{lexeme}; | ||
| 290 | match($state, NUMBER); | ||
| 291 | match($state, ord("=")); | ||
| 292 | my $value = $state->{lexeme}; | ||
| 293 | match($state, NUMBER); | ||
| 294 | $attrs{$type} = $value; | ||
| 295 | last if $state->{token} != ord(","); | ||
| 296 | match($state, ord(",")); | ||
| 297 | } | ||
| 298 | |||
| 299 | return bless \%attrs, $state->{class} . '::DefaultAttrs'; | ||
| 300 | } | ||
| 301 | |||
| 302 | |||
| 303 | sub parse_attrsetSpec { | ||
| 304 | my($state) = @_; | ||
| 305 | |||
| 306 | match($state, ATTRSET); | ||
| 307 | my $attrset; | ||
| 308 | if ($state->{token} == SYMBOL || $state->{token} == STRING) { | ||
| 309 | # Recognise symbolic OIDs (e.g. "bib1") as well as literals. | ||
| 310 | my $sym; | ||
| 311 | if ($state->{token} == STRING) { | ||
| 312 | $sym = parse_string($state); | ||
| 313 | } else { | ||
| 314 | $sym = $state->{lexeme}; | ||
| 315 | match($state, $state->{token}); | ||
| 316 | } | ||
| 317 | $attrset = lookup_attrset($state, $sym); | ||
| 318 | } else { | ||
| 319 | $attrset = [ $state->{lexeme} ]; | ||
| 320 | match($state, NUMBER, "attribute set"); | ||
| 321 | while ($state->{token} == ord(".")) { | ||
| 322 | match($state, ord(".")); | ||
| 323 | push @$attrset, $state->{lexeme}; | ||
| 324 | match($state, NUMBER); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | my $specs = []; | ||
| 329 | match($state, ord("{")); | ||
| 330 | while ($state->{token} != ord("}")) { | ||
| 331 | push @$specs, parse_accessSpec($state); | ||
| 332 | } | ||
| 333 | match($state, ord("}")); | ||
| 334 | |||
| 335 | ### I think the word "bless" in the next line shouldn't be there | ||
| 336 | return bless { attrset => join('.', @$attrset), specs => $specs }; | ||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | # The prefix query compiler does this with: | ||
| 341 | # oid_value topSet = query_oid_getvalbyname (li); | ||
| 342 | # zq->attributeSetId = yaz_oidval_to_z3950oid(o, CLASS_ATTSET, topSet); | ||
| 343 | # But those functions aren't wired out through Net::Z3950 or | ||
| 344 | # Net::Z3950::SimpleServer, so for now we need to hardwire the values. | ||
| 345 | # ### fix that bug in Net::Z3950* | ||
| 346 | # | ||
| 347 | my %_attributeSets = ( | ||
| 348 | bib1 => 1, | ||
| 349 | exp1 => 2, | ||
| 350 | ext1 => 3, | ||
| 351 | ccl1 => 4, | ||
| 352 | gils => 5, | ||
| 353 | stas => 6, | ||
| 354 | collections => 7, | ||
| 355 | cimi => 8, | ||
| 356 | geo => 9, | ||
| 357 | zbig => 10, | ||
| 358 | util => 11, | ||
| 359 | xd => 12, | ||
| 360 | xd1 => 12, # This is what YAZ uses | ||
| 361 | zthes => 13, | ||
| 362 | fin1 => 14, | ||
| 363 | dan1 => 15, | ||
| 364 | holdings => 16, | ||
| 365 | usmarc => 17, | ||
| 366 | bib2 => 18, | ||
| 367 | zeerex => 19, | ||
| 368 | ); | ||
| 369 | |||
| 370 | sub lookup_attrset { | ||
| 371 | my($state, $sym) = @_; | ||
| 372 | |||
| 373 | my $orig = $sym; | ||
| 374 | $sym =~ s/-//; | ||
| 375 | $sym = lc($sym); | ||
| 376 | my $val = $_attributeSets{$sym}; | ||
| 377 | fatal($state, "unrecognised attribute set '$orig'") | ||
| 378 | if !defined $val; | ||
| 379 | |||
| 380 | return [ 1, 2, 840, 10003, 3, $val ]; | ||
| 381 | } | ||
| 382 | |||
| 383 | |||
| 384 | sub parse_accessSpec { | ||
| 385 | my($state) = @_; | ||
| 386 | |||
| 387 | match($state, ACCESS); | ||
| 388 | my $accesspoint = $state->{lexeme}; | ||
| 389 | match($state, NUMBER); | ||
| 390 | match($state, ord("=")); | ||
| 391 | |||
| 392 | my($fulltext, $uppercase); | ||
| 393 | while (1) { | ||
| 394 | if ($state->{token} == FULLTEXT) { | ||
| 395 | $fulltext = 1; | ||
| 396 | match($state, FULLTEXT); | ||
| 397 | } elsif ($state->{token} == UPPERCASE) { | ||
| 398 | $uppercase = 1; | ||
| 399 | match($state, UPPERCASE); | ||
| 400 | } else { | ||
| 401 | last; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | my $columnname = parse_string($state); | ||
| 406 | my $spec = bless { | ||
| 407 | accesspoint => $accesspoint, | ||
| 408 | columnname => $columnname, | ||
| 409 | }, $state->{class} . '::AccessSpec'; | ||
| 410 | $spec->{fulltext} = 1 if $fulltext; | ||
| 411 | $spec->{uppercase} = 1 if $uppercase; | ||
| 412 | return $spec; | ||
| 413 | } | ||
| 414 | |||
| 415 | |||
| 416 | sub parse_dataSpec { | ||
| 417 | my($state) = @_; | ||
| 418 | |||
| 419 | match($state, DATA); | ||
| 420 | my $hashref = {}; | ||
| 421 | if ($state->{token} == ord("{")) { | ||
| 422 | # Multiple record-syntax specifications | ||
| 423 | match($state, ord("{")); | ||
| 424 | while ($state->{token} != ord("}")) { | ||
| 425 | parse_formatSpec($state, $hashref); | ||
| 426 | } | ||
| 427 | match($state, ord("}")); | ||
| 428 | } else { | ||
| 429 | # Single record-syntax specification | ||
| 430 | parse_formatSpec($state, $hashref); | ||
| 431 | } | ||
| 432 | |||
| 433 | return bless $hashref, $state->{class} . '::DataSpec'; | ||
| 434 | } | ||
| 435 | |||
| 436 | |||
| 437 | # Note that this messes with %$hashref and does not return anything | ||
| 438 | sub parse_formatSpec { | ||
| 439 | my($state, $hashref) = @_; | ||
| 440 | |||
| 441 | # Special case: treat as a magic format-name | ||
| 442 | if ($state->{token} == CACHESIZE) { | ||
| 443 | match($state, CACHESIZE); | ||
| 444 | match($state, ord("=")); | ||
| 445 | $hashref->{"*cacheSize"} = $state->{lexeme}; | ||
| 446 | match($state, NUMBER); | ||
| 447 | return; | ||
| 448 | } | ||
| 449 | |||
| 450 | match($state, FORMAT); | ||
| 451 | if ($state->{token} == GRS1) { | ||
| 452 | match($state, GRS1); | ||
| 453 | $hashref->{GRS1} = parse_GRS1Spec($state); | ||
| 454 | } elsif ($state->{token} == XML) { | ||
| 455 | match($state, XML); | ||
| 456 | $hashref->{XML} = parse_XMLSpec($state); | ||
| 457 | } elsif ($state->{token} == MARC) { | ||
| 458 | match($state, MARC); | ||
| 459 | $hashref->{MARC} = parse_XMLSpec($state); | ||
| 460 | } elsif ($state->{token} == SUTRS) { | ||
| 461 | match($state, SUTRS); | ||
| 462 | $hashref->{SUTRS} = parse_XMLSpec($state); | ||
| 463 | } else { | ||
| 464 | match($state, GRS1, "format type: GRS1, XML, MARC or SUTRS"); | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | |||
| 469 | sub parse_GRS1Spec { | ||
| 470 | my($state) = @_; | ||
| 471 | |||
| 472 | match($state, ord("{")); | ||
| 473 | my $specs = []; | ||
| 474 | while ($state->{token} != ord("}")) { | ||
| 475 | push @$specs, parse_GRS1ElementSpec($state); | ||
| 476 | } | ||
| 477 | match($state, ord("}")); | ||
| 478 | return bless { | ||
| 479 | specs => $specs, | ||
| 480 | map => { map { $_->tagname(), $_ } @$specs }, | ||
| 481 | }, $state->{class} . '::GRS1Spec'; | ||
| 482 | } | ||
| 483 | |||
| 484 | |||
| 485 | sub parse_GRS1ElementSpec { | ||
| 486 | my($state) = @_; | ||
| 487 | |||
| 488 | my($columnname, $tagpath); | ||
| 489 | if ($state->{token} == COLUMN) { | ||
| 490 | # 'column "x" = "y"' (obsolescent) is equivalent to 'field "y" = "x"' | ||
| 491 | match($state, COLUMN); | ||
| 492 | $columnname = parse_string($state); | ||
| 493 | match($state, ord("=")); | ||
| 494 | $tagpath = parse_tagPath($state); | ||
| 495 | } else { | ||
| 496 | match($state, FIELD); | ||
| 497 | $tagpath = parse_tagPath($state); | ||
| 498 | match($state, ord("=")); | ||
| 499 | $columnname = parse_string($state); | ||
| 500 | } | ||
| 501 | |||
| 502 | return bless { | ||
| 503 | columnname => $columnname, | ||
| 504 | tagpath => $tagpath, | ||
| 505 | tagname => "$tagpath", # opaque string used as index in map | ||
| 506 | }, $state->{class} . '::GRS1ElementSpec'; | ||
| 507 | } | ||
| 508 | |||
| 509 | |||
| 510 | sub parse_tagPath { | ||
| 511 | my($state) = @_; | ||
| 512 | |||
| 513 | my $tags = []; | ||
| 514 | while ($state->{token} == ord("(")) { | ||
| 515 | match($state, ord("(")); | ||
| 516 | my $type = $state->{lexeme}; | ||
| 517 | match($state, NUMBER); | ||
| 518 | match($state, ord(",")); | ||
| 519 | my $value; | ||
| 520 | if ($state->{token} == NUMBER) { | ||
| 521 | $value = $state->{lexeme}; | ||
| 522 | match($state, $state->{token}); | ||
| 523 | } elsif ($state->{token} == STRING) { | ||
| 524 | $value = parse_string($state); | ||
| 525 | } else { | ||
| 526 | match($state, 0, "STRING or NUMBER tag-value"); | ||
| 527 | } | ||
| 528 | match($state, ord(")")); | ||
| 529 | push @$tags, [ $type, $value ]; | ||
| 530 | } | ||
| 531 | return bless $tags, $state->{class} . '::TagPath'; | ||
| 532 | } | ||
| 533 | |||
| 534 | |||
| 535 | sub parse_XMLSpec { | ||
| 536 | my($state) = @_; | ||
| 537 | |||
| 538 | match($state, ord("{")); | ||
| 539 | my $specs = []; | ||
| 540 | while ($state->{token} != ord("}")) { | ||
| 541 | push @$specs, parse_XMLElementSpec($state); | ||
| 542 | } | ||
| 543 | match($state, ord("}")); | ||
| 544 | return bless { | ||
| 545 | specs => $specs, | ||
| 546 | map => { map { $_->tagname(), $_ } @$specs }, | ||
| 547 | }, $state->{class} . '::XMLSpec'; | ||
| 548 | } | ||
| 549 | |||
| 550 | |||
| 551 | sub parse_XMLElementSpec { | ||
| 552 | my($state) = @_; | ||
| 553 | |||
| 554 | my($columnname, $tagname); | ||
| 555 | if ($state->{token} == COLUMN) { | ||
| 556 | # 'column "x" = "y"' (obsolescent) is equivalent to 'field "y" = "x"' | ||
| 557 | match($state, COLUMN); | ||
| 558 | $columnname = parse_string($state); | ||
| 559 | match($state, ord("=")); | ||
| 560 | $tagname = parse_string($state); | ||
| 561 | } else { | ||
| 562 | # Treat record-name, attrs and transform as special fields | ||
| 563 | if ($state->{token} == RECORD) { | ||
| 564 | match($state, RECORD); | ||
| 565 | $tagname = "*record"; | ||
| 566 | } elsif ($state->{token} == ATTRS) { | ||
| 567 | match($state, ATTRS); | ||
| 568 | $tagname = "*attrs"; | ||
| 569 | } elsif ($state->{token} == TRANSFORM) { | ||
| 570 | match($state, TRANSFORM); | ||
| 571 | $tagname = "*transform"; | ||
| 572 | } elsif ($state->{token} == SCHEMA) { | ||
| 573 | match($state, SCHEMA); | ||
| 574 | $tagname = "*schema"; | ||
| 575 | } else { | ||
| 576 | match($state, FIELD); | ||
| 577 | $tagname = parse_string($state); | ||
| 578 | } | ||
| 579 | match($state, ord("=")); | ||
| 580 | $columnname = parse_string($state); | ||
| 581 | } | ||
| 582 | |||
| 583 | return bless { | ||
| 584 | tagname => $tagname, | ||
| 585 | columnname => $columnname, | ||
| 586 | }, $state->{class} . '::XMLElementSpec'; | ||
| 587 | } | ||
| 588 | |||
| 589 | |||
| 590 | sub parse_string { | ||
| 591 | my($state) = @_; | ||
| 592 | |||
| 593 | my $string = $state->{lexeme}; | ||
| 594 | match($state, STRING); | ||
| 595 | # Allow concatenation of multiple strings with "+" | ||
| 596 | while ($state->{token} == ord("+")) { | ||
| 597 | match($state, ord("+")); | ||
| 598 | $string .= $state->{lexeme}; | ||
| 599 | match($state, STRING); | ||
| 600 | } | ||
| 601 | return $string; | ||
| 602 | } | ||
| 603 | |||
| 604 | |||
| 605 | # PRIVATE to lex() | ||
| 606 | my %keywords = (database => DATABASE, | ||
| 607 | search => SEARCH, | ||
| 608 | access => ACCESS, | ||
| 609 | attr => ATTR, | ||
| 610 | attrset => ATTRSET, | ||
| 611 | data => DATA, | ||
| 612 | column => COLUMN, | ||
| 613 | format => FORMAT, | ||
| 614 | grs1 => GRS1, | ||
| 615 | xml => XML, | ||
| 616 | record => RECORD, | ||
| 617 | datasource => DATASOURCE, | ||
| 618 | cachesize => CACHESIZE, | ||
| 619 | table => TABLE, | ||
| 620 | field => FIELD, | ||
| 621 | auxiliary => AUXILIARY, | ||
| 622 | with => WITH, | ||
| 623 | username => USERNAME, | ||
| 624 | password => PASSWORD, | ||
| 625 | attrs => ATTRS, | ||
| 626 | restriction => RESTRICTION, | ||
| 627 | transform => TRANSFORM, | ||
| 628 | marc => MARC, | ||
| 629 | defaultattrs => DEFAULTATTRS, | ||
| 630 | fulltext => FULLTEXT, | ||
| 631 | sutrs => SUTRS, | ||
| 632 | schema => SCHEMA, | ||
| 633 | join => JOIN, | ||
| 634 | uppercase => UPPERCASE, | ||
| 635 | option => OPTION, | ||
| 636 | ); | ||
| 637 | |||
| 638 | my %invert = reverse %keywords; | ||
| 639 | $invert{STRING()} = "STRING"; | ||
| 640 | $invert{NUMBER()} = "NUMBER"; | ||
| 641 | $invert{SYMBOL()} = "SYMBOL"; | ||
| 642 | |||
| 643 | |||
| 644 | sub match { | ||
| 645 | my($state, $token, $msg) = @_; | ||
| 646 | |||
| 647 | if ($state->{token} != $token) { | ||
| 648 | # Really throwing an exception | ||
| 649 | if (defined $msg) { | ||
| 650 | $token = $msg; | ||
| 651 | } else { | ||
| 652 | $token = "'" . ($invert{$token} || chr($token)) . "'" . | ||
| 653 | " (" . $token . ")"; | ||
| 654 | } | ||
| 655 | |||
| 656 | fatal($state, "expected $token, got '" . $state->{lexeme} . "'" . | ||
| 657 | " (" . $state->{token} . ")"); | ||
| 658 | } | ||
| 659 | |||
| 660 | lex($state); | ||
| 661 | } | ||
| 662 | |||
| 663 | |||
| 664 | sub fatal { | ||
| 665 | my $state = shift(); | ||
| 666 | |||
| 667 | die($state->{filename} . ":" . $state->{line} . ": " . | ||
| 668 | join('', @_) . "\n"); | ||
| 669 | } | ||
| 670 | |||
| 671 | |||
| 672 | sub lex { | ||
| 673 | my($state) = @_; | ||
| 674 | my $token = _lex($state); | ||
| 675 | $state->{token} = $token; | ||
| 676 | #warn "lexed $token (" . $state->{lexeme} . ")"; | ||
| 677 | } | ||
| 678 | |||
| 679 | |||
| 680 | sub _lex { | ||
| 681 | my($state) = @_; | ||
| 682 | |||
| 683 | $state->{buffer} =~ s/^\s+//; | ||
| 684 | while (!$state->{buffer}) { | ||
| 685 | if (!defined ($state->{buffer} = $state->{fh}->getline())) { | ||
| 686 | return undef; | ||
| 687 | } | ||
| 688 | $state->{line}++; | ||
| 689 | #warn "read line " . $state->{line} . ": " . $state->{buffer}; | ||
| 690 | $state->{buffer} =~ s/(?<!\\)#.*$//; | ||
| 691 | $state->{buffer} =~ s/\\#/#/g; | ||
| 692 | $state->{buffer} =~ s/\s+$//; | ||
| 693 | $state->{buffer} =~ s/^\s+//; | ||
| 694 | } | ||
| 695 | |||
| 696 | my $q = '"'; # this avoids confusing emacs's Perl mode | ||
| 697 | if ($state->{buffer} =~ s/^"([^$q]*)"//) { | ||
| 698 | $state->{lexeme} = $1; | ||
| 699 | # We need these special cases so we can specify a literal | ||
| 700 | # newline in data-source names such as "DBD::CSV::csv_eol=\n" | ||
| 701 | $state->{lexeme} =~ s/\\t/\t/g; | ||
| 702 | $state->{lexeme} =~ s/\\n/\n/g; | ||
| 703 | return STRING | ||
| 704 | } elsif ($state->{buffer} =~ s/^([0-9]+)//) { | ||
| 705 | $state->{lexeme} = $1; | ||
| 706 | return NUMBER; | ||
| 707 | } elsif ($state->{buffer} =~ s/^([_a-z][a-z_0-9]*)//i) { | ||
| 708 | $state->{lexeme} = $1; | ||
| 709 | return $keywords{lc($1)} || SYMBOL; | ||
| 710 | } | ||
| 711 | |||
| 712 | # Must be a single character | ||
| 713 | $state->{buffer} =~ s/^(.)//; | ||
| 714 | $state->{lexeme} = $1; | ||
| 715 | return ord($1); | ||
| 716 | } | ||
| 717 | |||
| 718 | ### should consider documenting access methods | ||
| 719 | |||
| 720 | |||
| 721 | =head1 AUTHOR | ||
| 722 | |||
| 723 | Mike Taylor E<lt>mike@miketaylor.org.ukE<gt> | ||
| 724 | |||
| 725 | First version Sunday 3rd February 2002. | ||
| 726 | |||
| 727 | =head1 SEE ALSO | ||
| 728 | |||
| 729 | C<Net::Z3950::DBIServer> | ||
| 730 | is the module that uses this, and the only one that would ever want | ||
| 731 | to, I'm sure. | ||
| 732 | |||
| 733 | You might like to look at the | ||
| 734 | C<Parse::RecDescent> | ||
| 735 | module, but I didn't :-) | ||
| 736 | |||
| 737 | =cut | ||
| 738 | |||
| 739 | |||
| 740 | |||
| 741 | # And now, a few undocumented (because they're obvious) utility methods | ||
| 742 | package Net::Z3950::DBIServer::Config; | ||
| 743 | |||
| 744 | sub forDb { | ||
| 745 | my $this = shift(); | ||
| 746 | my($dbname) = @_; | ||
| 747 | |||
| 748 | # To make this do case-insensitive matching, we jump through some | ||
| 749 | # rather silly hoops. | ||
| 750 | foreach my $key (keys %$this) { | ||
| 751 | if (lc($key) eq lc($dbname)) { | ||
| 752 | return $this->{$key}; | ||
| 753 | } | ||
| 754 | } | ||
| 755 | return undef; | ||
| 756 | } | ||
| 757 | |||
| 758 | sub dataSource { my $this = shift(); return $this->{"*dataSource"}; } | ||
| 759 | sub userName { my $this = shift(); return $this->{"*userName"}; } | ||
| 760 | sub passWord { my $this = shift(); return $this->{"*passWord"}; } | ||
| 761 | sub options { my $this = shift(); return $this->{"*options"}; } | ||
| 762 | |||
| 763 | |||
| 764 | package Net::Z3950::DBIServer::Config::PerDB; | ||
| 765 | |||
| 766 | sub dbname { my $this = shift(); return $this->{dbname} } | ||
| 767 | sub tablename { my $this = shift(); return $this->{tablename} } | ||
| 768 | sub auxiliary { my $this = shift(); return $this->{auxiliary} } | ||
| 769 | sub restriction { my $this = shift(); return $this->{restriction} } | ||
| 770 | sub join { my $this = shift(); return $this->{join} } | ||
| 771 | sub searchSpec { my $this = shift(); return $this->{searchSpec} } | ||
| 772 | sub dataSpec { my $this = shift(); return $this->{dataSpec} } | ||
| 773 | |||
| 774 | =head2 Net::Z3950::DBIServer::Config::PerDB::columns() | ||
| 775 | |||
| 776 | Return a list of strings, each of which is the name of one column that | ||
| 777 | should be included in SELECT statements for the database on whose | ||
| 778 | configuration object it is invoked. It gathers these from the | ||
| 779 | information about all configured record syntaxes, and discards | ||
| 780 | ``constant columns'' whose names begin with ``*''. | ||
| 781 | |||
| 782 | =cut | ||
| 783 | |||
| 784 | sub columns { | ||
| 785 | my $this = shift(); | ||
| 786 | |||
| 787 | if (!defined $this->{columns}) { | ||
| 788 | my $tablename = $this->tablename(); | ||
| 789 | #warn "calculating columns for '$tablename'"; | ||
| 790 | my $aux = $this->auxiliary(); | ||
| 791 | my $has_aux = (@$aux > 0) ? 1 : 0; | ||
| 792 | my @columns; | ||
| 793 | my %register; | ||
| 794 | |||
| 795 | my $dataSpec = $this->dataSpec(); | ||
| 796 | my @formatNames = $dataSpec->formatNames(); | ||
| 797 | foreach my $formatName (@formatNames) { | ||
| 798 | foreach my $field ($dataSpec->{$formatName}->fields()) { | ||
| 799 | my $colSpec = $field->columnName(); | ||
| 800 | foreach my $col (columnsInSpec($colSpec)) { | ||
| 801 | $col = "$tablename.$col" if $has_aux && $col !~ /\./; | ||
| 802 | if (!exists $register{$col}) { | ||
| 803 | #warn "adding $formatName/$colSpec -> $col\n"; | ||
| 804 | push @columns, $col; | ||
| 805 | $register{$col} = 1; | ||
| 806 | } | ||
| 807 | } | ||
| 808 | } | ||
| 809 | } | ||
| 810 | $this->{columns} = [ @columns ]; | ||
| 811 | #warn "calculated " . join(', ', @{ $this->{columns} }); | ||
| 812 | } | ||
| 813 | |||
| 814 | return @{ $this->{columns} }; | ||
| 815 | } | ||
| 816 | |||
| 817 | |||
| 818 | sub columnsInSpec { | ||
| 819 | my($spec) = @_; | ||
| 820 | |||
| 821 | return () if $spec =~ /^\*/; | ||
| 822 | if ($spec !~ /%/) { | ||
| 823 | $spec =~ s/^\?//; | ||
| 824 | return $spec; | ||
| 825 | } | ||
| 826 | |||
| 827 | my @cols; | ||
| 828 | while ($spec =~ s/.*?%\{(.*?)\}//) { | ||
| 829 | my $fieldname = $1; | ||
| 830 | $fieldname =~ s/^\?//; | ||
| 831 | push @cols, $fieldname; | ||
| 832 | } | ||
| 833 | |||
| 834 | return @cols; | ||
| 835 | } | ||
| 836 | |||
| 837 | |||
| 838 | package Net::Z3950::DBIServer::Config::AuxSpec; | ||
| 839 | sub tableName { my $this = shift(); return $this->{tableName} } | ||
| 840 | sub cond { my $this = shift(); return $this->{cond} } | ||
| 841 | |||
| 842 | |||
| 843 | package Net::Z3950::DBIServer::Config::SearchSpec; | ||
| 844 | |||
| 845 | sub accessPoint { | ||
| 846 | my $this = shift(); | ||
| 847 | my($attrset, $attrval) = @_; | ||
| 848 | |||
| 849 | return $this->{"$attrset:$attrval"}; | ||
| 850 | } | ||
| 851 | |||
| 852 | |||
| 853 | package Net::Z3950::DBIServer::Config::DataSpec; | ||
| 854 | |||
| 855 | sub cacheSize { | ||
| 856 | my $this = shift(); | ||
| 857 | |||
| 858 | my $cacheSize = $this->{"*cacheSize"}; | ||
| 859 | $cacheSize = 10 if !defined $cacheSize; | ||
| 860 | return $cacheSize | ||
| 861 | } | ||
| 862 | |||
| 863 | sub formatNames { | ||
| 864 | my $this = shift(); | ||
| 865 | |||
| 866 | my %formats = %$this; | ||
| 867 | delete $formats{"*cacheSize"}; | ||
| 868 | return sort keys %formats; | ||
| 869 | } | ||
| 870 | |||
| 871 | |||
| 872 | package Net::Z3950::DBIServer::Config::FormatSpec; | ||
| 873 | |||
| 874 | sub fields { | ||
| 875 | my $this = shift(); | ||
| 876 | |||
| 877 | my @fields = (); | ||
| 878 | my $specs = $this->{specs}; | ||
| 879 | foreach my $field (@{ $this->{specs} }) { | ||
| 880 | push @fields, $field | ||
| 881 | if $field->tagname() !~ /^\*/; | ||
| 882 | } | ||
| 883 | |||
| 884 | return @fields; | ||
| 885 | } | ||
| 886 | |||
| 887 | sub fieldNames { | ||
| 888 | my $this = shift(); | ||
| 889 | Carp::confess("fieldNames($this) is deprecated\n"); | ||
| 890 | |||
| 891 | my @fields = (); | ||
| 892 | my $specs = $this->{specs}; | ||
| 893 | foreach my $i (0..@$specs-1) { | ||
| 894 | my $field = $specs->[$i]->tagname(); | ||
| 895 | push @fields, $field if $field !~ /^\*/; | ||
| 896 | } | ||
| 897 | |||
| 898 | return @fields; | ||
| 899 | } | ||
| 900 | |||
| 901 | sub fieldSpec { | ||
| 902 | my $this = shift(); | ||
| 903 | my($field) = @_; | ||
| 904 | Carp::confess("fieldSpec($this, '$field') is deprecated\n") | ||
| 905 | if $field !~ /^\*/; | ||
| 906 | |||
| 907 | return $this->{map}->{$field}; | ||
| 908 | } | ||
| 909 | |||
| 910 | |||
| 911 | package Net::Z3950::DBIServer::Config::GRS1Spec; | ||
| 912 | use vars qw(@ISA); | ||
| 913 | @ISA = qw(Net::Z3950::DBIServer::Config::FormatSpec); | ||
| 914 | |||
| 915 | |||
| 916 | package Net::Z3950::DBIServer::Config::XMLSpec; | ||
| 917 | use vars qw(@ISA); | ||
| 918 | @ISA = qw(Net::Z3950::DBIServer::Config::FormatSpec); | ||
| 919 | |||
| 920 | sub recordName { | ||
| 921 | my $this = shift(); | ||
| 922 | my $entry = $this->fieldSpec("*record"); | ||
| 923 | return !defined $entry ? undef : $entry->{columnname}; | ||
| 924 | } | ||
| 925 | |||
| 926 | sub recordAttrs { | ||
| 927 | my $this = shift(); | ||
| 928 | my $entry = $this->fieldSpec("*attrs"); | ||
| 929 | return !defined $entry ? undef : $entry->{columnname}; | ||
| 930 | } | ||
| 931 | |||
| 932 | sub recordTransform { | ||
| 933 | my $this = shift(); | ||
| 934 | my $entry = $this->fieldSpec("*transform"); | ||
| 935 | return !defined $entry ? undef : $entry->{columnname}; | ||
| 936 | } | ||
| 937 | |||
| 938 | sub recordSchema { | ||
| 939 | my $this = shift(); | ||
| 940 | my $entry = $this->fieldSpec("*schema"); | ||
| 941 | return !defined $entry ? undef : $entry->{columnname}; | ||
| 942 | } | ||
| 943 | |||
| 944 | |||
| 945 | package Net::Z3950::DBIServer::Config::ElementSpec; | ||
| 946 | |||
| 947 | sub tagname { | ||
| 948 | my $this = shift(); | ||
| 949 | return $this->{tagname}; | ||
| 950 | } | ||
| 951 | |||
| 952 | sub columnName { | ||
| 953 | my $this = shift(); | ||
| 954 | my($field) = @_; | ||
| 955 | |||
| 956 | return $this->{columnname}; | ||
| 957 | } | ||
| 958 | |||
| 959 | |||
| 960 | package Net::Z3950::DBIServer::Config::GRS1ElementSpec; | ||
| 961 | use vars qw(@ISA); | ||
| 962 | @ISA = qw(Net::Z3950::DBIServer::Config::ElementSpec); | ||
| 963 | |||
| 964 | sub tagpath { | ||
| 965 | my $this = shift(); | ||
| 966 | my($field) = @_; | ||
| 967 | |||
| 968 | return $this->{tagpath}; | ||
| 969 | } | ||
| 970 | |||
| 971 | package Net::Z3950::DBIServer::Config::XMLElementSpec; | ||
| 972 | use vars qw(@ISA); | ||
| 973 | @ISA = qw(Net::Z3950::DBIServer::Config::ElementSpec); | ||
| 974 | |||
| 975 | 1; |