/src/agent/lib/XML/Handler/CanonXMLWriter.pm

http://keywatch.googlecode.com/ · Perl · 180 lines · 144 code · 27 blank · 9 comment · 6 complexity · 0383535104e196523cdd7917cd26419b MD5 · raw file

  1. #
  2. # Copyright (C) 1998, 1999 Ken MacLeod
  3. # XML::Handler::CanonXMLWriter is free software; you can redistribute
  4. # it and/or modify it under the same terms as Perl itself.
  5. #
  6. # $Id: CanonXMLWriter.pm,v 1.2 1999/12/22 21:15:00 kmacleod Exp $
  7. #
  8. use strict;
  9. package XML::Handler::CanonXMLWriter;
  10. use vars qw{ $VERSION %char_entities };
  11. # will be substituted by make-rel script
  12. $VERSION = "0.08";
  13. %char_entities = (
  14. "\x09" => '	',
  15. "\x0a" => '
',
  16. "\x0d" => '
',
  17. '&' => '&',
  18. '<' => '&lt;',
  19. '>' => '&gt;',
  20. '"' => '&quot;',
  21. );
  22. sub new {
  23. my ($class, %args) = @_;
  24. my $self = \%args;
  25. return bless $self, $class;
  26. }
  27. sub start_document {
  28. my $self = shift; my $document = shift;
  29. $self->{'_text_array'} = [];
  30. }
  31. sub end_document {
  32. my $self = shift; my $document = shift;
  33. if (defined $self->{IOHandle}) {
  34. return ();
  35. } else {
  36. my $text = join ('', @{$self->{'_text_array'}});
  37. undef $self->{'_text_array'};
  38. return $text;
  39. }
  40. }
  41. sub start_element {
  42. my $self = shift; my $element = shift;
  43. $self->_print('<' . $element->{Name});
  44. my $key;
  45. my $attrs = $element->{Attributes};
  46. foreach $key (sort keys %$attrs) {
  47. $self->_print(" $key=\"" . $self->_escape($attrs->{$key}) . '"');
  48. }
  49. $self->_print('>');
  50. }
  51. sub end_element {
  52. my $self = shift; my $element = shift;
  53. $self->_print('</' . $element->{Name} . '>');
  54. }
  55. sub characters {
  56. my $self = shift; my $characters = shift;
  57. $self->_print($self->_escape($characters->{Data}));
  58. }
  59. sub ignorable_whitespace {
  60. my $self = shift; my $characters = shift;
  61. $self->_print($self->_escape($characters->{Data}));
  62. }
  63. sub processing_instruction {
  64. my $self = shift; my $pi = shift;
  65. $self->_print('<?' . $pi->{Target} . ' ' . $pi->{Data} . '?>');
  66. }
  67. sub entity {
  68. # entities don't occur in text
  69. return ();
  70. }
  71. sub comment {
  72. my $self = shift; my $comment = shift;
  73. if ($self->{PrintComments}) {
  74. $self->_print('<!--' . $comment->{Data} . '-->');
  75. } else {
  76. return ();
  77. }
  78. }
  79. sub _print {
  80. my $self = shift; my $string = shift;
  81. if (defined $self->{IOHandle}) {
  82. $self->{IOHandle}->print($string);
  83. return ();
  84. } else {
  85. push @{$self->{'_text_array'}}, $string;
  86. }
  87. }
  88. sub _escape {
  89. my $self = shift; my $string = shift;
  90. $string =~ s/([\x09\x0a\x0d&<>"])/$char_entities{$1}/ge;
  91. return $string;
  92. }
  93. 1;
  94. __END__
  95. =head1 NAME
  96. XML::Handler::CanonXMLWriter - output XML in canonical XML format
  97. =head1 SYNOPSIS
  98. use XML::Handler::CanonXMLWriter;
  99. $writer = XML::Handler::CanonXMLWriter OPTIONS;
  100. $parser->parse(Handler => $writer);
  101. =head1 DESCRIPTION
  102. C<XML::Handler::CanonXMLWriter> is a PerlSAX handler that will return
  103. a string or write a stream of canonical XML for an XML instance and it's
  104. content.
  105. C<XML::Handler::CanonXMLWriter> objects hold the options used for
  106. writing the XML objects. Options can be supplied when the the object
  107. is created,
  108. $writer = new XML::Handler::CanonXMLWriter PrintComments => 1;
  109. or modified at any time before calling the parser's `C<parse()>' method:
  110. $writer->{PrintComments} = 0;
  111. =head1 OPTIONS
  112. =over 4
  113. =item IOHandle
  114. IOHandle contains a handle for writing the canonical XML to. If an
  115. IOHandle is not provided, the canonical XML string will be returned
  116. from `C<parse()>'.
  117. =item PrintComments
  118. By default comments are not written to the output. Setting comment to
  119. a true value will include comments in the output.
  120. =back
  121. =head1 AUTHOR
  122. Ken MacLeod, ken@bitsko.slc.ut.us
  123. =head1 SEE ALSO
  124. perl(1), PerlSAX
  125. James Clark's Canonical XML definition
  126. <http://www.jclark.com/xml/canonxml.html>
  127. =cut