| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Util::XmlMapper; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Util::XmlMapper - xml file parser | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | my $mapper = Wombat::Util::XmlMapper->new(); | ||
| 14 | $mapper->setValidating(1); | ||
| 15 | |||
| 16 | $mapper->addRule('Server', $mapper->objectCreate('Wombat::Core::Server')); | ||
| 17 | $mapper->addRule('Server', $mapper->setProperties()); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Configures a set of actions to take while parsing an XML file. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | use fields qw(attributes body elements objects rules); | ||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | use XML::Parser::PerlSAX (); | ||
| 30 | use Wombat::Exception (); | ||
| 31 | use Wombat::Util::XmlAction (); | ||
| 32 | |||
| 33 | =pod | ||
| 34 | |||
| 35 | =head1 CONSTRUCTOR | ||
| 36 | |||
| 37 | =over | ||
| 38 | |||
| 39 | =item new() | ||
| 40 | |||
| 41 | Create and return an instance, initializing fields to default values. | ||
| 42 | |||
| 43 | =back | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my $self = shift; | ||
| 49 | |||
| 50 | $self = fields::new($self) unless ref $self; | ||
| 51 | |||
| 52 | $self->{attributes} = []; | ||
| 53 | $self->{body} = ''; | ||
| 54 | $self->{elements} = []; | ||
| 55 | $self->{objects} = []; | ||
| 56 | $self->{rules} = {}; | ||
| 57 | |||
| 58 | return $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | =pod | ||
| 62 | |||
| 63 | =head1 PUBLIC METHODS | ||
| 64 | |||
| 65 | =over | ||
| 66 | |||
| 67 | =item addRule($xpath, $action) | ||
| 68 | |||
| 69 | Add a rule that specifies an action to be taken for each node with the | ||
| 70 | given xpath. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub addRule { | ||
| 75 | my $self = shift; | ||
| 76 | my $xpath = shift; | ||
| 77 | my $action = shift; | ||
| 78 | |||
| 79 | return 1 unless $xpath && $action; | ||
| 80 | |||
| 81 | $self->{rules}->{$xpath} ||= []; | ||
| 82 | push @{ $self->{rules}->{$xpath} }, $action; | ||
| 83 | |||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | =pod | ||
| 88 | |||
| 89 | =item objectCreate($class, [$attribute]) | ||
| 90 | |||
| 91 | Return an action that creates an object and adds it to the stack. If | ||
| 92 | an attribute is specified, that attribute's value will be used as the | ||
| 93 | class name; otherwise the specified class will be defaulted to. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub objectCreate { | ||
| 98 | my $self = shift; | ||
| 99 | my $class = shift; | ||
| 100 | my $attr = shift; | ||
| 101 | |||
| 102 | return Wombat::Util::XmlMapper::ObjectCreateAction->new($class, $attr); | ||
| 103 | } | ||
| 104 | |||
| 105 | =pod | ||
| 106 | |||
| 107 | =item setProperties() | ||
| 108 | |||
| 109 | Return an action that calls a property setter method on the object at | ||
| 110 | the top of the stack for each attribute in the curent element. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub setProperties { | ||
| 115 | my $self = shift; | ||
| 116 | |||
| 117 | return Wombat::Util::XmlMapper::SetPropertiesAction->new(); | ||
| 118 | } | ||
| 119 | |||
| 120 | =pod | ||
| 121 | |||
| 122 | =item addChild($method) | ||
| 123 | |||
| 124 | Return an action that uses the named method to add the object at the | ||
| 125 | top the stack as a child of the object just below it in the stack. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub addChild { | ||
| 130 | my $self = shift; | ||
| 131 | my $method = shift; | ||
| 132 | |||
| 133 | return Wombat::Util::XmlMapper::AddChildAction->new($method); | ||
| 134 | } | ||
| 135 | |||
| 136 | =pod | ||
| 137 | |||
| 138 | =item methodSetter($method, [$num]) | ||
| 139 | |||
| 140 | Return an action that calls the named method on the object at the top | ||
| 141 | of the stack. Method parameters are taken from the specified number of | ||
| 142 | sub-elements of the current element, or from the body of the current | ||
| 143 | element if 0 sub-elements are specified. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub methodSetter { | ||
| 148 | my $self = shift; | ||
| 149 | my $method = shift; | ||
| 150 | my $num = shift; | ||
| 151 | |||
| 152 | return Wombat::Util::XmlMapper::MethodSetterAction->new($method, $num); | ||
| 153 | } | ||
| 154 | |||
| 155 | =pod | ||
| 156 | |||
| 157 | =item methodParam($index, [$attribute]) | ||
| 158 | |||
| 159 | Return an action that extracts the value for the named attribute on | ||
| 160 | the current element, or the body of the current element if no | ||
| 161 | attribute is provided, and sets it in the array at the top of the | ||
| 162 | stack ath tindicated index. Use this method to accumulate parameter | ||
| 163 | values for a previously called C<methodSetter()> action. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub methodParam { | ||
| 168 | my $self = shift; | ||
| 169 | my $index = shift; | ||
| 170 | my $attribute = shift; | ||
| 171 | |||
| 172 | return Wombat::Util::XmlMapper::MethodParamAction->new($index, | ||
| 173 | $attribute); | ||
| 174 | } | ||
| 175 | |||
| 176 | =pod | ||
| 177 | |||
| 178 | =item readXml($fh. [$root]) | ||
| 179 | |||
| 180 | Read and parse an XML stream and perform all configured | ||
| 181 | actions. Return the root of the generated object hierarchy, optionally | ||
| 182 | using the provided root object. | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub readXml { | ||
| 187 | my $self = shift; | ||
| 188 | my $fh = shift; | ||
| 189 | my $root = shift; | ||
| 190 | |||
| 191 | push @{ $self->{objects} }, $root if $root; | ||
| 192 | |||
| 193 | eval { | ||
| 194 | my $parser = XML::Parser::PerlSAX->new(Handler => $self); | ||
| 195 | $parser->parse(Source => {ByteStream => $fh}); | ||
| 196 | }; | ||
| 197 | Wombat::XmlException->throw($@) if $@; | ||
| 198 | |||
| 199 | return $self->{objects}->[0]; | ||
| 200 | } | ||
| 201 | |||
| 202 | =pod | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | # private methods | ||
| 209 | |||
| 210 | sub match { | ||
| 211 | my $self = shift; | ||
| 212 | |||
| 213 | my $xpath = join '/', @{ $self->{elements} }; | ||
| 214 | my @matches = $self->{rules}->{$xpath} ? | ||
| 215 | @{ $self->{rules}->{$xpath} } : | ||
| 216 | (); | ||
| 217 | |||
| 218 | return wantarray ? @matches : \@matches; | ||
| 219 | } | ||
| 220 | |||
| 221 | # private SAX handler methods | ||
| 222 | |||
| 223 | sub start_element { | ||
| 224 | my $self = shift; | ||
| 225 | my $props = shift; | ||
| 226 | |||
| 227 | push @{ $self->{elements} }, $props->{Name}; | ||
| 228 | push @{ $self->{attributes} }, $props->{Attributes}; | ||
| 229 | |||
| 230 | my @matches = $self->match(); | ||
| 231 | for my $action (@matches) { | ||
| 232 | $action->start($self); | ||
| 233 | } | ||
| 234 | |||
| 235 | $self->{body} = ''; | ||
| 236 | |||
| 237 | return 1; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub end_element { | ||
| 241 | my $self = shift; | ||
| 242 | my $props = shift; | ||
| 243 | |||
| 244 | my @matches = $self->match(); | ||
| 245 | for my $action (@matches) { | ||
| 246 | $action->end($self); | ||
| 247 | } | ||
| 248 | for my $action (@matches) { | ||
| 249 | $action->cleanup($self); | ||
| 250 | } | ||
| 251 | |||
| 252 | pop @{ $self->{elements} }; | ||
| 253 | pop @{ $self->{attributes} }; | ||
| 254 | |||
| 255 | return 1; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub characters { | ||
| 259 | my $self = shift; | ||
| 260 | my $props = shift; | ||
| 261 | |||
| 262 | $self->{body} .= $props->{Data}; | ||
| 263 | |||
| 264 | return 1; | ||
| 265 | } | ||
| 266 | |||
| 267 | package Wombat::Util::XmlMapper::ObjectCreateAction; | ||
| 268 | |||
| 269 | use base qw(Wombat::Util::XmlAction); | ||
| 270 | use fields qw(attribute class); | ||
| 271 | |||
| 272 | sub new { | ||
| 273 | my $self = shift; | ||
| 274 | my $class = shift; | ||
| 275 | my $attribute = shift; | ||
| 276 | |||
| 277 | $self = fields::new($self) unless ref $self; | ||
| 278 | $self->SUPER::new(); | ||
| 279 | |||
| 280 | $self->{attribute} = $attribute; | ||
| 281 | $self->{class} = $class; | ||
| 282 | |||
| 283 | return $self; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub start { | ||
| 287 | my $self = shift; | ||
| 288 | my $mapper = shift; | ||
| 289 | |||
| 290 | my $clazz; | ||
| 291 | if ($self->{attribute}) { | ||
| 292 | my $attrs = $mapper->{attributes}; | ||
| 293 | my $top = @$attrs - 1; | ||
| 294 | $clazz = $attrs->[$top]->{$self->{attribute}} if $top >= 0; | ||
| 295 | } | ||
| 296 | $clazz ||= $self->{class}; | ||
| 297 | |||
| 298 | unless ($clazz) { | ||
| 299 | my $elements = $mapper->{elements}; | ||
| 300 | my $eltop = (keys %$elements) - 1; | ||
| 301 | my $msg = | ||
| 302 | "no class found for object create action [$elements->[$eltop]]"; | ||
| 303 | Wombat::ConfigException->throw($msg); | ||
| 304 | } | ||
| 305 | |||
| 306 | eval "require $clazz"; | ||
| 307 | Wombat::ConfigException->throw($@) if $@; | ||
| 308 | |||
| 309 | my $obj = eval { $clazz->new() }; | ||
| 310 | Wombat::ConfigException->throw($@) if $@; | ||
| 311 | |||
| 312 | push @{ $mapper->{objects} }, $obj; | ||
| 313 | |||
| 314 | return 1; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub cleanup { | ||
| 318 | my $self = shift; | ||
| 319 | my $mapper = shift; | ||
| 320 | |||
| 321 | # leave the "root" object on | ||
| 322 | pop @{ $mapper->{objects} } unless @{ $mapper->{objects} } == 1; | ||
| 323 | |||
| 324 | return 1; | ||
| 325 | } | ||
| 326 | |||
| 327 | package Wombat::Util::XmlMapper::SetPropertiesAction; | ||
| 328 | |||
| 329 | use base qw(Wombat::Util::XmlAction); | ||
| 330 | |||
| 331 | sub start { | ||
| 332 | my $self = shift; | ||
| 333 | my $mapper = shift; | ||
| 334 | |||
| 335 | my $objtop = @{ $mapper->{objects} } - 1; | ||
| 336 | my $obj = $mapper->{objects}->[$objtop]; | ||
| 337 | return 1 unless $obj; | ||
| 338 | |||
| 339 | my $attrtop = @{ $mapper->{attributes} } - 1; | ||
| 340 | my $attrs = $mapper->{attributes}->[$attrtop]; | ||
| 341 | return 1 unless $attrs; | ||
| 342 | |||
| 343 | while (my ($name, $val) = each %$attrs) { | ||
| 344 | my $setter = 'set' . ucfirst $name; | ||
| 345 | $obj->$setter($self->trim($val)) if $obj->can($setter); | ||
| 346 | } | ||
| 347 | |||
| 348 | return 1; | ||
| 349 | } | ||
| 350 | |||
| 351 | package Wombat::Util::XmlMapper::AddChildAction; | ||
| 352 | |||
| 353 | use base qw(Wombat::Util::XmlAction); | ||
| 354 | use fields qw(method); | ||
| 355 | |||
| 356 | sub new { | ||
| 357 | my $self = shift; | ||
| 358 | my $method = shift; | ||
| 359 | |||
| 360 | $self = fields::new($self) unless ref $self; | ||
| 361 | $self->SUPER::new(); | ||
| 362 | |||
| 363 | $self->{method} = $method; | ||
| 364 | |||
| 365 | return $self; | ||
| 366 | } | ||
| 367 | |||
| 368 | sub end { | ||
| 369 | my $self = shift; | ||
| 370 | my $mapper = shift; | ||
| 371 | |||
| 372 | my $top = @{ $mapper->{objects} } - 1; | ||
| 373 | my $obj = $mapper->{objects}->[$top]; | ||
| 374 | my $parent = $mapper->{objects}->[$top - 1]; | ||
| 375 | |||
| 376 | my $meth = $self->{method}; | ||
| 377 | $parent->can($meth) or | ||
| 378 | Wombat::ConfigException->throw("no method $meth for parent $parent"); | ||
| 379 | $parent->$meth($obj); | ||
| 380 | |||
| 381 | return 1; | ||
| 382 | } | ||
| 383 | |||
| 384 | package Wombat::Util::XmlMapper::MethodSetterAction; | ||
| 385 | |||
| 386 | use base qw(Wombat::Util::XmlAction); | ||
| 387 | use fields qw(method numParams); | ||
| 388 | |||
| 389 | sub new { | ||
| 390 | my $self = shift; | ||
| 391 | my $method = shift; | ||
| 392 | my $numParams = shift; | ||
| 393 | |||
| 394 | $self = fields::new($self) unless ref $self; | ||
| 395 | $self->SUPER::new(); | ||
| 396 | |||
| 397 | $self->{method} = $method; | ||
| 398 | $self->{numParams} = $numParams; | ||
| 399 | |||
| 400 | return $self; | ||
| 401 | } | ||
| 402 | |||
| 403 | sub start { | ||
| 404 | my $self = shift; | ||
| 405 | my $mapper = shift; | ||
| 406 | |||
| 407 | push @{ $mapper->{objects} }, [] if $self->{numParams}; | ||
| 408 | |||
| 409 | return 1; | ||
| 410 | } | ||
| 411 | |||
| 412 | sub end { | ||
| 413 | my $self = shift; | ||
| 414 | my $mapper = shift; | ||
| 415 | |||
| 416 | my $params = $self->{numParams} ? | ||
| 417 | pop @{ $mapper->{objects} } : | ||
| 418 | [ $mapper->{body} ]; | ||
| 419 | |||
| 420 | my $top = @{ $mapper->{objects} } - 1; | ||
| 421 | my $parent = $mapper->{objects}->[$top]; | ||
| 422 | return 1 unless $parent; | ||
| 423 | |||
| 424 | my $meth = $self->{method}; | ||
| 425 | $parent->can($meth) or | ||
| 426 | Wombat::ConfigException->throw("no method $meth for parent $parent"); | ||
| 427 | $parent->$meth(map { $self->trim($_) } @$params); | ||
| 428 | |||
| 429 | return 1; | ||
| 430 | } | ||
| 431 | |||
| 432 | package Wombat::Util::XmlMapper::MethodParamAction; | ||
| 433 | |||
| 434 | use base qw(Wombat::Util::XmlAction); | ||
| 435 | use fields qw(attribute index); | ||
| 436 | |||
| 437 | sub new { | ||
| 438 | my $self = shift; | ||
| 439 | my $index = shift; | ||
| 440 | my $attribute = shift; | ||
| 441 | |||
| 442 | $self = fields::new($self) unless ref $self; | ||
| 443 | $self->SUPER::new(); | ||
| 444 | |||
| 445 | $self->{attribute} = $attribute; | ||
| 446 | $self->{index} = $index; | ||
| 447 | |||
| 448 | return $self; | ||
| 449 | } | ||
| 450 | |||
| 451 | sub start { | ||
| 452 | my $self = shift; | ||
| 453 | my $mapper = shift; | ||
| 454 | |||
| 455 | return 1 unless $self->{attribute}; | ||
| 456 | |||
| 457 | my $top = @{ $mapper->{objects} } - 1; | ||
| 458 | my $array = $mapper->{objects}->[$top]; | ||
| 459 | return 1 unless $array && ref $array eq 'ARRAY'; | ||
| 460 | |||
| 461 | my $attrtop = @{ $mapper->{attributes} } - 1; | ||
| 462 | my $attr = $mapper->{attributes}->[$attrtop]; | ||
| 463 | return 1 unless $attr; | ||
| 464 | |||
| 465 | $array->[$self->{index}] = $self->trim($attr->{$self->{attribute}}); | ||
| 466 | |||
| 467 | return 1; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub end { | ||
| 471 | my $self = shift; | ||
| 472 | my $mapper = shift; | ||
| 473 | |||
| 474 | return 1 if $self->{attribute}; | ||
| 475 | |||
| 476 | my $top = @{ $mapper->{objects} } - 1; | ||
| 477 | my $array = $mapper->{objects}->[$top]; | ||
| 478 | return 1 unless $array && ref $array eq 'ARRAY'; | ||
| 479 | |||
| 480 | $array->[$self->{index}] = $self->trim($mapper->{body}); | ||
| 481 | |||
| 482 | return 1; | ||
| 483 | } | ||
| 484 | |||
| 485 | 1; | ||
| 486 | __END__ |