| 1 | # | ||
| 2 | # XML::Writer version v 0.4 | ||
| 3 | # (renamed into Uplug::XML::Writer for compatibility reasons) | ||
| 4 | # | ||
| 5 | |||
| 6 | ######################################################################## | ||
| 7 | # Writer.pm - write an XML document. | ||
| 8 | # Copyright (c) 1999 by Megginson Technologies. | ||
| 9 | # No warranty. Commercial and non-commercial use freely permitted. | ||
| 10 | # | ||
| 11 | # $Id$ | ||
| 12 | ######################################################################## | ||
| 13 | |||
| 14 | package Uplug::XML::Writer; | ||
| 15 | |||
| 16 | require 5.004; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use vars qw($VERSION); | ||
| 20 | use Carp; | ||
| 21 | use IO; | ||
| 22 | |||
| 23 | $VERSION = "0.4"; | ||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | ######################################################################## | ||
| 28 | # Constructor. | ||
| 29 | ######################################################################## | ||
| 30 | |||
| 31 | # | ||
| 32 | # Public constructor. | ||
| 33 | # | ||
| 34 | # This actually does most of the work of the module: it defines closures | ||
| 35 | # for all of the real processing, and selects the appropriate closures | ||
| 36 | # to use based on the value of the UNSAFE parameter. The actual methods | ||
| 37 | # are just stubs. | ||
| 38 | # | ||
| 39 | sub new { | ||
| 40 | my ($class, %params) = (@_); | ||
| 41 | |||
| 42 | # If the user wants namespaces, | ||
| 43 | # intercept the request here; it will | ||
| 44 | # come back to this constructor | ||
| 45 | # from within XML::Writer::Namespaces::new() | ||
| 46 | if ($params{NAMESPACES}) { | ||
| 47 | delete $params{NAMESPACES}; | ||
| 48 | return new Uplug::XML::Writer::Namespaces(%params); | ||
| 49 | } | ||
| 50 | |||
| 51 | # Set up $self and basic parameters | ||
| 52 | my $self; | ||
| 53 | my $output; | ||
| 54 | my $unsafe = $params{UNSAFE}; | ||
| 55 | my $newlines = $params{NEWLINES}; | ||
| 56 | my $dataMode = $params{DATA_MODE}; | ||
| 57 | my $dataIndent = $params{DATA_INDENT}; | ||
| 58 | |||
| 59 | # If the NEWLINES parameter is specified, | ||
| 60 | # set the $nl variable appropriately | ||
| 61 | my $nl = ''; | ||
| 62 | if ($newlines) { | ||
| 63 | $nl = "\n"; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | # Parse variables | ||
| 68 | my @elementStack = (); | ||
| 69 | my $elementLevel = 0; | ||
| 70 | my %seen = (); | ||
| 71 | |||
| 72 | my $hasData = 0; | ||
| 73 | my @hasDataStack = (); | ||
| 74 | my $hasElement = 0; | ||
| 75 | my @hasElementStack = (); | ||
| 76 | |||
| 77 | # | ||
| 78 | # Private method to show attributes. | ||
| 79 | # | ||
| 80 | my $showAttributes = sub { | ||
| 81 | my $atts = $_[0]; | ||
| 82 | my $i = 1; | ||
| 83 | while ($atts->[$i]) { | ||
| 84 | my $aname = $atts->[$i++]; | ||
| 85 | my $value = _escapeLiteral($atts->[$i++]); | ||
| 86 | $output->print(" $aname=\"$value\""); | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | # Method implementations: the SAFE_ | ||
| 91 | # versions perform error checking | ||
| 92 | # and then call the regular ones. | ||
| 93 | my $end = sub { | ||
| 94 | $output->print("\n"); | ||
| 95 | }; | ||
| 96 | |||
| 97 | my $SAFE_end = sub { | ||
| 98 | if (!$seen{ELEMENT}) { | ||
| 99 | croak("Document cannot end without a document element"); | ||
| 100 | } elsif ($elementLevel > 0) { | ||
| 101 | croak("Document ended with unmatched start tag(s): @elementStack"); | ||
| 102 | } else { | ||
| 103 | @elementStack = (); | ||
| 104 | $elementLevel = 0; | ||
| 105 | %seen = (); | ||
| 106 | &{$end}; | ||
| 107 | } | ||
| 108 | }; | ||
| 109 | |||
| 110 | my $xmlDecl = sub { | ||
| 111 | my ($encoding, $standalone) = (@_); | ||
| 112 | if ($standalone && $standalone ne 'no') { | ||
| 113 | $standalone = 'yes'; | ||
| 114 | } | ||
| 115 | $encoding = "UTF-8" unless $encoding; | ||
| 116 | $output->print("<?xml version=\"1.0\""); | ||
| 117 | if ($encoding) { | ||
| 118 | $output->print(" encoding=\"$encoding\""); | ||
| 119 | } | ||
| 120 | if ($standalone) { | ||
| 121 | $output->print(" standalone=\"$standalone\""); | ||
| 122 | } | ||
| 123 | $output->print("?>\n"); | ||
| 124 | }; | ||
| 125 | |||
| 126 | my $SAFE_xmlDecl = sub { | ||
| 127 | if ($seen{ANYTHING}) { | ||
| 128 | croak("The XML declaration is not the first thing in the document"); | ||
| 129 | } else { | ||
| 130 | $seen{ANYTHING} = 1; | ||
| 131 | $seen{XMLDECL} = 1; | ||
| 132 | &{$xmlDecl}; | ||
| 133 | } | ||
| 134 | }; | ||
| 135 | |||
| 136 | my $pi = sub { | ||
| 137 | my ($target, $data) = (@_); | ||
| 138 | if ($data) { | ||
| 139 | $output->print("<?$target $data?>"); | ||
| 140 | } else { | ||
| 141 | $output->print("<?$target?>"); | ||
| 142 | } | ||
| 143 | if ($elementLevel == 0) { | ||
| 144 | $output->print("\n"); | ||
| 145 | } | ||
| 146 | }; | ||
| 147 | |||
| 148 | my $SAFE_pi = sub { | ||
| 149 | my ($name, $data) = (@_); | ||
| 150 | $seen{ANYTHING} = 1; | ||
| 151 | if ($name =~ /xml/i) { | ||
| 152 | carp("Processing instruction target begins with 'xml'"); | ||
| 153 | } | ||
| 154 | |||
| 155 | if ($name =~ /\?\>/ || $data =~ /\?\>/) { | ||
| 156 | croak("Processing instruction may not contain '?>'"); | ||
| 157 | } else { | ||
| 158 | &{$pi}; | ||
| 159 | } | ||
| 160 | }; | ||
| 161 | |||
| 162 | my $comment = sub { | ||
| 163 | my $data = $_[0]; | ||
| 164 | $output->print("<!-- $data -->"); | ||
| 165 | if ($elementLevel == 0) { | ||
| 166 | $output->print("\n"); | ||
| 167 | } | ||
| 168 | }; | ||
| 169 | |||
| 170 | my $SAFE_comment = sub { | ||
| 171 | my $data = $_[0]; | ||
| 172 | if ($data =~ /--/) { | ||
| 173 | carp("Interoperability problem: \"--\" in comment text"); | ||
| 174 | } | ||
| 175 | |||
| 176 | if ($data =~ /-->/) { | ||
| 177 | croak("Comment may not contain '-->'"); | ||
| 178 | } else { | ||
| 179 | $seen{ANYTHING} = 1; | ||
| 180 | &{$comment}; | ||
| 181 | } | ||
| 182 | }; | ||
| 183 | |||
| 184 | my $doctype = sub { | ||
| 185 | my ($name, $publicId, $systemId) = (@_); | ||
| 186 | $output->print("<!DOCTYPE $name"); | ||
| 187 | if ($publicId) { | ||
| 188 | $output->print(" PUBLIC \"$publicId\" \"$systemId\""); | ||
| 189 | } elsif ($systemId) { | ||
| 190 | $output->print(" SYSTEM \"$systemId\""); | ||
| 191 | } | ||
| 192 | $output->print(">\n"); | ||
| 193 | }; | ||
| 194 | |||
| 195 | my $SAFE_doctype = sub { | ||
| 196 | my $name = $_[0]; | ||
| 197 | if ($seen{DOCTYPE}) { | ||
| 198 | croak("Attempt to insert second DOCTYPE declaration"); | ||
| 199 | } elsif ($seen{ELEMENT}) { | ||
| 200 | croak("The DOCTYPE declaration must come before the first start tag"); | ||
| 201 | } else { | ||
| 202 | $seen{ANYTHING} = 1; | ||
| 203 | $seen{DOCTYPE} = $name; | ||
| 204 | &{$doctype}; | ||
| 205 | } | ||
| 206 | }; | ||
| 207 | |||
| 208 | my $startTag = sub { | ||
| 209 | my $name = $_[0]; | ||
| 210 | if ($dataMode) { | ||
| 211 | $output->print("\n"); | ||
| 212 | $output->print(" " x ($elementLevel * $dataIndent)); | ||
| 213 | } | ||
| 214 | $elementLevel++; | ||
| 215 | push @elementStack, $name; | ||
| 216 | $output->print("<$name"); | ||
| 217 | &{$showAttributes}(\@_); | ||
| 218 | $output->print("$nl>"); | ||
| 219 | if ($dataMode) { | ||
| 220 | $hasElement = 1; | ||
| 221 | push @hasDataStack, $hasData; | ||
| 222 | $hasData = 0; | ||
| 223 | push @hasElementStack, $hasElement; | ||
| 224 | $hasElement = 0; | ||
| 225 | } | ||
| 226 | }; | ||
| 227 | |||
| 228 | my $SAFE_startTag = sub { | ||
| 229 | my $name = $_[0]; | ||
| 230 | |||
| 231 | _checkAttributes(\@_); | ||
| 232 | |||
| 233 | if ($seen{ELEMENT} && $elementLevel == 0) { | ||
| 234 | croak("Attempt to insert start tag after close of document element"); | ||
| 235 | } elsif ($elementLevel == 0 && $seen{DOCTYPE} && $name ne $seen{DOCTYPE}) { | ||
| 236 | croak("Document element is \"$name\", but DOCTYPE is \"" | ||
| 237 | . $seen{DOCTYPE} | ||
| 238 | . "\""); | ||
| 239 | } elsif ($dataMode && $hasData) { | ||
| 240 | croak("Mixed content not allowed in data mode: element $name"); | ||
| 241 | } else { | ||
| 242 | $seen{ANYTHING} = 1; | ||
| 243 | $seen{ELEMENT} = 1; | ||
| 244 | &{$startTag}; | ||
| 245 | } | ||
| 246 | }; | ||
| 247 | |||
| 248 | my $emptyTag = sub { | ||
| 249 | my $name = $_[0]; | ||
| 250 | if ($dataMode) { | ||
| 251 | $output->print("\n"); | ||
| 252 | $output->print(" " x ($elementLevel * $dataIndent)); | ||
| 253 | } | ||
| 254 | $output->print("<$name"); | ||
| 255 | &{$showAttributes}(\@_); | ||
| 256 | $output->print("$nl />"); | ||
| 257 | if ($dataMode) { | ||
| 258 | $hasElement = 1; | ||
| 259 | } | ||
| 260 | }; | ||
| 261 | |||
| 262 | my $SAFE_emptyTag = sub { | ||
| 263 | my $name = $_[0]; | ||
| 264 | |||
| 265 | _checkAttributes(\@_); | ||
| 266 | |||
| 267 | if ($seen{ELEMENT} && $elementLevel == 0) { | ||
| 268 | croak("Attempt to insert empty tag after close of document element"); | ||
| 269 | } elsif ($elementLevel == 0 && $seen{DOCTYPE} && $name ne $seen{DOCTYPE}) { | ||
| 270 | croak("Document element is \"$name\", but DOCTYPE is \"" | ||
| 271 | . $seen{DOCTYPE} | ||
| 272 | . "\""); | ||
| 273 | } elsif ($dataMode && $hasData) { | ||
| 274 | croak("Mixed content not allowed in data mode: element $name"); | ||
| 275 | } else { | ||
| 276 | $seen{ANYTHING} = 1; | ||
| 277 | $seen{ELEMENT} = 1; | ||
| 278 | &{$emptyTag}; | ||
| 279 | } | ||
| 280 | }; | ||
| 281 | |||
| 282 | my $endTag = sub { | ||
| 283 | my $name = $_[0]; | ||
| 284 | my $currentName = pop @elementStack; | ||
| 285 | $name = $currentName unless $name; | ||
| 286 | $elementLevel--; | ||
| 287 | if ($dataMode && $hasElement) { | ||
| 288 | $output->print("\n"); | ||
| 289 | $output->print(" " x ($elementLevel * $dataIndent)); | ||
| 290 | } | ||
| 291 | $output->print("</$name$nl>"); | ||
| 292 | if ($dataMode) { | ||
| 293 | $hasData = pop @hasDataStack; | ||
| 294 | $hasElement = pop @hasElementStack; | ||
| 295 | } | ||
| 296 | }; | ||
| 297 | |||
| 298 | my $SAFE_endTag = sub { | ||
| 299 | my $name = $_[0]; | ||
| 300 | my $oldName = $elementStack[$#elementStack]; | ||
| 301 | if ($elementLevel <= 0) { | ||
| 302 | croak("End tag \"$name\" does not close any open element"); | ||
| 303 | } elsif ($name && ($name ne $oldName)) { | ||
| 304 | croak("Attempt to end element \"$oldName\" with \"$name\" tag"); | ||
| 305 | } else { | ||
| 306 | &{$endTag}; | ||
| 307 | } | ||
| 308 | }; | ||
| 309 | |||
| 310 | my $characters = sub { | ||
| 311 | my $data = $_[0]; | ||
| 312 | if ($data =~ /[\&\<\>]/) { | ||
| 313 | $data =~ s/\&/\&\;/g; | ||
| 314 | $data =~ s/\</\<\;/g; | ||
| 315 | $data =~ s/\>/\>\;/g; | ||
| 316 | } | ||
| 317 | $output->print($data); | ||
| 318 | $hasData = 1; | ||
| 319 | }; | ||
| 320 | |||
| 321 | my $SAFE_characters = sub { | ||
| 322 | if ($elementLevel < 1) { | ||
| 323 | croak("Attempt to insert characters outside of document element"); | ||
| 324 | } elsif ($dataMode && $hasElement) { | ||
| 325 | croak("Mixed content not allowed in data mode: characters"); | ||
| 326 | } else { | ||
| 327 | &{$characters}; | ||
| 328 | } | ||
| 329 | }; | ||
| 330 | |||
| 331 | |||
| 332 | # Assign the correct closures based on | ||
| 333 | # the UNSAFE parameter | ||
| 334 | if ($unsafe) { | ||
| 335 | $self = {'END' => $end, | ||
| 336 | 'XMLDECL' => $xmlDecl, | ||
| 337 | 'PI' => $pi, | ||
| 338 | 'COMMENT' => $comment, | ||
| 339 | 'DOCTYPE' => $doctype, | ||
| 340 | 'STARTTAG' => $startTag, | ||
| 341 | 'EMPTYTAG' => $emptyTag, | ||
| 342 | 'ENDTAG' => $endTag, | ||
| 343 | 'CHARACTERS' => $characters}; | ||
| 344 | } else { | ||
| 345 | $self = {'END' => $SAFE_end, | ||
| 346 | 'XMLDECL' => $SAFE_xmlDecl, | ||
| 347 | 'PI' => $SAFE_pi, | ||
| 348 | 'COMMENT' => $SAFE_comment, | ||
| 349 | 'DOCTYPE' => $SAFE_doctype, | ||
| 350 | 'STARTTAG' => $SAFE_startTag, | ||
| 351 | 'EMPTYTAG' => $SAFE_emptyTag, | ||
| 352 | 'ENDTAG' => $SAFE_endTag, | ||
| 353 | 'CHARACTERS' => $SAFE_characters}; | ||
| 354 | } | ||
| 355 | |||
| 356 | # Query methods | ||
| 357 | $self->{'IN_ELEMENT'} = sub { | ||
| 358 | my ($ancestor) = (@_); | ||
| 359 | return $elementStack[$#elementStack] eq $ancestor; | ||
| 360 | }; | ||
| 361 | |||
| 362 | $self->{'WITHIN_ELEMENT'} = sub { | ||
| 363 | my ($ancestor) = (@_); | ||
| 364 | my $el; | ||
| 365 | foreach $el (@elementStack) { | ||
| 366 | return 1 if $el eq $ancestor; | ||
| 367 | } | ||
| 368 | return 0; | ||
| 369 | }; | ||
| 370 | |||
| 371 | $self->{'CURRENT_ELEMENT'} = sub { | ||
| 372 | return $elementStack[$#elementStack]; | ||
| 373 | }; | ||
| 374 | |||
| 375 | $self->{'ANCESTOR'} = sub { | ||
| 376 | my ($n) = (@_); | ||
| 377 | return $elementStack[$#elementStack-$n]; | ||
| 378 | }; | ||
| 379 | |||
| 380 | # Set and get the output destination. | ||
| 381 | $self->{'GETOUTPUT'} = sub { | ||
| 382 | return $output; | ||
| 383 | }; | ||
| 384 | |||
| 385 | $self->{'SETOUTPUT'} = sub { | ||
| 386 | my $newOutput = $_[0]; | ||
| 387 | # If there is no OUTPUT parameter, | ||
| 388 | # use standard output | ||
| 389 | unless ($newOutput) { | ||
| 390 | $newOutput = new IO::Handle(); | ||
| 391 | $newOutput->fdopen(fileno(STDOUT), "w") || | ||
| 392 | croak("Cannot write to standard output"); | ||
| 393 | } | ||
| 394 | $output = $newOutput; | ||
| 395 | }; | ||
| 396 | |||
| 397 | $self->{'SETDATAMODE'} = sub { | ||
| 398 | $dataMode = $_[0]; | ||
| 399 | }; | ||
| 400 | |||
| 401 | $self->{'GETDATAMODE'} = sub { | ||
| 402 | return $dataMode; | ||
| 403 | }; | ||
| 404 | |||
| 405 | $self->{'SETDATAINDENT'} = sub { | ||
| 406 | $dataIndent = $_[0]; | ||
| 407 | }; | ||
| 408 | |||
| 409 | $self->{'GETDATAINDENT'} = sub { | ||
| 410 | return $dataIndent; | ||
| 411 | }; | ||
| 412 | |||
| 413 | # Set the output. | ||
| 414 | &{$self->{'SETOUTPUT'}}($params{'OUTPUT'}); | ||
| 415 | |||
| 416 | # Return the blessed object. | ||
| 417 | return bless $self, $class; | ||
| 418 | } | ||
| 419 | |||
| 420 | |||
| 421 | |||
| 422 | ######################################################################## | ||
| 423 | # Public methods | ||
| 424 | ######################################################################## | ||
| 425 | |||
| 426 | # | ||
| 427 | # Finish writing the document. | ||
| 428 | # | ||
| 429 | sub end { | ||
| 430 | my $self = shift; | ||
| 431 | &{$self->{END}}; | ||
| 432 | } | ||
| 433 | |||
| 434 | # | ||
| 435 | # Write an XML declaration. | ||
| 436 | # | ||
| 437 | sub xmlDecl { | ||
| 438 | my $self = shift; | ||
| 439 | &{$self->{XMLDECL}}; | ||
| 440 | } | ||
| 441 | |||
| 442 | # | ||
| 443 | # Write a processing instruction. | ||
| 444 | # | ||
| 445 | sub pi { | ||
| 446 | my $self = shift; | ||
| 447 | &{$self->{PI}}; | ||
| 448 | } | ||
| 449 | |||
| 450 | # | ||
| 451 | # Write a comment. | ||
| 452 | # | ||
| 453 | sub comment { | ||
| 454 | my $self = shift; | ||
| 455 | &{$self->{COMMENT}}; | ||
| 456 | } | ||
| 457 | |||
| 458 | # | ||
| 459 | # Write a DOCTYPE declaration. | ||
| 460 | # | ||
| 461 | sub doctype { | ||
| 462 | my $self = shift; | ||
| 463 | &{$self->{DOCTYPE}}; | ||
| 464 | } | ||
| 465 | |||
| 466 | # | ||
| 467 | # Write a start tag. | ||
| 468 | # | ||
| 469 | sub startTag { | ||
| 470 | my $self = shift; | ||
| 471 | &{$self->{STARTTAG}}; | ||
| 472 | } | ||
| 473 | |||
| 474 | # | ||
| 475 | # Write an empty tag. | ||
| 476 | # | ||
| 477 | sub emptyTag { | ||
| 478 | my $self = shift; | ||
| 479 | &{$self->{EMPTYTAG}}; | ||
| 480 | } | ||
| 481 | |||
| 482 | # | ||
| 483 | # Write an end tag. | ||
| 484 | # | ||
| 485 | sub endTag { | ||
| 486 | my $self = shift; | ||
| 487 | &{$self->{ENDTAG}}; | ||
| 488 | } | ||
| 489 | |||
| 490 | # | ||
| 491 | # Write a simple data element. | ||
| 492 | # | ||
| 493 | sub dataElement { | ||
| 494 | my ($self, $name, $data, %atts) = (@_); | ||
| 495 | $self->startTag($name, %atts); | ||
| 496 | $self->characters($data); | ||
| 497 | $self->endTag($name); | ||
| 498 | } | ||
| 499 | |||
| 500 | # | ||
| 501 | # Write character data. | ||
| 502 | # | ||
| 503 | sub characters { | ||
| 504 | my $self = shift; | ||
| 505 | &{$self->{CHARACTERS}}; | ||
| 506 | } | ||
| 507 | |||
| 508 | # | ||
| 509 | # Query the current element. | ||
| 510 | # | ||
| 511 | sub in_element { | ||
| 512 | my $self = shift; | ||
| 513 | return &{$self->{IN_ELEMENT}}; | ||
| 514 | } | ||
| 515 | |||
| 516 | # | ||
| 517 | # Query the ancestors. | ||
| 518 | # | ||
| 519 | sub within_element { | ||
| 520 | my $self = shift; | ||
| 521 | return &{$self->{WITHIN_ELEMENT}}; | ||
| 522 | } | ||
| 523 | |||
| 524 | # | ||
| 525 | # Get the name of the current element. | ||
| 526 | # | ||
| 527 | sub current_element { | ||
| 528 | my $self = shift; | ||
| 529 | return &{$self->{CURRENT_ELEMENT}}; | ||
| 530 | } | ||
| 531 | |||
| 532 | # | ||
| 533 | # Get the name of the numbered ancestor (zero-based). | ||
| 534 | # | ||
| 535 | sub ancestor { | ||
| 536 | my $self = shift; | ||
| 537 | return &{$self->{ANCESTOR}}; | ||
| 538 | } | ||
| 539 | |||
| 540 | # | ||
| 541 | # Get the current output destination. | ||
| 542 | # | ||
| 543 | sub getOutput { | ||
| 544 | my $self = shift; | ||
| 545 | return &{$self->{GETOUTPUT}}; | ||
| 546 | } | ||
| 547 | |||
| 548 | |||
| 549 | # | ||
| 550 | # Set the current output destination. | ||
| 551 | # | ||
| 552 | sub setOutput { | ||
| 553 | my $self = shift; | ||
| 554 | return &{$self->{SETOUTPUT}}; | ||
| 555 | } | ||
| 556 | |||
| 557 | # | ||
| 558 | # Set the current data mode (true or false). | ||
| 559 | # | ||
| 560 | sub setDataMode { | ||
| 561 | my $self = shift; | ||
| 562 | return &{$self->{SETDATAMODE}}; | ||
| 563 | } | ||
| 564 | |||
| 565 | |||
| 566 | # | ||
| 567 | # Get the current data mode (true or false). | ||
| 568 | # | ||
| 569 | sub getDataMode { | ||
| 570 | my $self = shift; | ||
| 571 | return &{$self->{GETDATAMODE}}; | ||
| 572 | } | ||
| 573 | |||
| 574 | |||
| 575 | # | ||
| 576 | # Set the current data indent step. | ||
| 577 | # | ||
| 578 | sub setDataIndent { | ||
| 579 | my $self = shift; | ||
| 580 | return &{$self->{SETDATAINDENT}}; | ||
| 581 | } | ||
| 582 | |||
| 583 | |||
| 584 | # | ||
| 585 | # Get the current data indent step. | ||
| 586 | # | ||
| 587 | sub getDataIndent { | ||
| 588 | my $self = shift; | ||
| 589 | return &{$self->{GETDATAINDENT}}; | ||
| 590 | } | ||
| 591 | |||
| 592 | |||
| 593 | # | ||
| 594 | # Empty stub. | ||
| 595 | # | ||
| 596 | sub addPrefix { | ||
| 597 | } | ||
| 598 | |||
| 599 | |||
| 600 | # | ||
| 601 | # Empty stub. | ||
| 602 | # | ||
| 603 | sub removePrefix { | ||
| 604 | } | ||
| 605 | |||
| 606 | |||
| 607 | |||
| 608 | ######################################################################## | ||
| 609 | # Private functions. | ||
| 610 | ######################################################################## | ||
| 611 | |||
| 612 | # | ||
| 613 | # Private: check for duplicate attributes. | ||
| 614 | # Note - this starts at $_[1], because $_[0] is assumed to be an | ||
| 615 | # element name. | ||
| 616 | # | ||
| 617 | sub _checkAttributes { | ||
| 618 | my %anames; | ||
| 619 | my $i = 1; | ||
| 620 | while ($_[$i]) { | ||
| 621 | my $name = $_[$i]; | ||
| 622 | $i += 2; | ||
| 623 | if ($anames{$name}) { | ||
| 624 | croak("Two attributes named \"$name\""); | ||
| 625 | } else { | ||
| 626 | $anames{$name} = 1; | ||
| 627 | } | ||
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | # | ||
| 632 | # Private: escape an attribute value literal. | ||
| 633 | # | ||
| 634 | sub _escapeLiteral { | ||
| 635 | my $data = $_[0]; | ||
| 636 | if ($data =~ /[\&\<\>\"]/) { | ||
| 637 | $data =~ s/\&/\&\;/g; | ||
| 638 | $data =~ s/\</\<\;/g; | ||
| 639 | $data =~ s/\>/\>\;/g; | ||
| 640 | $data =~ s/\"/\"\;/g; | ||
| 641 | } | ||
| 642 | return $data; | ||
| 643 | } | ||
| 644 | |||
| 645 | |||
| 646 | |||
| 647 | ######################################################################## | ||
| 648 | # XML::Writer::Namespaces - subclass for Namespace processing. | ||
| 649 | ######################################################################## | ||
| 650 | |||
| 651 | package Uplug::XML::Writer::Namespaces; | ||
| 652 | use strict; | ||
| 653 | use vars qw(@ISA); | ||
| 654 | use Carp; | ||
| 655 | |||
| 656 | @ISA = qw(Uplug::XML::Writer); | ||
| 657 | |||
| 658 | # | ||
| 659 | # Constructor | ||
| 660 | # | ||
| 661 | sub new { | ||
| 662 | my ($class, %params) = (@_); | ||
| 663 | |||
| 664 | my $unsafe = $params{UNSAFE}; | ||
| 665 | |||
| 666 | # Snarf the prefix map, if any, and | ||
| 667 | # note the default prefix. | ||
| 668 | my %prefixMap = (); | ||
| 669 | if ($params{PREFIX_MAP}) { | ||
| 670 | %prefixMap = (%{$params{PREFIX_MAP}}); | ||
| 671 | delete $params{PREFIX_MAP}; | ||
| 672 | } | ||
| 673 | my $defaultPrefix = $prefixMap{''}; | ||
| 674 | delete $prefixMap{''}; | ||
| 675 | |||
| 676 | # Generate the reverse map for URIs | ||
| 677 | my %uriMap = (); | ||
| 678 | my $key; | ||
| 679 | foreach $key (keys(%prefixMap)) { | ||
| 680 | $uriMap{$prefixMap{$key}} = $key; | ||
| 681 | } | ||
| 682 | |||
| 683 | # Create an instance of the parent. | ||
| 684 | my $self = new Uplug::XML::Writer(%params); | ||
| 685 | |||
| 686 | # Snarf the parent's methods that we're | ||
| 687 | # going to override. | ||
| 688 | my $OLD_startTag = $self->{STARTTAG}; | ||
| 689 | my $OLD_emptyTag = $self->{EMPTYTAG}; | ||
| 690 | my $OLD_endTag = $self->{ENDTAG}; | ||
| 691 | |||
| 692 | # State variables | ||
| 693 | my $prefixCounter = 1; | ||
| 694 | my @nsDecls = (); | ||
| 695 | my $nsDecls = {}; | ||
| 696 | my @nsDefaultDecl = (); | ||
| 697 | my $nsDefaultDecl = undef; | ||
| 698 | my @nsCopyFlag = (); | ||
| 699 | my $nsCopyFlag = 0; | ||
| 700 | |||
| 701 | # | ||
| 702 | # Push the current declaration state. | ||
| 703 | # | ||
| 704 | my $pushState = sub { | ||
| 705 | push @nsDecls, $nsDecls; | ||
| 706 | push @nsDefaultDecl, $nsDefaultDecl; | ||
| 707 | push @nsCopyFlag, $nsCopyFlag; | ||
| 708 | $nsCopyFlag = 0; | ||
| 709 | }; | ||
| 710 | |||
| 711 | |||
| 712 | # | ||
| 713 | # Pop the current declaration state. | ||
| 714 | # | ||
| 715 | my $popState = sub { | ||
| 716 | $nsDecls = pop @nsDecls; | ||
| 717 | $nsDefaultDecl = pop @nsDefaultDecl; | ||
| 718 | $nsCopyFlag = pop @nsCopyFlag; | ||
| 719 | }; | ||
| 720 | |||
| 721 | # | ||
| 722 | # Generate a new prefix. | ||
| 723 | # | ||
| 724 | my $genPrefix = sub { | ||
| 725 | my $prefix; | ||
| 726 | do { | ||
| 727 | $prefix = "__NS$prefixCounter"; | ||
| 728 | $prefixCounter++; | ||
| 729 | } while ($uriMap{$prefix}); | ||
| 730 | return $prefix; | ||
| 731 | }; | ||
| 732 | |||
| 733 | # | ||
| 734 | # Perform namespace processing on a single name. | ||
| 735 | # | ||
| 736 | my $processName = sub { | ||
| 737 | my ($nameref, $atts, $attFlag) = (@_); | ||
| 738 | my ($uri, $local) = @{$$nameref}; | ||
| 739 | my $prefix = $prefixMap{$uri}; | ||
| 740 | |||
| 741 | # Is this an element name that matches | ||
| 742 | # the default NS? | ||
| 743 | if (!$attFlag && ($uri eq $defaultPrefix)) { | ||
| 744 | unless ($nsDefaultDecl) { | ||
| 745 | push @{$atts}, 'xmlns'; | ||
| 746 | push @{$atts}, $uri; | ||
| 747 | $nsDefaultDecl = 1; | ||
| 748 | } | ||
| 749 | $$nameref = $local; | ||
| 750 | |||
| 751 | # Is there a straight-forward prefix? | ||
| 752 | } elsif ($prefix) { | ||
| 753 | unless ($nsDecls->{$uri}) { | ||
| 754 | # Copy on write (FIXME: duplicated) | ||
| 755 | unless ($nsCopyFlag) { | ||
| 756 | $nsCopyFlag = 1; | ||
| 757 | my %decls = (%{$nsDecls}); | ||
| 758 | $nsDecls = \%decls; | ||
| 759 | } | ||
| 760 | $nsDecls->{$uri} = $prefix; | ||
| 761 | push @{$atts}, "xmlns:$prefix"; | ||
| 762 | push @{$atts}, $uri; | ||
| 763 | } | ||
| 764 | $$nameref = "$prefix:$local"; | ||
| 765 | |||
| 766 | } else { | ||
| 767 | $prefix = &{$genPrefix}(); | ||
| 768 | $prefixMap{$uri} = $prefix; | ||
| 769 | $uriMap{$prefix} = $uri; | ||
| 770 | unless ($nsCopyFlag) { | ||
| 771 | $nsCopyFlag = 1; | ||
| 772 | my %decls = (%{$nsDecls}); | ||
| 773 | $nsDecls = \%decls; | ||
| 774 | } | ||
| 775 | $nsDecls->{$uri} = $prefix; | ||
| 776 | push @{$atts}, "xmlns:$prefix"; | ||
| 777 | push @{$atts}, $uri; | ||
| 778 | $$nameref = "$prefix:$local"; | ||
| 779 | } | ||
| 780 | }; | ||
| 781 | |||
| 782 | |||
| 783 | # | ||
| 784 | # Perform namespace processing on element and attribute names. | ||
| 785 | # | ||
| 786 | my $nsProcess = sub { | ||
| 787 | if (ref($_[0]->[0]) eq 'ARRAY') { | ||
| 788 | &{$processName}(\$_[0]->[0], $_[0], 0); | ||
| 789 | } | ||
| 790 | my $i = 1; | ||
| 791 | while ($_[0]->[$i]) { | ||
| 792 | if (ref($_[0]->[$i]) eq 'ARRAY') { | ||
| 793 | &{$processName}(\$_[0]->[$i], $_[0], 1); | ||
| 794 | } | ||
| 795 | $i += 2; | ||
| 796 | } | ||
| 797 | }; | ||
| 798 | |||
| 799 | # | ||
| 800 | # Start tag, with NS processing | ||
| 801 | # | ||
| 802 | $self->{STARTTAG} = sub { | ||
| 803 | my $name = $_[0]; | ||
| 804 | unless ($unsafe) { | ||
| 805 | _checkNSNames(\@_); | ||
| 806 | } | ||
| 807 | &{$pushState}(); | ||
| 808 | &{$nsProcess}(\@_); | ||
| 809 | &{$OLD_startTag}; | ||
| 810 | }; | ||
| 811 | |||
| 812 | |||
| 813 | # | ||
| 814 | # Empty tag, with NS processing | ||
| 815 | # | ||
| 816 | $self->{EMPTYTAG} = sub { | ||
| 817 | unless ($unsafe) { | ||
| 818 | _checkNSNames(\@_); | ||
| 819 | } | ||
| 820 | &{$pushState}(); | ||
| 821 | &{$nsProcess}(\@_); | ||
| 822 | &{$OLD_emptyTag}; | ||
| 823 | &{$popState}(); | ||
| 824 | }; | ||
| 825 | |||
| 826 | |||
| 827 | # | ||
| 828 | # End tag, with NS processing | ||
| 829 | # | ||
| 830 | $self->{ENDTAG} = sub { | ||
| 831 | my $name = $_[0]; | ||
| 832 | &{$nsProcess}(\@_); | ||
| 833 | &{$OLD_endTag}; | ||
| 834 | &{$popState}(); | ||
| 835 | }; | ||
| 836 | |||
| 837 | |||
| 838 | # | ||
| 839 | # Processing instruction, but only if not UNSAFE. | ||
| 840 | # | ||
| 841 | unless ($unsafe) { | ||
| 842 | my $OLD_pi = $self->{PI}; | ||
| 843 | $self->{PI} = sub { | ||
| 844 | my $target = $_[0]; | ||
| 845 | if ($target =~ /:/) { | ||
| 846 | croak "PI target '$target' contains a colon."; | ||
| 847 | } | ||
| 848 | &{$OLD_pi}; | ||
| 849 | } | ||
| 850 | }; | ||
| 851 | |||
| 852 | |||
| 853 | # | ||
| 854 | # Add a prefix to the prefix map. | ||
| 855 | # | ||
| 856 | $self->{ADDPREFIX} = sub { | ||
| 857 | my ($uri, $prefix) = (@_); | ||
| 858 | if ($prefix) { | ||
| 859 | $prefixMap{$uri} = $prefix; | ||
| 860 | $uriMap{$prefix} = $uri; | ||
| 861 | } else { | ||
| 862 | $defaultPrefix = $uri; | ||
| 863 | } | ||
| 864 | }; | ||
| 865 | |||
| 866 | |||
| 867 | # | ||
| 868 | # Remove a prefix from the prefix map. | ||
| 869 | # | ||
| 870 | $self->{REMOVEPREFIX} = sub { | ||
| 871 | my ($uri) = (@_); | ||
| 872 | if ($defaultPrefix eq $uri) { | ||
| 873 | $defaultPrefix = undef; | ||
| 874 | } | ||
| 875 | delete $prefixMap{$uri}; | ||
| 876 | }; | ||
| 877 | |||
| 878 | |||
| 879 | # | ||
| 880 | # Bless and return the object. | ||
| 881 | # | ||
| 882 | return bless $self, $class; | ||
| 883 | } | ||
| 884 | |||
| 885 | |||
| 886 | # | ||
| 887 | # Add a preferred prefix for a namespace URI. | ||
| 888 | # | ||
| 889 | sub addPrefix { | ||
| 890 | my $self = shift; | ||
| 891 | return &{$self->{ADDPREFIX}}; | ||
| 892 | } | ||
| 893 | |||
| 894 | |||
| 895 | # | ||
| 896 | # Remove a preferred prefix for a namespace URI. | ||
| 897 | # | ||
| 898 | sub removePrefix { | ||
| 899 | my $self = shift; | ||
| 900 | return &{$self->{REMOVEPREFIX}}; | ||
| 901 | } | ||
| 902 | |||
| 903 | |||
| 904 | # | ||
| 905 | # Check names. | ||
| 906 | # | ||
| 907 | sub _checkNSNames { | ||
| 908 | my $names = $_[0]; | ||
| 909 | my $i = 1; | ||
| 910 | my $name = $names->[0]; | ||
| 911 | |||
| 912 | # Check the element name. | ||
| 913 | if (ref($name) eq 'ARRAY') { | ||
| 914 | if ($name->[1] =~ /:/) { | ||
| 915 | croak("Local part of element name '" . | ||
| 916 | $name->[1] . | ||
| 917 | "' contains a colon."); | ||
| 918 | } | ||
| 919 | } elsif ($name =~ /:/) { | ||
| 920 | croak("Element name '$name' contains a colon."); | ||
| 921 | } | ||
| 922 | |||
| 923 | # Check the attribute names. | ||
| 924 | while ($names->[$i]) { | ||
| 925 | my $name = $names->[$i]; | ||
| 926 | if (ref($name) eq 'ARRAY') { | ||
| 927 | my $local = $name->[1]; | ||
| 928 | if ($local =~ /:/) { | ||
| 929 | croak "Local part of attribute name '$local' contains a colon."; | ||
| 930 | } | ||
| 931 | } else { | ||
| 932 | if ($name =~ /^(xmlns|.*:)/) { | ||
| 933 | if ($name =~ /^xmlns/) { | ||
| 934 | croak "Attribute name '$name' begins with 'xmlns'"; | ||
| 935 | } elsif ($name =~ /:/) { | ||
| 936 | croak "Attribute name '$name' contains ':'"; | ||
| 937 | } | ||
| 938 | } | ||
| 939 | } | ||
| 940 | $i += 2; | ||
| 941 | } | ||
| 942 | } | ||
| 943 | |||
| 944 | |||
| 945 | 1; | ||
| 946 | __END__ |