d5221c5accc1b88f50fb944c7229f2fbacddb1c5
[p5sagit/Config-Any.git] / lib / Config / Any / XML.pm
1 package Config::Any::XML;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Config::Any::XML - Load XML config files
9
10 =head1 DESCRIPTION
11
12 Loads XML files. Example:
13
14     <config>
15         <name>TestApp</name>
16         <component name="Controller::Foo">
17             <foo>bar</foo>
18         </component>
19         <model name="Baz">
20             <qux>xyzzy</qux>
21         </model>
22     </config>
23
24 =head1 METHODS
25
26 =head2 extensions( )
27
28 return an array of valid extensions (C<xml>).
29
30 =cut
31
32 sub extensions {
33     return qw( xml );
34 }
35
36 =head2 load( $file )
37
38 Attempts to load C<$file> as an XML file.
39
40 =cut
41
42 sub load {
43     my $class = shift;
44     my $file  = shift;
45     my $args  = shift || {};
46
47     require XML::Simple;
48     my $config = XML::Simple::XMLin( 
49         $file, 
50         ForceArray => [ qw( component model view controller ) ],
51         %$args
52     );
53
54     return $class->_coerce($config);
55 }
56
57 sub _coerce {
58     # coerce the XML-parsed config into the correct format
59     my $class = shift;
60     my $config = shift;
61     my $out;
62     for my $k (keys %$config) {
63         my $ref = $config->{$k};
64         my $name = ref $ref ? delete $ref->{name} : undef;
65         if (defined $name) {
66             $out->{$k}->{$name} = $ref; 
67         } else {
68             $out->{$k} = $ref;
69         }
70     }
71     $out;
72 }
73
74 =head1 AUTHOR
75
76 =over 4 
77
78 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
79
80 =item * Joel Bernstein E<lt>rataxis@cpan.orgE<gt>
81
82 =back
83
84 =head1 COPYRIGHT AND LICENSE
85
86 Copyright 2006 by Brian Cassidy
87
88 This library is free software; you can redistribute it and/or modify
89 it under the same terms as Perl itself. 
90
91 =head1 SEE ALSO
92
93 =over 4 
94
95 =item * L<Catalyst>
96
97 =item * L<Config::Any>
98
99 =item * L<XML::Simple>
100
101 =back
102
103 =cut
104
105 1;