Added producer args to control indenting, newlines and namespace prefixing.
Mark Addison [Thu, 8 Jul 2004 20:37:26 +0000 (20:37 +0000)]
lib/SQL/Translator/Producer/XML/SQLFairy.pm
t/17sqlfxml-producer.t

index 7db90bd..2b4834d 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Producer::XML::SQLFairy;
 
 # -------------------------------------------------------------------
-# $Id: SQLFairy.pm,v 1.13 2004-07-08 19:34:29 grommit Exp $
+# $Id: SQLFairy.pm,v 1.14 2004-07-08 20:37:26 grommit Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
 #                    darren chamberlain <darren@cpan.org>,
@@ -101,7 +101,33 @@ To see a complete example of the XML translate one of your schema :)
 
 =head1 ARGS
 
-Doesn't take any extra arguments.
+=over 4
+
+=item add_prefix
+
+Set to true to use the default namespace prefix of 'sqlf', instead of using
+the default namespace for
+C<http://sqlfairy.sourceforge.net/sqlfairy.xml namespace>
+
+e.g.
+
+ <!-- add_prefix=0 -->
+ <field name="foo" />
+
+ <!-- add_prefix=1 -->
+ <sqlf:field name="foo" />
+
+=item prefix
+
+Set to the namespace prefix you want to use for the
+C<http://sqlfairy.sourceforge.net/sqlfairy.xml namespace>
+
+e.g.
+
+ <!-- prefix='foo' -->
+ <foo:field name="foo" />
+
+=back
 
 =head1 LEGACY FORMAT
 
@@ -121,7 +147,7 @@ To convert your old format files simply pass them through the translator;
 
 use strict;
 use vars qw[ $VERSION @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/;
 
 use Exporter;
 use base qw(Exporter);
@@ -144,13 +170,19 @@ sub produce {
     my $translator  = shift;
     my $schema      = $translator->schema;
     $PArgs          = $translator->producer_args;
+    my $newlines    = defined $PArgs->{newlines} ? $PArgs->{newlines} : 1;
+    my $indent      = defined $PArgs->{indent}   ? $PArgs->{indent}   : 2;
     my $io          = IO::Scalar->new;
+
+    my $prefix = "";
+    $prefix    = $Name            if $PArgs->{add_prefix};
+    $prefix    = $PArgs->{prefix} if $PArgs->{prefix};
     my $xml         = XML::Writer->new(
         OUTPUT      => $io,
         NAMESPACES  => 1,
-        PREFIX_MAP  => { $Namespace => $Name },
-        DATA_MODE   => 1,
-        DATA_INDENT => 2,
+        PREFIX_MAP  => { $Namespace => $prefix },
+        DATA_MODE   => $newlines,
+        DATA_INDENT => $indent,
     );
 
     $xml->xmlDecl('UTF-8');
index 0f032e7..bfe808c 100644 (file)
@@ -47,31 +47,31 @@ use SQL::Translator::Producer::XML::SQLFairy;
 my ($obj,$ans,$xml);
 
 $ans = <<EOXML;
-<sqlf:schema name="" database="" xmlns:sqlf="http://sqlfairy.sourceforge.net/sqlfairy.xml">
-  <sqlf:table name="Basic" order="1">
-    <sqlf:fields>
-      <sqlf:field name="id" data_type="integer" size="10" is_nullable="0" is_auto_increment="1" is_primary_key="1" is_foreign_key="0" order="1">
-        <sqlf:comments>comment on id field</sqlf:comments>
-      </sqlf:field>
-      <sqlf:field name="title" data_type="varchar" size="100" is_nullable="0" default_value="hello" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="2">
-        <sqlf:comments></sqlf:comments>
-      </sqlf:field>
-      <sqlf:field name="description" data_type="text" size="65535" is_nullable="1" default_value="" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="3">
-        <sqlf:comments></sqlf:comments>
-      </sqlf:field>
-      <sqlf:field name="email" data_type="varchar" size="255" is_nullable="1" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="4">
-        <sqlf:comments></sqlf:comments>
-      </sqlf:field>
-    </sqlf:fields>
-    <sqlf:indices>
-      <sqlf:index name="titleindex" type="NORMAL" fields="title" options="" />
-    </sqlf:indices>
-    <sqlf:constraints>
-      <sqlf:constraint name="" type="PRIMARY KEY" fields="id" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
-      <sqlf:constraint name="" type="UNIQUE" fields="email" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
-    </sqlf:constraints>
-  </sqlf:table>
-</sqlf:schema>
+<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
+  <table name="Basic" order="1">
+    <fields>
+      <field name="id" data_type="integer" size="10" is_nullable="0" is_auto_increment="1" is_primary_key="1" is_foreign_key="0" order="1">
+        <comments>comment on id field</comments>
+      </field>
+      <field name="title" data_type="varchar" size="100" is_nullable="0" default_value="hello" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="2">
+        <comments></comments>
+      </field>
+      <field name="description" data_type="text" size="65535" is_nullable="1" default_value="" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="3">
+        <comments></comments>
+      </field>
+      <field name="email" data_type="varchar" size="255" is_nullable="1" is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="4">
+        <comments></comments>
+      </field>
+    </fields>
+    <indices>
+      <index name="titleindex" type="NORMAL" fields="title" options="" />
+    </indices>
+    <constraints>
+      <constraint name="" type="PRIMARY KEY" fields="id" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
+      <constraint name="" type="UNIQUE" fields="email" reference_table="" reference_fields="" on_delete="" on_update="" match_type="" expression="" options="" deferrable="1" />
+    </constraints>
+  </table>
+</schema>
 EOXML
 
 $obj = SQL::Translator->new(
@@ -99,11 +99,11 @@ eq_or_diff $xml, $ans, "XML looks right";
 my ($obj,$ans,$xml);
 
 $ans = <<EOXML;
-<sqlf:schema name="" database="" xmlns:sqlf="http://sqlfairy.sourceforge.net/sqlfairy.xml">
-  <sqlf:view name="foo_view" fields="name,age" order="1">
-    <sqlf:sql>select name, age from person</sqlf:sql>
-  </sqlf:view>
-</sqlf:schema>
+<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
+  <view name="foo_view" fields="name,age" order="1">
+    <sql>select name, age from person</sql>
+  </view>
+</schema>
 EOXML
 
     $obj = SQL::Translator->new(
@@ -143,11 +143,11 @@ EOXML
 my ($obj,$ans,$xml);
 
 $ans = <<EOXML;
-<sqlf:schema name="" database="" xmlns:sqlf="http://sqlfairy.sourceforge.net/sqlfairy.xml">
-  <sqlf:trigger name="foo_trigger" database_event="insert" on_table="foo" perform_action_when="after" order="1">
-    <sqlf:action>update modified=timestamp();</sqlf:action>
-  </sqlf:trigger>
-</sqlf:schema>
+<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
+  <trigger name="foo_trigger" database_event="insert" on_table="foo" perform_action_when="after" order="1">
+    <action>update modified=timestamp();</action>
+  </trigger>
+</schema>
 EOXML
 
     $obj = SQL::Translator->new(
@@ -190,12 +190,12 @@ EOXML
 my ($obj,$ans,$xml);
 
 $ans = <<EOXML;
-<sqlf:schema name="" database="" xmlns:sqlf="http://sqlfairy.sourceforge.net/sqlfairy.xml">
-  <sqlf:procedure name="foo_proc" parameters="foo,bar" owner="Nomar" order="1">
-    <sqlf:sql>select foo from bar</sqlf:sql>
-    <sqlf:comments>Go Sox!</sqlf:comments>
-  </sqlf:procedure>
-</sqlf:schema>
+<schema name="" database="" xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml">
+  <procedure name="foo_proc" parameters="foo,bar" owner="Nomar" order="1">
+    <sql>select foo from bar</sql>
+    <comments>Go Sox!</comments>
+  </procedure>
+</schema>
 EOXML
 
     $obj = SQL::Translator->new(