| 1 | |||
| 2 | package Xmldoom::Definition::SAXHandler; | ||
| 3 | use base qw(XML::SAX::Base Exporter); | ||
| 4 | |||
| 5 | use Xmldoom::Definition::Property::Simple; | ||
| 6 | use Xmldoom::Definition::Property::Object; | ||
| 7 | use Xmldoom::Definition::Property::PlaceHolder; | ||
| 8 | use Xmldoom::Criteria::XML; | ||
| 9 | use Xmldoom::Threads; | ||
| 10 | use DBIx::Romani::Query::XML::Util qw/parse_boolean/; | ||
| 11 | use XML::GDOME; | ||
| 12 | use Module::Runtime qw(use_module); | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | use Data::Dumper; | ||
| 16 | |||
| 17 | our @EXPORT_OK = qw( $OBJECT_NS $OBJECT_PERL_NS ); | ||
| 18 | |||
| 19 | our $OBJECT_NS = "http://gna.org/projects/xmldoom/object"; | ||
| 20 | our $OBJECT_PERL_NS = "http://gna.org/projects/xmldoom/object-perl"; | ||
| 21 | |||
| 22 | # In the interest of speed and memory and Perl's poor garbage collection, we actually | ||
| 23 | # run this parser twice over the same file to get all its data. | ||
| 24 | |||
| 25 | sub new | ||
| 26 | { | ||
| 27 | my $class = shift; | ||
| 28 | my $args = shift; | ||
| 29 | |||
| 30 | my $database; | ||
| 31 | |||
| 32 | if ( ref($args) eq 'HASH' ) | ||
| 33 | { | ||
| 34 | $database = $args->{database}; | ||
| 35 | } | ||
| 36 | else | ||
| 37 | { | ||
| 38 | $database = $args; | ||
| 39 | } | ||
| 40 | |||
| 41 | my $self = { | ||
| 42 | database => $database, | ||
| 43 | phase => 1, | ||
| 44 | |||
| 45 | # for phase 2 | ||
| 46 | cur_obj => undef, | ||
| 47 | prop_name => undef, | ||
| 48 | prop_type => undef, | ||
| 49 | prop_args => undef, | ||
| 50 | dom_doc => undef, | ||
| 51 | dom_stack => [], | ||
| 52 | ignore_obj => 0, | ||
| 53 | in_criteria => 0, | ||
| 54 | }; | ||
| 55 | |||
| 56 | bless $self, $class; | ||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub start_document | ||
| 61 | { | ||
| 62 | my ($self, $doc) = @_; | ||
| 63 | |||
| 64 | if ( $self->{phase} > 2 ) | ||
| 65 | { | ||
| 66 | die "Cannot run more than twice on the same input"; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub end_document | ||
| 71 | { | ||
| 72 | my ($self, $doc) = @_; | ||
| 73 | |||
| 74 | # move to next phase | ||
| 75 | $self->{phase}++; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub start_element | ||
| 79 | { | ||
| 80 | my ($self, $el) = @_; | ||
| 81 | |||
| 82 | # simple aliases | ||
| 83 | my $name = $el->{'LocalName'}; | ||
| 84 | my $attrs = $el->{'Attributes'}; | ||
| 85 | |||
| 86 | # automatically return if we are ignoring the object | ||
| 87 | if ( $self->{ignore_obj} ) | ||
| 88 | { | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | # if we are in criteria, we operate in super quaazy mode | ||
| 93 | if ( $self->{in_criteria} ) | ||
| 94 | { | ||
| 95 | if ( $self->{phase} == 2 ) | ||
| 96 | { | ||
| 97 | # create a new element | ||
| 98 | my $element = $self->{dom_doc}->createElement($name); | ||
| 99 | while ( my ($key, $value) = each %$attrs ) | ||
| 100 | { | ||
| 101 | # remove crazy NS symbols that Perl SAX uses. | ||
| 102 | $key =~ s/^\{\}//; | ||
| 103 | |||
| 104 | $element->setAttribute($key, $value->{Value}); | ||
| 105 | } | ||
| 106 | |||
| 107 | # add to parent and push to stack | ||
| 108 | $self->{dom_stack}->[-1]->appendChild($element); | ||
| 109 | push @{$self->{dom_stack}}, $element; | ||
| 110 | } | ||
| 111 | |||
| 112 | # drop the monkey and run! | ||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | if ( $name eq 'objects' ) | ||
| 117 | { | ||
| 118 | # just let pass through | ||
| 119 | } | ||
| 120 | elsif ( $name eq 'object' and not $self->{prop_name} ) | ||
| 121 | { | ||
| 122 | if ( $self->{phase} == 1 ) | ||
| 123 | { | ||
| 124 | # actually create the object in the definition, and attach | ||
| 125 | # it to the appropriate table. | ||
| 126 | my $object_name = $attrs->{'{}name'}->{Value}; | ||
| 127 | my $table_name = $attrs->{'{}table'}->{Value}; | ||
| 128 | |||
| 129 | if ( $self->{database}->has_table( $table_name ) ) | ||
| 130 | { | ||
| 131 | my $object = $self->{database}->create_object( $object_name, $table_name ); | ||
| 132 | |||
| 133 | # set the Perl class | ||
| 134 | if ( $attrs->{"{$OBJECT_PERL_NS}class"} ) | ||
| 135 | { | ||
| 136 | Xmldoom::Object::BindToObject( $attrs->{"{$OBJECT_PERL_NS}class"}->{Value}, $object ); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | else | ||
| 140 | { | ||
| 141 | print STDERR "WARNING: Can't find table '$table_name' in the database schema, so we are ignoring the '$object_name' object definition\n"; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | elsif ( $self->{phase} == 2 ) | ||
| 145 | { | ||
| 146 | # retrieve and set as the current object | ||
| 147 | my $object_name = $attrs->{'{}name'}->{Value}; | ||
| 148 | |||
| 149 | if ( $self->{database}->has_object( $object_name ) ) | ||
| 150 | { | ||
| 151 | $self->{cur_obj} = $self->{database}->get_object( $object_name ); | ||
| 152 | $self->{ignore_obj} = 0; | ||
| 153 | } | ||
| 154 | else | ||
| 155 | { | ||
| 156 | $self->{ignore_obj} = 1; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | elsif ( $name eq 'property' ) | ||
| 161 | { | ||
| 162 | $self->{prop_name} = $attrs->{'{}name'}->{Value}; | ||
| 163 | if ( not $self->{prop_name} ) | ||
| 164 | { | ||
| 165 | die "Invalid property name: '$self->{prop_name}'"; | ||
| 166 | } | ||
| 167 | |||
| 168 | my $args = { | ||
| 169 | parent => $self->{cur_obj}, | ||
| 170 | name => $self->{prop_name}, | ||
| 171 | shared => Xmldoom::Threads::is_shared($self->{database}), | ||
| 172 | }; | ||
| 173 | |||
| 174 | if ( defined $attrs->{'{}description'} ) | ||
| 175 | { | ||
| 176 | $args->{description} = $attrs->{'{}description'}->{Value}; | ||
| 177 | } | ||
| 178 | if ( defined $attrs->{'{}searchable'} ) | ||
| 179 | { | ||
| 180 | $args->{searchable} = parse_boolean( $attrs->{'{}searchable'}->{Value} ); | ||
| 181 | } | ||
| 182 | if ( defined $attrs->{'{}reportable'} ) | ||
| 183 | { | ||
| 184 | $args->{reportable} = parse_boolean( $attrs->{'{}reportable'}->{Value} ); | ||
| 185 | } | ||
| 186 | if ( defined $attrs->{'{}get_name'} ) | ||
| 187 | { | ||
| 188 | $args->{get_name} = $attrs->{'{}get_name'}->{Value}; | ||
| 189 | } | ||
| 190 | if ( defined $attrs->{'{}set_name'} ) | ||
| 191 | { | ||
| 192 | $args->{set_name} = $attrs->{'{}set_name'}->{Value}; | ||
| 193 | } | ||
| 194 | |||
| 195 | $self->{prop_args} = $args; | ||
| 196 | } | ||
| 197 | elsif ( $name eq 'simple' or $name eq 'custom' or ( $name eq 'object' and $self->{prop_name} ) ) | ||
| 198 | { | ||
| 199 | if ( $self->{phase} == 1 ) | ||
| 200 | { | ||
| 201 | # we skip over these in phase 1 !! | ||
| 202 | return; | ||
| 203 | } | ||
| 204 | |||
| 205 | # convenience | ||
| 206 | my $args = $self->{prop_args}; | ||
| 207 | |||
| 208 | # do the specifics | ||
| 209 | if ( $name eq 'simple' ) | ||
| 210 | { | ||
| 211 | if ( defined $attrs->{'{}attribute'} ) | ||
| 212 | { | ||
| 213 | $args->{attribute} = $attrs->{'{}attribute'}->{Value}; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | elsif ( $name eq 'object' ) | ||
| 217 | { | ||
| 218 | $args->{object_name} = $attrs->{'{}name'}->{Value}; | ||
| 219 | |||
| 220 | if ( defined $attrs->{'{}inter_table'} ) | ||
| 221 | { | ||
| 222 | $args->{inter_table} = $attrs->{'{}inter_table'}->{Value}; | ||
| 223 | } | ||
| 224 | |||
| 225 | } | ||
| 226 | elsif ( $name eq 'custom' ) | ||
| 227 | { | ||
| 228 | if ( defined $attrs->{"{$OBJECT_PERL_NS}class"} ) | ||
| 229 | { | ||
| 230 | $args->{perl_class} = $attrs->{"{$OBJECT_PERL_NS}class"}->{Value}; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | # set our property name | ||
| 235 | $self->{prop_type} = $name; | ||
| 236 | } | ||
| 237 | elsif ( $name eq 'trans' ) | ||
| 238 | { | ||
| 239 | if ( $self->{phase} == 1 ) | ||
| 240 | { | ||
| 241 | # we skip over these in phase 1 !! | ||
| 242 | return; | ||
| 243 | } | ||
| 244 | |||
| 245 | if ( $self->{prop_type} ne 'simple' ) | ||
| 246 | { | ||
| 247 | die "<trans/> tag must be inside of <simple> property"; | ||
| 248 | } | ||
| 249 | |||
| 250 | my $dir = $attrs->{'{}dir'}->{Value} || 'both'; | ||
| 251 | my $from = $attrs->{'{}from'}->{Value}; | ||
| 252 | my $to = $attrs->{'{}to'}->{Value}; | ||
| 253 | |||
| 254 | if ( $dir eq 'out' or $dir eq 'both' ) | ||
| 255 | { | ||
| 256 | if ( not defined $self->{prop_args}->{trans_from} ) | ||
| 257 | { | ||
| 258 | $self->{prop_args}->{trans_from} = { }; | ||
| 259 | } | ||
| 260 | $self->{prop_args}->{trans_from}->{$from} = $to; | ||
| 261 | } | ||
| 262 | |||
| 263 | if ( $dir eq 'both' ) | ||
| 264 | { | ||
| 265 | # reverse these to fill in the next section | ||
| 266 | my $temp = $from; | ||
| 267 | $from = $to; | ||
| 268 | $to = $temp; | ||
| 269 | } | ||
| 270 | |||
| 271 | if ( $dir eq 'in' or $dir eq 'both' ) | ||
| 272 | { | ||
| 273 | if ( not defined $self->{prop_args}->{trans_to} ) | ||
| 274 | { | ||
| 275 | $self->{prop_args}->{trans_to} = { }; | ||
| 276 | } | ||
| 277 | $self->{prop_args}->{trans_to}->{$from} = $to; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | elsif ( $name eq 'options' ) | ||
| 281 | { | ||
| 282 | if ( $self->{phase} == 1 ) | ||
| 283 | { | ||
| 284 | # we skip over these in phase 1 !! | ||
| 285 | return; | ||
| 286 | } | ||
| 287 | |||
| 288 | if ( exists $self->{prop_args}->{options} ) | ||
| 289 | { | ||
| 290 | die "<options> tag cannot be defined twice"; | ||
| 291 | } | ||
| 292 | |||
| 293 | # set us up | ||
| 294 | $self->{prop_args}->{options} = [ ]; | ||
| 295 | if ( defined $attrs->{'{}inclusive'} ) | ||
| 296 | { | ||
| 297 | $self->{prop_args}->{inclusive} = parse_boolean( $attrs->{'{}inclusive'}->{Value} ); | ||
| 298 | } | ||
| 299 | if ( defined $attrs->{'{}dependent'} ) | ||
| 300 | { | ||
| 301 | $self->{prop_args}->{options_dependent} = parse_boolean( $attrs->{'{}dependent'}->{Value} ); | ||
| 302 | } | ||
| 303 | |||
| 304 | # split for the two property types | ||
| 305 | if ( $self->{prop_type} eq 'simple' ) | ||
| 306 | { | ||
| 307 | if ( defined $attrs->{'{}table'} ) | ||
| 308 | { | ||
| 309 | $self->{prop_args}->{options_table} = $attrs->{'{}table'}->{Value}; | ||
| 310 | } | ||
| 311 | if ( defined $attrs->{'{}column'} ) | ||
| 312 | { | ||
| 313 | $self->{prop_args}->{options_column} = $attrs->{'{}column'}->{Value}; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | elsif ( $self->{prop_type} eq 'object' ) | ||
| 317 | { | ||
| 318 | if ( defined $attrs->{'{}property'} ) | ||
| 319 | { | ||
| 320 | $self->{prop_args}->{options_property} = $attrs->{'{}property'}->{Value}; | ||
| 321 | } | ||
| 322 | } | ||
| 323 | } | ||
| 324 | elsif ( $name eq 'criteria' ) | ||
| 325 | { | ||
| 326 | # mark our special mode | ||
| 327 | $self->{in_criteria} = 1; | ||
| 328 | |||
| 329 | if ( $self->{phase} == 1 ) | ||
| 330 | { | ||
| 331 | # we skip over these in phase 1 !! | ||
| 332 | return; | ||
| 333 | } | ||
| 334 | |||
| 335 | if ( not exists $self->{prop_args}->{options} ) | ||
| 336 | { | ||
| 337 | die "<criteria/> tag must be inside of <options> section"; | ||
| 338 | } | ||
| 339 | |||
| 340 | # here we create our initial DOM document | ||
| 341 | $self->{dom_doc} = XML::GDOME->createDocument( undef, 'criteria', undef ); | ||
| 342 | push @{$self->{dom_stack}}, $self->{dom_doc}->documentElement; | ||
| 343 | } | ||
| 344 | elsif ( $name eq 'option' ) | ||
| 345 | { | ||
| 346 | if ( $self->{phase} == 1 ) | ||
| 347 | { | ||
| 348 | # we skip over these in phase 1 !! | ||
| 349 | return; | ||
| 350 | } | ||
| 351 | |||
| 352 | if ( $self->{prop_type} ne 'simple' or not exists $self->{prop_args}->{options} ) | ||
| 353 | { | ||
| 354 | die "<option/> tag must be inside of <options> section"; | ||
| 355 | } | ||
| 356 | |||
| 357 | my $opt = { | ||
| 358 | value => $attrs->{'{}value'}->{Value}, | ||
| 359 | description => $attrs->{'{}description'}->{Value} | ||
| 360 | }; | ||
| 361 | |||
| 362 | push @{$self->{prop_args}->{options}}, $opt; | ||
| 363 | } | ||
| 364 | elsif ( $name eq 'hints' ) | ||
| 365 | { | ||
| 366 | if ( $self->{phase} == 1 ) | ||
| 367 | { | ||
| 368 | # we skip over these in phase 1 !! | ||
| 369 | return; | ||
| 370 | } | ||
| 371 | |||
| 372 | if ( exists $self->{prop_args}->{hints} ) | ||
| 373 | { | ||
| 374 | die "Cannot nest <hints> tags"; | ||
| 375 | } | ||
| 376 | |||
| 377 | # set us up | ||
| 378 | $self->{prop_args}->{hints} = { }; | ||
| 379 | } | ||
| 380 | elsif ( $name eq 'hint' ) | ||
| 381 | { | ||
| 382 | if ( $self->{phase} == 1 ) | ||
| 383 | { | ||
| 384 | # we skip over these in phase 1 !! | ||
| 385 | return; | ||
| 386 | } | ||
| 387 | |||
| 388 | if ( not exists $self->{prop_args}->{hints} ) | ||
| 389 | { | ||
| 390 | die "<hint/> tag must be inside of <hints> section"; | ||
| 391 | } | ||
| 392 | |||
| 393 | my $name = $attrs->{'{}name'}->{Value}; | ||
| 394 | my $value = $attrs->{'{}value'}->{Value}; | ||
| 395 | |||
| 396 | if ( not defined $value ) | ||
| 397 | { | ||
| 398 | $value = 1; | ||
| 399 | } | ||
| 400 | |||
| 401 | if ( defined $self->{prop_args}->{$name} ) | ||
| 402 | { | ||
| 403 | die "Cannot defined multiple property hint's named '$name'"; | ||
| 404 | } | ||
| 405 | |||
| 406 | $self->{prop_args}->{hints}->{$name} = $value; | ||
| 407 | } | ||
| 408 | elsif ( $name eq 'key' ) | ||
| 409 | { | ||
| 410 | if ( $self->{phase} == 1 ) | ||
| 411 | { | ||
| 412 | # we skip over these in phase 1 !! | ||
| 413 | return; | ||
| 414 | } | ||
| 415 | |||
| 416 | if ( $self->{prop_type} eq 'object' ) | ||
| 417 | { | ||
| 418 | $self->{prop_args}->{key_attributes} = [ ]; | ||
| 419 | } | ||
| 420 | else | ||
| 421 | { | ||
| 422 | die "Can only put a <key/> section inside of an <object/> property."; | ||
| 423 | } | ||
| 424 | } | ||
| 425 | elsif ( $name eq 'attribute' ) | ||
| 426 | { | ||
| 427 | if ( $self->{phase} == 1 ) | ||
| 428 | { | ||
| 429 | # we skip over these in phase 1 !! | ||
| 430 | return; | ||
| 431 | } | ||
| 432 | |||
| 433 | if ( not defined $self->{prop_args}->{key_attributes} ) | ||
| 434 | { | ||
| 435 | die "<attribute/> tag must be inside of <key> section"; | ||
| 436 | } | ||
| 437 | |||
| 438 | if ( defined $attrs->{'{}name'} ) | ||
| 439 | { | ||
| 440 | push @{$self->{prop_args}->{key_attributes}}, $attrs->{'{}name'}->{Value}; | ||
| 441 | } | ||
| 442 | } | ||
| 443 | else | ||
| 444 | { | ||
| 445 | die "Unknown tag: $name"; | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | sub characters | ||
| 450 | { | ||
| 451 | my ($self, $h) = @_; | ||
| 452 | |||
| 453 | # simple alias | ||
| 454 | my $text = $h->{'Data'}; | ||
| 455 | |||
| 456 | if ( $self->{in_criteria} and $self->{phase} == 2 ) | ||
| 457 | { | ||
| 458 | # add text nodes to the object on the top of the stack | ||
| 459 | my $node = $self->{dom_doc}->createTextNode($text); | ||
| 460 | $self->{dom_stack}->[-1]->appendChild($node); | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | sub end_element | ||
| 465 | { | ||
| 466 | my ($self, $el) = @_; | ||
| 467 | |||
| 468 | # simple alias | ||
| 469 | my $name = $el->{'LocalName'}; | ||
| 470 | |||
| 471 | if ( $self->{in_criteria} ) | ||
| 472 | { | ||
| 473 | if ( $name eq 'criteria' ) | ||
| 474 | { | ||
| 475 | # exist freaky-deeky mode | ||
| 476 | $self->{in_criteria} = 0; | ||
| 477 | } | ||
| 478 | |||
| 479 | if ( $self->{phase} == 2 ) | ||
| 480 | { | ||
| 481 | # pop tag off the stack. | ||
| 482 | pop @{$self->{dom_stack}}; | ||
| 483 | |||
| 484 | # now, take care of business | ||
| 485 | if ( $name eq 'criteria' ) | ||
| 486 | { | ||
| 487 | # parse the criteria DOM and add it to the property options. | ||
| 488 | my $criteria = Xmldoom::Criteria::XML::parse_dom( $self->{dom_doc} ); | ||
| 489 | $self->{prop_args}->{options_criteria} = $criteria; | ||
| 490 | |||
| 491 | # release the dom from memory | ||
| 492 | $self->{dom_doc} = undef; | ||
| 493 | } | ||
| 494 | } | ||
| 495 | |||
| 496 | return; | ||
| 497 | } | ||
| 498 | |||
| 499 | # clear state vars... | ||
| 500 | if ( $name eq 'object' and not $self->{prop_name} ) | ||
| 501 | { | ||
| 502 | $self->{cur_obj} = undef; | ||
| 503 | $self->{ignore_obj} = 0; | ||
| 504 | } | ||
| 505 | elsif ( $name eq 'property' ) | ||
| 506 | { | ||
| 507 | $self->{prop_name} = undef; | ||
| 508 | } | ||
| 509 | elsif ( $name eq 'simple' or $name eq 'custom' or ( $name eq 'object' and $self->{prop_name} ) ) | ||
| 510 | { | ||
| 511 | if ( $self->{phase} == 1 or $self->{ignore_obj} ) | ||
| 512 | { | ||
| 513 | # we skip over these in phase 1 !! | ||
| 514 | return; | ||
| 515 | } | ||
| 516 | |||
| 517 | my $prop; | ||
| 518 | |||
| 519 | # do the specifics | ||
| 520 | if ( $name eq 'simple' ) | ||
| 521 | { | ||
| 522 | # create simple! | ||
| 523 | $prop = Xmldoom::Definition::Property::Simple->new($self->{prop_args}); | ||
| 524 | } | ||
| 525 | elsif ( $name eq 'object' ) | ||
| 526 | { | ||
| 527 | # create object! | ||
| 528 | $prop = Xmldoom::Definition::Property::Object->new($self->{prop_args}); | ||
| 529 | } | ||
| 530 | elsif ( $name eq 'custom' ) | ||
| 531 | { | ||
| 532 | my $perl_class; | ||
| 533 | |||
| 534 | # choose either the given class or use the PlaceHolder | ||
| 535 | if ( defined $self->{prop_args}->{perl_class} ) | ||
| 536 | { | ||
| 537 | $perl_class = delete $self->{prop_args}->{perl_class}; | ||
| 538 | use_module($perl_class); | ||
| 539 | } | ||
| 540 | else | ||
| 541 | { | ||
| 542 | $perl_class = 'Xmldoom::Definition::Property::PlaceHolder'; | ||
| 543 | } | ||
| 544 | |||
| 545 | # create a place holder property | ||
| 546 | $prop = $perl_class->new($self->{prop_args}); | ||
| 547 | } | ||
| 548 | |||
| 549 | # add 'er! | ||
| 550 | $self->{cur_obj}->add_property( $prop ); | ||
| 551 | |||
| 552 | # clear state | ||
| 553 | $self->{prop_type} = undef; | ||
| 554 | } | ||
| 555 | } | ||
| 556 | |||
| 557 | 1; | ||
| 558 |