From: Darren Chamberlain Date: Tue, 6 May 2003 12:47:27 +0000 (+0000) Subject: Uses XML::Writer instead of aggregate() and a global. X-Git-Tag: v0.02~140 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fb6b0318edba55bf778078083531a6fbcd94323e;p=dbsrgits%2FSQL-Translator.git Uses XML::Writer instead of aggregate() and a global. Uses sqlfairy namespace (without a DTD yet, sorry). --- diff --git a/lib/SQL/Translator/Producer/XML.pm b/lib/SQL/Translator/Producer/XML.pm index 01727ac..326792c 100644 --- a/lib/SQL/Translator/Producer/XML.pm +++ b/lib/SQL/Translator/Producer/XML.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::XML; # ------------------------------------------------------------------- -# $Id: XML.pm,v 1.7 2003-05-03 15:21:12 kycl4rk Exp $ +# $Id: XML.pm,v 1.8 2003-05-06 12:47:27 dlc Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark , # darren chamberlain , @@ -23,92 +23,91 @@ package SQL::Translator::Producer::XML; # ------------------------------------------------------------------- use strict; -use vars qw[ $VERSION $XML ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/; +use vars qw[ $VERSION ]; +$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/; +use IO::Scalar; use SQL::Translator::Utils qw(header_comment); +use XML::Writer; + +my $sqlf_ns = 'http://sqlfairy.sourceforge.net/sqlfairy.xml'; # ------------------------------------------------------------------- sub produce { my ( $translator, $data ) = @_; - my $prargs = $translator->producer_args; - my $indent = 0; - aggregate('', $indent); - aggregate('', $indent); - aggregate(''); + #my $prargs = $translator->producer_args; + my $prargs = { }; + my $io = IO::Scalar->new; + + my $xml = XML::Writer->new(OUTPUT => $io, + NAMESPACES => 1, + PREFIX_MAP => { $sqlf_ns => 'sqlf' }, + DATA_MODE => 1, + DATA_INDENT => 2); + + + $xml->xmlDecl("UTF-8"); + $xml->comment(header_comment('', '')); + $xml->startTag([ $sqlf_ns => "schema" ]); - $indent++; for my $table ( map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ $_->{'order'}, $_ ] } values %$data - ) { - aggregate( '', $indent ); - $indent++; + ) { + $xml->startTag([ $sqlf_ns => "table" ]); - aggregate( "$table->{'table_name'}", $indent ); - aggregate( "$table->{'order'}", $indent ); + $xml->dataElement([ $sqlf_ns => "name" ], $table->{'table_name'}); + $xml->dataElement([ $sqlf_ns => "order" ], $table->{'order'}); # # Fields # - aggregate( '', $indent ); + $xml->startTag([ $sqlf_ns => "fields" ]); for my $field ( map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ $_->{'order'}, $_ ] } values %{ $table->{'fields'} } ) { - aggregate( '', ++$indent ); - $indent++; + $xml->startTag([ $sqlf_ns => "field" ]); for my $key ( keys %$field ) { my $val = defined $field->{ $key } ? $field->{ $key } : ''; $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val; - aggregate("<$key>$val", $indent) + $xml->dataElement([ $sqlf_ns => $key ], $val) if ($val || (!$val && $prargs->{'emit_empty_tags'})); } - $indent--; - aggregate("", $indent--); + $xml->endTag([ $sqlf_ns => "field" ]); } - aggregate( "", $indent ); + $xml->endTag([ $sqlf_ns => "fields" ]); # # Indices # - aggregate( '', $indent ); - for my $index ( @{ $table->{'indices'} } ) { - aggregate( '', ++$indent ); - $indent++; + $xml->startTag([ $sqlf_ns => "indices" ]); + for my $index (@{$table->{'indices'}}) { + $xml->startTag([ $sqlf_ns => "index" ]); - for my $key ( keys %$index ) { + for my $key (keys %$index) { my $val = defined $index->{ $key } ? $index->{ $key } : ''; $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val; - aggregate( "<$key>$val", $indent ); + $xml->dataElement([ $sqlf_ns => $key ], $val); } - $indent--; - aggregate( "", $indent-- ); + $xml->endTag([ $sqlf_ns => "index" ]); } - aggregate( "", $indent ); + $xml->endTag([ $sqlf_ns => "indices" ]); - $indent--; - aggregate( "
", $indent ); + $xml->endTag([ $sqlf_ns => "table" ]); } - $indent--; - aggregate( '
', $indent ); + $xml->endTag([ $sqlf_ns => "schema" ]); + $xml->end; - return $XML; -} - -# ------------------------------------------------------------------- -sub aggregate { - my $text = shift or return; - my $indent = shift || 0; - $XML .= (' ' x $indent) . "$text\n"; + return $io; } 1;