1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.joran.action;
15
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.xml.sax.Attributes;
20
21 import ch.qos.logback.core.CoreConstants;
22 import ch.qos.logback.core.joran.JoranConstants;
23 import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
24 import ch.qos.logback.core.util.OptionHelper;
25
26 public class ConversionRuleAction extends Action {
27 boolean inError = false;
28
29
30
31
32
33 @SuppressWarnings("unchecked")
34 public void begin(SaxEventInterpretationContext ec, String localName, Attributes attributes) {
35
36 inError = false;
37
38 String errorMsg;
39 String conversionWord = attributes.getValue(JoranConstants.CONVERSION_WORD_ATTRIBUTE);
40 String converterClass = attributes.getValue(JoranConstants.CONVERTER_CLASS_ATTRIBUTE);
41
42 if (OptionHelper.isNullOrEmptyOrAllSpaces(conversionWord)) {
43 inError = true;
44 errorMsg = "No 'conversionWord' attribute in <conversionRule>";
45 addError(errorMsg);
46
47 return;
48 }
49
50 if (OptionHelper.isNullOrEmptyOrAllSpaces(converterClass)) {
51 inError = true;
52 errorMsg = "No 'converterClass' attribute in <conversionRule>";
53 ec.addError(errorMsg);
54
55 return;
56 }
57
58 try {
59 Map<String, String> ruleRegistry = (Map<String, String>) context
60 .getObject(CoreConstants.PATTERN_RULE_REGISTRY);
61 if (ruleRegistry == null) {
62 ruleRegistry = new HashMap<String, String>();
63 context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry);
64 }
65
66 addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");
67 ruleRegistry.put(conversionWord, converterClass);
68 } catch (Exception oops) {
69 inError = true;
70 errorMsg = "Could not add conversion rule to PatternLayout.";
71 addError(errorMsg);
72 }
73 }
74
75
76
77
78
79 public void end(SaxEventInterpretationContext ec, String n) {
80 }
81
82 public void finish(SaxEventInterpretationContext ec) {
83 }
84 }