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