| 1 | ############################################################################### | ||
| 2 | # XML::Template::Parser | ||
| 3 | # | ||
| 4 | # Copyright (c) 2003 Jonathan A. Waxman <jowaxman@bbl.med.upenn.edu> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Parser; | ||
| 11 | use base qw(XML::Template::Base); | ||
| 12 | |||
| 13 | |||
| 14 | =pod | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | XML::Template::Parser - SAX handler for parsing XML::Template documents. | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use XML::SAX::ParserFactory; | ||
| 23 | use XML::Template::Parser | ||
| 24 | |||
| 25 | my $handler = XML::Template::Parser->new (); | ||
| 26 | my $parser = XML::SAX::ParserFactory->parser (Handler => $handler); | ||
| 27 | my $code = eval { $parser->parse_string ($xml) }; | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | This module is the XML::Template document parser. It is implemented as an | ||
| 32 | XML::SAX handler. Whenever an element in a namespace that has been | ||
| 33 | configured is encountered, a subroutine is called in the Perl module | ||
| 34 | associated with the namespace. The subroutine should return Perl code | ||
| 35 | that generates the content of the element. Much of the default element | ||
| 36 | and attribute processing behavior can be modified in the XML::Template | ||
| 37 | configuration file. See L<XML::Template::Config> for more details. | ||
| 38 | |||
| 39 | =head1 CONSTRUCTOR | ||
| 40 | |||
| 41 | A constructor method C<new> is provided by L<XML::Template::Base>. A list | ||
| 42 | of named configuration parameters may be passed to the constructor. The | ||
| 43 | constructor returns a reference to a new parser object or under if an | ||
| 44 | error occurred. If undef is returned, you can use the method C<error> to | ||
| 45 | retrieve the error. For instance: | ||
| 46 | |||
| 47 | my $parser = XML::Template::Parser->new (%config) | ||
| 48 | || die XML::Template::Parser->error; | ||
| 49 | |||
| 50 | The following named configuration parameters may be passed to the | ||
| 51 | constructor: | ||
| 52 | |||
| 53 | =over 4 | ||
| 54 | |||
| 55 | =item String | ||
| 56 | |||
| 57 | A blessed reference to an object that parses strings and returns an | ||
| 58 | XML::Template Perl code representation. This value will override the | ||
| 59 | default value C<$STRING> in L<XML::Template::Config>. The default string | ||
| 60 | parser object is of the class XML::Template::Parser::String. This module | ||
| 61 | is generated from the grammar file C<XML/Template/Parser/string.grammar> | ||
| 62 | by Parse::RecDescent. To generate a new string parser, see | ||
| 63 | C<XML/Template/Parser/README>. | ||
| 64 | |||
| 65 | =back | ||
| 66 | |||
| 67 | =head1 PRIVATE METHODS | ||
| 68 | |||
| 69 | =head2 _init | ||
| 70 | |||
| 71 | This method is the internal initialization function called from | ||
| 72 | L<XML::Template::Base> when a new parser object is created. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub _init { | ||
| 77 | my $self = shift; | ||
| 78 | my %params = @_; | ||
| 79 | |||
| 80 | $self->{_string} = $params{String} || XML::Template::Config->string (%params) | ||
| 81 | || return $self->error (XML::Template::Config->error); | ||
| 82 | |||
| 83 | # XXX Should keep all this on a stack so multiple documents can be parsed from | ||
| 84 | # a single parser object - push on start_document, pop and end_document. | ||
| 85 | |||
| 86 | $self->{_namespaces} = []; | ||
| 87 | $self->{_objects} = {}; | ||
| 88 | $self->{_attribs} = []; | ||
| 89 | $self->{_code} = ['']; | ||
| 90 | $self->{_text} = ''; | ||
| 91 | $self->{_line} = 0; | ||
| 92 | $self->{_depth} = 0; | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | =pod | ||
| 98 | |||
| 99 | =head1 PUBLIC METHODS | ||
| 100 | |||
| 101 | =head2 element_string | ||
| 102 | |||
| 103 | my $text = $self->element_string ($type, $element); | ||
| 104 | |||
| 105 | This method constructs the XML for an XML::SAX element data structure. If | ||
| 106 | type is 1, attributes are generated. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub element_string { | ||
| 111 | my $self = shift; | ||
| 112 | my ($type, $element) = @_; | ||
| 113 | |||
| 114 | my $string; | ||
| 115 | if ($type == 1) { | ||
| 116 | $string = "<$element->{Name}"; | ||
| 117 | while (my ($key, $attrib) = each %{$element->{Attributes}}) { | ||
| 118 | $string .= qq{ $attrib->{Name}="$attrib->{Value}"}; | ||
| 119 | } | ||
| 120 | $string .= '>'; | ||
| 121 | |||
| 122 | } else { | ||
| 123 | $string = "</$element->{Name}>"; | ||
| 124 | } | ||
| 125 | |||
| 126 | return $string; | ||
| 127 | } | ||
| 128 | |||
| 129 | =pod | ||
| 130 | |||
| 131 | =head1 XML::SAX SUBROUTINES | ||
| 132 | |||
| 133 | =head2 start_element | ||
| 134 | |||
| 135 | This method is invoked at the beginning of every element. The following | ||
| 136 | algorithm is used to parse start elements: | ||
| 137 | |||
| 138 | - Increment depth level. | ||
| 139 | - If not skipping | ||
| 140 | - Add new namespaces to list, push onto stack. | ||
| 141 | - If namespace configured | ||
| 142 | - Check proper nesting. | ||
| 143 | - If content type is not 'xml', begin skipping at current depth | ||
| 144 | level. | ||
| 145 | - Check for missing required attributes. | ||
| 146 | - Generate code for attributes (with configured string parsers), | ||
| 147 | add to attribs hash. | ||
| 148 | - Create and cache element object, if not nested. Call constructor | ||
| 149 | with hash of defined namespaces and current namespace. | ||
| 150 | - Push attribs hash and code onto stacks. | ||
| 151 | - Else | ||
| 152 | - Generate code to print XML, append to code private data. | ||
| 153 | - Else | ||
| 154 | - Generate XML, append to text private data. | ||
| 155 | - Push element onto stack. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub start_element { | ||
| 160 | my $self = shift; | ||
| 161 | my $element = shift; | ||
| 162 | |||
| 163 | # The element type __xml is used to wrap pieces of XML that may | ||
| 164 | # not have a root element. Skip it. | ||
| 165 | return if $element->{Name} eq '__xml'; | ||
| 166 | |||
| 167 | $self->{_depth}++; | ||
| 168 | |||
| 169 | if (! defined $self->{_skip_until}) { | ||
| 170 | # Add new namespaces to list. | ||
| 171 | my %namespaces; | ||
| 172 | while (my ($key, $attrib) = each %{$element->{Attributes}}) { | ||
| 173 | if ($attrib->{Prefix} eq 'xmlns') { | ||
| 174 | $namespaces{$attrib->{LocalName}} = $attrib->{Value}; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | unshift (@{$self->{_namespaces}}, \%namespaces); | ||
| 178 | |||
| 179 | my $namespace = $element->{NamespaceURI}; | ||
| 180 | |||
| 181 | # Namespace is defined - process. | ||
| 182 | my $namespace_info = $self->get_namespace_info ($namespace); | ||
| 183 | if (defined $namespace_info) { | ||
| 184 | my $name = $element->{LocalName}; | ||
| 185 | |||
| 186 | # Get parent element name. | ||
| 187 | my $parent_element = $self->{_stack}->[0]; | ||
| 188 | |||
| 189 | # Check proper element nesting. If nested, don't need to create new | ||
| 190 | # object - use parent one. | ||
| 191 | my $create_object = 1; | ||
| 192 | my $element_info = $self->get_element_info ($namespace, $name); | ||
| 193 | if (defined $element_info->{nestedin}) { | ||
| 194 | my @nestedin_names = ref ($element_info->{nestedin}) | ||
| 195 | ? @{$element_info->{nestedin}} | ||
| 196 | : $element_info->{nestedin}; | ||
| 197 | foreach my $nestedin_name (@nestedin_names) { | ||
| 198 | if ($nestedin_name eq $parent_element->{LocalName} | ||
| 199 | && $namespace eq $parent_element->{NamespaceURI}) { | ||
| 200 | $create_object = 0; | ||
| 201 | last; | ||
| 202 | } else { | ||
| 203 | die "$self->{_line}: Element '$element->{Name}' not properly nested (nested in {$parent_element->{NamespaceURI}}$parent_element->{LocalName})"; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | # Determine content type. | ||
| 209 | $self->{_skip_until} = $self->{_depth} | ||
| 210 | if defined $element_info->{content} | ||
| 211 | && $element_info->{content} ne 'xml'; | ||
| 212 | |||
| 213 | # Check for missing required attributes. | ||
| 214 | foreach my $attrib ($self->get_attribs ($namespace, $name)) { | ||
| 215 | my $attrib_info = $self->get_attrib_info ($namespace, $name, $attrib); | ||
| 216 | if (defined $attrib_info->{required} | ||
| 217 | && $attrib_info->{required} eq 'true' | ||
| 218 | && ! exists $element->{Attributes}->{"{$namespace}$attrib"} | ||
| 219 | && ! exists $element->{Attributes}->{$attrib}) { | ||
| 220 | die "$self->{_line}: Required attribute '$attrib' not present in tag '$name'"; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | # Generate code for attributes. | ||
| 225 | my %attribs; | ||
| 226 | while (my ($key, $attrib) = each %{$element->{Attributes}}) { | ||
| 227 | if (! defined $attrib->{NamespaceURI} | ||
| 228 | || $attrib->{NamespaceURI} eq $namespace) { | ||
| 229 | my $attrib_name = defined $attrib->{NamespaceURI} | ||
| 230 | ? $attrib->{LocalName} : $attrib->{Name}; | ||
| 231 | if ($attrib->{Value} eq '') { | ||
| 232 | $attribs{$key} = '""'; | ||
| 233 | } else { | ||
| 234 | my $attrib_info = $self->get_attrib_info ($namespace, $name, $attrib_name); | ||
| 235 | if (defined $attrib_info) { | ||
| 236 | if (defined $attrib_info->{parse} | ||
| 237 | && $attrib_info->{parse} eq 'false') { | ||
| 238 | # xxx backspace special chars?? | ||
| 239 | $attribs{$key} = qq{"$attrib->{Value}"}; | ||
| 240 | } else { | ||
| 241 | if (defined $attrib_info->{parser}) { | ||
| 242 | # xxx use load () ? | ||
| 243 | my $parser = $attrib_info->{parser}; | ||
| 244 | eval "use $parser"; | ||
| 245 | die $@ if $@; | ||
| 246 | my $string_parser = $parser->new (); | ||
| 247 | $attribs{$key} = $string_parser->text ($attrib->{Value}); | ||
| 248 | } else { | ||
| 249 | $attribs{$key} = $self->{_string}->text ($attrib->{Value}); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } else { | ||
| 253 | $attribs{$key} = $self->{_string}->text ($attrib->{Value}); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | # Create and cache element object. | ||
| 260 | if ($create_object) { | ||
| 261 | # Load element module. | ||
| 262 | my $module = $namespace_info->{module}; | ||
| 263 | XML::Template::Config->load ($module) | ||
| 264 | || die $self->error (XML::Template::Config->error ()); | ||
| 265 | |||
| 266 | # Create object and push onto object stack. | ||
| 267 | my %tnamespaces; | ||
| 268 | foreach my $namespaces (@{$self->{_namespaces}}) { | ||
| 269 | while (my ($key, $val) = each %$namespaces) { | ||
| 270 | $tnamespaces{$key} = $val; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | my $object = $module->new (\%tnamespaces, $namespace); | ||
| 274 | unshift (@{$self->{_objects}->{$module}}, $object); | ||
| 275 | } | ||
| 276 | |||
| 277 | # Push attribs and new code block. | ||
| 278 | unshift (@{$self->{_attribs}}, \%attribs); | ||
| 279 | unshift (@{$self->{_code}}, ''); | ||
| 280 | |||
| 281 | } else { | ||
| 282 | my $text = $self->element_string (1, $element); | ||
| 283 | $text = $self->{_string}->text ($text); | ||
| 284 | $self->{_code}->[0] .= "\$process->print ($text);\n"; | ||
| 285 | } | ||
| 286 | |||
| 287 | } else { | ||
| 288 | my $text = $self->element_string (1, $element); | ||
| 289 | $text = $self->{_string}->text ($text); | ||
| 290 | $self->{_text} .= $text; | ||
| 291 | } | ||
| 292 | |||
| 293 | unshift (@{$self->{_stack}}, $element); | ||
| 294 | } | ||
| 295 | |||
| 296 | =pod | ||
| 297 | |||
| 298 | =head2 end_element | ||
| 299 | |||
| 300 | This method is invoked at the end of every element. The following | ||
| 301 | algorithm is used to parse end elements: | ||
| 302 | |||
| 303 | - Pop element from stack. | ||
| 304 | - If skipping should stop, retrieve collected text from private data, | ||
| 305 | stop skipping. | ||
| 306 | - If not skipping | ||
| 307 | - Pop namespace list from stack. | ||
| 308 | - If namespace configured | ||
| 309 | - If not nested, pop element object from stack, otherwise just get | ||
| 310 | it. | ||
| 311 | - Pop attribs and code from stacks. | ||
| 312 | - If content type is 'empty', call element subroutine with undef and | ||
| 313 | attribs, else if content type is 'text' call element subroutine | ||
| 314 | with skipped text and attribs, else call element subroutine with | ||
| 315 | code and attribs. | ||
| 316 | - Else | ||
| 317 | - Generate code to print XML, append to code private data. | ||
| 318 | - Else | ||
| 319 | - Generate XML, append to text private data. | ||
| 320 | - Decrement depth level. | ||
| 321 | |||
| 322 | |||
| 323 | =cut | ||
| 324 | |||
| 325 | sub end_element { | ||
| 326 | my $self = shift; | ||
| 327 | my $element = shift; | ||
| 328 | |||
| 329 | # The element type __xml is used to wrap pieces of XML that may | ||
| 330 | # not have a root element. Skip it. | ||
| 331 | return if $element->{Name} eq '__xml'; | ||
| 332 | |||
| 333 | # xxx move inside skip until block? | ||
| 334 | shift (@{$self->{_stack}}); | ||
| 335 | |||
| 336 | return if $element->{Name} eq 'br'; | ||
| 337 | |||
| 338 | my $text; | ||
| 339 | if (defined $self->{_skip_until} | ||
| 340 | && $self->{_skip_until} eq $self->{_depth}) { | ||
| 341 | undef $self->{_skip_until}; | ||
| 342 | $text = $self->{_text}; | ||
| 343 | $self->{_text} = ''; | ||
| 344 | } | ||
| 345 | |||
| 346 | if (! defined $self->{_skip_until}) { | ||
| 347 | # Get namespace info for the current element type. | ||
| 348 | # If namespace info is defined, call the element subroutine. | ||
| 349 | my $namespace = $element->{NamespaceURI}; | ||
| 350 | |||
| 351 | # Remove namespaces. | ||
| 352 | shift (@{$self->{_namespaces}}); | ||
| 353 | |||
| 354 | my $namespace_info = $self->get_namespace_info ($namespace); | ||
| 355 | if (defined $namespace_info) { | ||
| 356 | my $name = $element->{LocalName}; | ||
| 357 | |||
| 358 | # Get element object. If element is not nested, pop the object | ||
| 359 | # out of the cache. | ||
| 360 | my $object; | ||
| 361 | if (defined $self->get_element_info ($namespace, $name, 'nestedin')) { | ||
| 362 | $object = $self->{_objects}->{$namespace_info->{module}}->[0]; | ||
| 363 | } else { | ||
| 364 | $object = shift (@{$self->{_objects}->{$namespace_info->{module}}}); | ||
| 365 | } | ||
| 366 | |||
| 367 | # Pop attribs and code block. | ||
| 368 | my $attribs = shift (@{$self->{_attribs}}); | ||
| 369 | my $code = shift (@{$self->{_code}}); | ||
| 370 | |||
| 371 | # Call module sub. | ||
| 372 | my ($content) = $self->get_element_info ($namespace, $name, 'content'); | ||
| 373 | if ($content eq 'empty') { | ||
| 374 | $self->{_code}->[0] .= $object->$name (undef, $attribs); | ||
| 375 | } elsif ($content eq 'text') { | ||
| 376 | $self->{_code}->[0] .= $object->$name ($text, $attribs); | ||
| 377 | } else { | ||
| 378 | $self->{_code}->[0] .= $object->$name ($code, $attribs); | ||
| 379 | } | ||
| 380 | die $@ if $@; | ||
| 381 | |||
| 382 | undef ($object); | ||
| 383 | |||
| 384 | } else { | ||
| 385 | my $text = $self->element_string (0, $element); | ||
| 386 | $self->{_code}->[0] .= "\$process->print ('$text');\n"; | ||
| 387 | } | ||
| 388 | } else { | ||
| 389 | my $text = $self->element_string (0, $element); | ||
| 390 | $self->{_text} .= $text; | ||
| 391 | } | ||
| 392 | |||
| 393 | $self->{_depth}--; | ||
| 394 | } | ||
| 395 | |||
| 396 | =pod | ||
| 397 | |||
| 398 | =head2 characters | ||
| 399 | |||
| 400 | This method is invoked for each chunk of character data. The following | ||
| 401 | algorithm is used to parse character data: | ||
| 402 | |||
| 403 | - If not skipping | ||
| 404 | - Generate code to print text, append to code private data. | ||
| 405 | - Else | ||
| 406 | - Append character data to text private data. | ||
| 407 | |||
| 408 | =cut | ||
| 409 | |||
| 410 | sub characters { | ||
| 411 | my $self = shift; | ||
| 412 | my $chars = shift; | ||
| 413 | |||
| 414 | my $text = $chars->{Data}; | ||
| 415 | my $n = ($text =~ tr/\n//); | ||
| 416 | $self->{_line} += $n; | ||
| 417 | |||
| 418 | if (! defined $self->{_skip_until}) { | ||
| 419 | $text = $self->{_string}->text ($text); | ||
| 420 | # Force $text into a scalar context so variable returning will work | ||
| 421 | # properly. | ||
| 422 | $self->{_code}->[0] .= "\$process->print (scalar ($text));\n" if $text ne ''; | ||
| 423 | } else { | ||
| 424 | $self->{_text} .= $text; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | =pod | ||
| 429 | |||
| 430 | =head2 end_document | ||
| 431 | |||
| 432 | This method is invoked at the end of the document. It returns a string | ||
| 433 | containing the Perl code representation of the parsed XML document. | ||
| 434 | |||
| 435 | =cut | ||
| 436 | |||
| 437 | sub end_document { | ||
| 438 | my $self = shift; | ||
| 439 | my $doc = shift; | ||
| 440 | |||
| 441 | my $code = $self->{_code}->[0]; | ||
| 442 | $code = qq{ | ||
| 443 | sub { | ||
| 444 | my \$process = shift; | ||
| 445 | |||
| 446 | my \$vars = \$process->{_vars}; | ||
| 447 | $code | ||
| 448 | } | ||
| 449 | }; | ||
| 450 | |||
| 451 | return $code; | ||
| 452 | } | ||
| 453 | |||
| 454 | =pod | ||
| 455 | |||
| 456 | =head1 AUTHOR | ||
| 457 | |||
| 458 | Jonathan Waxman | ||
| 459 | <jowaxman@bbl.med.upenn.edu> | ||
| 460 | |||
| 461 | =head1 COPYRIGHT | ||
| 462 | |||
| 463 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 464 | All rights reserved. | ||
| 465 | |||
| 466 | This program is free software; you can redistribute it and/or | ||
| 467 | modify it under the same terms as Perl itself. | ||
| 468 | |||
| 469 | =cut | ||
| 470 | |||
| 471 | |||
| 472 | 1; |