Documentation fixes; added Chris' name to copyright notice; updated copyright year.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / XML.pm
1 package SQL::Translator::Producer::XML;
2
3 # -------------------------------------------------------------------
4 # $Id: XML.pm,v 1.5 2003-01-27 17:04:48 dlc Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 #                    darren chamberlain <darren@cpan.org>,
8 #                    Chris Mungall <cjm@fruitfly.org>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; version 2.
13 #
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # 02111-1307  USA
23 # -------------------------------------------------------------------
24
25 =head1 NAME
26
27 SQL::Translator::Producer::XML - XML output
28
29 =head1 SYNOPSIS
30
31   use SQL::Translator::Producer::XML;
32
33 =head1 DESCRIPTION
34
35 Meant to create some sort of usable XML output.
36
37 =cut
38
39 use strict;
40 use vars qw[ $VERSION $XML ];
41 $VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
42
43 # -------------------------------------------------------------------
44 sub produce {
45     my ( $translator, $data ) = @_;
46     my $indent = 0;
47     aggregate( '<schema>', $indent );
48     
49     $indent++;
50     for my $table ( 
51         map  { $_->[1] }
52         sort { $a->[0] <=> $b->[0] }
53         map  { [ $_->{'order'}, $_ ] }
54         values %$data
55     ) { 
56         aggregate( '<table>', $indent );
57         $indent++;
58
59         aggregate( "<name>$table->{'table_name'}</name>", $indent );
60         aggregate( "<order>$table->{'order'}</order>", $indent );
61
62         #
63         # Fields
64         #
65         aggregate( '<fields>', $indent );
66         for my $field ( 
67             map  { $_->[1] }
68             sort { $a->[0] <=> $b->[0] }
69             map  { [ $_->{'order'}, $_ ] }
70             values %{ $table->{'fields'} }
71         ) {
72             aggregate( '<field>', ++$indent );
73             $indent++;
74
75             for my $key ( keys %$field ) {
76                 my $val = defined $field->{ $key } ? $field->{ $key } : '';
77                    $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
78                 aggregate( "<$key>$val</$key>", $indent );
79             }
80
81             $indent--;
82             aggregate( "</field>", $indent-- );
83         }
84         aggregate( "</fields>", $indent );
85
86         #
87         # Indices
88         #
89         aggregate( '<indices>', $indent );
90         for my $index ( @{ $table->{'indices'} } ) {
91             aggregate( '<index>', ++$indent );
92             $indent++;
93
94             for my $key ( keys %$index ) {
95                 my $val = defined $index->{ $key } ? $index->{ $key } : '';
96                    $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val;
97                 aggregate( "<$key>$val</$key>", $indent );
98             }
99
100             $indent--;
101             aggregate( "</index>", $indent-- );
102         }
103         aggregate( "</indices>", $indent );
104
105         $indent--;
106         aggregate( "</table>", $indent );
107     }
108
109     $indent--;
110     aggregate( '</schema>', $indent );
111
112     return $XML;
113 }
114
115 # -------------------------------------------------------------------
116 sub aggregate {
117     my ( $text, $indent ) = @_;
118     $XML .= ('  ' x $indent) . "$text\n";
119 }
120
121 1;
122
123 # -------------------------------------------------------------------
124 # The eyes of fire, the nostrils of air,
125 # The mouth of water, the beard of earth.
126 # William Blake
127 # -------------------------------------------------------------------
128
129 =pod
130
131 =head1 AUTHOR
132
133 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
134
135 =head1 SEE ALSO
136
137 XML::Dumper;
138
139 =cut