File Coverage

blib/lib/XML/OPML/SimpleGen.pm
Criterion Covered Total %
statement 39 79 49.3
branch 0 6 0.0
condition n/a
subroutine 9 15 60.0
pod 8 8 100.0
total 56 108 51.8


line stmt bran cond sub pod time code
1             package XML::OPML::SimpleGen;
2              
3 3     3   86995 use strict;
  3         7  
  3         116  
4 3     3   18 use warnings;
  3         6  
  3         96  
5              
6 3     3   18 use base 'Class::Accessor';
  3         4  
  3         4181  
7              
8 3     3   12428 use DateTime;
  3         902734  
  3         215  
9              
10 3     3   47 use POSIX qw(setlocale LC_TIME LC_CTYPE);
  3         7  
  3         42  
11              
12             __PACKAGE__->mk_accessors(qw|groups xml_options outline group xml_head xml_outlines xml|);
13              
14             # Version set by dist.ini; do not change here.
15             our $VERSION = '0.07'; # VERSION
16              
17             sub new {
18 2     2 1 1357 my $class = shift;
19 2         10 my @args = @_;
20              
21 2         66 my $args = {
22             groups => {},
23              
24             xml => {
25             version => '1.1',
26             @args,
27             },
28              
29             # XML::Simple options
30             xml_options => {
31             RootName => 'opml',
32             XMLDecl => '',
33             AttrIndent => 1,
34             },
35              
36             # default values for nodes
37             outline => {
38             type => 'rss',
39             version => 'RSS',
40             text => '',
41             title => '',
42             description => '',
43             },
44              
45             group => {
46             isOpen => 'true',
47             },
48              
49             xml_head => {},
50             xml_outlines => [],
51              
52             id => 1,
53             };
54              
55 2         9 my $self = bless $args, $class;
56              
57             # Force locale to 'C' rather than local, then reset after setting times.
58             # Fixes RT51000. Thanks to KAPPA for the patch.
59 2         26 my $old_loc = POSIX::setlocale(LC_TIME, "C");
60 2         294 my $ts_ar = [ localtime() ];
61 2         17 $self->head(
62             title => '',
63             $self->_date( dateCreated => $ts_ar ),
64             $self->_date( dateModified => $ts_ar ),
65             );
66 2         13 POSIX::setlocale(LC_TIME,$old_loc);
67              
68 2         12 return $self;
69             }
70              
71             sub _date {
72 4     4   550 my $self = shift;
73 4         9 my $type = shift; # dateCreated or dateModified.
74 4         8 my $ts_ar = shift; # e.g [ localtime() ]
75              
76 4         8 my %arg;
77 4         34 @arg{qw(second minute hour day month year)} = (
78 4         10 @{$ts_ar}[0..3],
79             $ts_ar->[4]+1,
80             $ts_ar->[5]+1900 );
81 4         44 my $dt = DateTime->new( %arg );
82 4         1638 return ( $type => $dt->strftime('%a, %e %b %Y %H:%M:%S %z') );
83             }
84              
85             sub id {
86 0     0 1 0 my $self = shift;
87            
88 0         0 return $self->{id}++;
89             }
90              
91             sub head {
92 3     3 1 311 my $self = shift;
93 3         17 my $data = {@_};
94              
95             #this is necessary, otherwise XML::Simple will just generate attributes
96 3         8 while (my ($key,$value) = each %{ $data }) {
  11         146  
97 8         32 $self->xml_head->{$key} = [ $value ];
98             }
99             }
100              
101             sub add_group {
102 0     0 1 0 my $self = shift;
103 0         0 my %defaults = %{$self->group};
  0         0  
104 0         0 my $data = {
105             id => $self->id,
106             %defaults,
107             @_ };
108            
109 0 0       0 die "Need to define 'text' attribute" unless defined $data->{text};
110              
111 0         0 $data->{outline} = [];
112              
113 0         0 push @{$self->xml_outlines}, $data;
  0         0  
114 0         0 $self->groups->{$data->{text}} = $data->{outline};
115             }
116              
117             sub insert_outline {
118 0     0 1 0 my $self = shift;
119 0         0 my %defaults = %{$self->outline};
  0         0  
120 0         0 my $data = {
121             id => $self->id,
122             %defaults,
123             @_};
124              
125 0         0 my $parent = $self->xml_outlines;
126              
127 0 0       0 if (exists $data->{group}) {
128 0 0       0 if (exists $self->groups->{$data->{group}}) {
129 0         0 $parent = $self->groups->{$data->{group}};
130 0         0 delete($data->{group});
131             }
132             else {
133 0         0 $self->add_group('text' => $data->{group});
134 0         0 $self->insert_outline(%$data);
135 0         0 return;
136             }
137             }
138              
139 0         0 push @{$parent}, $data;
  0         0  
140             }
141              
142             sub add_outline {
143 0     0 1 0 my $self = shift;
144 0         0 $self->insert_outline(@_);
145             }
146              
147             sub as_string {
148 2     2 1 9 my $self = shift;
149              
150 2         2083 require XML::Simple;
151 0           my $xs = XML::Simple->new();
152              
153 0           return $xs->XMLout( $self->_mk_hashref, %{$self->xml_options} );
  0            
154             }
155              
156             sub _mk_hashref {
157 0     0     my $self = shift;
158              
159 0           my $hashref = {
160 0           %{$self->xml},
161             head => $self->xml_head,
162             body => { outline => $self->xml_outlines },
163             };
164              
165 0           return $hashref;
166             }
167              
168             sub save {
169 0     0 1   my $self = shift;
170 0           my $filename = shift;
171              
172 0           require XML::Simple;
173 0           my $xs = XML::Simple->new();
174              
175 0           $xs->XMLout( $self->_mk_hashref, %{$self->xml_options}, OutputFile => $filename );
  0            
176             }
177              
178             1;
179              
180             # ABSTRACT: create OPML using XML::Simple
181              
182             __END__