prep release
[p5sagit/Config-Any.git] / lib / Config / Any / XML.pm
CommitLineData
f0e3c221 1package Config::Any::XML;
2
3use strict;
4use warnings;
5
dcfb1d1d 6use base 'Config::Any::Base';
7
f0e3c221 8=head1 NAME
9
10Config::Any::XML - Load XML config files
11
12=head1 DESCRIPTION
13
14Loads 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
30return an array of valid extensions (C<xml>).
31
32=cut
33
34sub extensions {
35 return qw( xml );
36}
37
38=head2 load( $file )
39
40Attempts to load C<$file> as an XML file.
41
42=cut
43
44sub load {
45 my $class = shift;
46 my $file = shift;
7d9d2803 47 my $args = shift || {};
f0e3c221 48
49 require XML::Simple;
92a04e78 50 my $config = XML::Simple::XMLin(
51 $file,
f0e3c221 52 ForceArray => [ qw( component model view controller ) ],
7d9d2803 53 %$args
f0e3c221 54 );
55
92a04e78 56 return $class->_coerce( $config );
f0e3c221 57}
58
59sub _coerce {
60 # coerce the XML-parsed config into the correct format
92a04e78 61 my $class = shift;
f0e3c221 62 my $config = shift;
63 my $out;
92a04e78 64 for my $k ( keys %$config ) {
65 my $ref = $config->{ $k };
bb941906 66 my $name = ref $ref eq 'HASH' ? delete $ref->{ name } : undef;
92a04e78 67 if ( defined $name ) {
68 $out->{ $k }->{ $name } = $ref;
69 }
70 else {
71 $out->{ $k } = $ref;
f0e3c221 72 }
73 }
74 $out;
75}
76
dcfb1d1d 77=head2 requires_all_of( )
72628dc7 78
dcfb1d1d 79Specifies that this module requires L<XML::Simple> in order to work.
72628dc7 80
81=cut
82
dcfb1d1d 83sub requires_all_of { 'XML::Simple' }
72628dc7 84
11501cf7 85=head1 CAVEATS
86
87=head2 Strict Mode
88
89If, by some chance, L<XML::Simple> has already been loaded with the strict
90flag turned on, then you will likely get errors as warnings will become
91fatal exceptions and certain arguments to XMLin() will no longer be optional.
92
93See L<XML::Simple's strict mode documentation|XML::Simple/STRICT_MODE> for
94more information.
95
a918b0b8 96=head1 AUTHORS
f0e3c221 97
a918b0b8 98Brian Cassidy E<lt>bricas@cpan.orgE<gt>
f0e3c221 99
a918b0b8 100Joel Bernstein E<lt>rataxis@cpan.orgE<gt>
f0e3c221 101
102=head1 COPYRIGHT AND LICENSE
103
f07b7a17 104Copyright 2006-2010 by Brian Cassidy
f0e3c221 105
106This library is free software; you can redistribute it and/or modify
107it 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
1231;