deprecated/buildtools/buildsystemtools/lib/XML/Handler/XMLWriter.pm
author kelvzhu
Mon, 18 Oct 2010 16:16:46 +0800
changeset 655 3f65fd25dfd4
permissions -rw-r--r--
sync up SVN codes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     1
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     2
# Copyright (C) 1999 Ken MacLeod
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     3
# Portions derived from code in XML::Writer by David Megginson
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     4
# XML::Handler::XMLWriter is free software; you can redistribute it and/or
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     5
# modify it under the same terms as Perl itself.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     6
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     7
# $Id: XMLWriter.pm,v 1.2 1999/12/22 21:15:00 kmacleod Exp $
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     8
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     9
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    10
use strict;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    11
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    12
package XML::Handler::XMLWriter;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    13
use XML::Handler::Subs;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    14
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    15
use vars qw{ $VERSION @ISA $escapes };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    16
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    17
# will be substituted by make-rel script
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    18
$VERSION = "0.07";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    19
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    20
@ISA = qw{ XML::Handler::Subs };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    21
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    22
$escapes = { '&' => '&',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    23
	     '<' => '&lt;',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    24
	     '>' => '&gt;',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    25
	     '"' => '&quot;'
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    26
	 };
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    27
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    28
sub start_document {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    29
    my ($self, $document) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    30
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    31
    $self->SUPER::start_document($document);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    32
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    33
    # create a temporary Output_ in case we're creating a standard
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    34
    # output file that we'll delete later.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    35
    if (!$self->{AsString} && !defined($self->{Output})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    36
	require IO::File;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    37
	import IO::File;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    38
	$self->{Output_} = new IO::File(">-");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    39
    } elsif (defined($self->{Output})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    40
	$self->{Output_} = $self->{Output};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    41
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    42
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    43
    if ($self->{AsString}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    44
	$self->{Strings} = [];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    45
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    46
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    47
    $self->print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    48
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    49
    # FIXME support Doctype declarations
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    50
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    51
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    52
sub end_document {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    53
    my ($self, $document) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    54
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    55
    if (defined($self->{Output_})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    56
	$self->{Output_}->print("\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    57
	delete $self->{Output_};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    58
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    59
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    60
    my $string = undef;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    61
    if (defined($self->{AsString})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    62
	push @{$self->{Strings}}, "\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    63
	$string = join('', @{$self->{Strings}});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    64
	delete $self->{Strings};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    65
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    66
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    67
    $self->SUPER::end_document($document);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    68
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    69
    return($string);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    70
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    71
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    72
sub start_element {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    73
    my ($self, $element) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    74
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    75
    if ($self->SUPER::start_element($element) == 0) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    76
	$self->print_start_element($element);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    77
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    78
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    79
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    80
sub print_start_element {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    81
    my ($self, $element)  = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    82
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    83
    my $output = "<$element->{Name}";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    84
    if (defined($element->{Attributes})) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    85
	foreach my $name (sort keys %{$element->{Attributes}}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    86
	    my $esc_value = $element->{Attributes}{$name};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    87
	    $esc_value =~ s/([\&\<\>\"])/$escapes->{$1}/ge;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    88
	    $output .= " $name=\"$esc_value\"";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    89
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    90
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    91
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    92
    if ($self->{Newlines}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    93
	$output .= "\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    94
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    95
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    96
    $output .= ">";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    97
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    98
    $self->print($output);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    99
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   100
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   101
sub end_element {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   102
    my ($self, $element) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   103
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   104
    if ($self->SUPER::end_element($element) == 0) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   105
	$self->print_end_element($element);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   106
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   107
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   108
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   109
sub print_end_element {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   110
    my ($self, $element) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   111
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   112
    my $output = "</$element->{Name}"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   113
	. ($self->{Newlines} ? "\n" : "") . ">";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   114
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   115
    $self->print($output);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   116
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   117
sub characters {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   118
    my ($self, $characters) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   119
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   120
    my $output = $characters->{Data};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   121
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   122
    $output =~ s/([\&\<\>])/$escapes->{$1}/ge;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   123
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   124
    $self->print($output);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   125
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   126
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   127
sub processing_instruction {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   128
    my ($self, $pi) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   129
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   130
    my $nl = ($#{$self->{Names}} == -1) ? "\n" : "";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   131
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   132
    my $output;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   133
    if ($self->{IsSGML}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   134
	$output = "<?$pi->{Data}>\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   135
    } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   136
	if ($pi->{Data}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   137
	    $output = "<?$pi->{Target} $pi->{Data}?>$nl";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   138
	} else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   139
	    $output = "<?$pi->{Target}?>$nl";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   140
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   141
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   142
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   143
    $self->print($output);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   144
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   145
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   146
sub ignorable_whitespace {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   147
    my ($self, $whitespace) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   148
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   149
    $self->print($whitespace->{Data});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   150
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   151
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   152
sub comment {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   153
    my ($self, $comment) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   154
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   155
    my $nl = ($#{$self->{Names}} == -1) ? "\n" : "";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   156
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   157
    my $output = "<!-- $comment->{Data} -->$nl";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   158
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   159
    $self->print($output);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   160
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   161
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   162
sub print {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   163
    my ($self, $output) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   164
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   165
    $self->{Output_}->print($output)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   166
	if (defined($self->{Output_}));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   167
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   168
    push(@{$self->{Strings}}, $output)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   169
	if (defined($self->{AsString}));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   170
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   171
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   172
1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   173
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   174
__END__
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   175
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   176
=head1 NAME
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   177
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   178
XML::Handler::XMLWriter - a PerlSAX handler for writing readable XML
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   179
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   180
=head1 SYNOPSIS
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   181
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   182
 use XML::Parser::PerlSAX;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   183
 use XML::Handler::XMLWriter;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   184
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   185
 $my_handler = XML::Handler::XMLWriter->new( I<OPTIONS> );
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   186
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   187
 XML::Parser::PerlSAX->new->parse(Source => { SystemId => 'REC-xml-19980210.xml' },
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   188
                                  Handler => $my_handler);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   189
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   190
=head1 DESCRIPTION
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   191
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   192
C<XML::Handler::XMLWriter> is a PerlSAX handler for writing readable
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   193
XML (in contrast to Canonical XML, for example).
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   194
XML::Handler::XMLWriter can be used with a parser to reformat XML,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   195
with XML::DOM or XML::Grove to write out XML, or with other PerlSAX
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   196
modules that generate events.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   197
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   198
C<XML::Handler::XMLWriter> is intended to be used with PerlSAX event
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   199
generators and does not perform any checking itself (for example,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   200
matching start and end element events).  If you want to generate XML
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   201
directly from your Perl code, use the XML::Writer module.  XML::Writer
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   202
has an easy to use interface and performs many checks to make sure
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   203
that the XML you generate is well-formed.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   204
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   205
C<XML::Handler::XMLWriter> is a subclass of C<XML::Handler::Subs>.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   206
C<XML::Handler::XMLWriter> can be further subclassed to alter it's
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   207
behavior or to add element-specific handling.  In the subclass, each
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   208
time an element starts, a method by that name prefixed with `s_' is
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   209
called with the element to be processed.  Each time an element ends, a
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   210
method with that name prefixed with `e_' is called.  Any special
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   211
characters in the element name are replaced by underscores.  If there
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   212
isn't a start or end method for an element, the default action is to
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   213
write the start or end tag.  Start and end methods can use the
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   214
`C<print_start_element()>' and `C<print_end_element()>' methods to
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   215
print start or end tags.  Subclasses can call the `C<print()>' method
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   216
to write additional output.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   217
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   218
Subclassing XML::Handler::XMLWriter in this way is similar to
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   219
XML::Parser's Stream style.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   220
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   221
XML::Handler::Subs maintains a stack of element names,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   222
`C<$self->{Names}', and a stack of element nodes, `C<$self->{Nodes}>'
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   223
that can be used by subclasses.  The current element is pushed on the
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   224
stacks before calling an element-name start method and popped off the
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   225
stacks after calling the element-name end method.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   226
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   227
See XML::Handler::Subs for additional methods.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   228
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   229
In addition to the standard PerlSAX handler methods (see PerlSAX for
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   230
descriptions), XML::Handler::XMLWriter supports the following methods:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   231
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   232
=over 4
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   233
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   234
=item new( I<OPTIONS> )
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   235
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   236
Creates and returns a new instance of XML::Handler::XMLWriter with the
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   237
given I<OPTIONS>.  Options may be changed at any time by modifying
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   238
them directly in the hash returned.  I<OPTIONS> can be a list of key,
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   239
value pairs or a hash.  The following I<OPTIONS> are supported:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   240
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   241
=over 4
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   242
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   243
=item Output
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   244
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   245
An IO::Handle or one of it's subclasses (such as IO::File), if this
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   246
parameter is not present and the AsString option is not used, the
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   247
module will write to standard output.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   248
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   249
=item AsString
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   250
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   251
Return the generated XML as a string from the `C<parse()>' method of
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   252
the PerlSAX event generator.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   253
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   254
=item Newlines
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   255
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   256
A true or false value; if this parameter is present and its value is
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   257
true, then the module will insert an extra newline before the closing
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   258
delimiter of start, end, and empty tags to guarantee that the document
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   259
does not end up as a single, long line.  If the paramter is not
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   260
present, the module will not insert the newlines.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   261
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   262
=item IsSGML
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   263
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   264
A true or false value; if this parameter is present and its value is
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   265
true, then the module will generate SGML rather than XML.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   266
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   267
=back
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   268
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   269
=item print_start_element($element)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   270
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   271
Print a start tag for `C<$element>'.  This is the default action for
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   272
the PerlSAX `C<start_element()>' handler, but subclasses may use this
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   273
if they define a start method for an element.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   274
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   275
=item print_end_element($element)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   276
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   277
Prints an end tag for `C<$element>'.  This is the default action for
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   278
the PerlSAX `C<end_element()>' handler, but subclasses may use this
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   279
if they define a start method for an element.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   280
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   281
=item print($output)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   282
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   283
Write `C<$output>' to Output and/or append it to the string to be
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   284
returned.  Subclasses may use this to write additional output.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   285
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   286
=back
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   287
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   288
=head1 TODO
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   289
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   290
=over 4
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   291
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   292
=item *
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   293
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   294
An Elements option that provides finer control over newlines than the
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   295
Newlines option, where you can choose before and after newline for
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   296
element start and end tags.  Inspired by the Python XMLWriter.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   297
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   298
=item *
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   299
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   300
Support Doctype and XML declarations.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   301
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   302
=back
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   303
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   304
=head1 AUTHOR
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   305
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   306
Ken MacLeod, ken@bitsko.slc.ut.us
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   307
This module is partially derived from XML::Writer by David Megginson.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   308
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   309
=head1 SEE ALSO
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   310
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   311
perl(1), PerlSAX.pod(3)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   312
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   313
=cut