blib/lib/XML/RSS.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/XML/RSS/Private/Output/Roles/ModulesElems.pm ↑ Summary blib/lib/XML/RSS/Private/Output/V0_9.pm »
1 package XML::RSS;
2 $XML::RSS::VERSION = '1.62';
3 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 42 ▶use Carp qw/ confess croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 42 ▶use XML::Parser ();
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 use XML::RSS::Private::Output::Base ();
10 use XML::RSS::Private::Output::V0_9 ();
11 use XML::RSS::Private::Output::V0_91 ();
12 use XML::RSS::Private::Output::V1_0 ();
13 use XML::RSS::Private::Output::V2_0 ();
14  
15 use vars qw($VERSION $AUTOLOAD @ISA $AUTO_ADD);
16  
17 require 5.008;
18  
19 $VERSION = '1.59';
20  
21 $AUTO_ADD = 0;
22  
23 sub _get_ok_fields {
24 return {
25 "0.9" => {
26 channel => {
27 title => undef,
28 description => undef,
29 link => undef,
30 },
31 image => {
32 title => undef,
33 url => undef,
34 link => undef,
35 },
36 textinput => {
37 title => undef,
38 description => undef,
39 name => undef,
40 link => undef,
41 },
42 },
43 "0.91" => {
44 channel => {
45 title => undef,
46 copyright => undef,
47 description => undef,
48 docs => undef,
49 language => undef,
50 lastBuildDate => undef,
51 'link' => undef,
52 managingEditor => undef,
53 pubDate => undef,
54 rating => undef,
55 webMaster => undef,
56 },
57 image => {
58 title => undef,
59 url => undef,
60 'link' => undef,
61 width => undef,
62 height => undef,
63 description => undef,
64 },
65 skipDays => {day => undef,},
66 skipHours => {hour => undef,},
67 textinput => {
68 title => undef,
69 description => undef,
70 name => undef,
71 'link' => undef,
72 },
73 },
74 "2.0" => {
75 channel => {
76 title => undef,
77 'link' => undef,
78 description => undef,
79 language => undef,
80 copyright => undef,
81 managingEditor => undef,
82 webMaster => undef,
83 pubDate => undef,
84 lastBuildDate => undef,
85 category => undef,
86 generator => undef,
87 docs => undef,
88 cloud => '',
89 ttl => undef,
90 image => '',
91 textinput => '',
92 skipHours => '',
93 skipDays => '',
94 },
95 image => {
96 title => undef,
97 url => undef,
98 'link' => undef,
99 width => undef,
100 height => undef,
101 description => undef,
102 },
103 skipDays => {day => undef,},
104 skipHours => {hour => undef,},
105 textinput => {
106 title => undef,
107 description => undef,
108 name => undef,
109 'link' => undef,
110 },
111 },
112 'default' => {
113 channel => {
114 title => undef,
115 description => undef,
116 link => undef,
117 },
118 image => {
119 title => undef,
120 url => undef,
121 link => undef,
122 },
123 textinput => {
124 title => undef,
125 description => undef,
126 name => undef,
127 link => undef,
128 },
129 },
130 };
131 }
132  
133 # define required elements for RSS 0.9
134 my $_REQ_v0_9 = {
135 channel => {
136 "title" => [1, 40],
137 "description" => [1, 500],
138 "link" => [1, 500]
139 },
140 image => {
141 "title" => [1, 40],
142 "url" => [1, 500],
143 "link" => [1, 500]
144 },
145 item => {
146 "title" => [1, 100],
147 "link" => [1, 500]
148 },
149 textinput => {
150 "title" => [1, 40],
151 "description" => [1, 100],
152 "name" => [1, 500],
153 "link" => [1, 500]
154 }
155 };
156  
157 # define required elements for RSS 0.91
158 my $_REQ_v0_9_1 = {
159 channel => {
160 "title" => [1, 100],
161 "description" => [1, 500],
162 "link" => [1, 500],
163 "language" => [1, 5],
164 "rating" => [0, 500],
165 "copyright" => [0, 100],
166 "pubDate" => [0, 100],
167 "lastBuildDate" => [0, 100],
168 "docs" => [0, 500],
169 "managingEditor" => [0, 100],
170 "webMaster" => [0, 100],
171 },
172 image => {
173 "title" => [1, 100],
174 "url" => [1, 500],
175 "link" => [0, 500],
176 "width" => [0, 144],
177 "height" => [0, 400],
178 "description" => [0, 500]
179 },
180 item => {
181 "title" => [1, 100],
182 "link" => [1, 500],
183 "description" => [0, 500]
184 },
185 textinput => {
186 "title" => [1, 100],
187 "description" => [1, 500],
188 "name" => [1, 20],
189 "link" => [1, 500]
190 },
191 skipHours => {"hour" => [1, 23]},
192 skipDays => {"day" => [1, 10]}
193 };
194  
195 # define required elements for RSS 2.0
196 my $_REQ_v2_0 = {
197 channel => {
198 "title" => [1, 100],
199 "description" => [1, 500],
200 "link" => [1, 500],
201 "language" => [0, 5],
202 "rating" => [0, 500],
203 "copyright" => [0, 100],
204 "pubDate" => [0, 100],
205 "lastBuildDate" => [0, 100],
206 "docs" => [0, 500],
207 "managingEditor" => [0, 100],
208 "webMaster" => [0, 100],
209 },
210 image => {
211 "title" => [1, 100],
212 "url" => [1, 500],
213 "link" => [0, 500],
214 "width" => [0, 144],
215 "height" => [0, 400],
216 "description" => [0, 500]
217 },
218 item => {
219 "title" => [1, 100],
220 "link" => [1, 500],
221 "description" => [0, 500]
222 },
223 textinput => {
224 "title" => [1, 100],
225 "description" => [1, 500],
226 "name" => [1, 20],
227 "link" => [1, 500]
228 },
229 skipHours => {"hour" => [1, 23]},
230 skipDays => {"day" => [1, 10]}
231 };
232  
233 my $namespace_map = {
234 rss10 => 'http://purl.org/rss/1.0/',
235 rss09 => 'http://my.netscape.com/rdf/simple/0.9/',
236  
237 # rss091 => 'http://purl.org/rss/1.0/modules/rss091/',
238 rss20 => 'http://backend.userland.com/blogChannelModule',
239 };
240  
241 sub _rdf_resource_fields {
242 return {
243 'http://webns.net/mvcb/' => {
244 'generatorAgent' => 1,
245 'errorReportsTo' => 1
246 },
247 'http://purl.org/rss/1.0/modules/annotate/' => {'reference' => 1},
248 'http://my.theinfo.org/changed/1.0/rss/' => {'server' => 1}
249 };
250 }
251  
252 my %empty_ok_elements = (enclosure => 1);
253 my %hashref_ok_elements = (description => 1);
254  
255 sub _get_default_modules {
256 return {
257 'http://purl.org/rss/1.0/modules/syndication/' => 'syn',
258 'http://purl.org/dc/elements/1.1/' => 'dc',
259 'http://purl.org/rss/1.0/modules/taxonomy/' => 'taxo',
260 'http://webns.net/mvcb/' => 'admin',
261 'http://purl.org/rss/1.0/modules/content/' => 'content',
262 };
263 }
264  
265 sub _get_default_rss_2_0_modules {
266 return {'http://backend.userland.com/blogChannelModule' => 'blogChannel',};
267 }
268  
269 sub _get_syn_ok_fields {
270 return [qw(updateBase updateFrequency updatePeriod)];
271 }
272  
273 sub _get_dc_ok_fields {
274 return [
275 qw(
276 contributor
277 coverage
278 creator
279 date
280 description
281 format
282 identifier
283 language
284 publisher
285 relation
286 rights
287 source
288 subject
289 title
290 type
291 )
292 ];
293 }
294  
295 sub new {
296 my $class = shift;
297  
298 my $self = {};
299  
300 bless $self, $class;
301  
302 $self->_initialize(@_);
303  
304 return $self;
305 }
306  
307 sub _get_init_default_key_assignments {
308 return [
309 {key => "version", default => '1.0',},
310 {key => "encode_output", default => 1,},
311 {key => "output", default => "",},
312 {key => "encoding", default => "UTF-8",},
313 {key => "encode_cb", default => undef(),},
314 {key => "xml:base", default => undef(),},
315 ];
316 }
317  
318 # This method resets the contents of the instance to an empty one (with no
319 # items, empty keys, etc.). Useful before parsing or during initialization.
320  
321 sub _reset {
322 my $self = shift;
323  
324 # internal hash
325 $self->{_internal} = {};
326  
327 # init num of items to 0
328 $self->{num_items} = 0;
329  
330 # initialize items
331 $self->{items} = [];
332  
333 delete $self->{_allow_multiple};
334  
335 my $ok_fields = $self->_get_ok_fields();
336  
337 my $ver_ok_fields =
338 exists($ok_fields->{$self->{version}})
339 ? $ok_fields->{$self->{version}}
340 : $ok_fields->{default};
341  
342 while (my ($k, $v) = each(%$ver_ok_fields)) {
343 $self->{$k} = +{%{$v}};
344 }
345  
346 return;
347 }
348  
349 sub _initialize {
350 my $self = shift;
351 my %hash = @_;
352  
353 # adhere to Netscape limits; no by default
354 $self->{'strict'} = 0;
355  
356 # namespaces
357 $self->{namespaces} = {};
358 $self->{rss_namespace} = '';
359 foreach my $k (@{$self->_get_init_default_key_assignments()}) {
360 my $key = $k->{key};
361 $self->{$key} = exists($hash{$key}) ? $hash{$key} : $k->{default};
362 }
363  
364 # modules
365 $self->{modules} = (
366 ($self->{version} eq "2.0")
367 ? $self->_get_default_rss_2_0_modules()
368 : $self->_get_default_modules()
369 );
370  
371 # stylesheet
372 if (exists($hash{stylesheet})) {
373 $self->{stylesheet} = $hash{stylesheet};
374 }
375  
376 if ($self->{version} eq "2.0") {
377 $self->{namespaces}->{'blogChannel'} = "http://backend.userland.com/blogChannelModule";
378 }
379  
380 $self->_reset;
381  
382 return;
383 }
384  
385 sub add_module {
386 my $self = shift;
387 my $hash = {@_};
388  
389 $hash->{prefix} =~ /^[a-z_][a-z0-9.\-_]*$/i
390 or croak "a namespace prefix should look like [A-Za-z_][A-Za-z0-9.\\-_]*";
391  
392 $hash->{uri}
393 or croak "a URI must be provided in a namespace declaration";
394  
395 $self->{modules}->{$hash->{uri}} = $hash->{prefix};
396 }
397  
398 sub add_item {
399 my $self = shift;
400 my $hash = {@_};
401  
402 # strict Netscape Netcenter length checks
403 if ($self->{'strict'}) {
404  
405 # make sure we have a title and link
406 croak "title and link elements are required"
407 unless ($hash->{title} && $hash->{'link'});
408  
409 # check string lengths
410 croak "title cannot exceed 100 characters in length"
411 if (length($hash->{title}) > 100);
412 croak "link cannot exceed 500 characters in length"
413 if (length($hash->{'link'}) > 500);
414 croak "description cannot exceed 500 characters in length"
415 if (exists($hash->{description})
416 && length($hash->{description}) > 500);
417  
418 # make sure there aren't already 15 items
419 croak "total items cannot exceed 15 " if (@{$self->{items}} >= 15);
420 }
421  
422 # add the item to the list
423 if (defined($hash->{mode}) && $hash->{mode} eq 'insert') {
424 unshift(@{$self->{items}}, $hash);
425 }
426 else {
427 push(@{$self->{items}}, $hash);
428 }
429  
430 # return reference to the list of items
431 return $self->{items};
432 }
433  
434  
435 # $self->_render_complete_rss_output($xml_version)
436 #
437 # This function is the workhorse of the XML output and does all the work of
438 # rendering the RSS, delegating the work to specialised functions.
439 #
440 # It accepts the requested version number as its argument.
441  
442 sub _get_rendering_class {
443 my ($self, $ver) = @_;
444  
445 if ($ver eq "1.0") {
446 return "XML::RSS::Private::Output::V1_0";
447 }
448 elsif ($ver eq "0.9") {
449 return "XML::RSS::Private::Output::V0_9";
450 }
451 elsif ($ver eq "0.91") {
452 return "XML::RSS::Private::Output::V0_91";
453 }
454 else {
455 return "XML::RSS::Private::Output::V2_0";
456 }
457 }
458  
459 sub _get_encode_cb_params {
460 my $self = shift;
461  
462 return defined($self->{encode_cb})
463 ? ("encode_cb" => $self->{encode_cb})
464 : ();
465 }
466  
467 sub _get_rendering_obj {
468 my ($self, $ver) = @_;
469  
470 return $self->_get_rendering_class($ver)->new(
471 { main => $self,
472 version => $ver,
473 $self->_get_encode_cb_params(),
474 }
475 );
476 }
477  
478 sub _render_complete_rss_output {
479 my ($self, $ver) = @_;
480  
481 return $self->_get_rendering_obj($ver)->_render_complete_rss_output();
482 }
483  
484 sub as_rss_0_9 {
485 return shift->_render_complete_rss_output("0.9");
486 }
487  
488 sub as_rss_0_9_1 {
489 return shift->_render_complete_rss_output("0.91");
490 }
491  
492 sub as_rss_1_0 {
493 return shift->_render_complete_rss_output("1.0");
494 }
495  
496 sub as_rss_2_0 {
497 return shift->_render_complete_rss_output("2.0");
498 }
499  
500  
501 sub _get_output_methods_map {
502 return {
503 '0.9' => "as_rss_0_9",
504 '0.91' => "as_rss_0_9_1",
505 '2.0' => "as_rss_2_0",
506 '1.0' => "as_rss_1_0",
507 };
508 }
509  
510 sub _get_default_output_method {
511 return "as_rss_1_0";
512 }
513  
514 sub _get_output_method {
515 my ($self, $version) = @_;
516  
517 if (my $output_method = $self->_get_output_methods_map()->{$version}) {
518 return $output_method;
519 }
520 else {
521 return $self->_get_default_output_method();
522 }
523 }
524  
525 sub _get_output_version {
526 my $self = shift;
527 return ($self->{output} =~ /\d/) ? $self->{output} : $self->{version};
528 }
529  
530 # This is done to preserve backwards compatibility with older versions
531 # of XML-RSS that had the channel/{link,description,title} as the empty
532 # string by default.
533 sub _output_env {
534 my $self = shift;
535 my $callback = shift;
536  
537 local $self->{channel}->{'link'} = $self->{channel}->{'link'};
538 local $self->{channel}->{'description'} = $self->{channel}->{'description'};
539 local $self->{channel}->{'title'} = $self->{channel}->{'title'};
540  
541 foreach my $field (qw(link description title)) {
542 if (!defined($self->{channel}->{$field})) {
543 $self->{channel}->{$field} = '';
544 }
545 }
546  
547 return $callback->();
548 }
549  
550 sub as_string {
551 my $self = shift;
552  
553 my $version = $self->_get_output_version();
554  
555 my $output_method = $self->_get_output_method($version);
556  
557 return $self->_output_env(sub { return $self->$output_method(); });
558 }
559  
560 # Checks if inside a possibly namespaced element
561 # TODO : After increasing test coverage convert all such conditionals to this
562 # method.
563 sub _my_in_element {
564 my ($self, $elem) = @_;
565  
566 my $parser = $self->_parser;
567  
568 return $parser->within_element($elem)
569 || $parser->within_element($parser->generate_ns_name($elem, $self->{rss_namespace}));
570 }
571  
572 sub _get_elem_namespace_helper {
573 my ($self, $el) = @_;
574  
575 my $ns = $self->_parser->namespace($el);
576  
577 return (defined($ns) ? $ns : "");
578 }
579  
580 sub _get_elem_namespace {
581 my $self = shift;
582  
583 my ($el) = @_;
584  
585 my $ns = $self->_get_elem_namespace_helper(@_);
586  
587 my $verdict = (!$ns && !$self->{rss_namespace})
588 || ($ns eq $self->{rss_namespace});
589  
590 return ($ns, $verdict);
591 }
592  
593 sub _current_element {
594 my $self = shift;
595  
596 return $self->_parser->current_element;
597 }
598  
599 sub _get_current_namespace {
600 my $self = shift;
601  
602 return $self->_get_elem_namespace($self->_current_element);
603 }
604  
605 sub _is_rdf_resource {
606 my $self = shift;
607 my $el = shift;
608  
609 my $ns = shift;
610 if (!defined($ns)) {
611 $ns = $self->_parser->namespace($el);
612 }
613  
614 return ( exists($self->_rdf_resource_fields->{$ns})
615 && exists($self->_rdf_resource_fields->{$ns}{$el}));
616 }
617  
618 sub _get_ns_arrayity {
619 my ($self, $ns) = @_;
620  
621 my $is_array = $self->_parse_options()->{'modules_as_arrays'}
622 && (!exists($self->_get_default_modules()->{$ns}))
623  
624 # RDF
625 && ($ns ne "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
626  
627 my $default_ref = sub { $is_array ? [] : {} };
628  
629 return ($is_array, $default_ref);
630 }
631  
632 sub _append_text_to_elem_struct {
633 my ($self, $struct, $cdata, $mapping_sub, $is_array_sub) = @_;
634  
635 my $elem = $self->_current_element;
636  
637 my ($ns, $verdict) = $self->_get_current_namespace;
638  
639 # If it's in the default namespace
640 if ($verdict) {
641 $self->_append_struct(
642 $struct,
643 scalar($mapping_sub->($struct, $elem)),
644 scalar($is_array_sub->($struct, $elem)), $cdata
645 );
646 }
647 else {
648 my $prefix = $self->{modules}->{$ns};
649  
650 my ($is_array, $default_ref) = $self->_get_ns_arrayity($ns);
651  
652 $self->_append_struct(
653 ($struct->{$ns} ||= $default_ref->()), $elem,
654 (defined($prefix) && $prefix eq "dc"), $cdata
655 );
656  
657 # If it's in a module namespace, provide a friendlier prefix duplicate
658 if ($prefix) {
659 $self->_append_struct(($struct->{$prefix} ||= $default_ref->()),
660 $elem, ($prefix eq "dc"), $cdata);
661 }
662 }
663  
664 return;
665 }
666  
667 {
668 my @_ITEM_KEYS_ELEM_STACK = ("rss", "channel", "item", "link");
669  
670 sub _should_skip_item_keys_in_custom_tags {
671 my ($self, $struct, $key) = @_;
672  
673 if (length $struct->{$key}) {
674 if ($self->{_internal}->{version} eq "2.0") {
675 if ($key eq "link") {
676 my @context = $self->_parser->context();
677 if (@context > @_ITEM_KEYS_ELEM_STACK) {
678 return 1;
679 }
680 }
681 }
682 }
683 return;
684 }
685 }
686  
687  
688 sub _append_struct {
689 my ($self, $struct, $key, $can_be_array, $cdata) = @_;
690  
691 if (ref($struct) eq 'ARRAY') {
692 $struct->[-1]->{'val'} .= $cdata;
693 return;
694 }
695 elsif (defined $struct->{$key}) {
696 if (ref($struct->{$key}) eq 'HASH') {
697 $struct->{$key}->{content} .= $cdata;
698 return;
699 }
700 elsif ($can_be_array && ref($struct->{$key}) eq 'ARRAY') {
701 $struct->{$key}->[-1] .= $cdata;
702 return;
703 }
704 }
705  
706 # Somewhat sympotamtic cure for item/link nested inside
707 # custom tags:
708 #
709 # https://github.com/shlomif/perl-XML-RSS/issues/7
710 #
711 # Thanks to @jkramer .
712 if ($self->_should_skip_item_keys_in_custom_tags($struct, $key)) {
713 return;
714 }
715  
716 $struct->{$key} .= $cdata;
717 return;
718 }
719  
720 sub _return_elem {
721 my ($struct, $elem) = @_;
722 return $elem;
723 }
724  
725 sub _return_elem_is_array {
726 my ($struct, $elem) = @_;
727  
728 # Always return false because no element should be an array.
729 return;
730 }
731  
732 sub _append_text_to_elem {
733 my ($self, $ext_tag, $cdata) = @_;
734  
735 return $self->_append_text_to_elem_struct($self->$ext_tag(),
736 $cdata, \&_return_elem, \&_return_elem_is_array,);
737 }
738  
739 sub _within_topics {
740 my $self = shift;
741  
742 my $parser = $self->_parser;
743  
744 return $parser->within_element(
745 $parser->generate_ns_name("topics", 'http://purl.org/rss/1.0/modules/taxonomy/'));
746 }
747  
748 sub _return_item_elem {
749 my ($item, $elem) = @_;
750 if ($elem eq "guid") {
751 return $item->{isPermaLink} ? "permaLink" : "guid";
752 }
753 else {
754 return $elem;
755 }
756 }
757  
758 sub _return_item_elem_is_array {
759 my ($item, $elem) = @_;
760  
761 return ($elem eq "category");
762 }
763  
764 sub _append_text_to_item {
765 my ($self, $cdata) = @_;
766  
767 if (@{$self->{'items'}} < $self->{num_items}) {
768 push @{$self->{items}}, {};
769 }
770  
771 $self->_append_text_to_elem_struct($self->_last_item,
772 $cdata, \&_return_item_elem, \&_return_item_elem_is_array);
773 }
774  
775 sub _append_to_array_elem {
776 my ($self, $category, $cdata) = @_;
777  
778 if (!$self->_my_in_element($category)) {
779 return;
780 }
781  
782 my $el = $self->_current_element;
783  
784 if (ref($self->{$category}->{$el}) eq "ARRAY") {
785 $self->{$category}->{$el}->[-1] .= $cdata;
786 }
787 else {
788 $self->{$category}->{$el} .= $cdata;
789 }
790  
791 return 1;
792 }
793  
794 sub _handle_char {
795 my ($self, $cdata) = (@_);
796  
797 # image element
798 if ($self->_my_in_element("image")) {
799 $self->_append_text_to_elem("image", $cdata);
800 }
801  
802 # item element
803 elsif (defined($self->{_inside_item_elem})) {
804 return if $self->_within_topics;
805  
806 $self->_append_text_to_item($cdata);
807 }
808  
809 # textinput element
810 elsif ($self->_my_in_element("textinput") || $self->_my_in_element("textInput")) {
811 $self->_append_text_to_elem("textinput", $cdata);
812 }
813  
814 # skipHours element
815 elsif ($self->_append_to_array_elem("skipHours", $cdata)) {
816  
817 # Do nothing - already done in the predicate.
818 }
819 elsif ($self->_append_to_array_elem("skipDays", $cdata)) {
820  
821 # Do nothing - already done in the predicate.
822 }
823  
824 # channel element
825 elsif ($self->_my_in_element("channel")) {
826 if ($self->_within_topics() || $self->_my_in_element("items")) {
827 return;
828 }
829  
830 if ($self->_current_element eq "category") {
831 $self->_append_to_array_elem("channel", $cdata);
832 }
833 else {
834 $self->_append_text_to_elem("channel", $cdata);
835 }
836 }
837 }
838  
839 sub _handle_dec {
840 my ($self, $version, $encoding, $standalone) = (@_);
841 $self->{encoding} = $encoding;
842  
843 #print "ENCODING: $encoding\n";
844 }
845  
846 sub _should_be_hashref {
847 my ($self, $el) = @_;
848  
849 return (
850 $empty_ok_elements{$el} || ($self->_parse_options()->{'hashrefs_instead_of_strings'}
851 && $hashref_ok_elements{$el})
852 );
853 }
854  
855 sub _start_array_element_in_struct {
856 my ($self, $input_struct, $el, $prefix) = @_;
857  
858 my ($el_ns, $el_verdict) = $self->_get_elem_namespace($el);
859  
860 my ($is_array, $default_ref) = $self->_get_ns_arrayity($el_ns);
861  
862 my @structs =
863 (!$el_verdict)
864 ? (
865 ( exists($self->{modules}->{$el_ns})
866 ? ($input_struct->{$self->{modules}->{$el_ns}} ||= $default_ref->())
867 : ()
868 ),
869 ($input_struct->{$el_ns} ||= $default_ref->()),
870 )
871 : ($input_struct);
872  
873 foreach my $struct (@structs) {
874 if (ref($struct) eq 'ARRAY') {
875 push @$struct, {el => $el, val => "",};
876 }
877  
878 # If it's an array - append a new empty element because a new one
879 # was started.
880 elsif (ref($struct->{$el}) eq "ARRAY") {
881 push @{$struct->{$el}}, "";
882 }
883  
884 # If it's not an array but still full (i.e: it's only the second
885 # element), then turn it into an array
886 elsif (defined($struct->{$el}) && length($struct->{$el})) {
887 $struct->{$el} = [$struct->{$el}, ""];
888 }
889  
890 # Else - do nothing and let the function append to the new value
891 #
892 }
893 return 1;
894 }
895  
896 sub _start_array_element {
897 my ($self, $cat, $el) = @_;
898  
899 if (!$self->_my_in_element($cat)) {
900 return;
901 }
902  
903 $self->_start_array_element_in_struct($self->{$cat}, $el);
904 return 1;
905 }
906  
907 sub _last_item {
908 my $self = shift;
909  
910 return ($self->{'items'}->[$self->{num_items} - 1] ||= {});
911 }
912  
913 sub _handle_start {
914 my $self = shift;
915 my $el = shift;
916 my %attribs = @_;
917  
918 my $parser = $self->_parser;
919  
920 my ($el_ns, $el_verdict) = $self->_get_elem_namespace($el);
921  
922 if ($el eq "image") {
923 if (exists($attribs{'resource'})) {
924 $self->image("rdf:resource", $attribs{'resource'});
925 }
926 }
927  
928 # beginning of RSS 0.91
929 if ($el eq 'rss') {
930 if (exists($attribs{version})) {
931 $self->{_internal}->{version} = $attribs{version};
932 }
933 else {
934 croak "Malformed RSS: invalid version\n";
935 }
936  
937 # handle xml:base
938 $self->{'xml:base'} = $attribs{'base'} if exists $attribs{'base'};
939  
940 # beginning of RSS 1.0 or RSS 0.9
941 }
942 elsif ($el eq 'RDF') {
943 my @prefixes = $parser->new_ns_prefixes;
944 foreach my $prefix (@prefixes) {
945 my $uri = $parser->expand_ns_prefix($prefix);
946 $self->{namespaces}->{$prefix} = $uri;
947  
948 #print "$prefix = $uri\n";
949 }
950  
951 # removed assumption that RSS is the default namespace - kellan, 11/5/02
952 #
953 foreach my $uri (values %{$self->{namespaces}}) {
954 if ($namespace_map->{'rss10'} eq $uri) {
955 $self->{_internal}->{version} = '1.0';
956 $self->{rss_namespace} = $uri;
957 last;
958 }
959 elsif ($namespace_map->{'rss09'} eq $uri) {
960 $self->{_internal}->{version} = '0.9';
961 $self->{rss_namespace} = $uri;
962 last;
963 }
964 }
965  
966 # failed to match a namespace
967 if (!defined($self->{_internal}->{version})) {
968 croak "Malformed RSS: invalid version\n";
969 }
970  
971 #if ($self->expand_ns_prefix('#default') =~ /\/1.0\//) {
972 # $self->{_internal}->{version} = '1.0';
973 #} elsif ($self->expand_ns_prefix('#default') =~ /\/0.9\//) {
974 # $self->{_internal}->{version} = '0.9';
975 #} else {
976 # croak "Malformed RSS: invalid version\n";
977 #}
978  
979 # handle xml:base
980 $self->{'xml:base'} = $attribs{'base'} if exists $attribs{'base'};
981  
982 # beginning of item element
983 }
984 elsif ($self->_start_array_element("skipHours", $el)) {
985  
986 # Do nothing - already done in the predicate.
987 }
988 elsif ($self->_start_array_element("skipDays", $el)) {
989  
990 # Do nothing - already done in the predicate.
991 }
992 elsif ($el eq 'cloud') {
993 if (keys %attribs) {
994 $self->{channel}{cloud} = \%attribs;
995 }
996 }
997 elsif ($el eq 'item') {
998  
999 # deal with trouble makers who use mod_content :)
1000  
1001 my ($ns, $verdict) = $self->_get_elem_namespace($el);
1002  
1003 if ($verdict) {
1004  
1005 # Sanity check to make sure we don't have nested elements that
1006 # can confuse the parser.
1007 if (!defined($self->{_inside_item_elem})) {
1008  
1009 # increment item count
1010 $self->{num_items}++;
1011 $self->{_inside_item_elem} = $parser->depth();
1012 }
1013 }
1014  
1015 # handle xml:base
1016 $self->_last_item->{'xml:base'} = $attribs{'base'} if exists $attribs{'base'};
1017  
1018  
1019 # guid element is a permanent link unless isPermaLink attribute is set to false
1020 }
1021 elsif ($el eq 'guid') {
1022 $self->_last_item->{'isPermaLink'} =
1023 ((!exists($attribs{'isPermaLink'})) || (lc($attribs{'isPermaLink'}) ne 'false'));
1024  
1025 # beginning of taxo li element in item element
1026 #'http://purl.org/rss/1.0/modules/taxonomy/' => 'taxo'
1027 }
1028 elsif (
1029 $self->_current_element eq "item"
1030 && (($el eq "category")
1031 || (exists($self->{modules}->{$el_ns})
1032 && ($self->{modules}->{$el_ns} eq "dc"))
1033 )
1034 )
1035 {
1036 $self->_start_array_element_in_struct($self->_last_item, $el);
1037 }
1038 elsif (
1039 $parser->within_element(
1040 $parser->generate_ns_name("topics", 'http://purl.org/rss/1.0/modules/taxonomy/')
1041 )
1042 && $parser->within_element($parser->generate_ns_name("item", $namespace_map->{'rss10'}))
1043 && $self->_current_element eq 'Bag'
1044 && $el eq 'li'
1045 )
1046 {
1047  
1048 #print "taxo: ", $attribs{'resource'},"\n";
1049 push(@{$self->_last_item->{'taxo'}}, $attribs{'resource'});
1050 $self->{'modules'}->{'http://purl.org/rss/1.0/modules/taxonomy/'} = 'taxo';
1051  
1052 # beginning of taxo li in channel element
1053 }
1054 elsif (
1055 $parser->within_element(
1056 $parser->generate_ns_name("topics", 'http://purl.org/rss/1.0/modules/taxonomy/')
1057 )
1058 && $parser->within_element($parser->generate_ns_name("channel", $namespace_map->{'rss10'}))
1059 && $self->_current_element eq 'Bag'
1060 && $el eq 'li'
1061 )
1062 {
1063 push(@{$self->{'channel'}->{'taxo'}}, $attribs{'resource'});
1064 $self->{'modules'}->{'http://purl.org/rss/1.0/modules/taxonomy/'} = 'taxo';
1065 }
1066  
1067 # beginning of a channel element that stores its info in rdf:resource
1068 elsif ($parser->namespace($el)
1069 && $self->_is_rdf_resource($el)
1070 && $self->_current_element eq 'channel')
1071 {
1072 my $ns = $parser->namespace($el);
1073  
1074 # Commented out by shlomif - the RSS namespaces are not present
1075 # in the 'rdf_resource_fields' so this condition always evaluates
1076 # to false.
1077 # if ( $ns eq $self->{rss_namespace} ) {
1078 # $self->{channel}->{$el} = $attribs{resource};
1079 # }
1080 # else
1081  
1082 {
1083 $self->{channel}->{$ns}->{$el} = $attribs{resource};
1084  
1085 # add short cut
1086 #
1087 if (exists($self->{modules}->{$ns})) {
1088 $ns = $self->{modules}->{$ns};
1089 $self->{channel}->{$ns}->{$el} = $attribs{resource};
1090 }
1091 }
1092 }
1093  
1094 # beginning of an item element that stores its info in rdf:resource
1095 elsif ($parser->namespace($el)
1096 && $self->_is_rdf_resource($el)
1097 && $self->_current_element eq 'item')
1098 {
1099 my $ns = $parser->namespace($el);
1100  
1101 # Commented out by shlomif - the RSS namespaces are not present
1102 # in the 'rdf_resource_fields' so this condition always evaluates
1103 # to false.
1104 # if ( $ns eq $self->{rss_namespace} ) {
1105 # $self->_last_item->{ $el } = $attribs{resource};
1106 # }
1107 # else
1108 {
1109 $self->_last_item->{$ns}->{$el} = $attribs{resource};
1110  
1111 # add short cut
1112 #
1113 if (exists($self->{modules}->{$ns})) {
1114 $ns = $self->{modules}->{$ns};
1115 $self->_last_item->{$ns}->{$el} = $attribs{resource};
1116 }
1117 }
1118 }
1119 elsif ($self->_should_be_hashref($el) and $self->_current_element eq 'item') {
1120 if (defined $attribs{base}) {
1121 $attribs{'xml:base'} = delete $attribs{base};
1122 }
1123 if (keys(%attribs)) {
1124 if ($el_verdict) {
1125 $self->_last_item->{$el} =
1126 $self->_make_array($el, $self->_last_item->{$el}, \%attribs);
1127 }
1128 else {
1129 $self->_last_item->{$el_ns}->{$el} =
1130 $self->_make_array($el, $self->_last_item->{$el_ns}->{$el}, \%attribs);
1131  
1132 my $prefix = $self->{modules}->{$el_ns};
1133  
1134 if ($prefix) {
1135 $self->_last_item->{$prefix}->{$el} =
1136 $self->_make_array($el, $self->_last_item->{$prefix}->{$el}, \%attribs);
1137 }
1138 }
1139 }
1140 }
1141 elsif ($self->_start_array_element("image", $el)) {
1142  
1143 # Do nothing - already done in the predicate.
1144 }
1145 elsif (($el eq "category")
1146 && (!$parser->within_element("item"))
1147 && $self->_start_array_element("channel", $el))
1148 {
1149 # Do nothing - already done in the predicate.
1150 }
1151 elsif (($self->_current_element eq 'channel')
1152 && ($el_verdict))
1153 {
1154 # Make sure an opening tag signifies that the element has been
1155 # encountered.
1156 if (exists($self->{'channel'}->{$el})
1157 && (!defined($self->{'channel'}->{$el})))
1158 {
1159 $self->{'channel'}->{$el} = "";
1160 }
1161 }
1162 }
1163  
1164 sub _make_array {
1165 my $self = shift;
1166 my $el = shift;
1167 my $old = shift;
1168 my $new = shift;
1169  
1170 if (!$self->_allow_multiple($el)) {
1171 return $new;
1172 }
1173  
1174 if (!defined $old) {
1175 $old = [];
1176 }
1177 elsif (ref($old) ne 'ARRAY') {
1178 $old = [$old];
1179 }
1180 push @$old, $new;
1181 return $old;
1182 }
1183  
1184 sub _allow_multiple {
1185 my $self = shift;
1186 my $el = shift;
1187  
1188 $self->{_allow_multiple} ||= {map { $_ => 1 } @{$self->_parse_options->{allow_multiple} || []}};
1189  
1190 return $self->{_allow_multiple}->{$el};
1191 }
1192  
1193 sub _handle_end {
1194 my ($self, $el) = @_;
1195  
1196 if (defined($self->{_inside_item_elem})
1197 && $self->{_inside_item_elem} == $self->_parser->depth())
1198 {
1199 delete($self->{_inside_item_elem});
1200 }
1201 }
1202  
1203 sub _auto_add_modules {
1204 my $self = shift;
1205  
1206 for my $ns (keys %{$self->{namespaces}}) {
1207  
1208 # skip default namespaces
1209 next
1210 if $ns eq "rdf"
1211 || $ns eq "#default"
1212 || exists $self->{modules}{$self->{namespaces}{$ns}};
1213 $self->add_module(prefix => $ns, uri => $self->{namespaces}{$ns});
1214 }
1215  
1216 $self;
1217 }
1218  
1219 sub _parser {
1220 my $self = shift;
1221  
1222 if (@_) {
1223 $self->{_parser} = shift;
1224 }
1225 return $self->{_parser};
1226 }
1227  
1228 sub _get_parser {
1229 my $self = shift;
1230  
1231 return XML::Parser->new(
1232 Namespaces => 1,
1233 NoExpand => 1,
1234 ParseParamEnt => 0,
1235 Handlers => {
1236 Char => sub {
1237 my ($parser, $cdata) = @_;
1238 $self->_parser($parser);
1239 $self->_handle_char($cdata);
1240  
1241 # Detach the parser to avoid reference loops.
1242 $self->_parser(undef);
1243 },
1244 XMLDecl => sub {
1245 my $parser = shift;
1246 $self->_parser($parser);
1247 $self->_handle_dec(@_);
1248  
1249 # Detach the parser to avoid reference loops.
1250 $self->_parser(undef);
1251 },
1252 Start => sub {
1253 my $parser = shift;
1254 $self->_parser($parser);
1255 $self->_handle_start(@_);
1256  
1257 # Detach the parser to avoid reference loops.
1258 $self->_parser(undef);
1259 },
1260 End => sub {
1261 my $parser = shift;
1262 $self->_parser($parser);
1263 $self->_handle_end(@_);
1264  
1265 # Detach the parser to avoid reference loops.
1266 $self->_parser(undef);
1267 },
1268 ExternEnt => sub {
1269 return '';
1270 },
1271 }
1272 );
1273 }
1274  
1275 sub _parse_options {
1276 my $self = shift;
1277  
1278 if (@_) {
1279 $self->{_parse_options} = shift;
1280 }
1281  
1282 return $self->{_parse_options};
1283 }
1284  
1285 sub _empty { }
1286  
1287 sub _generic_parse {
1288 my $self = shift;
1289 my $method = shift;
1290 my $arg = shift;
1291 my $options = shift;
1292  
1293 $self->_reset;
1294  
1295 $self->_parse_options($options || {});
1296  
1297 # Workaround to make sure that if we were defined with version => "2.0"
1298 # then we can still parse 1.0 and 0.9.x feeds correctly.
1299 if ($self->{version} eq "2.0") {
1300 $self->{modules} = +{%{$self->_get_default_modules()}, %{$self->{modules}}};
1301 }
1302  
1303 {
1304 my $parser = $self->_get_parser();
1305  
1306 eval { $parser->$method($arg); };
1307  
1308 if ($@) {
1309 my $err = $@;
1310  
1311 # Cleanup so perl-5.6.2 will be happy.
1312 $parser->setHandlers(map { ($_ => \&_empty) } (qw(Char XMLDecl Start End)));
1313 $self->_parser(0);
1314  
1315 undef($parser);
1316  
1317 die $err;
1318 }
1319 }
1320  
1321 $self->_auto_add_modules if $AUTO_ADD;
1322 $self->{version} = $self->{_internal}->{version};
1323  
1324 return $self;
1325 }
1326  
1327 sub parse {
1328 my $self = shift;
1329 my $text_to_parse = shift;
1330 my $options = shift;
1331  
1332 return $self->_generic_parse("parse", $text_to_parse, $options);
1333 }
1334  
1335 sub parsefile {
1336 my $self = shift;
1337 my $file_to_parse = shift;
1338 my $options = shift;
1339  
1340 return $self->_generic_parse("parsefile", $file_to_parse, $options);
1341 }
1342  
1343 sub _untaint {
1344 my $self = shift;
1345  
1346 my $value = shift;
1347  
1348 my ($untainted) = ($value =~ m{(.*)}s);
1349  
1350 return $untainted;
1351 }
1352  
1353 sub _get_save_output_mode {
1354 my $self = shift;
1355  
1356 return (">:encoding(" . $self->_untaint($self->_encoding()) . ")");
1357 }
1358  
1359 sub save {
1360 my ($self, $file) = @_;
1361  
1362 local (*OUT);
1363  
1364 open(OUT, $self->_get_save_output_mode(), "$file")
1365 or croak "Cannot open file $file for write: $!";
1366 print OUT $self->as_string;
1367 close OUT;
1368 }
1369  
1370 sub strict {
1371 my ($self, $value) = @_;
1372 $self->{'strict'} = $value;
1373 }
1374  
1375 sub _handle_accessor {
1376 my $self = shift;
1377 my $name = shift;
1378  
1379 my $type = ref($self);
1380  
1381 croak "Unregistered entity: Can't access $name field in object of class $type"
1382 unless (exists $self->{$name});
1383  
1384 # return reference to RSS structure
1385 if (@_ == 1) {
1386 return $self->{$name}->{$_[0]};
1387  
1388 # we're going to set values here
1389 }
1390 elsif (@_ > 1) {
1391 my %hash = @_;
1392 my $_REQ;
1393  
1394 # make sure we have required elements and correct lengths
1395 if ($self->{'strict'}) {
1396 ($self->{version} eq '0.9')
1397 ? ($_REQ = $_REQ_v0_9)
1398 : ($_REQ = $_REQ_v0_9_1);
1399 }
1400  
1401 # store data in object
1402 foreach my $key (keys(%hash)) {
1403 if ($self->{'strict'}) {
1404 my $req_element = $_REQ->{$name}->{$key};
1405 confess "$key cannot exceed " . $req_element->[1] . " characters in length"
1406 if defined $req_element->[1] && length($hash{$key}) > $req_element->[1];
1407 }
1408 $self->{$name}->{$key} = $hash{$key};
1409 }
1410  
1411 # return value
1412 return $self->{$name};
1413  
1414 # otherwise, just return a reference to the whole thing
1415 }
1416 else {
1417 return $self->{$name};
1418 }
1419  
1420 # make sure we have all required elements
1421 #foreach my $key (keys(%{$_REQ->{$name}})) {
1422 #my $element = $_REQ->{$name}->{$key};
1423 #croak "$key is required in $name"
1424 #if ($element->[0] == 1) && (!defined($hash{$key}));
1425 #croak "$key cannot exceed ".$element->[1]." characters in length"
1426 #unless length($hash{$key}) <= $element->[1];
1427 #}
1428 }
1429  
1430 sub _modules {
1431 my $self = shift;
1432 return $self->_handle_accessor("modules", @_);
1433 }
1434  
1435 sub channel {
1436 my $self = shift;
1437  
1438 return $self->_handle_accessor("channel", @_);
1439 }
1440  
1441 sub image {
1442 my $self = shift;
1443  
1444 return $self->_handle_accessor("image", @_);
1445 }
1446  
1447 sub textinput {
1448 my $self = shift;
1449  
1450 return $self->_handle_accessor("textinput", @_);
1451 }
1452  
1453 sub skipDays {
1454 my $self = shift;
1455  
1456 return $self->_handle_accessor("skipDays", @_);
1457 }
1458  
1459 sub skipHours {
1460 my $self = shift;
1461  
1462 return $self->_handle_accessor("skipHours", @_);
1463 }
1464  
1465 ### Read only, scalar accessors
1466  
1467 sub _encode_output {
1468 my $self = shift;
1469  
1470 return $self->{'encode_output'};
1471 }
1472  
1473 sub _encoding {
1474 my $self = shift;
1475  
1476 return $self->{'encoding'};
1477 }
1478  
1479 sub _stylesheet {
1480 my $self = shift;
1481  
1482 return $self->{'stylesheet'};
1483 }
1484  
1485 sub _get_items {
1486 my $self = shift;
1487  
1488 return $self->{items};
1489 }
1490  
1491 1;
1492  
1493 __END__
1494  
1495 =pod
1496  
1497 =encoding UTF-8
1498  
1499 =head1 NAME
1500  
1501 XML::RSS - creates and updates RSS files
1502  
1503 =head1 VERSION
1504  
1505 version 1.62
1506  
1507 =head1 SYNOPSIS
1508  
1509 # create an RSS 1.0 file (http://purl.org/rss/1.0/)
1510 use XML::RSS;
1511 my $rss = XML::RSS->new(version => '1.0');
1512 $rss->channel(
1513 title => "freshmeat.net",
1514 link => "http://freshmeat.net",
1515 description => "the one-stop-shop for all your Linux software needs",
1516 dc => {
1517 date => '2000-08-23T07:00+00:00',
1518 subject => "Linux Software",
1519 creator => 'scoop@freshmeat.net',
1520 publisher => 'scoop@freshmeat.net',
1521 rights => 'Copyright 1999, Freshmeat.net',
1522 language => 'en-us',
1523 },
1524 syn => {
1525 updatePeriod => "hourly",
1526 updateFrequency => "1",
1527 updateBase => "1901-01-01T00:00+00:00",
1528 },
1529 taxo => [
1530 'http://dmoz.org/Computers/Internet',
1531 'http://dmoz.org/Computers/PC'
1532 ]
1533 );
1534  
1535 $rss->image(
1536 title => "freshmeat.net",
1537 url => "http://freshmeat.net/images/fm.mini.jpg",
1538 link => "http://freshmeat.net",
1539 dc => {
1540 creator => "G. Raphics (graphics at freshmeat.net)",
1541 },
1542 );
1543  
1544 $rss->add_item(
1545 title => "GTKeyboard 0.85",
1546 link => "http://freshmeat.net/news/1999/06/21/930003829.html",
1547 description => "GTKeyboard is a graphical keyboard that ...",
1548 dc => {
1549 subject => "X11/Utilities",
1550 creator => "David Allen (s2mdalle at titan.vcu.edu)",
1551 },
1552 taxo => [
1553 'http://dmoz.org/Computers/Internet',
1554 'http://dmoz.org/Computers/PC'
1555 ]
1556 );
1557  
1558 $rss->textinput(
1559 title => "quick finder",
1560 description => "Use the text input below to search freshmeat",
1561 name => "query",
1562 link => "http://core.freshmeat.net/search.php3",
1563 );
1564  
1565 # Optionally mixing in elements of a non-standard module/namespace
1566  
1567 $rss->add_module(prefix=>'my', uri=>'http://purl.org/my/rss/module/');
1568  
1569 $rss->add_item(
1570 title => "xIrc 2.4pre2",
1571 link => "http://freshmeat.net/projects/xirc/",
1572 description => "xIrc is an X11-based IRC client which ...",
1573 my => {
1574 rating => "A+",
1575 category => "X11/IRC",
1576 },
1577 );
1578  
1579 $rss->add_item (title=>$title, link=>$link, slash=>{ topic=>$topic });
1580  
1581 # create an RSS 2.0 file
1582 use XML::RSS;
1583 my $rss = XML::RSS->new (version => '2.0');
1584 $rss->channel(title => 'freshmeat.net',
1585 link => 'http://freshmeat.net',
1586 language => 'en',
1587 description => 'the one-stop-shop for all your Linux software needs',
1588 rating => '(PICS-1.1 "http://www.classify.org/safesurf/" 1 r (SS~~000 1))',
1589 copyright => 'Copyright 1999, Freshmeat.net',
1590 pubDate => 'Thu, 23 Aug 1999 07:00:00 GMT',
1591 lastBuildDate => 'Thu, 23 Aug 1999 16:20:26 GMT',
1592 docs => 'http://www.blahblah.org/fm.cdf',
1593 managingEditor => 'scoop@freshmeat.net',
1594 webMaster => 'scoop@freshmeat.net'
1595 );
1596  
1597 $rss->image(title => 'freshmeat.net',
1598 url => 'http://freshmeat.net/images/fm.mini.jpg',
1599 link => 'http://freshmeat.net',
1600 width => 88,
1601 height => 31,
1602 description => 'This is the Freshmeat image stupid'
1603 );
1604  
1605 $rss->add_item(title => "GTKeyboard 0.85",
1606 # creates a guid field with permaLink=true
1607 permaLink => "http://freshmeat.net/news/1999/06/21/930003829.html",
1608 # alternately creates a guid field with permaLink=false
1609 # guid => "gtkeyboard-0.85"
1610 enclosure => { url=>$url, type=>"application/x-bittorrent" },
1611 description => 'blah blah'
1612 );
1613  
1614 $rss->textinput(title => "quick finder",
1615 description => "Use the text input below to search freshmeat",
1616 name => "query",
1617 link => "http://core.freshmeat.net/search.php3"
1618 );
1619  
1620 # create an RSS 0.9 file
1621 use XML::RSS;
1622 my $rss = XML::RSS->new( version => '0.9' );
1623 $rss->channel(title => "freshmeat.net",
1624 link => "http://freshmeat.net",
1625 description => "the one-stop-shop for all your Linux software needs",
1626 );
1627  
1628 $rss->image(title => "freshmeat.net",
1629 url => "http://freshmeat.net/images/fm.mini.jpg",
1630 link => "http://freshmeat.net"
1631 );
1632  
1633 $rss->add_item(title => "GTKeyboard 0.85",
1634 link => "http://freshmeat.net/news/1999/06/21/930003829.html"
1635 );
1636  
1637 $rss->textinput(title => "quick finder",
1638 description => "Use the text input below to search freshmeat",
1639 name => "query",
1640 link => "http://core.freshmeat.net/search.php3"
1641 );
1642  
1643 # print the RSS as a string
1644 print $rss->as_string;
1645  
1646 # or save it to a file
1647 $rss->save("fm.rdf");
1648  
1649 # insert an item into an RSS file and removes the oldest ones if
1650 # there are already 15 items or more
1651 my $rss = XML::RSS->new;
1652 $rss->parsefile("fm.rdf");
1653  
1654 while (@{$rss->{'items'}} >= 15)
1655 {
1656 shift (@{ $rss->{'items'} });
1657 }
1658  
1659 $rss->add_item(title => "MpegTV Player (mtv) 1.0.9.7",
1660 link => "http://freshmeat.net/news/1999/06/21/930003958.html",
1661 mode => 'insert'
1662 );
1663  
1664 # parse a string instead of a file
1665 $rss->parse($string);
1666  
1667 # print the title and link of each RSS item
1668 foreach my $item (@{$rss->{'items'}}) {
1669 print "title: $item->{'title'}\n";
1670 print "link: $item->{'link'}\n\n";
1671 }
1672  
1673 # output the RSS 0.9 or 0.91 file as RSS 1.0
1674 $rss->{output} = '1.0';
1675 print $rss->as_string;
1676  
1677 =head1 DESCRIPTION
1678  
1679 This module provides a basic framework for creating and maintaining
1680 RDF Site Summary (RSS) files. This distribution also contains many
1681 examples that allow you to generate HTML from an RSS, convert between
1682 0.9, 0.91, 1.0, and 2.0 version, and other nifty things.
1683 This might be helpful if you want to include news feeds on your Web
1684 site from sources like Slashdot and Freshmeat or if you want to syndicate
1685 your own content.
1686  
1687 XML::RSS currently supports versions
1688 L<0.9|http://www.rssboard.org/rss-0-9-0>,
1689 L<0.91|http://www.rssboard.org/rss-0-9-1>,
1690 L<1.0|http://web.resource.org/rss/1.0/>, and
1691 L<2.0|http://www.rssboard.org/rss-2-0> of RSS.
1692  
1693 RSS was originally developed by Netscape as the format for
1694 Netscape Netcenter channels, however, many Web sites have since
1695 adopted it as a simple syndication format. With the advent of RSS 1.0,
1696 users are now able to syndication many different kinds of content
1697 including news headlines, threaded messages, products catalogs, etc.
1698  
1699 B<Note:> In order to parse and generate dates (such as C<pubDate>
1700 and C<dc:date>) it is recommended to use L<DateTime::Format::Mail> and
1701 L<DateTime::Format::W3CDTF> , which is what L<XML::RSS> uses internally
1702 and requires. It should also be possible to pass L<DateTime> objects
1703 which will be formatted accordingly. E.g:
1704  
1705 use DateTime ();
1706  
1707 my $dt = DateTime->from_epoch(epoch => 1_500_000_000);
1708  
1709 $rss->channel(
1710 pubDate => $dt,
1711 .
1712 .
1713 .
1714 );
1715  
1716 =head1 METHODS
1717  
1718 =over 4
1719  
1720 =item XML::RSS->new(version=>$version, encoding=>$encoding, output=>$output, stylesheet=>$stylesheet_url, 'xml:base'=>$base)
1721  
1722 Constructor for XML::RSS. It returns a reference to an XML::RSS object.
1723 You may also pass the RSS version and the XML encoding to use. The default
1724 B<version> is 1.0. The default B<encoding> is UTF-8. You may also specify
1725 the B<output> format regardless of the input version. This comes in handy
1726 when you want to convert RSS between versions. The XML::RSS modules
1727 will convert between any of the formats. If you set <encode_output> XML::RSS
1728 will make sure to encode any entities in generated RSS. This is now on by
1729 default.
1730  
1731 You can also pass an optional URL to an XSL stylesheet that can be used to
1732 output an C<<< <?xsl-stylesheet ... ?> >>> meta-tag in the header that will
1733 allow some browsers to render the RSS file as HTML.
1734  
1735 You can also set C<encode_cb> to a reference to a subroutine that will
1736 encode the output in a custom way. This subroutine accepts two parameters:
1737 a reference to the C<XML::RSS::Private::Output::Base>-derived object (which
1738 should normally not concern you) and the text to encode. It should return
1739 the text to encode. If not set, then the module will encode using its
1740 custom encoding routine.
1741  
1742 xml:base will set an C<xml:base> property as per
1743  
1744 http://www.w3.org/TR/xmlbase/
1745  
1746 Note that in order to encode properly, you need to handle "CDATA" sections
1747 properly. Look at L<XML::RSS::Private::Output::Base>'s C<_default_encode()>
1748 method for how to do it properly.
1749  
1750 =item add_item (title=>$title, link=>$link, description=>$desc, mode=>$mode)
1751  
1752 Adds an item to the XML::RSS object. B<mode> and B<description> are optional.
1753 The default B<mode>
1754 is append, which adds the item to the end of the list. To insert an item, set the mode
1755 to B<insert>.
1756  
1757 The items are stored in the array C<< @{$obj->{'items'}} >> where
1758 B<$obj> is a reference to an XML::RSS object.
1759  
1760 One can specify a category by using the B<'category'> key. B<'category'> can
1761 point to an array reference of categories:
1762  
1763 $rss->add_item(
1764 title => "Foo&Bar",
1765 link => "http://www.my.tld/",
1766 category => ["OneCat", "TooCat", "3Kitties"],
1767 );
1768  
1769 =item as_string;
1770  
1771 Returns a string containing the RSS for the XML::RSS object. This
1772 method will also encode special characters along the way.
1773  
1774 =item channel (title=>$title, link=>$link, description=>$desc, language=>$language, rating=>$rating, copyright=>$copyright, pubDate=>$pubDate, lastBuildDate=>$lastBuild, docs=>$docs, managingEditor=>$editor, webMaster=>$webMaster)
1775  
1776 Channel information is required in RSS. The B<title> cannot
1777 be more the 40 characters, the B<link> 500, and the B<description>
1778 500 when outputting RSS 0.9. B<title>, B<link>, and B<description>,
1779 are required for RSS 1.0. B<language> is required for RSS 0.91.
1780 The other parameters are optional for RSS 0.91 and 1.0.
1781  
1782 To retrieve the values of the channel, pass the name of the value
1783 (title, link, or description) as the first and only argument
1784 like so:
1785  
1786 $title = channel('title');
1787  
1788 =item image (title=>$title, url=>$url, link=>$link, width=>$width, height=>$height, description=>$desc)
1789  
1790 Adding an image is not required. B<url> is the URL of the
1791 image, B<link> is the URL the image is linked to. B<title>, B<url>,
1792 and B<link> parameters are required if you are going to
1793 use an image in your RSS file. The remaining image elements are used
1794 in RSS 0.91 or optionally imported into RSS 1.0 via the rss091 namespace.
1795  
1796 The method for retrieving the values for the image is the same as it
1797 is for B<channel()>.
1798  
1799 =item parse ($string, \%options)
1800  
1801 Parses an RDF Site Summary which is passed into B<parse()> as the first
1802 parameter. Returns the instance of the object so one can say
1803 C<< $rss->parse($string)->other_method() >>.
1804  
1805 See the add_module() method for instructions on automatically adding
1806 modules as a string is parsed.
1807  
1808 %options is a list of options that specify how parsing is to be done. The
1809 available options are:
1810  
1811 =over 4
1812  
1813 =item * allow_multiple
1814  
1815 Takes an array ref of names which indicates which elements should
1816 be allowed to have multiple occurrences. So, for example, to parse
1817 feeds with multiple enclosures
1818  
1819 $rss->parse($xml, { allow_multiple => ['enclosure'] });
1820  
1821 =item * hashrefs_instead_of_strings
1822  
1823 If true, then some items (so far "C<description>") will become hash-references
1824 instead of strings (with a B<content> key containing their content , B<if>
1825 they have XML attributes. Without this key, the attributes will be ignored
1826 and there will only be a string. Thus, specifying this option may break
1827 compatibility.
1828  
1829 =item * modules_as_arrays
1830  
1831 This option when true, will parse the modules key-value-pairs as an arrayref of
1832 C<<< { el => $key_name, value => $value, } >>> hash-refs to gracefully
1833 handle duplicate items (see below). It will not affect the known modules such
1834 as dc ("Dublin Core").
1835  
1836 =back
1837  
1838 =item parsefile ($file, \%options)
1839  
1840 Same as B<parse()> except it parses a file rather than a string.
1841  
1842 See the add_module() method for instructions on automatically adding
1843 modules as a string is parsed.
1844  
1845 =item save ($file)
1846  
1847 Saves the RSS to a specified file.
1848  
1849 =item skipDays (day => $day)
1850  
1851 Populates the skipDays element with the day $day.
1852  
1853 =item skipHours (hour => $hour)
1854  
1855 Populates the skipHours element, with the hour $hour.
1856  
1857 =item strict ($boolean)
1858  
1859 If it's set to 1, it will adhere to the lengths as specified
1860 by Netscape Netcenter requirements. It's set to 0 by default.
1861 Use it if the RSS file you're generating is for Netcenter.
1862 strict will only work for RSS 0.9 and 0.91. Do not use it for
1863 RSS 1.0.
1864  
1865 =item textinput (title=>$title, description=>$desc, name=>$name, link=>$link);
1866  
1867 This RSS element is also optional. Using it allows users to submit a Query
1868 to a program on a Web server via an HTML form. B<name> is the HTML form name
1869 and B<link> is the URL to the program. Content is submitted using the GET
1870 method.
1871  
1872 Access to the B<textinput> values is the same as B<channel()> and
1873 B<image()>.
1874  
1875 =item add_module(prefix=>$prefix, uri=>$uri)
1876  
1877 Adds a module namespace declaration to the XML::RSS object, allowing you
1878 to add modularity outside of the standard RSS 1.0 modules. At present,
1879 the standard modules Dublin Core (dc) and Syndication (syn) are predefined
1880 for your convenience. The Taxonomy (taxo) module is also internally supported.
1881  
1882 The modules are stored in the hash %{$obj->{'modules'}} where
1883 B<$obj> is a reference to an XML::RSS object.
1884  
1885 If you want to automatically add modules that the parser finds in
1886 namespaces, set the $XML::RSS::AUTO_ADD variable to a true value. By
1887 default the value is false. (N.B. AUTO_ADD only updates the
1888 %{$obj->{'modules'}} hash. It does not provide the other benefits
1889 of using add_module.)
1890  
1891 =back
1892  
1893 =head2 RSS 1.0 MODULES
1894  
1895 XML-Namespace-based modularization affords RSS 1.0 compartmentalized
1896 extensibility. The only modules that ship "in the box" with RSS 1.0
1897 are Dublin Core (http://purl.org/rss/1.0/modules/dc/), Syndication
1898 (http://purl.org/rss/1.0/modules/syndication/), and Taxonomy
1899 (http://purl.org/rss/1.0/modules/taxonomy/). Consult the appropriate
1900 module's documentation for further information.
1901  
1902 Adding items from these modules in XML::RSS is as simple as adding other
1903 attributes such as title, link, and description. The only difference
1904 is the compartmentalization of their key/value paris in a second-level
1905 hash.
1906  
1907 $rss->add_item (title=>$title, link=>$link, dc=>{ subject=>$subject, creator=>$creator, date=>$date });
1908  
1909 For elements of the Dublin Core module, use the key 'dc'. For elements
1910 of the Syndication module, 'syn'. For elements of the Taxonomy module,
1911 'taxo'. These are the prefixes used in the RSS XML document itself.
1912 They are associated with appropriate URI-based namespaces:
1913  
1914 syn: http://purl.org/rss/1.0/modules/syndication/
1915 dc: http://purl.org/dc/elements/1.1/
1916 taxo: http://purl.org/rss/1.0/modules/taxonomy/
1917  
1918 The Dublin Core ('dc') hash keys may be point to an array
1919 reference, which in turn will specify multiple such keys, and render them
1920 one after the other. For example:
1921  
1922 $rss->add_item (
1923 title => $title,
1924 link => $link,
1925 dc => {
1926 subject=> ["Jungle", "Desert", "Swamp"],
1927 creator=>$creator,
1928 date=>$date
1929 },
1930 );
1931  
1932 Dublin Core elements may occur in channel, image, item(s), and textinput
1933 -- albeit uncomming to find them under image and textinput. Syndication
1934 elements are limited to the channel element. Taxonomy elements can occur
1935 in the channel or item elements.
1936  
1937 Access to module elements after parsing an RSS 1.0 document using
1938 XML::RSS is via either the prefix or namespace URI for your convenience.
1939  
1940 print $rss->{items}->[0]->{dc}->{subject};
1941  
1942 or
1943  
1944 print $rss->{items}->[0]->{'http://purl.org/dc/elements/1.1/'}->{subject};
1945  
1946 XML::RSS also has support for "non-standard" RSS 1.0 modularization at
1947 the channel, image, item, and textinput levels. Parsing an RSS document
1948 grabs any elements of other namespaces which might appear. XML::RSS
1949 also allows the inclusion of arbitrary namespaces and associated elements
1950 when building RSS documents.
1951  
1952 For example, to add elements of a made-up "My" module, first declare the
1953 namespace by associating a prefix with a URI:
1954  
1955 $rss->add_module(prefix=>'my', uri=>'http://purl.org/my/rss/module/');
1956  
1957 Then proceed as usual:
1958  
1959 $rss->add_item (title=>$title, link=>$link, my=>{ rating=>$rating });
1960  
1961 You can also set the value of the module's prefix to an array reference
1962 of C<<< { el => , val => } >>> hash-references, in which case duplicate
1963 elements are possible:
1964  
1965 $rss->add_item(title=>$title, link=>$link, my=> [
1966 {el => "rating", value => $rating1, }
1967 {el => "rating", value => $rating2, },
1968 ]
1969  
1970 Non-standard namespaces are not, however, currently accessible via a simple
1971 prefix; access them via their namespace URL like so:
1972  
1973 print $rss->{items}->[0]->{'http://purl.org/my/rss/module/'}->{rating};
1974  
1975 XML::RSS will continue to provide built-in support for standard RSS 1.0
1976 modules as they appear.
1977  
1978 =head1 Non-API Methods
1979  
1980 =head2 $rss->as_rss_0_9()
1981  
1982 B<WARNING>: this function is not an API function and should not be called
1983 directly. It is kept as is for backwards compatibility with legacy code. Use
1984 the following code instead:
1985  
1986 $rss->{output} = "0.9";
1987 my $text = $rss->as_string();
1988  
1989 This function renders the data in the object as an RSS version 0.9 feed,
1990 and returns the resultant XML as text.
1991  
1992 =head2 $rss->as_rss_0_9_1()
1993  
1994 B<WARNING>: this function is not an API function and should not be called
1995 directly. It is kept as is for backwards compatibility with legacy code. Use
1996 the following code instead:
1997  
1998 $rss->{output} = "0.91";
1999 my $text = $rss->as_string();
2000  
2001 This function renders the data in the object as an RSS version 0.91 feed,
2002 and returns the resultant XML as text.
2003  
2004 =head2 $rss->as_rss_1_0()
2005  
2006 B<WARNING>: this function is not an API function and should not be called
2007 directly. It is kept as is for backwards compatibility with legacy code. Use
2008 the following code instead:
2009  
2010 $rss->{output} = "1.0";
2011 my $text = $rss->as_string();
2012  
2013 This function renders the data in the object as an RSS version 1.0 feed,
2014 and returns the resultant XML as text.
2015  
2016 =head2 $rss->as_rss_2_0()
2017  
2018 B<WARNING>: this function is not an API function and should not be called
2019 directly. It is kept as is for backwards compatibility with legacy code. Use
2020 the following code instead:
2021  
2022 $rss->{output} = "2.0";
2023 my $text = $rss->as_string();
2024  
2025 This function renders the data in the object as an RSS version 2.0 feed,
2026 and returns the resultant XML as text.
2027  
2028 =head2 $rss->handle_char()
2029  
2030 Needed for XML::Parser. Don't use this directly.
2031  
2032 =head2 $rss->handle_dec()
2033  
2034 Needed for XML::Parser. Don't use this directly.
2035  
2036 =head2 $rss->handle_start()
2037  
2038 Needed for XML::Parser. Don't use this directly.
2039  
2040 =head1 BUGS
2041  
2042 Please use rt.cpan.org for tracking bugs. The list of current open
2043 bugs is at
2044 L<http://rt.cpan.org/Dist/Display.html?Queue=XML-RSS>.
2045  
2046 To report a new bug, go to
2047 L<http://rt.cpan.org/Ticket/Create.html?Queue=XML-RSS>
2048  
2049 Please include a failing test in your bug report. I'd much rather
2050 have a well written test with the bug report than a patch.
2051  
2052 When you create diffs (for tests or patches), please use the C<-u>
2053 parameter to diff.
2054  
2055 =head1 SOURCE AVAILABILITY
2056  
2057 The source is available from the GitHub repository:
2058  
2059 L<https://github.com/shlomif/perl-XML-RSS>
2060  
2061 =head1 AUTHOR
2062  
2063 Original code: Jonathan Eisenzopf <eisen@pobox.com>
2064  
2065 Further changes: Rael Dornfest <rael@oreilly.com>, Ask Bjoern Hansen
2066 <ask@develooper.com>
2067  
2068 Currently: Shlomi Fish <shlomif@cpan.org>
2069  
2070 =head1 COPYRIGHT
2071  
2072 Copyright (c) 2001 Jonathan Eisenzopf <eisen@pobox.com> and Rael
2073 Dornfest <rael@oreilly.com>, Copyright (C) 2006-2007 Ask Bjoern Hansen
2074 <ask@develooper.com>.
2075  
2076 =head1 LICENSE
2077  
2078 XML::RSS is free software. You can redistribute it and/or
2079 modify it under the same terms as Perl itself.
2080  
2081 =head1 CREDITS
2082  
2083 Wojciech Zwiefka <wojtekz@cnt.pl>
2084 Chris Nandor <pudge@pobox.com>
2085 Jim Hebert <jim@cosource.com>
2086 Randal Schwartz <merlyn@stonehenge.com>
2087 rjp@browser.org
2088 Kellan Elliott-McCrea <kellan@protest.net>
2089 Rafe Colburn <rafe@rafe.us>
2090 Adam Trickett <atrickett@cpan.org>
2091 Aaron Straup Cope <asc@vineyard.net>
2092 Ian Davis <iand@internetalchemy.org>
2093 rayg@varchars.com
2094 Shlomi Fish <shlomif@cpan.org>
2095  
2096 =head1 SEE ALSO
2097  
2098 perl(1), XML::Parser(3).
2099  
2100 =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
2101  
2102 =head1 SUPPORT
2103  
2104 =head2 Websites
2105  
2106 The following websites have more information about this module, and may be of help to you. As always,
2107 in addition to those websites please use your favorite search engine to discover more resources.
2108  
2109 =over 4
2110  
2111 =item *
2112  
2113 MetaCPAN
2114  
2115 A modern, open-source CPAN search engine, useful to view POD in HTML format.
2116  
2117 L<https://metacpan.org/release/XML-RSS>
2118  
2119 =item *
2120  
2121 RT: CPAN's Bug Tracker
2122  
2123 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
2124  
2125 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-RSS>
2126  
2127 =item *
2128  
2129 CPANTS
2130  
2131 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
2132  
2133 L<http://cpants.cpanauthors.org/dist/XML-RSS>
2134  
2135 =item *
2136  
2137 CPAN Testers
2138  
2139 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
2140  
2141 L<http://www.cpantesters.org/distro/X/XML-RSS>
2142  
2143 =item *
2144  
2145 CPAN Testers Matrix
2146  
2147 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
2148  
2149 L<http://matrix.cpantesters.org/?dist=XML-RSS>
2150  
2151 =item *
2152  
2153 CPAN Testers Dependencies
2154  
2155 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
2156  
2157 L<http://deps.cpantesters.org/?module=XML::RSS>
2158  
2159 =back
2160  
2161 =head2 Bugs / Feature Requests
2162  
2163 Please report any bugs or feature requests by email to C<bug-xml-rss at rt.cpan.org>, or through
2164 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-RSS>. You will be automatically notified of any
2165 progress on the request by the system.
2166  
2167 =head2 Source Code
2168  
2169 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
2170 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
2171 from your repository :)
2172  
2173 L<https://github.com/shlomif/perl-XML-RSS>
2174  
2175 git clone git://github.com/shlomif/perl-XML-RSS.git
2176  
2177 =head1 AUTHOR
2178  
2179 Shlomi Fish <shlomif@cpan.org>
2180  
2181 =head1 BUGS
2182  
2183 Please report any bugs or feature requests on the bugtracker website
2184 L<https://github.com/shlomif/perl-XML-RSS/issues>
2185  
2186 When submitting a bug or request, please include a test-file or a
2187 patch to an existing test-file that illustrates the bug or desired
2188 feature.
2189  
2190 =head1 COPYRIGHT AND LICENSE
2191  
2192 This software is copyright (c) 2001 by Various.
2193  
2194 This is free software; you can redistribute it and/or modify it under
2195 the same terms as the Perl 5 programming language system itself.
2196  
2197 =cut
« blib/lib/XML/RSS/Private/Output/Roles/ModulesElems.pm ↑ Summary blib/lib/XML/RSS/Private/Output/V0_9.pm »