XML::Atomで atom:content[@type="text"] なentryを作りたい
XML::Atom::Contentに動的に追加する方向でやってみた.ついでに atom:content[@src] もできるように.
use XML::Atom; use XML::Atom::Entry; use XML::Atom::Content; $XML::Atom::DefaultVersion = '1.0'; { package XML::Atom::Content; use strict; use XML::Atom; no warnings 'redefine'; __PACKAGE__->mk_attr_accessors(qw( src )); *text = sub { my $content = shift; if (@_) { my $elem = $content->elem; my $data = shift; if (LIBXML) { $elem->removeChildNodes; $elem->appendChild(XML::LibXML::Text->new($data)); } else { $elem->removeChild($_) for $elem->getChildNodes; $elem->appendChild(XML::XPath::Node::Text->new($data)); } $content->version == 0.3 ? $content->mode('escaped') : $content->type('text'); } return $content->body; }; } my $entry = XML::Atom::Entry->new; $entry->title('title text'); my $content = XML::Atom::Content->new; $content->text('text content'); $entry->content($content); print $entry->as_xml; #<?xml version="1.0" encoding="utf-8"?> #<entry xmlns="http://www.w3.org/2005/Atom"> # <title>title text</title> # <content type="text">text content</content> #</entry>
text ってメソッド名はどうなんだろう,と我ながら思うがここはひとつ.
XML::Atom::Content (XML-Atom-0.25) に直接追加した patch はこちら.
--- Content.pm.orig Tue Nov 21 05:58:05 2006 +++ Content.pm Mon Jun 18 23:26:39 2007 @@ -4,7 +4,7 @@ use strict; use base qw( XML::Atom::Base ); -__PACKAGE__->mk_attr_accessors(qw( type mode )); +__PACKAGE__->mk_attr_accessors(qw( type mode src )); __PACKAGE__->mk_xml_attr_accessors(qw( lang base )); use Encode; @@ -137,6 +137,28 @@ } } $content->{__body}; +} + +sub text { + my $content = shift; + + if (@_) { + my $elem = $content->elem; + my $data = shift; + + if (LIBXML) { + $elem->removeChildNodes; + $elem->appendChild(XML::LibXML::Text->new($data)); + } + else { + $elem->removeChild($_) for $elem->getChildNodes; + $elem->appendChild(XML::XPath::Node::Text->new($data)); + } + + $content->version == 0.3 ? $content->mode('escaped') : $content->type('text'); + } + + return $content->body; } sub _is_printable {