home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / RSS.pm < prev    next >
Encoding:
Perl POD Document  |  2003-09-16  |  69.2 KB  |  2,216 lines

  1. # $Id: RSS.pm,v 1.2 2003/09/16 18:18:18 joker Exp $
  2. package XML::RSS;
  3.  
  4. use strict;
  5. use Carp;
  6. use XML::Parser;
  7. use vars qw($VERSION $AUTOLOAD @ISA $modules $AUTO_ADD);
  8.  
  9. $VERSION = '1.02';
  10. @ISA = qw(XML::Parser);
  11.  
  12. $AUTO_ADD = 0;
  13.  
  14. my %v0_9_ok_fields = (
  15.     channel => {
  16.         title       => '',
  17.         description => '',
  18.         link        => '',
  19.         },
  20.     image  => {
  21.         title => '',
  22.         url   => '',
  23.         link  => ''
  24.         },
  25.     textinput => {
  26.         title       => '',
  27.         description => '',
  28.         name        => '',
  29.         link        => ''
  30.         },
  31.     items => [],
  32.     num_items => 0,
  33.     version         => '',
  34.     encoding        => ''
  35. );
  36.  
  37. my %v0_9_1_ok_fields = (
  38.     channel => {
  39.         title          => '',
  40.         copyright      => '',
  41.         description    => '',
  42.         docs           => '',
  43.         language       => '',
  44.         lastBuildDate  => '',
  45.         'link'         => '',
  46.         managingEditor => '',
  47.         pubDate        => '',
  48.         rating         => '',
  49.         webMaster      => ''
  50.         },
  51.     image  => {
  52.         title       => '',
  53.         url         => '',
  54.         'link'      => '',
  55.         width       => '',
  56.         height      => '',
  57.         description => ''
  58.         },
  59.     skipDays  => {
  60.         day         => ''
  61.         },
  62.     skipHours => {
  63.         hour        => ''
  64.         },
  65.     textinput => {
  66.         title       => '',
  67.         description => '',
  68.         name        => '',
  69.         'link'      => ''
  70.         },
  71.     items           => [],
  72.     num_items       => 0,
  73.     version         => '',
  74.     encoding        => '',
  75.     category        => ''
  76. );
  77.  
  78. my %v1_0_ok_fields = (
  79.     channel => {
  80.         title       => '',
  81.         description => '',
  82.         link        => '',
  83.         },
  84.     image  => {
  85.         title => '',
  86.         url   => '',
  87.         link  => ''
  88.         },
  89.     textinput => {
  90.         title       => '',
  91.         description => '',
  92.         name        => '',
  93.         link        => ''
  94.         },
  95.     skipDays  => {
  96.         day         => ''
  97.         },
  98.     skipHours => {
  99.         hour        => ''
  100.         },
  101.     items => [],
  102.     num_items => 0,
  103.     version         => '',
  104.     encoding        => '',
  105.     output          => '',
  106. );
  107.  
  108. my %v2_0_ok_fields = (
  109.     channel => {
  110.         title          => '',
  111.         'link'         => '',
  112.         description    => '',
  113.         language       => '',
  114.         copyright      => '',
  115.         managingEditor => '',
  116.         webMaster      => '',
  117.         pubDate        => '',
  118.         lastBuildDate  => '',
  119.         category       => '',
  120.         generator      => '',
  121.         docs           => '',
  122.         cloud          => '',
  123.         ttl            => '',
  124.         image          => '',
  125.         textinput      => '',
  126.         skipHours      => '',
  127.         skipDays       => '',
  128.         },
  129.     image  => {
  130.         title       => '',
  131.         url         => '',
  132.         'link'      => '',
  133.         width       => '',
  134.         height      => '',
  135.         description => ''
  136.         },
  137.     skipDays  => {
  138.         day         => ''
  139.         },
  140.     skipHours => {
  141.         hour        => ''
  142.         },
  143.     textinput => {
  144.         title       => '',
  145.         description => '',
  146.         name        => '',
  147.         'link'      => ''
  148.         },
  149.     items           => [],
  150.     num_items       => 0,
  151.     version         => '',
  152.     encoding        => '',
  153.     category        => '',
  154.     cloud           => '',
  155.     ttl             => ''
  156. );
  157.  
  158. my %languages = (
  159.     'af'    => 'Afrikaans',
  160.     'sq'    => 'Albanian',
  161.     'eu'    => 'Basque',
  162.     'be'    => 'Belarusian',
  163.     'bg'    => 'Bulgarian',
  164.     'ca'    => 'Catalan',
  165.     'zh-cn' => 'Chinese (Simplified)',
  166.     'zh-tw' => 'Chinese (Traditional)',
  167.     'hr'    => 'Croatian',
  168.     'cs'    => 'Czech',
  169.     'da'    => 'Danish',
  170.     'nl'    => 'Dutch',
  171.     'nl-be' => 'Dutch (Belgium)',
  172.     'nl-nl' => 'Dutch (Netherlands)',
  173.     'en'    => 'English',
  174.     'en-au' => 'English (Australia)',
  175.     'en-bz' => 'English (Belize)',
  176.     'en-ca' => 'English (Canada)',
  177.     'en-ie' => 'English (Ireland)',
  178.     'en-jm' => 'English (Jamaica)',
  179.     'en-nz' => 'English (New Zealand)',
  180.     'en-ph' => 'English (Phillipines)',
  181.     'en-za' => 'English (South Africa)',
  182.     'en-tt' => 'English (Trinidad)',
  183.     'en-gb' => 'English (United Kingdom)',
  184.     'en-us' => 'English (United States)',
  185.     'en-zw' => 'English (Zimbabwe)',
  186.     'fo'    => 'Faeroese',
  187.     'fi'    => 'Finnish',
  188.     'fr'    => 'French',
  189.     'fr-be' => 'French (Belgium)',
  190.     'fr-ca' => 'French (Canada)',
  191.     'fr-fr' => 'French (France)',
  192.     'fr-lu' => 'French (Luxembourg)',
  193.     'fr-mc' => 'French (Monaco)',
  194.     'fr-ch' => 'French (Switzerland)',
  195.     'gl'    => 'Galician',
  196.     'gd'    => 'Gaelic',
  197.     'de'    => 'German',
  198.     'de-at' => 'German (Austria)',
  199.     'de-de' => 'German (Germany)',
  200.     'de-li' => 'German (Liechtenstein)',
  201.     'de-lu' => 'German (Luxembourg)',
  202.     'el'    => 'Greek',
  203.     'hu'    => 'Hungarian',
  204.     'is'    => 'Icelandic',
  205.     'in'    => 'Indonesian',
  206.     'ga'    => 'Irish',
  207.     'it'    => 'Italian',
  208.     'it-it' => 'Italian (Italy)',
  209.     'it-ch' => 'Italian (Switzerland)',
  210.     'ja'    => 'Japanese',
  211.     'ko'    => 'Korean',
  212.     'mk'    => 'Macedonian',
  213.     'no'    => 'Norwegian',
  214.     'pl'    => 'Polish',
  215.     'pt'    => 'Portuguese',
  216.     'pt-br' => 'Portuguese (Brazil)',
  217.     'pt-pt' => 'Portuguese (Portugal)',
  218.     'ro'    => 'Romanian',
  219.     'ro-mo' => 'Romanian (Moldova)',
  220.     'ro-ro' => 'Romanian (Romania)',
  221.     'ru'    => 'Russian',
  222.     'ru-mo' => 'Russian (Moldova)',
  223.     'ru-ru' => 'Russian (Russia)',
  224.     'sr'    => 'Serbian',
  225.     'sk'    => 'Slovak',
  226.     'sl'    => 'Slovenian',
  227.     'es'    => 'Spanish',
  228.     'es-ar' => 'Spanish (Argentina)',
  229.     'es-bo' => 'Spanish (Bolivia)',
  230.     'es-cl' => 'Spanish (Chile)',
  231.     'es-co' => 'Spanish (Colombia)',
  232.     'es-cr' => 'Spanish (Costa Rica)',
  233.     'es-do' => 'Spanish (Dominican Republic)',
  234.     'es-ec' => 'Spanish (Ecuador)',
  235.     'es-sv' => 'Spanish (El Salvador)',
  236.     'es-gt' => 'Spanish (Guatemala)',
  237.     'es-hn' => 'Spanish (Honduras)',
  238.     'es-mx' => 'Spanish (Mexico)',
  239.     'es-ni' => 'Spanish (Nicaragua)',
  240.     'es-pa' => 'Spanish (Panama)',
  241.     'es-py' => 'Spanish (Paraguay)',
  242.     'es-pe' => 'Spanish (Peru)',
  243.     'es-pr' => 'Spanish (Puerto Rico)',
  244.     'es-es' => 'Spanish (Spain)',
  245.     'es-uy' => 'Spanish (Uruguay)',
  246.     'es-ve' => 'Spanish (Venezuela)',
  247.     'sv'    => 'Swedish',
  248.     'sv-fi' => 'Swedish (Finland)',
  249.     'sv-se' => 'Swedish (Sweden)',
  250.     'tr'    => 'Turkish',
  251.     'uk'    => 'Ukranian'
  252.          );
  253.  
  254. # define required elements for RSS 0.9
  255. my $_REQ_v0_9 = {
  256.     channel => {
  257.     "title"          => [1,40],
  258.     "description"    => [1,500],
  259.     "link"           => [1,500]
  260.     },
  261.     image => {
  262.     "title"          => [1,40],
  263.     "url"            => [1,500],
  264.     "link"           => [1,500]
  265.     },
  266.     item => {
  267.     "title"          => [1,100],
  268.     "link"           => [1,500]
  269.     },
  270.     textinput => {
  271.     "title"          => [1,40],
  272.     "description"    => [1,100],
  273.     "name"           => [1,500],
  274.     "link"           => [1,500]
  275.     }
  276. };
  277.  
  278. # define required elements for RSS 0.91
  279. my $_REQ_v0_9_1 = {
  280.     channel => {
  281.     "title"          => [1,100],
  282.     "description"    => [1,500],
  283.     "link"           => [1,500],
  284.     "language"       => [1,5],
  285.     "rating"         => [0,500],
  286.     "copyright"      => [0,100],
  287.     "pubDate"        => [0,100],
  288.     "lastBuildDate"  => [0,100],
  289.     "docs"           => [0,500],
  290.     "managingEditor" => [0,100],
  291.     "webMaster"      => [0,100],
  292.     },
  293.     image => {
  294.     "title"          => [1,100],
  295.     "url"            => [1,500],
  296.     "link"           => [0,500],
  297.     "width"          => [0,144],
  298.     "height"         => [0,400],
  299.     "description"    => [0,500]
  300.     },
  301.     item => {
  302.     "title"          => [1,100],
  303.     "link"           => [1,500],
  304.     "description"    => [0,500]
  305.     },
  306.     textinput => {
  307.     "title"          => [1,100],
  308.     "description"    => [1,500],
  309.     "name"           => [1,20],
  310.     "link"           => [1,500]
  311.     },
  312.     skipHours => {
  313.     "hour"           => [1,23]
  314.     },
  315.     skipDays => {
  316.     "day"            => [1,10]
  317.     }
  318. };
  319.  
  320. # define required elements for RSS 2.0
  321. my $_REQ_v2_0 = {
  322.     channel => {
  323.         "title"          => [1,100],
  324.         "description"    => [1,500],
  325.         "link"           => [1,500],
  326.         "language"       => [0,5],
  327.         "rating"         => [0,500],
  328.         "copyright"      => [0,100],
  329.         "pubDate"        => [0,100],
  330.         "lastBuildDate"  => [0,100],
  331.         "docs"           => [0,500],
  332.         "managingEditor" => [0,100],
  333.         "webMaster"      => [0,100],
  334.     },
  335.     image => {
  336.         "title"          => [1,100],
  337.         "url"            => [1,500],
  338.         "link"           => [0,500],
  339.         "width"          => [0,144],
  340.         "height"         => [0,400],
  341.         "description"    => [0,500]
  342.         },
  343.     item => {
  344.         "title"          => [1,100],
  345.         "link"           => [1,500],
  346.         "description"    => [0,500]
  347.         },
  348.     textinput => {
  349.         "title"          => [1,100],
  350.         "description"    => [1,500],
  351.         "name"           => [1,20],
  352.         "link"           => [1,500]
  353.         },
  354.     skipHours => {
  355.         "hour"           => [1,23]
  356.         },
  357.     skipDays => {
  358.         "day"            => [1,10]
  359.         }
  360. };
  361.  
  362. my $namespace_map = {
  363.     rss10    => 'http://purl.org/rss/1.0/',
  364.     rss09    => 'http://my.netscape.com/rdf/simple/0.9/',
  365. #    rss091    => 'http://purl.org/rss/1.0/modules/rss091/',
  366.     rss20   => 'http://backend.userland.com/blogChannelModule',
  367. };
  368.  
  369. my $modules = {
  370.     'http://purl.org/rss/1.0/modules/syndication/' => 'syn',
  371.     'http://purl.org/dc/elements/1.1/' => 'dc',
  372.     'http://purl.org/rss/1.0/modules/taxonomy/' => 'taxo',
  373.     'http://webns.net/mvcb/' => 'admin'
  374. };
  375.  
  376. my %syn_ok_fields = (
  377.     'updateBase' => '',
  378.     'updateFrequency' => '',
  379.     'updatePeriod' => '',
  380. );
  381.  
  382. my %dc_ok_fields = (
  383.     'title' => '',
  384.     'creator' => '',
  385.     'subject' => '',
  386.     'description' => '',
  387.     'publisher' => '',
  388.     'contributor' => '',
  389.     'date' => '',
  390.     'type' => '',
  391.     'format' => '',
  392.     'identifier' => '',
  393.     'source' => '',
  394.     'language' => '',
  395.     'relation' => '',
  396.     'coverage' => '',
  397.     'rights' => '',
  398. );
  399.  
  400. my %rdf_resource_fields = (
  401.     'http://webns.net/mvcb/' =>  {
  402.             'generatorAgent' => 1,
  403.             'errorReportsTo' => 1
  404.     },
  405.     'http://purl.org/rss/1.0/modules/annotate/'    => {
  406.         'reference'    => 1
  407.     },
  408.     'http://my.theinfo.org/changed/1.0/rss/' => {
  409.         'server' => 1
  410.     }
  411. );
  412.  
  413. sub new {
  414.     my $class = shift;
  415.     
  416.     my $self = $class->SUPER::new(
  417.         Namespaces    => 1,
  418.         NoExpand      => 1,
  419.         ParseParamEnt => 0,
  420.         Handlers      => { 
  421.             Char    => \&handle_char,
  422.             XMLDecl => \&handle_dec,
  423.             Start   => \&handle_start
  424.             });
  425.             
  426.     bless $self, $class;
  427.     
  428.     $self->_initialize(@_);
  429.     
  430.     return $self;
  431. }
  432.  
  433. sub _initialize {
  434.     my $self = shift;
  435.     my %hash = @_;
  436.  
  437.     # internal hash
  438.     $self->{_internal} = {};
  439.  
  440.     # init num of items to 0
  441.     $self->{num_items} = 0;
  442.  
  443.     # adhere to Netscape limits; no by default
  444.     $self->{'strict'} = 0;
  445.  
  446.     # initialize items
  447.     $self->{items} = [];
  448.  
  449.     # namespaces
  450.     $self->{namespaces} = {};
  451.     $self->{rss_namespace} = '';
  452.  
  453.     # modules
  454.     $self->{modules} = $modules;
  455.  
  456.     # encode output from as_string?
  457.     (exists($hash{encode_output}))
  458.     ? ($self->{encode_output} = $hash{encode_output})
  459.         : ($self->{encode_output} = 1);
  460.  
  461.     #get version info
  462.     (exists($hash{version}))
  463.     ? ($self->{version} = $hash{version})
  464.         : ($self->{version} = '1.0');
  465.  
  466.     # set default output
  467.     (exists($hash{output}))
  468.     ? ($self->{output} = $hash{output})
  469.         : ($self->{output} = "");
  470.  
  471.     # encoding
  472.     (exists($hash{encoding}))
  473.     ? ($self->{encoding} = $hash{encoding})
  474.         : ($self->{encoding} = 'UTF-8');
  475.  
  476.     # initialize RSS data structure
  477.     # RSS version 0.9
  478.     if ($self->{version} eq '0.9') {
  479.     # Copy the hashes instead of using them directly to avoid
  480.         # problems with multiple XML::RSS objects being used concurrently
  481.         foreach my $i (qw(channel image textinput)) {
  482.         my %template=%{$v0_9_ok_fields{$i}};
  483.         $self->{$i} = \%template;
  484.         }
  485.  
  486.     # RSS version 0.91
  487.     } elsif ($self->{version} eq '0.91') {
  488.     foreach my $i (qw(channel image textinput skipDays skipHours)) {
  489.         my %template=%{$v0_9_1_ok_fields{$i}};
  490.         $self->{$i} = \%template;
  491.         }
  492.  
  493.     # RSS version 2.0
  494.     } elsif ($self->{version} eq '2.0') {
  495.         $self->{namespaces}->{'blogChannel'} = "http://backend.userland.com/blogChannelModule";
  496.         foreach my $i (qw(channel image textinput skipDays skipHours)) {
  497.             my %template=%{ $v2_0_ok_fields{$i} };
  498.             $self->{$i} = \%template;
  499.         }
  500.  
  501.     # RSS version 1.0
  502.     #} elsif ($self->{version} eq '1.0') {
  503.     } else {
  504.     foreach my $i (qw(channel image textinput)) {
  505.     #foreach my $i (keys(%v1_0_ok_fields)) {
  506.         my %template=%{$v1_0_ok_fields{$i}};
  507.         $self->{$i} = \%template;
  508.         }
  509.     }
  510. }
  511.  
  512. sub add_module {
  513.     my $self = shift;
  514.     my $hash = {@_};
  515.  
  516.     $hash->{prefix} =~ /^[a-z_][a-z0-9.-_]*$/ or
  517.     croak "a namespace prefix should look like [a-z_][a-z0-9.-_]*";
  518.  
  519.     $hash->{uri} or
  520.     croak "a URI must be provided in a namespace declaration";
  521.  
  522.       $self->{modules}->{$hash->{uri}} = $hash->{prefix};
  523. }
  524.  
  525. sub add_item {
  526.     my $self = shift;
  527.     my $hash = {@_};
  528.  
  529.     # strict Netscape Netcenter length checks
  530.     if ($self->{'strict'}) {
  531.         # make sure we have a title and link
  532.         croak "title and link elements are required"
  533.             unless ($hash->{title} && $hash->{'link'});
  534.  
  535.         # check string lengths
  536.         croak "title cannot exceed 100 characters in length"
  537.             if (length($hash->{title}) > 100);
  538.         croak "link cannot exceed 500 characters in length"
  539.             if (length($hash->{'link'}) > 500);
  540.         croak "description cannot exceed 500 characters in length"
  541.             if (exists($hash->{description})
  542.             && length($hash->{description}) > 500);
  543.     
  544.         # make sure there aren't already 15 items
  545.         croak "total items cannot exceed 15 " if (@{$self->{items}} >= 15);
  546.         }
  547.  
  548.     # add the item to the list
  549.     if (defined($hash->{mode}) && $hash->{mode} eq 'insert') {
  550.         unshift (@{$self->{items}}, $hash);
  551.         } else {
  552.         push (@{$self->{items}}, $hash);
  553.         }
  554.  
  555.     # return reference to the list of items
  556.     return $self->{items};
  557. }
  558.  
  559. sub as_rss_0_9 {
  560.     my $self = shift;
  561.     my $output;
  562.  
  563.     # XML declaration
  564.     my $encoding = exists $$self{encoding} ? qq| encoding="$$self{encoding}"| : '';
  565.     $output .= qq|<?xml version="1.0"$encoding?>\n\n|;
  566.  
  567.     # RDF root element
  568.     $output .= '<rdf:RDF'."\n".'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"'."\n";
  569.     $output .= 'xmlns="http://my.netscape.com/rdf/simple/0.9/">'."\n\n";
  570.  
  571.     ###################
  572.     # Channel Element #
  573.     ###################
  574.     $output .= '<channel>'."\n";
  575.     $output .= '<title>'. $self->encode($self->{channel}->{title}) .'</title>'."\n";
  576.     $output .= '<link>'. $self->encode($self->{channel}->{'link'}) .'</link>'."\n";
  577.     $output .= '<description>'. $self->encode($self->{channel}->{description}) .'</description>'."\n";
  578.     $output .= '</channel>'."\n\n";
  579.  
  580.     #################
  581.     # image element #
  582.     #################
  583.     if ($self->{image}->{url}) {
  584.     $output .= '<image>'."\n";
  585.  
  586.     # title
  587.     $output .= '<title>'. $self->encode($self->{image}->{title}) .'</title>'."\n";
  588.  
  589.     # url
  590.     $output .= '<url>'. $self->encode($self->{image}->{url}) .'</url>'."\n";
  591.  
  592.     # link
  593.     $output .= '<link>'. $self->encode($self->{image}->{'link'}) .'</link>'."\n"
  594.         if $self->{image}->{link};
  595.  
  596.     # end image element
  597.     $output .= '</image>'."\n\n";
  598.     }
  599.  
  600.     ################
  601.     # item element #
  602.     ################
  603.     foreach my $item (@{$self->{items}}) {
  604.     if ($item->{title}) {
  605.         $output .= '<item>'."\n";
  606.         $output .= '<title>'. $self->encode($item->{title}) .'</title>'."\n";
  607.         $output .= '<link>'. $self->encode($item->{'link'}) .'</link>'."\n";
  608.  
  609.         # end image element
  610.         $output .= '</item>'."\n\n";
  611.     }
  612.     }
  613.  
  614.     #####################
  615.     # textinput element #
  616.     #####################
  617.     if ($self->{textinput}->{'link'}) {
  618.     $output .= '<textinput>'."\n";
  619.     $output .= '<title>'. $self->encode($self->{textinput}->{title}) .'</title>'."\n";
  620.     $output .= '<description>'. $self->encode($self->{textinput}->{description}) .'</description>'."\n";
  621.     $output .= '<name>'. $self->encode($self->{textinput}->{name}) .'</name>'."\n";
  622.     $output .= '<link>'. $self->encode($self->{textinput}->{'link'}) .'</link>'."\n";
  623.     $output .= '</textinput>'."\n\n";
  624.     }
  625.  
  626.     $output .= '</rdf:RDF>';
  627.  
  628.     return $output;
  629. }
  630.  
  631. sub as_rss_0_9_1 {
  632.     my $self = shift;
  633.     my $output;
  634.  
  635.     # XML declaration
  636.     $output .= '<?xml version="1.0" encoding="'.$self->{encoding}.'"?>'."\n\n";
  637.  
  638.     # DOCTYPE
  639.     $output .= '<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"'."\n";
  640.     $output .= '            "http://my.netscape.com/publish/formats/rss-0.91.dtd">'."\n\n";
  641.  
  642.     # RSS root element
  643.     $output .= '<rss version="0.91">'."\n\n";
  644.  
  645.     ###################
  646.     # Channel Element #
  647.     ###################
  648.     $output .= '<channel>'."\n";
  649.     $output .= '<title>'. $self->encode($self->{channel}->{title}) .'</title>'."\n";
  650.     $output .= '<link>'. $self->encode($self->{channel}->{'link'}) .'</link>'."\n";
  651.     $output .= '<description>'. $self->encode($self->{channel}->{description}) .'</description>'."\n";
  652.  
  653.     # language
  654.     if ($self->{channel}->{'dc'}->{'language'}) {
  655.     $output .= '<language>'. $self->encode($self->{channel}->{'dc'}->{'language'}) .'</language>'."\n";
  656.     } elsif ($self->{channel}->{language}) {
  657.     $output .= '<language>'. $self->encode($self->{channel}->{language}).'</language>'."\n";
  658.     }
  659.  
  660.     # PICS rating
  661.     $output .= '<rating>'. $self->encode($self->{channel}->{rating}) .'</rating>'."\n"
  662.     if $self->{channel}->{rating};
  663.  
  664.     # copyright
  665.     if ($self->{channel}->{'dc'}->{'rights'}) {
  666.     $output .= '<copyright>'. $self->encode($self->{channel}->{'dc'}->{'rights'}) .'</copyright>'."\n";
  667.     } elsif ($self->{channel}->{copyright}) {
  668.     $output .= '<copyright>'. $self->encode($self->{channel}->{copyright}) .'</copyright>'."\n";
  669.     }
  670.  
  671.     # publication date
  672.     if ($self->{channel}->{'dc'}->{'date'}) {
  673.     $output .= '<pubDate>'. $self->encode($self->{channel}->{'dc'}->{'date'}) .'</pubDate>'."\n";
  674.     } elsif ($self->{channel}->{pubDate}) {
  675.     $output .= '<pubDate>'. $self->encode($self->{channel}->{pubDate}) .'</pubDate>'."\n";
  676.     }
  677.  
  678.     # last build date
  679.     if ($self->{channel}->{'dc'}->{'date'}) {
  680.     $output .= '<lastBuildDate>'. $self->encode($self->{channel}->{'dc'}->{'date'}) .'</lastBuildDate>'."\n";
  681.     } elsif ($self->{channel}->{lastBuildDate}) {
  682.     $output .= '<lastBuildDate>'. $self->encode($self->{channel}->{pubDate}) .'</lastBuildDate>'."\n";
  683.     }
  684.  
  685.     # external CDF URL
  686.     $output .= '<docs>'. $self->encode($self->{channel}->{docs}) .'</docs>'."\n"
  687.     if $self->{channel}->{docs};
  688.  
  689.     # managing editor
  690.     if ($self->{channel}->{'dc'}->{'publisher'}) {
  691.     $output .= '<managingEditor>'. $self->encode($self->{channel}->{'dc'}->{'publisher'}) .'</managingEditor>'."\n";
  692.     } elsif ($self->{channel}->{managingEditor}) {
  693.     $output .= '<managingEditor>'. $self->encode($self->{channel}->{managingEditor}) .'</managingEditor>'."\n";
  694.     }
  695.  
  696.     # webmaster
  697.     if ($self->{channel}->{'dc'}->{'creator'}) {
  698.     $output .= '<webMaster>'. $self->encode($self->{channel}->{'dc'}->{'creator'}) .'</webMaster>'."\n";
  699.     } elsif ($self->{channel}->{webMaster}) {
  700.     $output .= '<webMaster>'. $self->encode($self->{channel}->{webMaster}) .'</webMaster>'."\n";
  701.     }
  702.  
  703.     $output .= "\n";
  704.  
  705.     #################
  706.     # image element #
  707.     #################
  708.     if ($self->{image}->{url}) {
  709.     $output .= '<image>'."\n";
  710.  
  711.     # title
  712.     $output .= '<title>'. $self->encode($self->{image}->{title}) .'</title>'."\n";
  713.  
  714.     # url
  715.     $output .= '<url>'. $self->encode($self->{image}->{url}) .'</url>'."\n";
  716.  
  717.     # link
  718.     $output .= '<link>'. $self->encode($self->{image}->{'link'}) .'</link>'."\n"
  719.         if $self->{image}->{link};
  720.  
  721.     # image width
  722.     $output .= '<width>'. $self->encode($self->{image}->{width}) .'</width>'."\n"
  723.         if $self->{image}->{width};
  724.  
  725.     # image height
  726.     $output .= '<height>'. $self->encode($self->{image}->{height}) .'</height>'."\n"
  727.         if $self->{image}->{height};
  728.  
  729.     # description
  730.     $output .= '<description>'. $self->encode($self->{image}->{description}) .'</description>'."\n"
  731.         if $self->{image}->{description};
  732.  
  733.     # end image element
  734.     $output .= '</image>'."\n\n";
  735.     }
  736.  
  737.     ################
  738.     # item element #
  739.     ################
  740.     foreach my $item (@{$self->{items}}) {
  741.     if ($item->{title}) {
  742.         $output .= '<item>'."\n";
  743.         $output .= '<title>'. $self->encode($item->{title}) .'</title>'."\n";
  744.         $output .= '<link>'. $self->encode($item->{'link'}) .'</link>'."\n";
  745.  
  746.         $output .= '<description>'. $self->encode($item->{description}) .'</description>'."\n"
  747.         if $item->{description};
  748.  
  749.         # end image element
  750.         $output .= '</item>'."\n\n";
  751.     }
  752.     }
  753.  
  754.     #####################
  755.     # textinput element #
  756.     #####################
  757.     if ($self->{textinput}->{'link'}) {
  758.     $output .= '<textinput>'."\n";
  759.     $output .= '<title>'. $self->encode($self->{textinput}->{title}) .'</title>'."\n";
  760.     $output .= '<description>'. $self->encode($self->{textinput}->{description}) .'</description>'."\n";
  761.     $output .= '<name>'. $self->encode($self->{textinput}->{name}) .'</name>'."\n";
  762.     $output .= '<link>'. $self->encode($self->{textinput}->{'link'}) .'</link>'."\n";
  763.     $output .= '</textinput>'."\n\n";
  764.     }
  765.  
  766.     #####################
  767.     # skipHours element #
  768.     #####################
  769.     if ($self->{skipHours}->{hour}) {
  770.     $output .= '<skipHours>'."\n";
  771.     $output .= '<hour>'. $self->encode($self->{skipHours}->{hour}) .'</hour>'."\n";
  772.     $output .= '</skipHours>'."\n\n";
  773.     }
  774.  
  775.     ####################
  776.     # skipDays element #
  777.     ####################
  778.     if ($self->{skipDays}->{day}) {
  779.     $output .= '<skipDays>'."\n";
  780.     $output .= '<day>'. $self->encode($self->{skipDays}->{day}) .'</day>'."\n";
  781.     $output .= '</skipDays>'."\n\n";
  782.     }
  783.  
  784.     # end channel element
  785.     $output .= '</channel>'."\n";
  786.     $output .= '</rss>';
  787.  
  788.     return $output;
  789. }
  790.  
  791. sub as_rss_1_0 {
  792.     my $self = shift;
  793.     my $output;
  794.  
  795.     # XML declaration
  796.     $output .= '<?xml version="1.0" encoding="'.$self->{encoding}.'"?>'."\n\n";
  797.  
  798.     # RDF namespaces declaration
  799.     $output .="<rdf:RDF"."\n";
  800.     $output .=' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"'."\n";
  801.     $output .=' xmlns="http://purl.org/rss/1.0/"'."\n";
  802.  
  803.     # print all imported namespaces
  804.     while (my($k, $v) = each %{$self->{modules}}) {
  805.             $output.=" xmlns:$v=\"$k\"\n";
  806.     }
  807.  
  808.     $output .=">"."\n\n";
  809.  
  810.     ###################
  811.     # Channel Element #
  812.     ###################
  813.     unless ( defined($self->{channel}->{'about'}) ) {
  814.         $output .= '<channel rdf:about="'. $self->encode($self->{channel}->{'link'}) .'">'."\n";
  815.     } else {
  816.         $output .= '<channel rdf:about="'. $self->encode($self->{channel}->{'about'}) .'">'."\n";
  817.     }
  818.     # title
  819.     $output .= '<title>'. $self->encode($self->{channel}->{title}) .'</title>'."\n";
  820.  
  821.     # link
  822.     $output .= '<link>'. $self->encode($self->{channel}->{'link'}) .'</link>'."\n";
  823.  
  824.     # description
  825.     $output .= '<description>'. $self->encode($self->{channel}->{description}) .'</description>'."\n";
  826.  
  827.     # additional elements for RSS 0.91
  828.     # language
  829.     if ($self->{channel}->{'dc'}->{'language'}) {
  830.     $output .= '<dc:language>'. $self->encode($self->{channel}->{'dc'}->{'language'}) .'</dc:language>'."\n";
  831.     } elsif ($self->{channel}->{language}) {
  832.     $output .= '<dc:language>'.  $self->encode($self->{channel}->{language}) .'</dc:language>'."\n";
  833.     }
  834.  
  835.     # PICS rating - Dublin Core has not decided how to incorporate PICS ratings yet
  836.     #$$output .= '<rss091:rating>'.$self->{channel}->{rating}.'</rss091:rating>'."\n"
  837.     #$if $self->{channel}->{rating};
  838.  
  839.     # copyright
  840.     if ($self->{channel}->{'dc'}->{'rights'}) {
  841.     $output .= '<dc:rights>'.  $self->encode($self->{channel}->{'dc'}->{'rights'}) .'</dc:rights>'."\n";
  842.     } elsif ($self->{channel}->{copyright}) {
  843.     $output .= '<dc:rights>'.  $self->encode($self->{channel}->{copyright}) .'</dc:rights>'."\n";
  844.     }
  845.  
  846.     # publication date
  847.     if ($self->{channel}->{'dc'}->{'date'}) {
  848.     $output .= '<dc:date>'.  $self->encode($self->{channel}->{'dc'}->{'date'}) .'</dc:date>'."\n";
  849.     } elsif ($self->{channel}->{pubDate}) {
  850.     $output .= '<dc:date>'.  $self->encode($self->{channel}->{pubDate}) .'</dc:date>'."\n";
  851.     } elsif ($self->{channel}->{lastBuildDate}) {
  852.     $output .= '<dc:date>'.  $self->encode($self->{channel}->{lastBuildDate}) .'</dc:date>'."\n";
  853.     }
  854.  
  855.     # external CDF URL
  856.     #$output .= '<rss091:docs>'.$self->{channel}->{docs}.'</rss091:docs>'."\n"
  857.     #if $self->{channel}->{docs};
  858.  
  859.     # managing editor
  860.     if ($self->{channel}->{'dc'}->{'publisher'}) {
  861.     $output .= '<dc:publisher>'.  $self->encode($self->{channel}->{'dc'}->{'publisher'}) .'</dc:publisher>'."\n";
  862.     } elsif ($self->{channel}->{managingEditor}) {
  863.     $output .= '<dc:publisher>'.  $self->encode($self->{channel}->{managingEditor}) .'</dc:publisher>'."\n";
  864.     }
  865.  
  866.     # webmaster
  867.     if ($self->{channel}->{'dc'}->{'creator'}) {
  868.     $output .= '<dc:creator>'.  $self->encode($self->{channel}->{'dc'}->{'creator'}) .'</dc:creator>'."\n";
  869.     } elsif ($self->{channel}->{webMaster}) {
  870.     $output .= '<dc:creator>'.  $self->encode($self->{channel}->{webMaster})  .'</dc:creator>'."\n";
  871.     }
  872.  
  873.     # Dublin Core module
  874.     foreach my $dc ( keys %dc_ok_fields ) {
  875.     next if ($dc eq 'language'
  876.          || $dc eq 'creator'
  877.          || $dc eq 'publisher'
  878.          || $dc eq 'rights'
  879.          || $dc eq 'date');
  880.     $self->{channel}->{dc}->{$dc} and $output .= "<dc:$dc>".  $self->encode($self->{channel}->{dc}->{$dc}) ."</dc:$dc>\n";
  881.     }
  882.  
  883.     # Syndication module
  884.     foreach my $syn ( keys %syn_ok_fields ) {
  885.     $self->{channel}->{syn}->{$syn} and $output .= "<syn:$syn>".  $self->encode($self->{channel}->{syn}->{$syn}) ."</syn:$syn>\n";
  886.     }
  887.  
  888.     # Taxonomy module
  889.     if (exists($self->{'channel'}->{'taxo'}) && $self->{'channel'}->{'taxo'}) {
  890.     $output .= "<taxo:topics>\n  <rdf:Bag>\n";
  891.     foreach my $taxo (@{$self->{'channel'}->{'taxo'}}) {
  892.         $output.= "    <rdf:li resource=\"" . $self->encode($taxo) . "\" />\n";
  893.     }
  894.     $output .= "  </rdf:Bag>\n</taxo:topics>\n";
  895.     }
  896.  
  897.     # Ad-hoc modules
  898.     while ( my($url, $prefix) = each %{$self->{modules}} ) {
  899.         next if $prefix =~ /^(dc|syn|taxo)$/;
  900.         while ( my($el, $value) = each %{$self->{channel}->{$prefix}} ) {
  901.             if ( exists( $rdf_resource_fields{ $url } ) and
  902.                  exists( $rdf_resource_fields{ $url }{ $el }) )
  903.             {
  904.                 $output .= qq!<$prefix:$el rdf:resource="! .
  905.                            $self->encode($value) .
  906.                            qq!" />\n!;
  907.             }
  908.             else {
  909.                 $output .= "<$prefix:$el>".  $self->encode($value) ."</$prefix:$el>\n";
  910.             }
  911.         }
  912.       }
  913.  
  914.     # Seq items
  915.     $output .= "<items>\n <rdf:Seq>\n";
  916.  
  917.     foreach my $item (@{$self->{items}}) {
  918.     $output .= '  <rdf:li rdf:resource="'. $self->encode($item->{'link'}) .'" />'."\n";
  919.     }
  920.  
  921.     $output .= " </rdf:Seq>\n</items>\n";
  922.  
  923.     $self->{image}->{url} and
  924.         $output .= '<image rdf:resource="'. $self->encode($self->{image}->{url}) .'" />'."\n";
  925.  
  926.     $self->{textinput}->{'link'} and
  927.         $output .= '<textinput rdf:resource="'. $self->encode($self->{textinput}->{'link'}) .'" />'."\n";
  928.  
  929.     # end channel element
  930.     $output .= '</channel>'."\n\n";
  931.  
  932.     #################
  933.     # image element #
  934.     #################
  935.     if ($self->{image}->{url}) {
  936.         $output .= '<image rdf:about="'. $self->encode($self->{image}->{url}) .'">'."\n";
  937.  
  938.         # title
  939.         $output .= '<title>'.  $self->encode($self->{image}->{title}) .'</title>'."\n";
  940.  
  941.         # url
  942.         $output .= '<url>'.  $self->encode($self->{image}->{url}) .'</url>'."\n";
  943.  
  944.         # link
  945.         $output .= '<link>'.  $self->encode($self->{image}->{'link'}) .'</link>'."\n"
  946.             if $self->{image}->{link};
  947.  
  948.         # image width
  949.         #$output .= '<rss091:width>'.$self->{image}->{width}.'</rss091:width>'."\n"
  950.         #    if $self->{image}->{width};
  951.  
  952.         # image height
  953.         #$output .= '<rss091:height>'.$self->{image}->{height}.'</rss091:height>'."\n"
  954.         #    if $self->{image}->{height};
  955.  
  956.         # description
  957.         #$output .= '<rss091:description>'.$self->{image}->{description}.'</rss091:description>'."\n"
  958.         #    if $self->{image}->{description};
  959.  
  960.         # Dublin Core Modules
  961.         foreach my $dc ( keys %dc_ok_fields ) {
  962.             $self->{image}->{dc}->{$dc} and
  963.                 $output .= "<dc:$dc>".  $self->encode($self->{image}->{dc}->{$dc}) ."</dc:$dc>\n";
  964.         }
  965.  
  966.           # Ad-hoc modules for images
  967.         while ( my($url, $prefix) = each %{$self->{modules}} ) {
  968.             next if $prefix =~ /^(dc|syn|taxo)$/;
  969.             while ( my($el, $value) = each %{$self->{image}->{$prefix}} ) {
  970.                 if ( exists( $rdf_resource_fields{ $url } ) and
  971.                      exists( $rdf_resource_fields{ $url }{ $el }) )
  972.                 {
  973.                     $output .= qq!<$prefix:$el rdf:resource="! .
  974.                                $self->encode($value) .
  975.                                qq!" />\n!;
  976.                 }
  977.                 else {
  978.                     $output .= "<$prefix:$el>".  $self->encode($value) ."</$prefix:$el>\n";
  979.                 }
  980.             }
  981.           }
  982.         # end image element
  983.         $output .= '</image>'."\n\n";
  984.     } # end if ($self->{image}->{url}) {
  985.  
  986.     ################
  987.     # item element #
  988.     ################
  989.     foreach my $item (@{$self->{items}}) {
  990.     if ($item->{title}) {
  991.         $output .= '<item rdf:about="'. $self->encode($item->{'link'}) .'"';
  992.         $output .= ">\n";
  993.         $output .= '<title>'.  $self->encode($item->{title}) .'</title>'."\n";
  994.         $output .= '<link>'.  $self->encode($item->{'link'}) .'</link>'."\n";
  995.         $item->{description} and $output .= '<description>'.  $self->encode($item->{description}) .'</description>'."\n";
  996.  
  997.         # Dublin Core module
  998.         foreach my $dc ( keys %dc_ok_fields ) {
  999.             $item->{dc}->{$dc} and $output .= "<dc:$dc>".  $self->encode($item->{dc}->{$dc}) ."</dc:$dc>\n";
  1000.         }
  1001.  
  1002.         # Taxonomy module
  1003.         if (exists($item->{'taxo'})  && $item->{'taxo'}) {
  1004.         $output .= "<taxo:topics>\n  <rdf:Bag>\n";
  1005.         foreach my $taxo (@{$item->{'taxo'}}) {
  1006.             $output.= "    <rdf:li resource=\"$taxo\" />\n";
  1007.         }
  1008.         $output .= "  </rdf:Bag>\n</taxo:topics>\n";
  1009.         }
  1010.  
  1011.         # Ad-hoc modules
  1012.         while ( my($url, $prefix) = each %{$self->{modules}} ) {
  1013.             next if $prefix =~ /^(dc|syn|taxo)$/;
  1014.             while ( my($el, $value) = each %{$item->{$prefix}} ) {
  1015.                 if ( exists( $rdf_resource_fields{ $url } ) and
  1016.                      exists( $rdf_resource_fields{ $url }{ $el }) )
  1017.                 {
  1018.                     $output .= qq!<$prefix:$el rdf:resource="! .
  1019.                                $self->encode($value) .
  1020.                                qq!" />\n!;
  1021.                 }
  1022.                 else {
  1023.                     $output .= "<$prefix:$el>".  $self->encode($value) ."</$prefix:$el>\n";
  1024.                 }
  1025.             }
  1026.           }
  1027.         # end item element
  1028.         $output .= '</item>'."\n\n";
  1029.     }
  1030.     } # end foreach my $item (@{$self->{items}}) {
  1031.  
  1032.     #####################
  1033.     # textinput element #
  1034.     #####################
  1035.     if ($self->{textinput}->{'link'}) {
  1036.     $output .= '<textinput rdf:about="'. $self->encode($self->{textinput}->{'link'}) .'">'."\n";
  1037.     $output .= '<title>'.  $self->encode($self->{textinput}->{title}) .'</title>'."\n";
  1038.     $output .= '<description>'.  $self->encode($self->{textinput}->{description}) .'</description>'."\n";
  1039.     $output .= '<name>'.  $self->encode($self->{textinput}->{name}) .'</name>'."\n";
  1040.     $output .= '<link>'.  $self->encode($self->{textinput}->{'link'}) .'</link>'."\n";
  1041.  
  1042.     # Dublin Core module
  1043.     foreach my $dc ( keys %dc_ok_fields ) {
  1044.         $self->{textinput}->{dc}->{$dc}
  1045.             and $output .= "<dc:$dc>".  $self->encode($self->{textinput}->{dc}->{$dc}) ."</dc:$dc>\n";
  1046.     }
  1047.  
  1048.   # Ad-hoc modules
  1049.   while ( my($url, $prefix) = each %{$self->{modules}} ) {
  1050.     next if $prefix =~ /^(dc|syn|taxo)$/;
  1051.     while ( my($el, $value) = each %{$self->{textinput}->{$prefix}} ) {
  1052.           $output .= "<$prefix:$el>".  $self->encode($value) ."</$prefix:$el>\n";
  1053.     }
  1054.     }
  1055.  
  1056.     $output .= '</textinput>'."\n\n";
  1057.     }
  1058.  
  1059.     $output .= '</rdf:RDF>';
  1060. }
  1061.  
  1062. sub as_rss_2_0 {
  1063.     my $self = shift;
  1064.     my $output;
  1065.  
  1066.     # XML declaration
  1067.     $output .= '<?xml version="1.0" encoding="'.$self->{encoding}.'"?>'."\n\n";
  1068.  
  1069.     # DOCTYPE
  1070.     # $output .= '<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"'."\n";
  1071.     # $output .= '            "http://my.netscape.com/publish/formats/rss-0.91.dtd">'."\n\n";
  1072.  
  1073.     # RSS root element
  1074.     # $output .= '<rss version="0.91">'."\n\n";
  1075.     $output .= '<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">' . "\n\n";
  1076.  
  1077.     ###################
  1078.     # Channel Element #
  1079.     ###################
  1080.     $output .= '<channel>'."\n";
  1081.     $output .= '<title>'.$self->{channel}->{title}.'</title>'."\n";
  1082.     $output .= '<link>'.$self->{channel}->{'link'}.'</link>'."\n";
  1083.     $output .= '<description>'.$self->{channel}->{description}.'</description>'."\n";
  1084.  
  1085.     # language
  1086.     if ($self->{channel}->{'dc'}->{'language'}) {
  1087.         $output .= '<language>'.$self->{channel}->{'dc'}->{'language'}.'</language>'."\n";
  1088.     } elsif ($self->{channel}->{language}) {
  1089.         $output .= '<language>'.$self->{channel}->{language}.'</language>'."\n";
  1090.     }
  1091.  
  1092.     # PICS rating
  1093.     # Not supported by RSS 2.0
  1094.     # $output .= '<rating>'.$self->{channel}->{rating}.'</rating>'."\n"
  1095.     #    if $self->{channel}->{rating};
  1096.  
  1097.     # copyright
  1098.     if ($self->{channel}->{'dc'}->{'rights'}) {
  1099.         $output .= '<copyright>'.$self->{channel}->{'dc'}->{'rights'}.'</copyright>'."\n";
  1100.     } elsif ($self->{channel}->{copyright}) {
  1101.         $output .= '<copyright>'.$self->{channel}->{copyright}.'</copyright>'."\n";
  1102.     }
  1103.  
  1104.     # publication date
  1105.     if ($self->{channel}->{'dc'}->{'date'}) {
  1106.         $output .= '<pubDate>'.$self->{channel}->{'dc'}->{'date'}.'</pubDate>'."\n";
  1107.     } elsif ($self->{channel}->{pubDate}) {
  1108.         $output .= '<pubDate>'.$self->{channel}->{pubDate}.'</pubDate>'."\n";
  1109.     }
  1110.  
  1111.     # last build date
  1112.     if ($self->{channel}->{'dc'}->{'date'}) {
  1113.         $output .= '<lastBuildDate>'.$self->{channel}->{'dc'}->{lastBuildDate}.'</lastBuildDate>'."\n";
  1114.     } elsif ($self->{channel}->{lastBuildDate}) {
  1115.         $output .= '<lastBuildDate>'.$self->{channel}->{lastBuildDate}.'</lastBuildDate>'."\n";
  1116.     }
  1117.  
  1118.     # external CDF URL
  1119.     $output .= '<docs>'.$self->{channel}->{docs}.'</docs>'."\n"
  1120.         if $self->{channel}->{docs};
  1121.  
  1122.     # managing editor
  1123.     if ($self->{channel}->{'dc'}->{'publisher'}) {
  1124.         $output .= '<managingEditor>'.$self->{channel}->{'dc'}->{'publisher'}.'</managingEditor>'."\n";
  1125.     } elsif ($self->{channel}->{managingEditor}) {
  1126.         $output .= '<managingEditor>'.$self->{channel}->{managingEditor}.'</managingEditor>'."\n";
  1127.     }
  1128.  
  1129.     # webmaster
  1130.     if ($self->{channel}->{'dc'}->{'creator'}) {
  1131.         $output .= '<webMaster>'.$self->{channel}->{'dc'}->{'creator'}.'</webMaster>'."\n";
  1132.     } elsif ($self->{channel}->{webMaster}) {
  1133.         $output .= '<webMaster>'.$self->{channel}->{webMaster}.'</webMaster>'."\n";
  1134.     }
  1135.  
  1136.     # category
  1137.     if ($self->{channel}->{'dc'}->{'category'}) {
  1138.         $output .= '<category>'.$self->{channel}->{'dc'}->{'category'}.'</category>'."\n";
  1139.     } elsif ($self->{channel}->{category}) {
  1140.         $output .= '<category>'.$self->{channel}->{generator}.'</category>'."\n";
  1141.     }
  1142.  
  1143.     # generator
  1144.     if ($self->{channel}->{'dc'}->{'generator'}) {
  1145.         $output .= '<generator>'.$self->{channel}->{'dc'}->{'generator'}.'</generator>'."\n";
  1146.     } elsif ($self->{channel}->{generator}) {
  1147.         $output .= '<generator>'.$self->{channel}->{generator}.'</generator>'."\n";
  1148.     }
  1149.  
  1150.     # Insert cloud support here
  1151.  
  1152.     # ttl
  1153.     if ($self->{channel}->{'dc'}->{'ttl'}) {
  1154.         $output .= '<ttl>'.$self->{channel}->{'dc'}->{'ttl'}.'</ttl>'."\n";
  1155.     } elsif ($self->{channel}->{ttl}) {
  1156.         $output .= '<ttl>'.$self->{channel}->{ttl}.'</ttl>'."\n";
  1157.     }
  1158.  
  1159.  
  1160.  
  1161.     $output .= "\n";
  1162.  
  1163.     #################
  1164.     # image element #
  1165.     #################
  1166.     if ($self->{image}->{url}) {
  1167.         $output .= '<image>'."\n";
  1168.  
  1169.         # title
  1170.         $output .= '<title>'.$self->{image}->{title}.'</title>'."\n";
  1171.  
  1172.         # url
  1173.         $output .= '<url>'.$self->{image}->{url}.'</url>'."\n";
  1174.  
  1175.         # link
  1176.         $output .= '<link>'.$self->{image}->{'link'}.'</link>'."\n"
  1177.             if $self->{image}->{link};
  1178.  
  1179.         # image width
  1180.         $output .= '<width>'.$self->{image}->{width}.'</width>'."\n"
  1181.             if $self->{image}->{width};
  1182.  
  1183.         # image height
  1184.         $output .= '<height>'.$self->{image}->{height}.'</height>'."\n"
  1185.             if $self->{image}->{height};
  1186.  
  1187.         # description
  1188.         $output .= '<description>'.$self->{image}->{description}.'</description>'."\n"
  1189.             if $self->{image}->{description};
  1190.  
  1191.         # end image element
  1192.         $output .= '</image>'."\n\n";
  1193.     }
  1194.  
  1195.     ################
  1196.     # item element #
  1197.     ################
  1198.     foreach my $item (@{$self->{items}}) {
  1199.         if ($item->{title}) {
  1200.             $output .= '<item>'."\n";
  1201.             $output .= '<title>'.$item->{title}.'</title>'."\n";
  1202.             $output .= '<link>'.$item->{'link'}.'</link>'."\n";
  1203.  
  1204.             $output .= '<description>'.$item->{description}.'</description>'."\n"
  1205.                 if $item->{description};
  1206.  
  1207.             $output .= '<author>'.$item->{author}.'</author>'."\n"
  1208.                 if $item->{author};
  1209.  
  1210.             $output .= '<category>'.$item->{category}.'</category>'."\n"
  1211.                 if $item->{category};
  1212.  
  1213.             $output .= '<comments>'.$item->{comments}.'</comments>'."\n"
  1214.                 if $item->{comments};
  1215.  
  1216.             #TODO: Make this element work properly.
  1217.             # $output .= '<enclosure>'.$item->{enclosure}.'</enclosure>'."\n"
  1218.             #     if $item->{enclosure};
  1219.  
  1220.             # The unique identifier -- in this implementation we assume
  1221.             # that it's a permalink to the item, so we always include the
  1222.             # isPermaLink attribute.  Also, I call it permaLink in the
  1223.             # hash for purposes of clarity.
  1224.             $output .= '<guid isPermaLink="true">'.$item->{permaLink}.'</guid>'."\n"
  1225.                 if $item->{permaLink};
  1226.  
  1227.             $output .= '<pubDate>'.$item->{pubDate}.'</pubDate>'."\n"
  1228.                 if $item->{pubDate};
  1229.  
  1230.             $output .= '<source url="'.$item->{sourceUrl}.'">'.$item->{source}.'</source>'."\n"
  1231.                 if $item->{source} && $item->{sourceUrl};
  1232.  
  1233.             # end image element
  1234.             $output .= '</item>'."\n\n";
  1235.         }
  1236.     }
  1237.  
  1238.     #####################
  1239.     # textinput element #
  1240.     #####################
  1241.     if ($self->{textinput}->{'link'}) {
  1242.         $output .= '<textInput>'."\n";
  1243.         $output .= '<title>'.$self->{textinput}->{title}.'</title>'."\n";
  1244.         $output .= '<description>'.$self->{textinput}->{description}.'</description>'."\n";
  1245.         $output .= '<name>'.$self->{textinput}->{name}.'</name>'."\n";
  1246.         $output .= '<link>'.$self->{textinput}->{'link'}.'</link>'."\n";
  1247.         $output .= '</textInput>'."\n\n";
  1248.     }
  1249.  
  1250.     #####################
  1251.     # skipHours element #
  1252.     #####################
  1253.     if ($self->{skipHours}->{hour}) {
  1254.         $output .= '<skipHours>'."\n";
  1255.         $output .= '<hour>'.$self->{skipHours}->{hour}.'</hour>'."\n";
  1256.         $output .= '</skipHours>'."\n\n";
  1257.     }
  1258.  
  1259.     ####################
  1260.     # skipDays element #
  1261.     ####################
  1262.     if ($self->{skipDays}->{day}) {
  1263.         $output .= '<skipDays>'."\n";
  1264.         $output .= '<day>'.$self->{skipDays}->{day}.'</day>'."\n";
  1265.         $output .= '</skipDays>'."\n\n";
  1266.     }
  1267.  
  1268.     # end channel element
  1269.     $output .= '</channel>'."\n";
  1270.     $output .= '</rss>';
  1271.  
  1272.     return $output;
  1273. }
  1274.  
  1275. sub as_string {
  1276.     my $self = shift;
  1277.     my $version = ($self->{output} =~ /\d/) ? $self->{output} : $self->{version};
  1278.     my $output;
  1279.  
  1280.     ###########
  1281.     # RSS 0.9 #
  1282.     ###########
  1283.     if ($version eq '0.9') {
  1284.     $output = &as_rss_0_9($self);
  1285.  
  1286.     ############
  1287.     # RSS 0.91 #
  1288.     ############
  1289.     } elsif ($version eq '0.91') {
  1290.     $output = &as_rss_0_9_1($self);
  1291.  
  1292.     ###########
  1293.     # RSS 2.0 #
  1294.     ###########
  1295.     } elsif ($version eq '2.0') {
  1296.         $output = &as_rss_2_0($self);
  1297.  
  1298.     ###########
  1299.     # RSS 1.0 #
  1300.     ###########
  1301.     } else {
  1302.     $output = &as_rss_1_0($self);
  1303.     }
  1304.  
  1305.     return $output;
  1306. }
  1307.  
  1308. sub handle_char {
  1309.     # removed assumption that RSS is the default namespace - kellan, 11/5/02
  1310.  
  1311.     my ($self,$cdata) = (@_);
  1312.     
  1313.     # image element
  1314.     if (
  1315.         $self->within_element("image") ||
  1316.         $self->within_element($self->generate_ns_name("image",$self->{rss_namespace}))
  1317.     ) {
  1318.         my $ns = $self->namespace($self->current_element);
  1319.         # If it's in the default namespace
  1320.         if (
  1321.             (!$ns && !$self->{rss_namespace}) ||
  1322.             ($ns eq $self->{rss_namespace})
  1323.         ) {
  1324.             $self->{'image'}->{$self->current_element} .= $cdata;
  1325.         }
  1326.         else {
  1327.             # If it's in another namespace
  1328.             $self->{'image'}->{$ns}->{$self->current_element} .= $cdata;
  1329.  
  1330.             # If it's in a module namespace, provide a friendlier prefix duplicate
  1331.             $modules->{$ns} and $self->{'image'}->{$modules->{$ns}}->{$self->current_element} .= $cdata;
  1332.         }
  1333.  
  1334.     # item element
  1335.     }
  1336.     elsif (
  1337.          $self->within_element("item")
  1338.          || $self->within_element($self->generate_ns_name("item",$self->{rss_namespace}))
  1339.     ) {
  1340.         return if $self->within_element($self->generate_ns_name("topics",'http://purl.org/rss/1.0/modules/taxonomy/'));
  1341.  
  1342.         my $ns = $self->namespace($self->current_element);
  1343.  
  1344.         # If it's in the default RSS 1.0 namespace
  1345.         if (
  1346.             (!$ns && !$self->{rss_namespace}) ||
  1347.             ($ns eq $self->{rss_namespace})
  1348.         ) {
  1349.             $self->{'items'}->[$self->{num_items}-1]->{$self->current_element} .= $cdata;
  1350.         } else {
  1351.             # If it's in another namespace
  1352.             $self->{'items'}->[$self->{num_items}-1]->{$ns}->{$self->current_element} .= $cdata;
  1353.  
  1354.             # If it's in a module namespace, provide a friendlier prefix duplicate
  1355.             $modules->{$ns} and
  1356.                 $self->{'items'}->[$self->{num_items}-1]->{$modules->{$ns}}->{$self->current_element} .= $cdata;
  1357.         }
  1358.  
  1359.     # textinput element
  1360.     } elsif (
  1361.          $self->within_element("textinput")
  1362.          || $self->within_element($self->generate_ns_name("textinput",$self->{rss_namespace}))
  1363.     ) {
  1364.         my $ns = $self->namespace($self->current_element);
  1365.  
  1366.         # If it's in the default namespace
  1367.         if (
  1368.             (!$ns && !$self->{rss_namespace}) ||
  1369.             ($ns eq $self->{rss_namespace})
  1370.         ) {
  1371.             $self->{'textinput'}->{$self->current_element} .= $cdata;
  1372.         }
  1373.         else {
  1374.             # If it's in another namespace
  1375.             $self->{'textinput'}->{$ns}->{$self->current_element} .= $cdata;
  1376.  
  1377.             # If it's in a module namespace, provide a friendlier prefix duplicate
  1378.             $modules->{$ns} and $self->{'textinput'}->{$modules->{$ns}}->{$self->current_element} .= $cdata;
  1379.         }
  1380.  
  1381.     # skipHours element
  1382.     } elsif (
  1383.          $self->within_element("skipHours") ||
  1384.          $self->within_element($self->generate_ns_name("skipHours",$self->{rss_namespace}))
  1385.     ) {
  1386.         $self->{'skipHours'}->{$self->current_element} .= $cdata;
  1387.  
  1388.         # skipDays element
  1389.     } elsif (
  1390.          $self->within_element("skipDays") ||
  1391.         $self->within_element($self->generate_ns_name("skipDays",$self->{rss_namespace}))
  1392.     ) {
  1393.         $self->{'skipDays'}->{$self->current_element} .= $cdata;
  1394.  
  1395.     # channel element
  1396.     } elsif (
  1397.          $self->within_element("channel") ||
  1398.          $self->within_element($self->generate_ns_name("channel",$self->{rss_namespace}))
  1399.     ) {
  1400.         return if $self->within_element($self->generate_ns_name("topics",'http://purl.org/rss/1.0/modules/taxonomy/'));
  1401.  
  1402.         my $ns = $self->namespace($self->current_element);
  1403.  
  1404.         # If it's in the default namespace
  1405.         if (
  1406.             (!$ns && !$self->{rss_namespace}) ||
  1407.             ($ns eq $self->{rss_namespace})
  1408.         ) {
  1409.             $self->{'channel'}->{$self->current_element} .= $cdata;
  1410.         } else {
  1411.             # If it's in another namespace
  1412.             $self->{'channel'}->{$ns}->{$self->current_element} .= $cdata;
  1413.  
  1414.             # If it's in a module namespace, provide a friendlier prefix duplicate
  1415.             $modules->{$ns} and $self->{'channel'}->{$modules->{$ns}}->{$self->current_element} .= $cdata;
  1416.         }
  1417.     }
  1418. }
  1419.  
  1420. sub handle_dec {
  1421.     my ($self,$version,$encoding,$standalone) = (@_);
  1422.     $self->{encoding} = $encoding;
  1423.     #print "ENCODING: $encoding\n";
  1424. }
  1425.  
  1426. sub handle_start {
  1427.     my $self = shift;
  1428.     my $el   = shift;
  1429.     my %attribs = @_;
  1430.     
  1431.     # beginning of RSS 0.91
  1432.     if ($el eq 'rss') {
  1433.         if (exists($attribs{version})) {
  1434.             $self->{_internal}->{version} = $attribs{version};
  1435.         } else {
  1436.             croak "Malformed RSS: invalid version\n";
  1437.         }
  1438.  
  1439.         # beginning of RSS 1.0 or RSS 0.9
  1440.     } elsif ($el eq 'RDF') {
  1441.         my @prefixes = $self->new_ns_prefixes;
  1442.         foreach my $prefix (@prefixes) {
  1443.             my $uri = $self->expand_ns_prefix($prefix);
  1444.             $self->{namespaces}->{$prefix} = $uri;
  1445.             #print "$prefix = $uri\n";
  1446.         }
  1447.  
  1448.         # removed assumption that RSS is the default namespace - kellan, 11/5/02
  1449.         #
  1450.         foreach my $uri ( values %{ $self->{namespaces} } ) {
  1451.             if ( $namespace_map->{'rss10'} eq $uri ) {
  1452.                 $self->{_internal}->{version} = '1.0';
  1453.                 $self->{rss_namespace} = $uri;
  1454.                 last;
  1455.             }
  1456.             elsif ( $namespace_map->{'rss09'} eq $uri ) {
  1457.                 $self->{_internal}->{version} = '0.9';
  1458.                 $self->{rss_namespace} = $uri;
  1459.                 last;
  1460.             }
  1461.         }
  1462.  
  1463.         # failed to match a namespace
  1464.         if ( !defined($self->{_internal}->{version}) ) {
  1465.             croak "Malformed RSS: invalid version\n"
  1466.         }
  1467.         #if ($self->expand_ns_prefix('#default') =~ /\/1.0\//) {
  1468.         #    $self->{_internal}->{version} = '1.0';
  1469.         #} elsif ($self->expand_ns_prefix('#default') =~ /\/0.9\//) {
  1470.         #    $self->{_internal}->{version} = '0.9';
  1471.         #} else {
  1472.         #    croak "Malformed RSS: invalid version\n";
  1473.         #}
  1474.  
  1475.     # beginning of item element
  1476.     } elsif ($el eq 'item') {
  1477.         # deal with trouble makers who use mod_content :)
  1478.         my $ns =  $self->namespace( $el );
  1479.  
  1480.         if (
  1481.             (!$ns && !$self->{rss_namespace}) ||
  1482.             ($ns eq $self->{rss_namespace})
  1483.         ) {
  1484.             # increment item count
  1485.             $self->{num_items}++;
  1486.         }
  1487.     # beginning of taxo li element in item element
  1488.     #'http://purl.org/rss/1.0/modules/taxonomy/' => 'taxo'
  1489.     } elsif ($self->within_element($self->generate_ns_name("topics",'http://purl.org/rss/1.0/modules/taxonomy/'))
  1490.          && $self->within_element($self->generate_ns_name("item",$self->{namespace_map}->{'rss10'}))
  1491.          && $self->current_element eq 'Bag'
  1492.          && $el eq 'li') {
  1493.         #print "taxo: ", $attribs{'resource'},"\n";
  1494.         push(@{$self->{'items'}->[$self->{num_items}-1]->{'taxo'}},$attribs{'resource'});
  1495.         $self->{'modules'}->{'http://purl.org/rss/1.0/modules/taxonomy/'} = 'taxo';
  1496.  
  1497.     # beginning of taxo li in channel element
  1498.     } elsif ($self->within_element($self->generate_ns_name("topics",'http://purl.org/rss/1.0/modules/taxonomy/'))
  1499.          && $self->within_element($self->generate_ns_name("channel",$self->{namespace_map}->{'rss10'}))
  1500.          && $self->current_element eq 'Bag'
  1501.          && $el eq 'li') {
  1502.     push(@{$self->{'channel'}->{'taxo'}},$attribs{'resource'});
  1503.     $self->{'modules'}->{'http://purl.org/rss/1.0/modules/taxonomy/'} = 'taxo';
  1504.     }
  1505.     # beginning of a channel element that stores its info in rdf:resource
  1506.     elsif ( exists( $rdf_resource_fields{ $self->namespace($el) } ) and
  1507.             exists( $rdf_resource_fields{ $self->namespace($el) }{ $el } ) and
  1508.             $self->current_element eq 'channel' )
  1509.     {
  1510.         my $ns = $self->namespace( $el );
  1511.  
  1512.         if ( $ns eq $self->{rss_namespace} ) {
  1513.             $self->{channel}->{$el} = $attribs{resource};
  1514.         }
  1515.         else {
  1516.             $self->{channel}->{$ns}->{$el} = $attribs{resource};
  1517.             # add short cut
  1518.             #
  1519.             if ( exists( $modules->{ $ns } ) ) {
  1520.                 $ns = $modules->{ $ns };
  1521.                 $self->{channel}->{$ns}->{$el} = $attribs{resource};
  1522.             }
  1523.         }
  1524.     }
  1525.     # beginning of an item element that stores its info in rdf:resource
  1526.     elsif ( exists( $rdf_resource_fields{ $self->namespace($el) } ) and
  1527.             exists( $rdf_resource_fields{ $self->namespace($el) }{ $el } ) and
  1528.             $self->current_element eq 'item' )
  1529.     {
  1530.         my $ns = $self->namespace( $el );
  1531.  
  1532.         if ( $ns eq $self->{rss_namespace} ) {
  1533.             $self->{'items'}->[$self->{num_items}-1]->{ $el } = $attribs{resource};
  1534.         } else {
  1535.             $self->{'items'}->[$self->{num_items}-1]->{$ns}->{ $el } = $attribs{resource};
  1536.  
  1537.             # add short cut
  1538.             #
  1539.             if ( exists( $modules->{ $ns } ) ) {
  1540.                 $ns = $modules->{ $ns };
  1541.                 $self->{'items'}->[$self->{num_items}-1]->{$ns}->{ $el } = $attribs{resource};
  1542.             }
  1543.         }
  1544.     }
  1545. }
  1546.  
  1547. sub append {
  1548.     my($self, $inside, $cdata) = @_;
  1549.  
  1550.     my $ns = $self->namespace($self->current_element);
  1551.  
  1552.     # If it's in the default RSS 1.0 namespace
  1553.     if ($ns eq 'http://purl.org/rss/1.0/') {
  1554.         #$self->{'items'}->[$self->{num_items}-1]->{$self->current_element} .= $cdata;
  1555.         $inside->{$self->current_element} .= $cdata;
  1556.     }
  1557.  
  1558.     # If it's in another namespace
  1559.     #$self->{'items'}->[$self->{num_items}-1]->{$ns}->{$self->current_element} .= $cdata;
  1560.     $inside->{$ns}->{$self->current_element} .= $cdata;
  1561.  
  1562.     # If it's in a module namespace, provide a friendlier prefix duplicate
  1563.     #$modules->{$ns} and $self->{'items'}->[$self->{num_items}-1]->{$modules->{$ns}}->{$self->current_element} .= $cdata;
  1564.     $modules->{$ns} and $inside->{$modules->{$ns}}->{$self->current_element} .= $cdata;
  1565.  
  1566.     return $inside;
  1567. }
  1568.  
  1569. sub _auto_add_modules {
  1570.     my $self = shift;
  1571.     
  1572.     for my $ns (keys %{$self->{namespaces}}) {
  1573.        # skip default namespaces
  1574.        next if $ns eq "rdf" || $ns eq "#default"
  1575.             || exists $self->{modules}{ $self->{namespaces}{$ns} };
  1576.        $self->add_module(prefix => $ns, uri => $self->{namespaces}{$ns})
  1577.     }
  1578.     
  1579.     $self;
  1580. }
  1581.  
  1582. sub parse {
  1583.     my $self = shift;
  1584.     $self->_initialize((%$self));
  1585.     $self->SUPER::parse(shift);
  1586.     $self->_auto_add_modules if $AUTO_ADD;
  1587.     $self->{version} = $self->{_internal}->{version};
  1588. }
  1589.  
  1590. sub parsefile {
  1591.     my $self = shift;
  1592.     $self->_initialize((%$self));
  1593.     $self->SUPER::parsefile(shift);
  1594.     $self->_auto_add_modules if $AUTO_ADD;
  1595.     $self->{version} = $self->{_internal}->{version};
  1596. }
  1597.  
  1598. sub save {
  1599.     my ($self,$file) = @_;
  1600.     open(OUT,">$file") || croak "Cannot open file $file for write: $!";
  1601.     print OUT $self->as_string;
  1602.     close OUT;
  1603. }
  1604.  
  1605. sub strict {
  1606.     my ($self,$value) = @_;
  1607.     $self->{'strict'} = $value;
  1608. }
  1609.  
  1610. sub AUTOLOAD {
  1611.     my $self = shift;
  1612.     my $type = ref($self) || croak "$self is not an object\n";
  1613.     my $name = $AUTOLOAD;
  1614.     $name =~ s/.*://;
  1615.     return if $name eq 'DESTROY';
  1616.  
  1617.     croak "Unregistered entity: Can't access $name field in object of class $type"
  1618.         unless (exists $self->{$name});
  1619.  
  1620.     # return reference to RSS structure
  1621.     if (@_ == 1) {
  1622.     return $self->{$name}->{$_[0]} if defined $self->{$name}->{$_[0]};
  1623.  
  1624.     # we're going to set values here
  1625.     } elsif (@_ > 1) {
  1626.     my %hash = @_;
  1627.     my $_REQ;
  1628.  
  1629.     # make sure we have required elements and correct lengths
  1630.     if ($self->{'strict'}) {
  1631.         ($self->{version} eq '0.9')
  1632.         ? ($_REQ = $_REQ_v0_9)
  1633.             : ($_REQ = $_REQ_v0_9_1);
  1634.     }
  1635.  
  1636.     # store data in object
  1637.     foreach my $key (keys(%hash)) {
  1638.         if ($self->{'strict'}) {
  1639.         my $req_element = $_REQ->{$name}->{$key};
  1640.         confess "$key cannot exceed " . $req_element->[1] . " characters in length"
  1641.             if defined $req_element->[1] && length($hash{$key}) > $req_element->[1];
  1642.         }
  1643.         $self->{$name}->{$key} = $hash{$key};
  1644.     }
  1645.  
  1646.     # return value
  1647.     return $self->{$name};
  1648.  
  1649.     # otherwise, just return a reference to the whole thing
  1650.     } else {
  1651.     return $self->{$name};
  1652.     }
  1653.     return 0;
  1654.  
  1655.     # make sure we have all required elements
  1656.     #foreach my $key (keys(%{$_REQ->{$name}})) {
  1657.         #my $element = $_REQ->{$name}->{$key};
  1658.         #croak "$key is required in $name"
  1659.         #if ($element->[0] == 1) && (!defined($hash{$key}));
  1660.         #croak "$key cannot exceed ".$element->[1]." characters in length"
  1661.         #unless length($hash{$key}) <= $element->[1];
  1662.     #}
  1663. }
  1664.  
  1665. # the code here is a minorly tweaked version of code from
  1666. # Matts' rssmirror.pl script
  1667. #
  1668. my %entity = (
  1669.           nbsp   => " ",
  1670.           iexcl  => "¡",
  1671.           cent   => "¢",
  1672.           pound  => "£",
  1673.           curren => "¤",
  1674.           yen    => "¥",
  1675.           brvbar => "¦",
  1676.           sect   => "§",
  1677.           uml    => "¨",
  1678.           copy   => "©",
  1679.           ordf   => "ª",
  1680.           laquo  => "«",
  1681.           not    => "¬",
  1682.           shy    => "­",
  1683.           reg    => "®",
  1684.           macr   => "¯",
  1685.           deg    => "°",
  1686.           plusmn => "±",
  1687.           sup2   => "²",
  1688.           sup3   => "³",
  1689.           acute  => "´",
  1690.           micro  => "µ",
  1691.           para   => "¶",
  1692.           middot => "·",
  1693.           cedil  => "¸",
  1694.           sup1   => "¹",
  1695.           ordm   => "º",
  1696.           raquo  => "»",
  1697.           frac14 => "¼",
  1698.           frac12 => "½",
  1699.           frac34 => "¾",
  1700.           iquest => "¿",
  1701.           Agrave => "À",
  1702.           Aacute => "Á",
  1703.           Acirc  => "Â",
  1704.           Atilde => "Ã",
  1705.           Auml   => "Ä",
  1706.           Aring  => "Å",
  1707.           AElig  => "Æ",
  1708.           Ccedil => "Ç",
  1709.           Egrave => "È",
  1710.           Eacute => "É",
  1711.           Ecirc  => "Ê",
  1712.           Euml   => "Ë",
  1713.           Igrave => "Ì",
  1714.           Iacute => "Í",
  1715.           Icirc  => "Î",
  1716.           Iuml   => "Ï",
  1717.           ETH    => "Ð",
  1718.           Ntilde => "Ñ",
  1719.           Ograve => "Ò",
  1720.           Oacute => "Ó",
  1721.           Ocirc  => "Ô",
  1722.           Otilde => "Õ",
  1723.           Ouml   => "Ö",
  1724.           times  => "×",
  1725.           Oslash => "Ø",
  1726.           Ugrave => "Ù",
  1727.           Uacute => "Ú",
  1728.           Ucirc  => "Û",
  1729.           Uuml   => "Ü",
  1730.           Yacute => "Ý",
  1731.           THORN  => "Þ",
  1732.           szlig  => "ß",
  1733.           agrave => "à",
  1734.           aacute => "á",
  1735.           acirc  => "â",
  1736.           atilde => "ã",
  1737.           auml   => "ä",
  1738.           aring  => "å",
  1739.           aelig  => "æ",
  1740.           ccedil => "ç",
  1741.           egrave => "è",
  1742.           eacute => "é",
  1743.           ecirc  => "ê",
  1744.           euml   => "ë",
  1745.           igrave => "ì",
  1746.           iacute => "í",
  1747.           icirc  => "î",
  1748.           iuml   => "ï",
  1749.           eth    => "ð",
  1750.           ntilde => "ñ",
  1751.           ograve => "ò",
  1752.           oacute => "ó",
  1753.           ocirc  => "ô",
  1754.           otilde => "õ",
  1755.           ouml   => "ö",
  1756.           divide => "÷",
  1757.           oslash => "ø",
  1758.           ugrave => "ù",
  1759.           uacute => "ú",
  1760.           ucirc  => "û",
  1761.           uuml   => "ü",
  1762.           yacute => "ý",
  1763.           thorn  => "þ",
  1764.           yuml   => "ÿ",
  1765.           );
  1766.  
  1767. my $entities = join('|', keys %entity);
  1768.  
  1769. sub encode {
  1770.     my ($self, $text) = @_;
  1771.     return $text unless $self->{'encode_output'};
  1772.     
  1773.     my $encoded_text = '';
  1774.     
  1775.     while ( $text =~ s/(.*?)(\<\!\[CDATA\[.*?\]\]\>)//s ) {
  1776.         $encoded_text .= encode_text($1) . $2;
  1777.     }
  1778.     $encoded_text .= encode_text($text);
  1779.  
  1780.     return $encoded_text;
  1781. }
  1782.  
  1783. sub encode_text {
  1784.     my $text = shift;
  1785.     
  1786.     $text =~ s/&(?!(#[0-9]+|#x[0-9a-fA-F]+|\w+);)/&/g;
  1787.     $text =~ s/&($entities);/$entity{$1}/g;
  1788.     $text =~ s/</</g;
  1789.  
  1790.     return $text;
  1791. }
  1792.  
  1793. 1;
  1794. __END__
  1795.  
  1796. =head1 NAME
  1797.  
  1798. XML::RSS - creates and updates RSS files
  1799.  
  1800. =head1 SYNOPSIS
  1801.  
  1802.  # create an RSS 1.0 file (http://purl.org/rss/1.0/)
  1803.  use XML::RSS;
  1804.  my $rss = new XML::RSS (version => '1.0');
  1805.  $rss->channel(
  1806.    title        => "freshmeat.net",
  1807.    link         => "http://freshmeat.net",
  1808.    description  => "the one-stop-shop for all your Linux software needs",
  1809.    dc => {
  1810.      date       => '2000-08-23T07:00+00:00',
  1811.      subject    => "Linux Software",
  1812.      creator    => 'scoop@freshmeat.net',
  1813.      publisher  => 'scoop@freshmeat.net',
  1814.      rights     => 'Copyright 1999, Freshmeat.net',
  1815.      language   => 'en-us',
  1816.    },
  1817.    syn => {
  1818.      updatePeriod     => "hourly",
  1819.      updateFrequency  => "1",
  1820.      updateBase       => "1901-01-01T00:00+00:00",
  1821.    },
  1822.    taxo => [
  1823.      'http://dmoz.org/Computers/Internet',
  1824.      'http://dmoz.org/Computers/PC'
  1825.    ]
  1826.  );
  1827.  
  1828.  $rss->image(
  1829.    title  => "freshmeat.net",
  1830.    url    => "http://freshmeat.net/images/fm.mini.jpg",
  1831.    link   => "http://freshmeat.net",
  1832.    dc => {
  1833.      creator  => "G. Raphics (graphics at freshmeat.net)",
  1834.    },
  1835.  );
  1836.  
  1837.  $rss->add_item(
  1838.    title       => "GTKeyboard 0.85",
  1839.    link        => "http://freshmeat.net/news/1999/06/21/930003829.html",
  1840.    description => "GTKeyboard is a graphical keyboard that ...",
  1841.    dc => {
  1842.      subject  => "X11/Utilities",
  1843.      creator  => "David Allen (s2mdalle at titan.vcu.edu)",
  1844.    },
  1845.    taxo => [
  1846.      'http://dmoz.org/Computers/Internet',
  1847.      'http://dmoz.org/Computers/PC'
  1848.    ]
  1849.  );
  1850.  
  1851.  $rss->textinput(
  1852.    title        => "quick finder",
  1853.    description  => "Use the text input below to search freshmeat",
  1854.    name         => "query",
  1855.    link         => "http://core.freshmeat.net/search.php3",
  1856.  );
  1857.  
  1858.  # Optionally mixing in elements of a non-standard module/namespace
  1859.  
  1860.  $rss->add_module(prefix=>'my', uri=>'http://purl.org/my/rss/module/');
  1861.  
  1862.  $rss->add_item(
  1863.    title       => "xIrc 2.4pre2"
  1864.    link        => "http://freshmeat.net/projects/xirc/",
  1865.    description => "xIrc is an X11-based IRC client which ...",
  1866.    my => {
  1867.      rating    => "A+",
  1868.      category  => "X11/IRC",
  1869.    },
  1870.  );
  1871.  
  1872.   $rss->add_item (title=>$title, link=>$link, slash=>{ topic=>$topic });
  1873.  
  1874.  # create an RSS 0.91 file
  1875.  use XML::RSS;
  1876.  my $rss = new XML::RSS (version => '0.91');
  1877.  $rss->channel(title          => 'freshmeat.net',
  1878.                link           => 'http://freshmeat.net',
  1879.                language       => 'en',
  1880.                description    => 'the one-stop-shop for all your Linux software needs',
  1881.                rating         => '(PICS-1.1 "http://www.classify.org/safesurf/" 1 r (SS~~000 1))',
  1882.                copyright      => 'Copyright 1999, Freshmeat.net',
  1883.                pubDate        => 'Thu, 23 Aug 1999 07:00:00 GMT',
  1884.                lastBuildDate  => 'Thu, 23 Aug 1999 16:20:26 GMT',
  1885.                docs           => 'http://www.blahblah.org/fm.cdf',
  1886.                managingEditor => 'scoop@freshmeat.net',
  1887.                webMaster      => 'scoop@freshmeat.net'
  1888.                );
  1889.  
  1890.  $rss->image(title       => 'freshmeat.net',
  1891.              url         => 'http://freshmeat.net/images/fm.mini.jpg',
  1892.              link        => 'http://freshmeat.net',
  1893.              width       => 88,
  1894.              height      => 31,
  1895.              description => 'This is the Freshmeat image stupid'
  1896.              );
  1897.  
  1898.  $rss->add_item(title => "GTKeyboard 0.85",
  1899.                 link  => "http://freshmeat.net/news/1999/06/21/930003829.html",
  1900.         description => 'blah blah'
  1901.                 );
  1902.  
  1903.  $rss->skipHours(hour => 2);
  1904.  $rss->skipDays(day => 1);
  1905.  
  1906.  $rss->textinput(title => "quick finder",
  1907.                  description => "Use the text input below to search freshmeat",
  1908.                  name  => "query",
  1909.                  link  => "http://core.freshmeat.net/search.php3"
  1910.                  );
  1911.  
  1912.  # create an RSS 0.9 file
  1913.  use XML::RSS;
  1914.  my $rss = new XML::RSS (version => '0.9');
  1915.  $rss->channel(title => "freshmeat.net",
  1916.                link  => "http://freshmeat.net",
  1917.                description => "the one-stop-shop for all your Linux software needs",
  1918.                );
  1919.  
  1920.  $rss->image(title => "freshmeat.net",
  1921.              url   => "http://freshmeat.net/images/fm.mini.jpg",
  1922.              link  => "http://freshmeat.net"
  1923.              );
  1924.  
  1925.  $rss->add_item(title => "GTKeyboard 0.85",
  1926.                 link  => "http://freshmeat.net/news/1999/06/21/930003829.html"
  1927.                 );
  1928.  
  1929.  $rss->textinput(title => "quick finder",
  1930.                  description => "Use the text input below to search freshmeat",
  1931.                  name  => "query",
  1932.                  link  => "http://core.freshmeat.net/search.php3"
  1933.                  );
  1934.  
  1935.  # print the RSS as a string
  1936.  print $rss->as_string;
  1937.  
  1938.  # or save it to a file
  1939.  $rss->save("fm.rdf");
  1940.  
  1941.  # insert an item into an RSS file and removes the oldest item if
  1942.  # there are already 15 items
  1943.  my $rss = new XML::RSS;
  1944.  $rss->parsefile("fm.rdf");
  1945.  pop(@{$rss->{'items'}}) if (@{$rss->{'items'}} == 15);
  1946.  $rss->add_item(title => "MpegTV Player (mtv) 1.0.9.7",
  1947.                 link  => "http://freshmeat.net/news/1999/06/21/930003958.html",
  1948.                 mode  => 'insert'
  1949.                 );
  1950.  
  1951.  # parse a string instead of a file
  1952.  $rss->parse($string);
  1953.  
  1954.  # print the title and link of each RSS item
  1955.  foreach my $item (@{$rss->{'items'}}) {
  1956.      print "title: $item->{'title'}\n";
  1957.      print "link: $item->{'link'}\n\n";
  1958.  }
  1959.  
  1960.  # output the RSS 0.9 or 0.91 file as RSS 1.0
  1961.  $rss->{output} = '1.0';
  1962.  print $rss->as_string;
  1963.  
  1964. =head1 DESCRIPTION
  1965.  
  1966. This module provides a basic framework for creating and maintaining
  1967. RDF Site Summary (RSS) files. This distribution also contains many
  1968. examples that allow you to generate HTML from an RSS, convert between
  1969. 0.9, 0.91, and 1.0 version, and other nifty things.
  1970. This might be helpful if you want to include news feeds on your Web
  1971. site from sources like Slashot and Freshmeat or if you want to syndicate
  1972. your own content.
  1973.  
  1974. XML::RSS currently supports 0.9, 0.91, and 1.0 versions of RSS.
  1975. See http://my.netscape.com/publish/help/mnn20/quickstart.html
  1976. for information on RSS 0.91. See http://my.netscape.com/publish/help/
  1977. for RSS 0.9. See http://purl.org/rss/1.0/ for RSS 1.0.
  1978.  
  1979. RSS was originally developed by Netscape as the format for
  1980. Netscape Netcenter channels, however, many Web sites have since
  1981. adopted it as a simple syndication format. With the advent of RSS 1.0,
  1982. users are now able to syndication many different kinds of content
  1983. including news headlines, threaded measages, products catalogs, etc.
  1984.  
  1985. =head1 METHODS
  1986.  
  1987. =over 4
  1988.  
  1989. =item new XML::RSS (version=>$version, encoding=>$encoding,
  1990. output=>$output)
  1991.  
  1992. Constructor for XML::RSS. It returns a reference to an XML::RSS object.
  1993. You may also pass the RSS version and the XML encoding to use. The default
  1994. B<version> is 1.0. The default B<encoding> is UTF-8. You may also specify
  1995. the B<output> format regarless of the input version. This comes in handy
  1996. when you want to convert RSS between versions. The XML::RSS modules
  1997. will convert between any of the formats.  If you set <encode_output> XML::RSS
  1998. will make sure to encode any entities in generated RSS.  This is now on by default.
  1999.  
  2000. =item add_item (title=>$title, link=>$link, description=>$desc, mode=>$mode)
  2001.  
  2002. Adds an item to the XML::RSS object. B<mode> and B<description> are optional.
  2003. The default B<mode>
  2004. is append, which adds the item to the end of the list. To insert an item, set the mode
  2005. to B<insert>.
  2006.  
  2007. The items are stored in the array @{$obj->{'items'}} where
  2008. B<$obj> is a reference to an XML::RSS object.
  2009.  
  2010. =item as_string;
  2011.  
  2012. Returns a string containing the RSS for the XML::RSS object.  This
  2013. method will also encode special characters along the way.
  2014.  
  2015. =item channel (title=>$title, link=>$link, description=>$desc,
  2016. language=>$language, rating=>$rating, copyright=>$copyright,
  2017. pubDate=>$pubDate, lastBuildDate=>$lastBuild, docs=>$docs,
  2018. managingEditor=>$editor, webMaster=>$webMaster)
  2019.  
  2020.  
  2021. Channel information is required in RSS. The B<title> cannot
  2022. be more the 40 characters, the B<link> 500, and the B<description>
  2023. 500 when outputting RSS 0.9. B<title>, B<link>, and B<description>,
  2024. are required for RSS 1.0. B<language> is required for RSS 0.91.
  2025. The other parameters are optional for RSS 0.91 and 1.0.
  2026.  
  2027. To retreive the values of the channel, pass the name of the value
  2028. (title, link, or description) as the first and only argument
  2029. like so:
  2030.  
  2031. $title = channel('title');
  2032.  
  2033. =item image (title=>$title, url=>$url, link=>$link, width=>$width,
  2034. height=>$height, description=>$desc)
  2035.  
  2036. Adding an image is not required. B<url> is the URL of the
  2037. image, B<link> is the URL the image is linked to. B<title>, B<url>,
  2038. and B<link> parameters are required if you are going to
  2039. use an image in your RSS file. The remaining image elements are used
  2040. in RSS 0.91 or optionally imported into RSS 1.0 via the rss091 namespace.
  2041.  
  2042. The method for retrieving the values for the image is the same as it
  2043. is for B<channel()>.
  2044.  
  2045. =item parse ($string)
  2046.  
  2047. Parses an RDF Site Summary which is passed into B<parse()> as the first parameter.
  2048.  
  2049. See the add_module() method for instructions on automatically adding
  2050. modules as a string is parsed.
  2051.  
  2052. =item parsefile ($file)
  2053.  
  2054. Same as B<parse()> except it parses a file rather than a string.
  2055.  
  2056. See the add_module() method for instructions on automatically adding
  2057. modules as a string is parsed.
  2058.  
  2059. =item save ($file)
  2060.  
  2061. Saves the RSS to a specified file.
  2062.  
  2063. =item skipHours (hour=>$hour)
  2064.  
  2065. Specifies the number of hours that a server should wait before retrieving
  2066. the RSS file. The B<hour> parameter is required if the skipHours method
  2067. is used. This method is currently broken.
  2068.  
  2069. =item skipDays (day=>$day)
  2070.  
  2071. Specified the number of days that a server should wait before retrieving
  2072. the RSS file. The B<day> parameter is required if the skipDays method
  2073. is used. This method is currently broken.
  2074.  
  2075. =item strict ($boolean)
  2076.  
  2077. If it's set to 1, it will adhere to the lengths as specified
  2078. by Netscape Netcenter requirements. It's set to 0 by default.
  2079. Use it if the RSS file you're generating is for Netcenter.
  2080. strict will only work for RSS 0.9 and 0.91. Do not use it for
  2081. RSS 1.0.
  2082.  
  2083. =item textinput (title=>$title, description=>$desc, name=>$name, link=>$link);
  2084.  
  2085. This RSS element is also optional. Using it allows users to submit a Query
  2086. to a program on a Web server via an HTML form. B<name> is the HTML form name
  2087. and B<link> is the URL to the program. Content is submitted using the GET
  2088. method.
  2089.  
  2090. Access to the B<textinput> values is the the same as B<channel()> and
  2091. B<image()>.
  2092.  
  2093. =item add_module(prefix=>$prefix, uri=>$uri)
  2094.  
  2095. Adds a module namespace declaration to the XML::RSS object, allowing you
  2096. to add modularity outside of the the standard RSS 1.0 modules.  At present,
  2097. the standard modules Dublin Core (dc) and Syndication (syn) are predefined
  2098. for your convenience. The Taxonomy (taxo) module is also internally supported.
  2099.  
  2100. The modules are stored in the hash %{$obj->{'modules'}} where
  2101. B<$obj> is a reference to an XML::RSS object.
  2102.  
  2103. If you want to automatically add modules that the parser finds in
  2104. namespaces, set the $XML::RSS::AUTO_ADD variable to a true value.  By
  2105. default the value is false.
  2106.  
  2107. =back
  2108.  
  2109. =head2 RSS 1.0 MODULES
  2110.  
  2111. XML-Namespace-based modularization affords RSS 1.0 compartmentalized
  2112. extensibility.  The only modules that ship "in the box" with RSS 1.0
  2113. are Dublin Core (http://purl.org/rss/1.0/modules/dc/), Syndication
  2114. (http://purl.org/rss/1.0/modules/syndication/), and Taxonomy
  2115. (http://purl.org/rss/1.0/modules/taxonomy/).  Consult the appropriate
  2116. module's documentation for further information.
  2117.  
  2118. Adding items from these modules in XML::RSS is as simple as adding other
  2119. attributes such as title, link, and description.  The only difference
  2120. is the compartmentalization of their key/value paris in a second-level
  2121. hash.
  2122.  
  2123.   $rss->add_item (title=>$title, link=>$link, dc=>{ subject=>$subject, creator=>$creator });
  2124.  
  2125. For elements of the Dublin Core module, use the key 'dc'.  For elements
  2126. of the Syndication module, 'syn'.  For elements of the Taxonomy module,
  2127. 'taxo'. These are the prefixes used in the RSS XML document itself.
  2128. They are associated with appropriate URI-based namespaces:
  2129.  
  2130.   syn:  http://purl.org/rss/1.0/modules/syndication/
  2131.   dc:   http://purl.org/dc/elements/1.1/
  2132.   taxo: http://purl.org/rss/1.0/modules/taxonomy/
  2133.  
  2134. Dublin Core elements may occur in channel, image, item(s), and textinput
  2135. -- albeit uncomming to find them under image and textinput.  Syndication
  2136. elements are limited to the channel element. Taxonomy elements can occur
  2137. in the channel or item elements.
  2138.  
  2139. Access to module elements after parsing an RSS 1.0 document using
  2140. XML::RSS is via either the prefix or namespace URI for your convenience.
  2141.  
  2142.   print $rss->{items}->[0]->{dc}->{subject};
  2143.  
  2144.   or
  2145.  
  2146.   print $rss->{items}->[0]->{'http://purl.org/dc/elements/1.1/'}->{subject};
  2147.  
  2148. XML::RSS also has support for "non-standard" RSS 1.0 modularization at
  2149. the channel, image, item, and textinput levels.  Parsing an RSS document
  2150. grabs any elements of other namespaces which might appear.  XML::RSS
  2151. also allows the inclusion of arbitrary namespaces and associated elements
  2152. when building  RSS documents.
  2153.  
  2154. For example, to add elements of a made-up "My" module, first declare the
  2155. namespace by associating a prefix with a URI:
  2156.  
  2157.   $rss->add_module(prefix=>'my', uri=>'http://purl.org/my/rss/module/');
  2158.  
  2159. Then proceed as usual:
  2160.  
  2161.   $rss->add_item (title=>$title, link=>$link, my=>{ rating=>$rating });
  2162.  
  2163. Non-standard namespaces are not, however, currently accessible via a simple
  2164. prefix; access them via their namespace URL like so:
  2165.  
  2166.   print $rss->{items}->[0]->{'http://purl.org/my/rss/module/'}->{rating};
  2167.  
  2168. XML::RSS will continue to provide built-in support for standard RSS 1.0
  2169. modules as they appear.
  2170.  
  2171. =head1 SOURCE AVAILABILITY
  2172.  
  2173. This source is part of a SourceForge project which always has the
  2174. latest sources in CVS, as well as all of the previous releases.
  2175.  
  2176.     https://sourceforge.net/projects/perl-rss/
  2177.     http://perl-rss.sourceforge.net
  2178.  
  2179. If, for some reason, I disappear from the world, one of the other
  2180. members of the project can shepherd this module appropriately.
  2181.  
  2182. =head1 AUTHOR
  2183.  
  2184.     Original code: Jonathan Eisenzopf <eisen@pobox.com>
  2185.     Further changes: Rael Dornfest <rael@oreilly.com>
  2186.     
  2187.     Currently: perl-rss project (http://perl-rss.sourceforge.net)
  2188.  
  2189.  
  2190. =head1 COPYRIGHT
  2191.  
  2192. Copyright (c) 2001 Jonathan Eisenzopf <eisen@pobox.com>
  2193. and Rael Dornfest <rael@oreilly.com>
  2194.  
  2195. XML::RSS is free software. You can redistribute it and/or
  2196. modify it under the same terms as Perl itself.
  2197.  
  2198. =head1 CREDITS
  2199.  
  2200.  Wojciech Zwiefka <wojtekz@cnt.pl>
  2201.  Chris Nandor <pudge@pobox.com>
  2202.  Jim Hebert <jim@cosource.com>
  2203.  Randal Schwartz <merlyn@stonehenge.com>
  2204.  rjp@browser.org
  2205.  Kellan <kellan@protest.net>
  2206.  Rafe Colburn <rafe@rafe.us>
  2207.  Adam Trickett <adam.trickett@btinternet.com>
  2208.  Aaron Straup Cope <asc@vineyard.net>
  2209.  Ian Davis <iand@internetalchemy.org>
  2210.  
  2211. =head1 SEE ALSO
  2212.  
  2213. perl(1), XML::Parser(3).
  2214.  
  2215. =cut
  2216.