| 1 | package Any::Renderer::XML; | ||
| 2 | |||
| 3 | # $Id: XML.pm,v 1.8 2006/08/22 20:14:09 johna Exp $ | ||
| 4 | |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use XML::Simple; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | $VERSION = sprintf"%d.%03d", q$Revision: 1.8 $ =~ /: (\d+)\.(\d+)/; | ||
| 10 | |||
| 11 | use constant FORMAT_NAME => "XML"; | ||
| 12 | |||
| 13 | sub new | ||
| 14 | { | ||
| 15 | my ( $class, $format, $options ) = @_; | ||
| 16 | die("Invalid format $format") unless($format eq FORMAT_NAME); | ||
| 17 | |||
| 18 | my $self = { | ||
| 19 | 'options' => $options, | ||
| 20 | }; | ||
| 21 | |||
| 22 | bless $self, $class; | ||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub render | ||
| 27 | { | ||
| 28 | my ( $self, $data ) = @_; | ||
| 29 | |||
| 30 | TRACE ( "Rendering XML data" ); | ||
| 31 | DUMP ( $data ); | ||
| 32 | |||
| 33 | my $charset = $self->{options}{Encoding} || 'ISO-8859-1'; | ||
| 34 | my %xmlopts = ( | ||
| 35 | 'noattr' => 1, | ||
| 36 | 'keyattr' => undef, | ||
| 37 | 'keeproot' => 1, | ||
| 38 | 'rootname' => 'output', | ||
| 39 | 'xmldecl' => qq{<?xml version="1.0" encoding="$charset" standalone="yes"?>}, | ||
| 40 | 'contentkey' => undef, | ||
| 41 | 'noescape' => 0, | ||
| 42 | ); | ||
| 43 | |||
| 44 | while ( my ( $k, $v ) = each %{ $self->{ 'options' }->{ 'XmlOptions' } } ) | ||
| 45 | { | ||
| 46 | # smash case to ensure the options override defaults | ||
| 47 | $xmlopts { lc $k } = $v; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (my $varname = $self->{ 'options' }{ 'VariableName' }) { | ||
| 51 | # VariableName overrides the 'options' hash | ||
| 52 | $xmlopts { 'rootname' } = $varname; | ||
| 53 | } | ||
| 54 | |||
| 55 | my $out = ''; | ||
| 56 | |||
| 57 | { | ||
| 58 | # XML::Simple 1.23 produces use-of-uninitialized... warnings | ||
| 59 | local $^W = 0; | ||
| 60 | $out = XML::Simple::XMLout ( $data, %xmlopts ); | ||
| 61 | } | ||
| 62 | |||
| 63 | return $out; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub requires_template | ||
| 67 | { | ||
| 68 | my ( $format ) = @_; | ||
| 69 | |||
| 70 | return 0; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub available_formats | ||
| 74 | { | ||
| 75 | return [ FORMAT_NAME ]; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub TRACE {} | ||
| 79 | sub DUMP {} | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | =head1 NAME | ||
| 84 | |||
| 85 | Any::Renderer::XML - render a data structure as element-only XML | ||
| 86 | |||
| 87 | =head1 SYNOPSIS | ||
| 88 | |||
| 89 | use Any::Renderer; | ||
| 90 | |||
| 91 | my %xml_options = (); | ||
| 92 | my %options = ( 'XmlOptions' => \%xml_options ); | ||
| 93 | my $format = "XML"; | ||
| 94 | my $r = new Any::Renderer ( $format, \%options ); | ||
| 95 | |||
| 96 | my $data_structure = [...]; # arbitrary structure code | ||
| 97 | my $string = $r->render ( $data_structure ); | ||
| 98 | |||
| 99 | You can get a list of all formats that this module handles using the following syntax: | ||
| 100 | |||
| 101 | my $list_ref = Any::Renderer::XML::available_formats (); | ||
| 102 | |||
| 103 | Also, determine whether or not a format requires a template with requires_template: | ||
| 104 | |||
| 105 | my $bool = Any::Renderer::XML::requires_template ( $format ); | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | Any::Renderer::XML renders any Perl data structure passed to it as element-only XML. For example: | ||
| 110 | |||
| 111 | perl -MAny::Renderer -e "print Any::Renderer->new('XML')->render({a => 1, b => [2,3]})" | ||
| 112 | |||
| 113 | results in: | ||
| 114 | |||
| 115 | <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> | ||
| 116 | <output> | ||
| 117 | <a>1</a> | ||
| 118 | <b>2</b> | ||
| 119 | <b>3</b> | ||
| 120 | </output> | ||
| 121 | |||
| 122 | The rendering process comes with all the caveats cited in the XML::Simple documentation. For example if your data structure contains binary data or ASCII control characters, then the XML document that is generated may not be well-formed. | ||
| 123 | |||
| 124 | =head1 FORMATS | ||
| 125 | |||
| 126 | =over 4 | ||
| 127 | |||
| 128 | =item XML | ||
| 129 | |||
| 130 | =back | ||
| 131 | |||
| 132 | =head1 METHODS | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item $r = new Any::Renderer::XML($format,\%options) | ||
| 137 | |||
| 138 | C<$format> must be C<XML>. | ||
| 139 | See L</OPTIONS> for a description of available options. | ||
| 140 | |||
| 141 | =item $scalar = $r->render($data_structure) | ||
| 142 | |||
| 143 | The main method. | ||
| 144 | |||
| 145 | =item $bool = Any::Renderer::XML::requires_template($format) | ||
| 146 | |||
| 147 | False in this case. | ||
| 148 | |||
| 149 | =item $list_ref = Any::Renderer::XML::available_formats() | ||
| 150 | |||
| 151 | Just the one - C<XML>. | ||
| 152 | |||
| 153 | =back | ||
| 154 | |||
| 155 | =head1 OPTIONS | ||
| 156 | |||
| 157 | =over 4 | ||
| 158 | |||
| 159 | =item XmlOptions | ||
| 160 | |||
| 161 | A hash reference of options that can be passed to XML::Simple::XMLout, see | ||
| 162 | L<XML::Simple> for a detailed description of all of the available | ||
| 163 | options. | ||
| 164 | |||
| 165 | =item VariableName | ||
| 166 | |||
| 167 | Set the XML root element name. You can also achieve this by setting the | ||
| 168 | C<RootName> or C<rootname> options passed to XML::Simple in the C<XML> options | ||
| 169 | hash. This is a shortcut to make this renderer behave like some of the other | ||
| 170 | renderer backends. | ||
| 171 | |||
| 172 | =item Encoding | ||
| 173 | |||
| 174 | Character set of the generated XML document. Defaults to ISO-8859-1. | ||
| 175 | |||
| 176 | =back | ||
| 177 | |||
| 178 | =head1 SEE ALSO | ||
| 179 | |||
| 180 | L<XML::Simple>, L<Any::Renderer> | ||
| 181 | |||
| 182 | =head1 VERSION | ||
| 183 | |||
| 184 | $Revision: 1.8 $ on $Date: 2006/08/22 20:14:09 $ by $Author: johna $ | ||
| 185 | |||
| 186 | =head1 AUTHOR | ||
| 187 | |||
| 188 | Matt Wilson <cpan _at_ bbc _dot_ co _dot_ uk> | ||
| 189 | |||
| 190 | =head1 COPYRIGHT | ||
| 191 | |||
| 192 | (c) BBC 2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL. | ||
| 193 | |||
| 194 | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | ||
| 195 | |||
| 196 | =cut | ||