reformat Changes. bump copyright year.
[p5sagit/Config-Any.git] / lib / Config / Any / XML.pm
1 package Config::Any::XML;
2
3 use strict;
4 use warnings;
5
6 use base 'Config::Any::Base';
7
8 =head1 NAME
9
10 Config::Any::XML - Load XML config files
11
12 =head1 DESCRIPTION
13
14 Loads XML files. Example:
15
16     <config>
17         <name>TestApp</name>
18         <component name="Controller::Foo">
19             <foo>bar</foo>
20         </component>
21         <model name="Baz">
22             <qux>xyzzy</qux>
23         </model>
24     </config>
25
26 =head1 METHODS
27
28 =head2 extensions( )
29
30 return an array of valid extensions (C<xml>).
31
32 =cut
33
34 sub extensions {
35     return qw( xml );
36 }
37
38 =head2 load( $file )
39
40 Attempts to load C<$file> as an XML file.
41
42 =cut
43
44 sub load {
45     my $class = shift;
46     my $file  = shift;
47     my $args  = shift || {};
48
49     require XML::Simple;
50     my $config = XML::Simple::XMLin(
51         $file,
52         ForceArray => [ qw( component model view controller ) ],
53         %$args
54     );
55
56     return $class->_coerce( $config );
57 }
58
59 sub _coerce {
60     # coerce the XML-parsed config into the correct format
61     my $class  = shift;
62     my $config = shift;
63     my $out;
64     for my $k ( keys %$config ) {
65         my $ref = $config->{ $k };
66         my $name = ref $ref eq 'HASH' ? delete $ref->{ name } : undef;
67         if ( defined $name ) {
68             $out->{ $k }->{ $name } = $ref;
69         }
70         else {
71             $out->{ $k } = $ref;
72         }
73     }
74     $out;
75 }
76
77 =head2 requires_all_of( )
78
79 Specifies that this module requires L<XML::Simple> in order to work.
80
81 =cut
82
83 sub requires_all_of { 'XML::Simple' }
84
85 =head1 CAVEATS
86
87 =head2 Strict Mode
88
89 If, by some chance, L<XML::Simple> has already been loaded with the strict
90 flag turned on, then you will likely get errors as warnings will become
91 fatal exceptions and certain arguments to XMLin() will no longer be optional.
92
93 See L<XML::Simple's strict mode documentation|XML::Simple/STRICT_MODE> for
94 more information.
95
96 =head1 AUTHORS
97
98 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
99
100 Joel Bernstein E<lt>rataxis@cpan.orgE<gt>
101
102 =head1 COPYRIGHT AND LICENSE
103
104 Copyright 2006-2011 by Brian Cassidy
105
106 This library is free software; you can redistribute it and/or modify
107 it under the same terms as Perl itself. 
108
109 =head1 SEE ALSO
110
111 =over 4 
112
113 =item * L<Catalyst>
114
115 =item * L<Config::Any>
116
117 =item * L<XML::Simple>
118
119 =back
120
121 =cut
122
123 1;