File Coverage

blib/lib/Config/Setting/XMLParser.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # Copyright (C) 2004 by Dominic Mitchell. All rights reserved.
2             #
3             # Redistribution and use in source and binary forms, with or without
4             # modification, are permitted provided that the following conditions
5             # are met:
6             # 1. Redistributions of source code must retain the above copyright
7             # notice, this list of conditions and the following disclaimer.
8             # 2. Redistributions in binary form must reproduce the above copyright
9             # notice, this list of conditions and the following disclaimer in the
10             # documentation and/or other materials provided with the distribution.
11             #
12             # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13             # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15             # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
16             # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17             # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18             # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19             # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20             # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21             # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22             # SUCH DAMAGE.
23              
24             =pod
25              
26             =head1 NAME
27              
28             Config::Setting::XMLParser - parse XML settings file.
29              
30             =head1 SYNOPSIS
31              
32             use Config::Setting::XMLParser;
33              
34             my $ini = Config::Setting::XMLParser->new(Filename => $xmlfile);
35             foreach my $s ($ini->sections()) {
36             print "[$s]\n";
37             foreach my $k ($ini->keylist($s)) {
38             print $k, "=", $ini->get($s, $k), "\n";
39             }
40             print "\n";
41             }
42              
43              
44             =head1 DESCRIPTION
45              
46             This class provides access to settings stored in an XML File. The XML
47             File is expected to have the following structure:
48              
49            
50            
51             VALUE
52            
53            
54              
55             Multiple EsectionEs and EitemEs may be present. Any
56             leading and trailing whitespace within an EitemE tag will be
57             stripped.
58              
59             =head1 METHODS
60              
61             =over 4
62              
63             =item new ( ARGS )
64              
65             Instantiate a new object. ARGS is a set of keyword / value pairs which
66             will be passed to the L constructor.
67              
68             =item parse_file ( FILENAME )
69              
70             Parse FILENAME as XML.
71              
72             =item parse_string ( STRING )
73              
74             Parse the string as XML.
75              
76             =item sections ( )
77              
78             Return a list of all sections that occurred in the data. They are
79             returned in the order in which they originally occurred.
80              
81             =item keylist ( SECTION )
82              
83             Return a list of all keys in SECTION.
84              
85             =item get ( SECTION, KEY )
86              
87             Return the value of KEY in SECTION.
88              
89             =back
90              
91             =head1 SEE ALSO
92              
93             perl(1),
94             L,
95             L.
96              
97             =head1 AUTHOR
98              
99             Dominic Mitchell, Ecpan (at) happygiraffe.netE.
100              
101             =cut
102              
103             package Config::Setting::XMLParser;
104              
105 1     1   1234 use strict;
  1         2  
  1         145  
106 1     1   8 use vars qw($rcsid $VERSION);
  1         1  
  1         56  
107              
108 1     1   6 use Carp;
  1         2  
  1         70  
109 1     1   1044 use Config::Setting::Chunk;
  1         2  
  1         35  
110 1     1   3322 use XML::Parser;
  0            
  0            
111              
112             $rcsid = '@(#) $Id: XMLParser.pm 765 2005-08-31 20:05:59Z dom $ ';
113             $VERSION = (qw( $Revision: 765 $ ))[1];
114              
115             sub new {
116             my $class = shift;
117             my (%args) = @_;
118              
119             my $self = {
120             Contents => {},
121             Sections => [],
122             Args => \%args,
123             };
124             bless($self, $class);
125             return $self;
126             }
127              
128             sub parse_file {
129             my $self = shift;
130             my ( $filename ) = @_;
131             open my $fh, $filename
132             or croak "open($filename): $!";
133             my $string = do { local $/ ; <$fh> };
134             close $fh;
135             return $self->_parse( $string );
136             }
137              
138             sub parse_string {
139             my $self = shift;
140             my ( $string ) = @_;
141             return $self->_parse( $string );
142             }
143              
144             #---------------------------------------------------------------------
145              
146             {
147             my $chunk; # Copy of $self during parse.
148             my $CurSection;
149             my $CurItem;
150             my $CurVal;
151              
152             # Parse the stuff we hold.
153             sub _parse {
154             my $self = shift;
155             my ($string) = @_;
156             my $p = XML::Parser->new(
157             Style => "Subs",
158             Pkg => ref($self),
159             %{ $self->{ Args } },
160             );
161             $p->setHandlers(Char => \&Text);
162              
163             $chunk = Config::Setting::Chunk->new;
164             $CurSection = $CurItem = $CurVal = "";
165             eval { $p->parse($string) };
166             croak $@ if $@;
167              
168             return $chunk;
169             }
170              
171             sub section {
172             my ($expat, $tag, %attrs) = @_;
173             my $section = $attrs{name};
174             croak "no section name specified!"
175             unless $section;
176             $CurSection = $section;
177             $chunk->add_section( $section );
178             }
179              
180             sub item {
181             my ($expat, $tag, %attrs) = @_;
182             my $key = $attrs{name};
183             croak "no item name specified!"
184             unless $key;
185             $CurItem = $key;
186             }
187              
188             sub Text {
189             my ($expat, $val) = @_;
190             return unless $CurItem;
191             $CurVal .= $val;
192             }
193              
194             sub item_ {
195             my ($expat, $tag) = @_;
196             # Trim whitespace.
197             $CurVal =~ s/^\s*(.*)\s*$/$1/;
198             $chunk->set_item( $CurSection, $CurItem, $CurVal );
199             $CurItem = $CurVal = "";
200             }
201             }
202              
203             1;
204             __END__