Commit | Line | Data |
f0e3c221 |
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; |
7d9d2803 |
45 | my $args = shift || {}; |
f0e3c221 |
46 | |
47 | require XML::Simple; |
92a04e78 |
48 | my $config = XML::Simple::XMLin( |
49 | $file, |
f0e3c221 |
50 | ForceArray => [ qw( component model view controller ) ], |
7d9d2803 |
51 | %$args |
f0e3c221 |
52 | ); |
53 | |
92a04e78 |
54 | return $class->_coerce( $config ); |
f0e3c221 |
55 | } |
56 | |
57 | sub _coerce { |
92a04e78 |
58 | |
f0e3c221 |
59 | # coerce the XML-parsed config into the correct format |
92a04e78 |
60 | my $class = shift; |
f0e3c221 |
61 | my $config = shift; |
62 | my $out; |
92a04e78 |
63 | for my $k ( keys %$config ) { |
64 | my $ref = $config->{ $k }; |
65 | my $name = ref $ref ? delete $ref->{ name } : undef; |
66 | if ( defined $name ) { |
67 | $out->{ $k }->{ $name } = $ref; |
68 | } |
69 | else { |
70 | $out->{ $k } = $ref; |
f0e3c221 |
71 | } |
72 | } |
73 | $out; |
74 | } |
75 | |
72628dc7 |
76 | =head2 is_supported( ) |
77 | |
78 | Returns true if L<XML::Simple> is available. |
79 | |
80 | =cut |
81 | |
82 | sub is_supported { |
83 | eval { require XML::Simple; }; |
84 | return $@ ? 0 : 1; |
85 | } |
86 | |
11501cf7 |
87 | =head1 CAVEATS |
88 | |
89 | =head2 Strict Mode |
90 | |
91 | If, by some chance, L<XML::Simple> has already been loaded with the strict |
92 | flag turned on, then you will likely get errors as warnings will become |
93 | fatal exceptions and certain arguments to XMLin() will no longer be optional. |
94 | |
95 | See L<XML::Simple's strict mode documentation|XML::Simple/STRICT_MODE> for |
96 | more information. |
97 | |
a918b0b8 |
98 | =head1 AUTHORS |
f0e3c221 |
99 | |
a918b0b8 |
100 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> |
f0e3c221 |
101 | |
a918b0b8 |
102 | Joel Bernstein E<lt>rataxis@cpan.orgE<gt> |
f0e3c221 |
103 | |
104 | =head1 COPYRIGHT AND LICENSE |
105 | |
a918b0b8 |
106 | Copyright 2007 by Brian Cassidy |
f0e3c221 |
107 | |
108 | This library is free software; you can redistribute it and/or modify |
109 | it under the same terms as Perl itself. |
110 | |
111 | =head1 SEE ALSO |
112 | |
113 | =over 4 |
114 | |
115 | =item * L<Catalyst> |
116 | |
117 | =item * L<Config::Any> |
118 | |
119 | =item * L<XML::Simple> |
120 | |
121 | =back |
122 | |
123 | =cut |
124 | |
125 | 1; |