1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.joran.action;
15
16 import ch.qos.logback.core.CoreConstants;
17 import ch.qos.logback.core.joran.JoranConstants;
18 import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
19 import ch.qos.logback.core.model.ConversionRuleModel;
20 import ch.qos.logback.core.model.Model;
21 import ch.qos.logback.core.util.OptionHelper;
22 import org.xml.sax.Attributes;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import static ch.qos.logback.core.joran.JoranConstants.CONVERSION_WORD_ATTRIBUTE;
28
29 public class ConversionRuleAction extends BaseModelAction {
30
31 static public String CONVERTER_CLASS_ATTRIBUTE = "converterClass";
32
33
34 @Override
35 protected boolean validPreconditions(SaxEventInterpretationContext seic, String name, Attributes attributes) {
36 PreconditionValidator pv = new PreconditionValidator(this, seic, name, attributes);
37
38 boolean invalidConverterClassAttribute = pv.isInvalidAttribute(CONVERTER_CLASS_ATTRIBUTE);
39 boolean invalidClassAttribute = pv.isInvalidAttribute(CLASS_ATTRIBUTE);
40
41 if(!invalidConverterClassAttribute) {
42 pv.addWarn("["+CONVERTER_CLASS_ATTRIBUTE +"] attribute is deprecated and replaced by ["+CLASS_ATTRIBUTE+
43 "]. "+pv.getLocationSuffix());
44 }
45 boolean missingClass = invalidClassAttribute && invalidConverterClassAttribute;
46 if(missingClass) {
47 pv.addMissingAttributeError(CLASS_ATTRIBUTE);
48 return false;
49 }
50
51 boolean multipleClassAttributes = (!invalidClassAttribute) && (!invalidConverterClassAttribute);
52 if(multipleClassAttributes) {
53 pv.addWarn("Both ["+CONVERTER_CLASS_ATTRIBUTE+"] attribute and ["+CLASS_ATTRIBUTE+"] attribute specified. ");
54 pv.addWarn( "["+CLASS_ATTRIBUTE+"] attribute will override. ");
55 }
56 pv.validateGivenAttribute(CONVERSION_WORD_ATTRIBUTE);
57 return pv.isValid();
58 }
59
60
61
62 @Override
63 protected Model buildCurrentModel(SaxEventInterpretationContext interpretationContext, String name,
64 Attributes attributes) {
65 ConversionRuleModel conversionRuleModel = new ConversionRuleModel();
66 conversionRuleModel.setConversionWord(attributes.getValue(CONVERSION_WORD_ATTRIBUTE));
67
68 String converterClassStr = attributes.getValue(CONVERTER_CLASS_ATTRIBUTE);
69 if(!OptionHelper.isNullOrEmpty(converterClassStr)) {
70 conversionRuleModel.setClassName(converterClassStr);
71 }
72
73 String classStr = attributes.getValue(CLASS_ATTRIBUTE);
74 if(!OptionHelper.isNullOrEmpty(classStr)) {
75 conversionRuleModel.setClassName(classStr);
76 }
77 return conversionRuleModel;
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 }