Log4perl/Config/DOMConfigurator.pm untested

filter: Statementstmt 0.0% 0 / 230 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 461 CC 63 SCAR 43.5
CC
63
Cov
0%
parse_any_param
182.0
parse
132.0
parse_appender
90.0
CRAP 77.2
1 package Log::Log4perl::Config::DOMConfigurator;
2 use Log::Log4perl::Config::BaseConfigurator;
3  
4 our @ISA = qw(Log::Log4perl::Config::BaseConfigurator);
5  
6 #todo
7 # DONE(param-text) some params not attrs but values, like <sql>...</sql>
8 # DONE see DEBUG!!! below
9 # NO, (really is only used for AsyncAppender) appender-ref in <appender>
10 # DONE check multiple appenders in a category
11 # DONE in Config.pm re URL loading, steal from XML::DOM
12 # DONE, OK see PropConfigurator re importing unlog4j, eval_if_perl
13 # NO (is specified in DTD) - need to handle 0/1, true/false?
14 # DONE see Config, need to check version of XML::DOM
15 # OK user defined levels? see parse_level
16 # OK make sure 2nd test is using log4perl constructs, not log4j
17 # OK handle new filter stuff
18 # make sure sample code actually works
19 # try removing namespace prefixes in the xml
20  
21 use XML::DOM;
22 use Log::Log4perl::Level;
23 use strict;
24  
25 use constant _INTERNAL_DEBUG => 0;
26  
27 our $VERSION = 0.03;
28  
29 our $APPENDER_TAG = qr/^((log4j|log4perl):)?appender$/;
30  
31 our $FILTER_TAG = qr/^(log4perl:)?filter$/;
32 our $FILTER_REF_TAG = qr/^(log4perl:)?filter-ref$/;
33  
34 #can't use ValParser here because we're using namespaces?
35 #doesn't seem to work - kg 3/2003
36 our $PARSER_CLASS = 'XML::DOM::Parser';
37  
38 our $LOG4J_PREFIX = 'log4j';
39 our $LOG4PERL_PREFIX = 'log4perl';
40
41  
42 #poor man's export
43 *eval_if_perl = \&Log::Log4perl::Config::eval_if_perl;
44 *unlog4j = \&Log::Log4perl::Config::unlog4j;
45  
46  
47 ###################################################
48 sub parse {
49 ###################################################
50 my($self, $newtext) = @_;
51  
52 $self->text($newtext) if defined $newtext;
53 my $text = $self->{text};
54  
55 my $parser = $PARSER_CLASS->new;
56 my $doc = $parser->parse (join('',@$text));
57  
58  
59 my $l4p_tree = {};
60
61 my $config = $doc->getElementsByTagName("$LOG4J_PREFIX:configuration")->item(0)||
62 $doc->getElementsByTagName("$LOG4PERL_PREFIX:configuration")->item(0);
63  
64 my $threshold = uc(subst($config->getAttribute('threshold')));
65 if ($threshold) {
66 $l4p_tree->{threshold}{value} = $threshold;
67 }
68  
69 if (subst($config->getAttribute('oneMessagePerAppender')) eq 'true') {
70 $l4p_tree->{oneMessagePerAppender}{value} = 1;
71 }
72  
73 for my $kid ($config->getChildNodes){
74  
75 next unless $kid->getNodeType == ELEMENT_NODE;
76  
77 my $tag_name = $kid->getTagName;
78  
79 if ($tag_name =~ $APPENDER_TAG) {
80 &parse_appender($l4p_tree, $kid);
81  
82 }elsif ($tag_name eq 'category' || $tag_name eq 'logger'){
83 &parse_category($l4p_tree, $kid);
84 #Treating them the same is not entirely accurate,
85 #the dtd says 'logger' doesn't accept
86 #a 'class' attribute while 'category' does.
87 #But that's ok, log4perl doesn't do anything with that attribute
88  
89 }elsif ($tag_name eq 'root'){
90 &parse_root($l4p_tree, $kid);
91  
92 }elsif ($tag_name =~ $FILTER_TAG){
93 #parse log4perl's chainable boolean filters
94 &parse_l4p_filter($l4p_tree, $kid);
95  
96 }elsif ($tag_name eq 'renderer'){
97 warn "Log4perl: ignoring renderer tag in config, unimplemented";
98 #"log4j will render the content of the log message according to
99 # user specified criteria. For example, if you frequently need
100 # to log Oranges, an object type used in your current project,
101 # then you can register an OrangeRenderer that will be invoked
102 # whenever an orange needs to be logged. "
103
104 }elsif ($tag_name eq 'PatternLayout'){#log4perl only
105 &parse_patternlayout($l4p_tree, $kid);
106 }
107 }
108 $doc->dispose;
109  
110 return $l4p_tree;
111 }
112  
113 #this is just for toplevel log4perl.PatternLayout tags
114 #holding the custom cspecs
115 sub parse_patternlayout {
116 my ($l4p_tree, $node) = @_;
117  
118 my $l4p_branch = {};
119  
120 for my $child ($node->getChildNodes) {
121 next unless $child->getNodeType == ELEMENT_NODE;
122  
123 my $name = subst($child->getAttribute('name'));
124 my $value;
125  
126 foreach my $grandkid ($child->getChildNodes){
127 if ($grandkid->getNodeType == TEXT_NODE) {
128 $value .= $grandkid->getData;
129 }
130 }
131 $value =~ s/^ +//; #just to make the unit tests pass
132 $value =~ s/ +$//;
133 $l4p_branch->{$name}{value} = subst($value);
134 }
135 $l4p_tree->{PatternLayout}{cspec} = $l4p_branch;
136 }
137  
138  
139 #for parsing the root logger, if any
140 sub parse_root {
141 my ($l4p_tree, $node) = @_;
142  
143 my $l4p_branch = {};
144  
145 &parse_children_of_logger_element($l4p_branch, $node);
146  
147 $l4p_tree->{category}{value} = $l4p_branch->{value};
148  
149 }
150  
151  
152 #this parses a custom log4perl-specific filter set up under
153 #the root element, as opposed to children of the appenders
154 sub parse_l4p_filter {
155 my ($l4p_tree, $node) = @_;
156  
157 my $l4p_branch = {};
158  
159 my $name = subst($node->getAttribute('name'));
160  
161 my $class = subst($node->getAttribute('class'));
162 my $value = subst($node->getAttribute('value'));
163  
164 if ($class && $value) {
165 die "Log4perl: only one of class or value allowed, not both, "
166 ."in XMLConfig filter '$name'";
167 }elsif ($class || $value){
168 $l4p_branch->{value} = ($value || $class);
169  
170 }
171  
172 for my $child ($node->getChildNodes) {
173  
174 if ($child->getNodeType == ELEMENT_NODE){
175  
176 my $tag_name = $child->getTagName();
177  
178 if ($tag_name =~ /^(param|param-nested|param-text)$/) {
179 &parse_any_param($l4p_branch, $child);
180 }
181 }elsif ($child->getNodeType == TEXT_NODE){
182 my $text = $child->getData;
183 next unless $text =~ /\S/;
184 if ($class && $value) {
185 die "Log4perl: only one of class, value or PCDATA allowed, "
186 ."in XMLConfig filter '$name'";
187 }
188 $l4p_branch->{value} .= subst($text);
189 }
190 }
191  
192 $l4p_tree->{filter}{$name} = $l4p_branch;
193 }
194  
195
196 #for parsing a category/logger element
197 sub parse_category {
198 my ($l4p_tree, $node) = @_;
199  
200 my $name = subst($node->getAttribute('name'));
201  
202 $l4p_tree->{category} ||= {};
203
204 my $ptr = $l4p_tree->{category};
205  
206 for my $part (split /\.|::/, $name) {
207 $ptr->{$part} = {} unless exists $ptr->{$part};
208 $ptr = $ptr->{$part};
209 }
210  
211 my $l4p_branch = $ptr;
212  
213 my $class = subst($node->getAttribute('class'));
214 $class &&
215 $class ne 'Log::Log4perl' &&
216 $class ne 'org.apache.log4j.Logger' &&
217 warn "setting category $name to class $class ignored, only Log::Log4perl implemented";
218  
219 #this is kind of funky, additivity has its own spot in the tree
220 my $additivity = subst(subst($node->getAttribute('additivity')));
221 if (length $additivity > 0) {
222 $l4p_tree->{additivity} ||= {};
223 my $add_ptr = $l4p_tree->{additivity};
224  
225 for my $part (split /\.|::/, $name) {
226 $add_ptr->{$part} = {} unless exists $add_ptr->{$part};
227 $add_ptr = $add_ptr->{$part};
228 }
229 $add_ptr->{value} = &parse_boolean($additivity);
230 }
231  
232 &parse_children_of_logger_element($l4p_branch, $node);
233 }
234  
235 # parses the children of a category element
236 sub parse_children_of_logger_element {
237 my ($l4p_branch, $node) = @_;
238  
239 my (@appenders, $priority);
240  
241 for my $child ($node->getChildNodes) {
242 next unless $child->getNodeType == ELEMENT_NODE;
243
244 my $tag_name = $child->getTagName();
245  
246 if ($tag_name eq 'param') {
247 my $name = subst($child->getAttribute('name'));
248 my $value = subst($child->getAttribute('value'));
249 if ($value =~ /^(all|debug|info|warn|error|fatal|off|null)^/) {
250 $value = uc $value;
251 }
252 $l4p_branch->{$name} = {value => $value};
253
254 }elsif ($tag_name eq 'appender-ref'){
255 push @appenders, subst($child->getAttribute('ref'));
256
257 }elsif ($tag_name eq 'level' || $tag_name eq 'priority'){
258 $priority = &parse_level($child);
259 }
260 }
261 $l4p_branch->{value} = $priority.', '.join(',', @appenders);
262
263 return;
264 }
265  
266  
267 sub parse_level {
268 my $node = shift;
269  
270 my $level = uc (subst($node->getAttribute('value')));
271  
272 die "Log4perl: invalid level in config: $level"
273 unless Log::Log4perl::Level::is_valid($level);
274  
275 return $level;
276 }
277  
278  
279  
280 sub parse_appender {
281 my ($l4p_tree, $node) = @_;
282  
283 my $name = subst($node->getAttribute("name"));
284  
285 my $l4p_branch = {};
286  
287 my $class = subst($node->getAttribute("class"));
288  
289 $l4p_branch->{value} = $class;
290  
291 print "looking at $name----------------------\n" if _INTERNAL_DEBUG;
292  
293 for my $child ($node->getChildNodes) {
294 next unless $child->getNodeType == ELEMENT_NODE;
295  
296 my $tag_name = $child->getTagName();
297  
298 my $name = unlog4j(subst($child->getAttribute('name')));
299  
300 if ($tag_name =~ /^(param|param-nested|param-text)$/) {
301  
302 &parse_any_param($l4p_branch, $child);
303  
304 my $value;
305  
306 }elsif ($tag_name =~ /($LOG4PERL_PREFIX:)?layout/){
307 $l4p_branch->{layout} = parse_layout($child);
308  
309 }elsif ($tag_name =~ $FILTER_TAG){
310 $l4p_branch->{Filter} = parse_filter($child);
311  
312 }elsif ($tag_name =~ $FILTER_REF_TAG){
313 $l4p_branch->{Filter} = parse_filter_ref($child);
314  
315 }elsif ($tag_name eq 'errorHandler'){
316 die "errorHandlers not supported yet";
317  
318 }elsif ($tag_name eq 'appender-ref'){
319 #dtd: Appenders may also reference (or include) other appenders.
320 #This feature in log4j is only for appenders who implement the
321 #AppenderAttachable interface, and the only one that does that
322 #is the AsyncAppender, which writes logs in a separate thread.
323 #I don't see the need to support this on the perl side any
324 #time soon. --kg 3/2003
325 die "Log4perl: in config file, <appender-ref> tag is unsupported in <appender>";
326 }else{
327 die "Log4perl: in config file, <$tag_name> is unsupported\n";
328 }
329 }
330 $l4p_tree->{appender}{$name} = $l4p_branch;
331 }
332  
333 sub parse_any_param {
334 my ($l4p_branch, $child) = @_;
335  
336 my $tag_name = $child->getTagName();
337 my $name = subst($child->getAttribute('name'));
338 my $value;
339  
340 print "parse_any_param: <$tag_name name=$name\n" if _INTERNAL_DEBUG;
341  
342 #<param-nested>
343 #note we don't set it to { value => $value }
344 #and we don't test for multiple values
345 if ($tag_name eq 'param-nested'){
346
347 if ($l4p_branch->{$name}){
348 die "Log4perl: in config file, multiple param-nested tags for $name not supported";
349 }
350 $l4p_branch->{$name} = &parse_param_nested($child);
351  
352 return;
353  
354 #<param>
355 }elsif ($tag_name eq 'param') {
356  
357 $value = subst($child->getAttribute('value'));
358  
359 print "parse_param_nested: got param $name = $value\n"
360 if _INTERNAL_DEBUG;
361
362 if ($value =~ /^(all|debug|info|warn|error|fatal|off|null)$/) {
363 $value = uc $value;
364 }
365  
366 if ($name !~ /warp_message|filter/ &&
367 $child->getParentNode->getAttribute('name') ne 'cspec') {
368 $value = eval_if_perl($value);
369 }
370 #<param-text>
371 }elsif ($tag_name eq 'param-text'){
372  
373 foreach my $grandkid ($child->getChildNodes){
374 if ($grandkid->getNodeType == TEXT_NODE) {
375 $value .= $grandkid->getData;
376 }
377 }
378 if ($name !~ /warp_message|filter/ &&
379 $child->getParentNode->getAttribute('name') ne 'cspec') {
380 $value = eval_if_perl($value);
381 }
382 }
383  
384 $value = subst($value);
385  
386 #multiple values for the same param name
387 if (defined $l4p_branch->{$name}{value} ) {
388 if (ref $l4p_branch->{$name}{value} ne 'ARRAY'){
389 my $temp = $l4p_branch->{$name}{value};
390 $l4p_branch->{$name}{value} = [$temp];
391 }
392 push @{$l4p_branch->{$name}{value}}, $value;
393 }else{
394 $l4p_branch->{$name} = {value => $value};
395 }
396 }
397  
398 #handles an appender's <param-nested> elements
399 sub parse_param_nested {
400 my ($node) = shift;
401  
402 my $l4p_branch = {};
403  
404 for my $child ($node->getChildNodes) {
405 next unless $child->getNodeType == ELEMENT_NODE;
406  
407 my $tag_name = $child->getTagName();
408  
409 if ($tag_name =~ /^param|param-nested|param-text$/) {
410 &parse_any_param($l4p_branch, $child);
411 }
412 }
413  
414 return $l4p_branch;
415 }
416  
417 #this handles filters that are children of appenders, as opposed
418 #to the custom filters that go under the root element
419 sub parse_filter {
420 my $node = shift;
421  
422 my $filter_tree = {};
423  
424 my $class_name = subst($node->getAttribute('class'));
425  
426 $filter_tree->{value} = $class_name;
427  
428 print "\tparsing filter on class $class_name\n" if _INTERNAL_DEBUG;
429  
430 for my $child ($node->getChildNodes) {
431 next unless $child->getNodeType == ELEMENT_NODE;
432  
433 my $tag_name = $child->getTagName();
434  
435 if ($tag_name =~ 'param|param-nested|param-text') {
436 &parse_any_param($filter_tree, $child);
437
438 }else{
439 die "Log4perl: don't know what to do with a ".$child->getTagName()
440 ."inside a filter element";
441 }
442 }
443 return $filter_tree;
444 }
445  
446 sub parse_filter_ref {
447 my $node = shift;
448  
449 my $filter_tree = {};
450  
451 my $filter_id = subst($node->getAttribute('id'));
452  
453 $filter_tree->{value} = $filter_id;
454  
455 return $filter_tree;
456 }
457  
458  
459  
460 sub parse_layout {
461 my $node = shift;
462  
463 my $layout_tree = {};
464  
465 my $class_name = subst($node->getAttribute('class'));
466
467 $layout_tree->{value} = $class_name;
468 #
469 print "\tparsing layout $class_name\n" if _INTERNAL_DEBUG;
470 for my $child ($node->getChildNodes) {
471 next unless $child->getNodeType == ELEMENT_NODE;
472 if ($child->getTagName() eq 'param') {
473 my $name = subst($child->getAttribute('name'));
474 my $value = subst($child->getAttribute('value'));
475 if ($value =~ /^(all|debug|info|warn|error|fatal|off|null)$/) {
476 $value = uc $value;
477 }
478 print "\tparse_layout: got param $name = $value\n"
479 if _INTERNAL_DEBUG;
480 $layout_tree->{$name}{value} = $value;
481  
482 }elsif ($child->getTagName() eq 'cspec') {
483 my $name = subst($child->getAttribute('name'));
484 my $value;
485 foreach my $grandkid ($child->getChildNodes){
486 if ($grandkid->getNodeType == TEXT_NODE) {
487 $value .= $grandkid->getData;
488 }
489 }
490 $value =~ s/^ +//;
491 $value =~ s/ +$//;
492 $layout_tree->{cspec}{$name}{value} = subst($value);
493 }
494 }
495 return $layout_tree;
496 }
497  
498 sub parse_boolean {
499 my $a = shift;
500  
501 if ($a eq '0' || lc $a eq 'false') {
502 return '0';
503 }elsif ($a eq '1' || lc $a eq 'true'){
504 return '1';
505 }else{
506 return $a; #probably an error, punt
507 }
508 }
509  
510  
511 #this handles variable substitution
512 sub subst {
513 my $val = shift;
514  
515 $val =~ s/\$\{(.*?)}/
516 Log::Log4perl::Config::var_subst($1, {})/gex;
517 return $val;
518 }
519  
520 1;
521  
522 __END__