handle UTF-8 data in JSON correctly
[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> and L<XML::NamespaceSupport>
80 in order to work.
81
82 =cut
83
84 sub requires_all_of { 'XML::Simple', 'XML::NamespaceSupport' }
85
86 =head1 CAVEATS
87
88 =head2 Strict Mode
89
90 If, by some chance, L<XML::Simple> has already been loaded with the strict
91 flag turned on, then you will likely get errors as warnings will become
92 fatal exceptions and certain arguments to XMLin() will no longer be optional.
93
94 See L<XML::Simple's strict mode documentation|XML::Simple/STRICT_MODE> for
95 more information.
96
97 =head1 AUTHORS
98
99 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
100
101 Joel Bernstein E<lt>rataxis@cpan.orgE<gt>
102
103 =head1 COPYRIGHT AND LICENSE
104
105 Copyright 2006-2016 by Brian Cassidy
106
107 This library is free software; you can redistribute it and/or modify
108 it under the same terms as Perl itself. 
109
110 =head1 SEE ALSO
111
112 =over 4 
113
114 =item * L<Catalyst>
115
116 =item * L<Config::Any>
117
118 =item * L<XML::Simple>
119
120 =back
121
122 =cut
123
124 1;