XML::Atom::Syndication::Content - class representing Atom


XML-Atom-Syndication documentation Contained in the XML-Atom-Syndication distribution.

Index


Code Index:


XML-Atom-Syndication documentation Contained in the XML-Atom-Syndication distribution.

package XML::Atom::Syndication::Content;
use strict;

use base qw( XML::Atom::Syndication::Object );

use XML::Atom::Syndication::Util qw( utf8_off );
use MIME::Base64 qw( encode_base64 decode_base64 );

XML::Atom::Syndication::Content->mk_accessors('attribute', 'type', 'src');
XML::Atom::Syndication::Content->mk_accessors('attribute', 'mode')
  ;    # deprecated 0.3 accessors

sub init {
    my $content = shift;
    my %param = @_ == 1 ? (Body => $_[0]) : @_;    # escaped text is assumed.
    $content->SUPER::init(%param);
    my $e = $content->elem;
    if ($param{Body}) {
        $content->body($param{Body});
    }
    if ($param{Type}) {
        $content->type($param{Type});
    }
    $content;
}

sub element_name { 'content' }

sub body {
    my $content = shift;
    my $elem    = $content->elem;
    my $type    = $elem->attributes->{'{}type'};
    my $mode;
    if (!defined $type || $type eq 'text' || $type eq 'html') {
        $mode = 'escaped';
    } elsif (   $type eq 'xhtml'
             || $type =~
             m{^(text/xml|application/xml|text/xml-external-parsed-entity)$}
             || $type =~ m{[\+/]xml$}) {
        $mode = 'xml';
             } elsif ($type =~ m{text/.+}) {
        $mode = 'escaped';
             } else {
        $mode = 'base64';
    }
    if (@_) {    # set
        my $data = shift;
        if ($mode eq 'base64') {    # is binary
            utf8_off($data);
            require XML::Elemental::Characters;
            my $b = XML::Elemental::Characters->new;
            $b->data(encode_base64($data, ''));
            $b->parent($elem);
            $elem->contents([$b]);
        } elsif ($mode eq 'xml') {    # is xml
            my $node = $data;
            unless (ref $node) {
                my $copy =
                    '<div xmlns="http://www.w3.org/1999/xhtml">' . $data
                  . '</div>';
                eval {
                    require XML::Elemental;
                    my $parser = XML::Elemental->parser;
                    my $xml    = $parser->parse_string($copy);
                    $node = $xml->contents->[0];
                };
                return $content->error(
                                 "Error parsing content body string as XML: $@")
                  if $@;
            }
            $node->parent($elem);
            $elem->contents([$node]);
        } else {    # is text
            my $text = XML::Elemental::Characters->new;
            $text->data($data);
            $text->parent($elem);
            $elem->contents([$text]);
        }
        $content->{__body} = undef;
        1;
    } else {    # get
        unless (defined $content->{__body}) {
            if ($mode eq 'xml') {
                my @children =
                  grep { ref($_) eq 'XML::Elemental::Element' }
                  @{$elem->contents};
                if (@children) {
                    my ($local) =
                      $children[0]->name =~ /{.*}(.+)/;    # process name
                    @children = @{$children[0]->contents}
                      if (@children == 1 && $local eq 'div');

                    # $content->{__body} = '<div>';
                    my $w = XML::Atom::Syndication::Writer->new;
                    $w->set_prefix('', 'http://www.w3.org/1999/xhtml');
                    map { $content->{__body} .= $w->as_xml($_) } @children;

                    # $content->{__body} .= '</div>';
                } else {
                    $content->{__body} = $elem->text_content;
                }
                if ($] >= 5.008) {
                    require Encode;
                    Encode::_utf8_on($content->{__body});
                    $content->{__body} =~ s/&#x(\w{4});/chr(hex($1))/eg;
                    Encode::_utf8_off($content->{__body});
                }
            } elsif ($mode eq 'base64') {
                $content->{__body} = decode_base64($elem->text_content);
            } else {    # escaped
                $content->{__body} = $elem->text_content;
            }
        }
        $content->{__body};
    }
}

1;

__END__