1 ########################################################################
5 # Copyright 2005, Marcus Thiesen (marcus@thiesen.org) All rights reserved.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of either:
10 # a) the GNU General Public License as published by the Free Software
11 # Foundation; either version 1, or (at your option) any later
14 # b) the "Artistic License" which comes with Perl.
16 # On Debian GNU/Linux systems, the complete text of the GNU General
17 # Public License can be found in `/usr/share/common-licenses/GPL' and
18 # the Artistic Licence in `/usr/share/common-licenses/Artistic'.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 ########################################################################
26 package XML::OPML::SimpleGen;
31 use base 'Class::Accessor';
32 use POSIX qw(strftime);
34 __PACKAGE__->mk_accessors(qw|groups xml_options outline group xml_head xml_outlines xml|);
37 use version; $VERSION = version->new(0.04);
54 XMLDecl => '<?xml version="1.0" encoding="utf-8" ?>',
58 # default values for nodes
77 my $self = bless $args, $class;
81 dateCreated => strftime( "%a, %e %b %Y %H:%M:%S %z", localtime ),
82 dateModified => strftime( "%a, %e %b %Y %H:%M:%S %z", localtime ),
98 #this is necessary, otherwise XML::Simple will just generate attributes
99 while (my ($key,$value) = each %{ $data }) {
100 $self->xml_head->{$key} = [ $value ];
106 my %defaults = %{$self->group};
112 die "Need to define 'text' attribute" unless defined $data->{text};
114 $data->{outline} = [];
116 push @{$self->xml_outlines}, $data;
117 $self->groups->{$data->{text}} = $data->{outline};
122 my %defaults = %{$self->outline};
128 my $parent = $self->xml_outlines;
130 if (exists $data->{group}) {
131 if (exists $self->groups->{$data->{group}}) {
132 $parent = $self->groups->{$data->{group}};
133 delete($data->{group});
136 $self->add_group('text' => $data->{group});
137 $self->insert_outline(%$data);
142 push @{$parent}, $data;
147 $self->insert_outline(@_);
154 my $xs = new XML::Simple();
156 return $xs->XMLout( $self->_mk_hashref, %{$self->xml_options} );
164 head => $self->xml_head,
165 body => { outline => $self->xml_outlines },
173 my $filename = shift;
176 my $xs = new XML::Simple();
178 $xs->XMLout( $self->_mk_hashref, %{$self->xml_options}, OutputFile => $filename );
187 XML::OPML::SimpleGen - create OPML using XML::Simple
191 require XML::OPML::SimpleGen;
193 my $opml = new XML::OPML::SimpleGen();
196 title => 'FIFFS Subscriptions',
199 $opml->insert_outline(
200 group => 'news', # groups will be auto generated
202 xmlUrl => 'http://www.somepage.org/feed.xml',
205 # insert_outline and add_outline are the same
207 $opml->add_group( text => 'myGroup' ); # explicitly create groups
209 print $opml->to_string;
211 $opml->save('somefile.opml');
213 $opml->xml_options( $hashref ); # XML::Simple compatible options
215 # See XML::OPML's synopsis for more knowledge
220 XML::OPML::SimpleGen lets you simply generate OPML documents
221 without having too much to worry about.
222 It is a drop-in replacement for XML::OPML
223 in regards of generation.
224 As this module uses XML::Simple it is rather
225 generous in regards of attribute or element names.
227 =head1 COMMON METHODS
231 =item new( key => value )
233 Creates a new XML::OPML::SimpleGen instance. All key values will be
234 used as attributes for the <atom> element. The only thing you might
235 want to use here is the version => '1.1', which is default anyway.
237 =item head( key => value )
239 XML::OPML compatible head method to change header values.
243 Returns (and increments) a counter.
245 =item add_group ( text => 'name' )
247 Method to explicitly create a group which can hold multiple outline
250 =item insert_outline ( key => value )
252 XML::OPML compatible method to add an outline element. See
253 L<XML::OPML> for details. The group key is used to put elements in a
254 certain group. Non existent groups will be created automagically.
256 =item add_outline ( key => value )
258 Alias to insert_outline for XML::OPML compatibility.
262 Returns the given OPML XML data as a string
264 =item save ( $filename )
266 Saves the OPML data to a file
270 =head1 ADVANCED METHODS
274 =item xml_options ( $hashref )
276 $hashref may contain any XML::Simple options.
278 =item outline ( $hashref )
280 The outline method defines the 'template' for any new outline
281 element. You can preset key value pairs here to be used
282 in all outline elements that will be generated by XML::OPML::SimpleGen.
284 =item group ( $hashref )
286 This method is similar to outline, it defines the template for a
287 grouping outline element.
293 Marcus Thiesen, C<< <marcus@thiesen.org> >>
297 Please report any bugs or feature requests to
298 C<bug-xml-opml-simlegen@rt.cpan.org>, or through the web interface at
299 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-OPML-SimleGen>.
300 I will be notified, and then you'll automatically be notified of progress on
301 your bug as I make changes.
305 L<XML::OPML> L<XML::Simple>
307 =head1 COPYRIGHT & LICENSE
309 Copyright 2005-2007 Marcus Thiesen, All Rights Reserved.
311 This program is free software; you can redistribute it and/or modify it
312 under the same terms as Perl itself.
316 $Id: SimpleGen.pm,v 1.9 2008/02/08 10:33:43 stephenca Exp $