| 1 | |||
| 2 | package Xmldoom::Criteria::XML; | ||
| 3 | |||
| 4 | use Xmldoom::Criteria; | ||
| 5 | use Xmldoom::Criteria::UnknownObject; | ||
| 6 | use DBIx::Romani::Query::XML::Util qw(get_element_text); | ||
| 7 | use XML::GDOME; | ||
| 8 | use XML::DOM; | ||
| 9 | use strict; | ||
| 10 | |||
| 11 | use Data::Dumper; | ||
| 12 | |||
| 13 | sub parse_string | ||
| 14 | { | ||
| 15 | my $xml = shift; | ||
| 16 | my $database = shift; | ||
| 17 | |||
| 18 | my $doc = XML::GDOME->createDocFromString( $xml ); | ||
| 19 | |||
| 20 | return Xmldoom::Criteria::XML::parse_dom( $doc, $database ); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub parse_dom | ||
| 24 | { | ||
| 25 | my $doc = shift; | ||
| 26 | my $database = shift; | ||
| 27 | |||
| 28 | return Xmldoom::Criteria::XML::create_criteria_from_node( $doc->getDocumentElement(), $database ); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub create_criteria_from_node | ||
| 32 | { | ||
| 33 | my $parent_node = shift; | ||
| 34 | my $database = shift; | ||
| 35 | |||
| 36 | # this will grab the Criteria parent if such a section exists | ||
| 37 | my $parent = Xmldoom::Criteria::XML::_parse_parent_section($parent_node, $database); | ||
| 38 | |||
| 39 | my $criteria = Xmldoom::Criteria->new($parent); | ||
| 40 | |||
| 41 | my $limit = $parent_node->getAttribute('limit'); | ||
| 42 | my $offset = $parent_node->getAttribute('offset'); | ||
| 43 | |||
| 44 | if ( (defined $limit and $limit ne "") or | ||
| 45 | (defined $offset and $offset ne "") ) | ||
| 46 | { | ||
| 47 | $criteria->set_limit($limit, $offset); | ||
| 48 | } | ||
| 49 | |||
| 50 | my $has_constraints = 0; | ||
| 51 | my $has_order_by = 0; | ||
| 52 | my $has_group_by = 0; | ||
| 53 | |||
| 54 | my $node = $parent_node->getFirstChild(); | ||
| 55 | while ( defined $node ) | ||
| 56 | { | ||
| 57 | if ( $node->getNodeType() == XML::DOM::ELEMENT_NODE ) | ||
| 58 | { | ||
| 59 | my $name = $node->getTagName(); | ||
| 60 | |||
| 61 | if ( $name eq 'constraints' ) | ||
| 62 | { | ||
| 63 | if ( not $has_constraints ) | ||
| 64 | { | ||
| 65 | _parse_constraints_section( $criteria, $node ); | ||
| 66 | $has_constraints = 1; | ||
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | die "Cannot have multiple constraints sections"; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | elsif ( $name eq 'order-by' ) | ||
| 74 | { | ||
| 75 | if ( not $has_order_by ) | ||
| 76 | { | ||
| 77 | _parse_order_by_section( $criteria, $node ); | ||
| 78 | $has_order_by = 1; | ||
| 79 | } | ||
| 80 | else | ||
| 81 | { | ||
| 82 | die "Cannot have multiple order-by sections"; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | elsif ( $name eq 'group-by' ) | ||
| 86 | { | ||
| 87 | if ( not $has_group_by ) | ||
| 88 | { | ||
| 89 | _parse_group_by_section( $criteria, $node ); | ||
| 90 | $has_group_by = 1; | ||
| 91 | } | ||
| 92 | else | ||
| 93 | { | ||
| 94 | die "Cannot have multiple group-by sections"; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | elsif ( $name eq 'parent' ) | ||
| 98 | { | ||
| 99 | # Already processed this section in advance... | ||
| 100 | } | ||
| 101 | else | ||
| 102 | { | ||
| 103 | die "Unknown criteria section: $name"; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | $node = $node->getNextSibling(); | ||
| 108 | } | ||
| 109 | |||
| 110 | return $criteria; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _parse_parent_section | ||
| 114 | { | ||
| 115 | my $top_node = shift; | ||
| 116 | my $database = shift; | ||
| 117 | |||
| 118 | # try to find the parent section | ||
| 119 | my $parent_node = $top_node->getFirstChild(); | ||
| 120 | while ( defined $parent_node ) | ||
| 121 | { | ||
| 122 | if ( $parent_node->getNodeType() == XML::DOM::ELEMENT_NODE && | ||
| 123 | $parent_node->getTagName() eq 'parent' ) | ||
| 124 | { | ||
| 125 | last; | ||
| 126 | } | ||
| 127 | |||
| 128 | $parent_node = $parent_node->getNextSibling(); | ||
| 129 | } | ||
| 130 | |||
| 131 | # check it out | ||
| 132 | if ( not defined $parent_node ) | ||
| 133 | { | ||
| 134 | return undef; | ||
| 135 | } | ||
| 136 | elsif ( not defined $database ) | ||
| 137 | { | ||
| 138 | die "Cannot parse a criteria with a <parent/> section if you don't pass the database object into the parser"; | ||
| 139 | } | ||
| 140 | |||
| 141 | my $object_name = $parent_node->getAttribute('object_name'); | ||
| 142 | my $definition = $database->get_object($object_name); | ||
| 143 | |||
| 144 | my $load_args = { }; | ||
| 145 | |||
| 146 | # find the node with the keys in it | ||
| 147 | my $key_node = $parent_node->getFirstChild(); | ||
| 148 | while ( defined $key_node ) | ||
| 149 | { | ||
| 150 | if ( $key_node->getNodeType() == XML::DOM::ELEMENT_NODE && | ||
| 151 | $key_node->getTagName() eq 'key' ) | ||
| 152 | { | ||
| 153 | last; | ||
| 154 | } | ||
| 155 | |||
| 156 | $key_node = $key_node->getNextSibling(); | ||
| 157 | } | ||
| 158 | |||
| 159 | if ( not defined $key_node ) | ||
| 160 | { | ||
| 161 | die "No key given for <parent/> object of <criteria/>"; | ||
| 162 | } | ||
| 163 | |||
| 164 | # get all the attributes into our load hash | ||
| 165 | my $attrs = $key_node->getAttributes(); | ||
| 166 | for( my $i = 0; $i < $attrs->getLength(); $i++ ) | ||
| 167 | { | ||
| 168 | my $attr = $attrs->item($i); | ||
| 169 | $load_args->{$attr->getName()} = $attr->getValue(); | ||
| 170 | } | ||
| 171 | |||
| 172 | # actually load the object | ||
| 173 | my $object = $definition->class_load( $load_args ); | ||
| 174 | |||
| 175 | return $object; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub _parse_constraints_section | ||
| 179 | { | ||
| 180 | my ($criteria, $parent_node) = @_; | ||
| 181 | |||
| 182 | my $node = $parent_node->getFirstChild(); | ||
| 183 | while ( defined $node ) | ||
| 184 | { | ||
| 185 | if ( $node->getNodeType() == XML::DOM::ELEMENT_NODE ) | ||
| 186 | { | ||
| 187 | my $name = $node->getTagName(); | ||
| 188 | |||
| 189 | if ( $name eq 'property' or $name eq 'attribute' ) | ||
| 190 | { | ||
| 191 | my $prop_name = $node->getAttribute('name'); | ||
| 192 | my $cons = _parse_constraint_value( $node ); | ||
| 193 | |||
| 194 | if ( $name eq 'property' ) | ||
| 195 | { | ||
| 196 | $criteria->add_prop( $prop_name, $cons->{value}, $cons->{type} ); | ||
| 197 | } | ||
| 198 | else | ||
| 199 | { | ||
| 200 | $criteria->add_attr( $prop_name, $cons->{value}, $cons->{type} ); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | elsif ( $name eq 'join-properties' ) | ||
| 204 | { | ||
| 205 | my $attr_name1 = $node->getAttribute('name1'); | ||
| 206 | my $attr_name2 = $node->getAttribute('name2'); | ||
| 207 | |||
| 208 | $criteria->join_prop( $attr_name1, $attr_name2 ); | ||
| 209 | } | ||
| 210 | elsif ( $name eq 'join-attributes' ) | ||
| 211 | { | ||
| 212 | my $attr_name1 = $node->getAttribute('name1'); | ||
| 213 | my $attr_name2 = $node->getAttribute('name2'); | ||
| 214 | |||
| 215 | $criteria->join_attr( $attr_name1, $attr_name2 ); | ||
| 216 | } | ||
| 217 | elsif ( $name eq 'and' or $name eq 'or' ) | ||
| 218 | { | ||
| 219 | if ( $name eq 'and' and $criteria->get_type() eq 'AND' ) | ||
| 220 | { | ||
| 221 | # if the criteria is already of the AND type, then we don't need to | ||
| 222 | # put this is another section. | ||
| 223 | _parse_constraints_section( $criteria, $node ); | ||
| 224 | } | ||
| 225 | else | ||
| 226 | { | ||
| 227 | my $search; | ||
| 228 | |||
| 229 | if ( $name eq 'and' ) | ||
| 230 | { | ||
| 231 | $search = Xmldoom::Criteria::Search->new( $Xmldoom::Criteria::AND ); | ||
| 232 | } | ||
| 233 | else | ||
| 234 | { | ||
| 235 | $search = Xmldoom::Criteria::Search->new( $Xmldoom::Criteria::OR ); | ||
| 236 | } | ||
| 237 | |||
| 238 | _parse_constraints_section( $search, $node ); | ||
| 239 | $criteria->add( $search ); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | else | ||
| 243 | { | ||
| 244 | die "Unknown constraint tag: $name"; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | $node = $node->getNextSibling(); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | sub _parse_order_by_section | ||
| 253 | { | ||
| 254 | my ($criteria, $parent_node) = @_; | ||
| 255 | |||
| 256 | my $node = $parent_node->getFirstChild(); | ||
| 257 | while ( defined $node ) | ||
| 258 | { | ||
| 259 | if ( $node->getNodeType() == XML::DOM::ELEMENT_NODE ) | ||
| 260 | { | ||
| 261 | my $tag_name = $node->getTagName(); | ||
| 262 | |||
| 263 | my $name = $node->getAttribute("name"); | ||
| 264 | my $dir = $node->getAttribute("dir") || undef; | ||
| 265 | |||
| 266 | if ( $tag_name eq 'property' ) | ||
| 267 | { | ||
| 268 | $criteria->add_order_by_prop( $name, $dir ); | ||
| 269 | } | ||
| 270 | elsif ( $tag_name eq 'attribute' ) | ||
| 271 | { | ||
| 272 | $criteria->add_order_by_attr( $name, $dir ); | ||
| 273 | } | ||
| 274 | else | ||
| 275 | { | ||
| 276 | die "Unknown order-by tag: $name"; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | $node = $node->getNextSibling(); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | sub _parse_group_by_section | ||
| 285 | { | ||
| 286 | my ($criteria, $parent_node) = @_; | ||
| 287 | |||
| 288 | my $node = $parent_node->getFirstChild(); | ||
| 289 | while ( defined $node ) | ||
| 290 | { | ||
| 291 | if ( $node->getNodeType() == XML::DOM::ELEMENT_NODE ) | ||
| 292 | { | ||
| 293 | my $tag_name = $node->getTagName(); | ||
| 294 | |||
| 295 | my $name = $node->getAttribute("name"); | ||
| 296 | |||
| 297 | if ( $tag_name eq 'property' ) | ||
| 298 | { | ||
| 299 | $criteria->add_group_by_prop( $name ); | ||
| 300 | } | ||
| 301 | elsif ( $tag_name eq 'attribute' ) | ||
| 302 | { | ||
| 303 | $criteria->add_group_by_attr( $name ); | ||
| 304 | } | ||
| 305 | else | ||
| 306 | { | ||
| 307 | die "Unknown order-by tag: $name"; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | |||
| 311 | $node = $node->getNextSibling(); | ||
| 312 | } | ||
| 313 | } | ||
| 314 | |||
| 315 | sub _parse_constraint_value | ||
| 316 | { | ||
| 317 | my $parent_node = shift; | ||
| 318 | |||
| 319 | my $type; | ||
| 320 | my $value; | ||
| 321 | |||
| 322 | my $node = $parent_node->getFirstChild(); | ||
| 323 | while ( defined $node ) | ||
| 324 | { | ||
| 325 | if ( $node->getNodeType() == XML::DOM::ELEMENT_NODE ) | ||
| 326 | { | ||
| 327 | my $name = $node->getTagName(); | ||
| 328 | |||
| 329 | if ( $name eq 'in' or $name eq 'not-in' or $name eq 'between' ) | ||
| 330 | { | ||
| 331 | if ( $name eq 'between' ) | ||
| 332 | { | ||
| 333 | $type = $Xmldoom::Criteria::BETWEEN; | ||
| 334 | $value = [ | ||
| 335 | $node->getAttribute('min'), | ||
| 336 | $node->getAttribute('max'), | ||
| 337 | ]; | ||
| 338 | } | ||
| 339 | else | ||
| 340 | { | ||
| 341 | if ( $name eq 'in' ) | ||
| 342 | { | ||
| 343 | $type = $Xmldoom::Criteria::IN; | ||
| 344 | } | ||
| 345 | elsif ( $name eq 'not-in' ) | ||
| 346 | { | ||
| 347 | $type = $Xmldoom::Criteria::NOT_IN; | ||
| 348 | } | ||
| 349 | |||
| 350 | $value = _parse_value_list( $node ); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | else | ||
| 354 | { | ||
| 355 | if ( $name eq 'equal' ) | ||
| 356 | { | ||
| 357 | $type = $Xmldoom::Criteria::EQUAL; | ||
| 358 | } | ||
| 359 | elsif ( $name eq 'not-equal' ) | ||
| 360 | { | ||
| 361 | $type = $Xmldoom::Criteria::NOT_EQUAL; | ||
| 362 | } | ||
| 363 | elsif ( $name eq 'greater-than' ) | ||
| 364 | { | ||
| 365 | $type = $Xmldoom::Criteria::GREATER_THAN; | ||
| 366 | } | ||
| 367 | elsif ( $name eq 'greater-equal' ) | ||
| 368 | { | ||
| 369 | $type = $Xmldoom::Criteria::GREATER_EQUAL; | ||
| 370 | } | ||
| 371 | elsif ( $name eq 'less-than' ) | ||
| 372 | { | ||
| 373 | $type = $Xmldoom::Criteria::LESS_THAN; | ||
| 374 | } | ||
| 375 | elsif ( $name eq 'less-equal' ) | ||
| 376 | { | ||
| 377 | $type = $Xmldoom::Criteria::LESS_EQUAL; | ||
| 378 | } | ||
| 379 | elsif ( $name eq 'like' ) | ||
| 380 | { | ||
| 381 | $type = $Xmldoom::Criteria::LIKE; | ||
| 382 | } | ||
| 383 | elsif ( $name eq 'not-like' ) | ||
| 384 | { | ||
| 385 | $type = $Xmldoom::Criteria::NOT_LIKE; | ||
| 386 | } | ||
| 387 | elsif ( $name eq 'is-null' ) | ||
| 388 | { | ||
| 389 | $type = $Xmldoom::Criteria::IS_NULL; | ||
| 390 | } | ||
| 391 | elsif ( $name eq 'is-not-null' ) | ||
| 392 | { | ||
| 393 | $type = $Xmldoom::Criteria::IS_NOT_NULL; | ||
| 394 | } | ||
| 395 | else | ||
| 396 | { | ||
| 397 | die "Unknown comparison type: $name"; | ||
| 398 | } | ||
| 399 | |||
| 400 | if ( ($name eq 'equal' or $name eq 'not-equal') and | ||
| 401 | defined $node->getFirstChild() and | ||
| 402 | $node->getFirstChild()->getNodeType == XML::DOM::ELEMENT_NODE ) | ||
| 403 | { | ||
| 404 | $value = _parse_object( $node ); | ||
| 405 | } | ||
| 406 | elsif ( $type eq $Xmldoom::Criteria::IS_NULL or | ||
| 407 | $type eq $Xmldoom::Criteria::IS_NOT_NULL ) | ||
| 408 | { | ||
| 409 | # doen't do nothing because these can't take values | ||
| 410 | } | ||
| 411 | else | ||
| 412 | { | ||
| 413 | $value = get_element_text( $node ); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 418 | $node = $node->getNextSibling(); | ||
| 419 | } | ||
| 420 | |||
| 421 | #if ( $value eq '' ) | ||
| 422 | #{ | ||
| 423 | # $value = undef; | ||
| 424 | #} | ||
| 425 | |||
| 426 | return { type => $type, value => $value }; | ||
| 427 | } | ||
| 428 | |||
| 429 | sub _parse_object | ||
| 430 | { | ||
| 431 | my $parent_node = shift; | ||
| 432 | |||
| 433 | my $node = $parent_node->getFirstChild(); | ||
| 434 | if ( $node->getTagName() ne 'object' ) | ||
| 435 | { | ||
| 436 | die sprintf "%s tag can only contain text or an <object/> tag", $parent_node->getTagName(); | ||
| 437 | } | ||
| 438 | |||
| 439 | my %info; | ||
| 440 | |||
| 441 | my $attrs = $node->getAttributes(); | ||
| 442 | for( my $i = 0; $i < $attrs->getLength(); $i++ ) | ||
| 443 | { | ||
| 444 | my $attr = $attrs->item($i); | ||
| 445 | |||
| 446 | $info{$attr->getName()} = $attr->getValue(); | ||
| 447 | } | ||
| 448 | |||
| 449 | return Xmldoom::Criteria::UnknownObject->new( \%info ); | ||
| 450 | } | ||
| 451 | |||
| 452 | sub _parse_value_list | ||
| 453 | { | ||
| 454 | my $parent_node = shift; | ||
| 455 | |||
| 456 | my $node = $parent_node->getFirstChild(); | ||
| 457 | my @values; | ||
| 458 | |||
| 459 | while ( defined $node ) | ||
| 460 | { | ||
| 461 | if ( $node->getNodeType() == XML::DOM::ELEMENT_NODE ) | ||
| 462 | { | ||
| 463 | if ( $node->getTagName() eq 'value' ) | ||
| 464 | { | ||
| 465 | push @values, get_element_text( $node ); | ||
| 466 | } | ||
| 467 | else | ||
| 468 | { | ||
| 469 | die sprintf "Can only list <value/> tags inside of %s tag", $parent_node->getTagName(); | ||
| 470 | } | ||
| 471 | } | ||
| 472 | |||
| 473 | $node = $node->getNextSibling(); | ||
| 474 | } | ||
| 475 | |||
| 476 | return \@values; | ||
| 477 | } | ||
| 478 | |||
| 479 | 1; | ||
| 480 |