Commits

Shlomi Fish committed 32446e1

Extract a common writer/buffer.

Comments (0)

Files changed (3)

perl/modules/XML-Grammar-Fiction/lib/XML/Grammar/Fiction/FromProto.pm

 
 use Carp;
 use HTML::Entities ();
-use XML::Writer;
 
 use MooX 'late';
 
     return;
 }
 
+sub _get_default_xml_ns
+{
+    return $fiction_ns;
+}
+
 sub convert
 {
     my ($self, $args) = @_;
         Carp::confess("Parsing failed.");
     }
 
-    my $buffer = "";
-    $self->_buffer(\$buffer);
+    my $writer = $self->_writer;
 
-    my $writer = XML::Writer->new(
-        OUTPUT => $self->_buffer(),
-        ENCODING => "utf-8",
-        NAMESPACES => 1,
-        PREFIX_MAP =>
-        {
-             $fiction_ns => q{},
-             $xml_ns => 'xml',
-             $xlink_ns => 'xlink',
-        }
-    );
-
-    $writer->xmlDecl("utf-8");
     $writer->startTag([$fiction_ns, "document"], "version" => "0.2");
     $writer->startTag([$fiction_ns, "head"]);
     $writer->endTag();
 
-    # Now we're inside the body.
-    $self->_writer($writer);
-
     $self->_write_body({body => $tree});
 
     $writer->endTag();
 
-    return ${$self->_buffer()};
+    return ${$self->_flush_buffer()};
 }
 
 1;

perl/modules/XML-Grammar-Fiction/lib/XML/Grammar/FictionBase/TagsTree2XML.pm

 
 use XML::Grammar::Fiction::FromProto::Nodes;
 
+my $xml_ns = "http://www.w3.org/XML/1998/namespace";
+my $xlink_ns = "http://www.w3.org/1999/xlink";
 
 =head1 NAME
 
     },
 );
 
-has "_writer" => ('isa' => "Maybe[XML::Writer]", 'is' => "rw");
+sub _get_initial_writer
+{
+    my $self = shift;
 
-has '_buffer' => (is => "rw");
+    my $writer = XML::Writer->new(
+        OUTPUT => $self->_buffer(),
+        ENCODING => "utf-8",
+        NAMESPACES => 1,
+        PREFIX_MAP =>
+        {
+            $self->_get_default_xml_ns() => "",
+            $xml_ns => 'xml',
+            $xlink_ns => 'xlink',
+        },
+    );
+
+    $writer->xmlDecl("utf-8");
+
+    return $writer;
+}
+
+has "_writer" => (
+    'isa' => "Maybe[XML::Writer]",
+    'is' => "rw",
+    lazy => 1,
+    default => sub { return shift->_get_initial_writer(); },
+);
+
+sub _get_initial_buffer {
+    my $buffer = '';
+    return \$buffer;
+}
+
+has '_buffer' => (
+    is => "rw",
+    lazy => 1,
+    default => sub { return shift->_get_initial_buffer; },
+);
+
+sub _reset_buffer
+{
+    my $self = shift;
+
+    $self->_buffer($self->_get_initial_buffer());
+    $self->_writer($self->_get_initial_writer());
+
+    return;
+}
+
+sub _flush_buffer
+{
+    my $self = shift;
+
+    my $ret = $self->_buffer();
+    $self->_reset_buffer();
+
+    return $ret;
+}
 
 my %passthrough_elem =
 (

perl/modules/XML-Grammar-Fiction/lib/XML/Grammar/Screenplay/FromProto.pm

 package XML::Grammar::Screenplay::FromProto;
 
-use XML::Writer;
-
 use MooX 'late';
 
 extends("XML::Grammar::FictionBase::TagsTree2XML");
     return $self->_parser->process_text($self->_read_file($filename));
 }
 
+sub _get_default_xml_ns
+{
+    return $screenplay_ns;
+}
+
 sub convert
 {
     my ($self, $args) = @_;
         Carp::confess("Parsing failed.");
     }
 
-    my $buffer = "";
-    $self->_buffer(\$buffer);
+    my $writer = $self->_writer;
 
-    my $writer = XML::Writer->new(
-        OUTPUT => $self->_buffer(),
-        ENCODING => "utf-8",
-        NAMESPACES => 1,
-        PREFIX_MAP =>
-        {
-             $screenplay_ns => "",
-        }
-    );
-
-    $writer->xmlDecl("utf-8");
     $writer->startTag([$screenplay_ns, "document"]);
     $writer->startTag([$screenplay_ns, "head"]);
     $writer->endTag();
     $writer->startTag([$screenplay_ns, "body"], "id" => "index",);
 
-    # Now we're inside the body.
-    $self->_writer($writer);
-
     $self->_write_scene({scene => $tree});
 
     # Ending the body
 
     $writer->endTag();
 
-    return ${$self->_buffer()};
+    return ${$self->_flush_buffer()};
+    my $ret = $self->_buffer();
+    $self->_reset_buffer();
+    return ${$ret};
 }
 
 1;