line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::DotNetXML::Parser; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
23
|
use strict; |
|
7
|
|
|
|
|
9
|
|
|
7
|
|
|
|
|
151
|
|
4
|
7
|
|
|
7
|
|
20
|
use warnings; |
|
7
|
|
|
|
|
8
|
|
|
7
|
|
|
|
|
129
|
|
5
|
7
|
|
|
7
|
|
3040
|
use XML::XPath; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '1.6'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Config::DotNetXML::Parser - Parse a .NET XML .config file |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Config::DotNetXML::Parser; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $parser = Config::DotNetXML::Parser->new(File => $file); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $config = $parser->data(); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
This module implements the parsing for Config::DotNetXML it is designed to |
25
|
|
|
|
|
|
|
be used by that module but can be used on its own if the import feature is |
26
|
|
|
|
|
|
|
not required. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
The configuration files are XML documents like: |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
and the configuration is returned as a hash reference of the elements |
37
|
|
|
|
|
|
|
with the key and value attributes providing respectively the key and value |
38
|
|
|
|
|
|
|
to the hash. 'appSettings' is the default section and is the one that is |
39
|
|
|
|
|
|
|
exported into your namepace if you asked Config::DotNetXML to do so. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Named sections can also be introduced: |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
And the items in the named section can be accessed via the getConfigSection() |
54
|
|
|
|
|
|
|
method. Custom sections can appear in the same file as the appSettings default. |
55
|
|
|
|
|
|
|
It should be noted that single value sections and custom section handlers are |
56
|
|
|
|
|
|
|
currently not supported. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 METHODS |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=over 2 |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item new |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Returns a new Config::DotNetXML::Parser object - it takes parameters in |
67
|
|
|
|
|
|
|
key => value pairs: |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=over 2 |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item File |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
The filename containing the configuration. If this is supplied then the |
74
|
|
|
|
|
|
|
configuration will be available via the data() method immediately, otherwise |
75
|
|
|
|
|
|
|
at the minimum parse() will need to be called first. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=back |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub new |
82
|
|
|
|
|
|
|
{ |
83
|
|
|
|
|
|
|
my ( $class, %Args ) = @_; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $self = bless {}, $class; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
$self->parser(XML::XPath->new()); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
if ( exists $Args{File} ) |
90
|
|
|
|
|
|
|
{ |
91
|
|
|
|
|
|
|
$self->File($Args{File}); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
if ( defined $self->File() ) |
97
|
|
|
|
|
|
|
{ |
98
|
|
|
|
|
|
|
$self->parse(); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
return $self; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item parser |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Convenience accessor/mutator for the underlying XML parser. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub parser |
111
|
|
|
|
|
|
|
{ |
112
|
|
|
|
|
|
|
my ( $self, $parser ) = @_; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
if ( defined $parser ) |
115
|
|
|
|
|
|
|
{ |
116
|
|
|
|
|
|
|
$self->{_parser} = $parser; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
return $self->{_parser}; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item parse |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This causes the configuration file to be parsed, after which the configuration |
125
|
|
|
|
|
|
|
will be available via the data() method. It can be supplied with an optional |
126
|
|
|
|
|
|
|
filename which will remove the need to use the File() method previously. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub parse |
131
|
|
|
|
|
|
|
{ |
132
|
|
|
|
|
|
|
my ( $self, $file ) = @_; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
if ( defined $file ) |
135
|
|
|
|
|
|
|
{ |
136
|
|
|
|
|
|
|
$self->File($file); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
my @sections = qw(appSettings); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $configs = $self->parser()->find('/configuration/configSections/section'); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
my $data = {}; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
foreach my $section ( $configs->get_nodelist() ) |
147
|
|
|
|
|
|
|
{ |
148
|
|
|
|
|
|
|
push @sections,$section->getAttribute('name'); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
foreach my $section ( @sections ) |
152
|
|
|
|
|
|
|
{ |
153
|
|
|
|
|
|
|
my $adds = $self->parser()->find("/configuration/$section/add"); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
if ( defined $adds ) |
156
|
|
|
|
|
|
|
{ |
157
|
|
|
|
|
|
|
foreach my $add ( $adds->get_nodelist() ) |
158
|
|
|
|
|
|
|
{ |
159
|
|
|
|
|
|
|
my $key = $add->getAttribute('key'); |
160
|
|
|
|
|
|
|
my $value = $add->getAttribute('value'); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$data->{$section}->{$key} = $value; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
$self->configData($data); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item data |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Returns parsed data from the default (appSettings) section - this will be |
173
|
|
|
|
|
|
|
undefined if parse() has not previously been called or there is no appSettings |
174
|
|
|
|
|
|
|
section in the configuration. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub data |
179
|
|
|
|
|
|
|
{ |
180
|
|
|
|
|
|
|
my ( $self,$data ) = @_; |
181
|
|
|
|
|
|
|
return $self->getConfigSection('appSettings') || {}; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item getConfigSection |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Returns the named configuration section or a false value if there is no such |
187
|
|
|
|
|
|
|
section. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub getConfigSection |
192
|
|
|
|
|
|
|
{ |
193
|
|
|
|
|
|
|
my ( $self, $section ) = @_; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
return $self->configData()->{$section}; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item configData |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Accessor/Mutator for the underlying parsed configuration data, you will |
201
|
|
|
|
|
|
|
almost certainly be accessing the configuration through the methods of |
202
|
|
|
|
|
|
|
L rather than using this directly. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=cut |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub configData |
207
|
|
|
|
|
|
|
{ |
208
|
|
|
|
|
|
|
my ( $self, $data ) = @_; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
if ( defined $data ) |
211
|
|
|
|
|
|
|
{ |
212
|
|
|
|
|
|
|
$self->{_data} = $data; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
if ( not exists $self->{_data} ) |
216
|
|
|
|
|
|
|
{ |
217
|
|
|
|
|
|
|
$self->{_data} = {}; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
return $self->{_data}; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item File |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Returns or sets the name of the file to be parsed for the configuration. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=back |
230
|
|
|
|
|
|
|
=cut |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
sub File |
233
|
|
|
|
|
|
|
{ |
234
|
|
|
|
|
|
|
my ( $self , $file ) = @_; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
if ( defined $file ) |
237
|
|
|
|
|
|
|
{ |
238
|
|
|
|
|
|
|
$self->parser()->set_filename($file); |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
return $self->parser()->get_filename(); |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 BUGS |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Those familiar with the .NET Framework will realise that this is not a |
248
|
|
|
|
|
|
|
complete implementation of all of the facilities offered by the |
249
|
|
|
|
|
|
|
System.Configuration class: specifically custom section handlers, and |
250
|
|
|
|
|
|
|
single value sections - these should come later. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
The observant will have noticed that the use of configuration sections |
253
|
|
|
|
|
|
|
causes the file not to be strictly valid XML inasmuch as the schema cannot |
254
|
|
|
|
|
|
|
be defined prior to parsing - this is unfortunately the way that the .NET |
255
|
|
|
|
|
|
|
framework has it specified, the named sections should probably be dealt with |
256
|
|
|
|
|
|
|
using a 'name' attribute instead. |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head1 AUTHOR |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Jonathan Stowe |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head1 COPYRIGHT |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
This library is free software - it comes with no warranty whatsoever. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
Copyright (c) 2004, 2005, 2016 Jonathan Stowe |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
This module can be distributed under the same terms as Perl itself. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=cut |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
1; |