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.joran.spi.InterpretationContext;
17 import ch.qos.logback.core.joran.spi.ActionException;
18 import ch.qos.logback.core.util.OptionHelper;
19 import ch.qos.logback.core.spi.PropertyDefiner;
20 import org.xml.sax.Attributes;
21
22
23
24
25
26
27
28
29 public class DefinePropertyAction extends Action {
30
31 String propertyName;
32 PropertyDefiner definer;
33 boolean inError;
34
35 public void begin(InterpretationContext ec, String localName,
36 Attributes attributes) throws ActionException {
37
38 propertyName = null;
39 definer = null;
40 inError = false;
41
42
43 propertyName = attributes.getValue(NAME_ATTRIBUTE);
44 if (OptionHelper.isEmpty(propertyName)) {
45 addError("Missing property name for property definer. Near [" + localName
46 + "] line " + getLineNumber(ec));
47 inError = true;
48 return;
49 }
50
51
52 String className = attributes.getValue(CLASS_ATTRIBUTE);
53 if (OptionHelper.isEmpty(className)) {
54 addError("Missing class name for property definer. Near [" + localName
55 + "] line " + getLineNumber(ec));
56 inError = true;
57 return;
58 }
59
60
61 try {
62 addInfo("About to instantiate property definer of type [" + className
63 + "]");
64 definer = (PropertyDefiner) OptionHelper.instantiateByClassName(
65 className, PropertyDefiner.class, context);
66 definer.setContext(context);
67 ec.pushObject(definer);
68 } catch (Exception oops) {
69 inError = true;
70 addError("Could not create an PropertyDefiner of type [" + className
71 + "].", oops);
72 throw new ActionException(oops);
73 }
74 }
75
76
77
78
79
80 public void end(InterpretationContext ec, String name) {
81 if (inError) {
82 return;
83 }
84
85 Object o = ec.peekObject();
86
87 if (o != definer) {
88 addWarn("The object at the of the stack is not the property definer for property named ["
89 + propertyName + "] pushed earlier.");
90 } else {
91 addInfo("Popping property definer for property named [" + propertyName
92 + "] from the object stack");
93 ec.popObject();
94
95
96 String propertyValue = definer.getPropertyValue();
97 if(propertyValue != null) {
98 context.putProperty(propertyName, propertyValue);
99 }
100 }
101 }
102 }