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