1
14 package ch.qos.logback.core.joran.action;
15
16 import java.util.Stack;
17
18 import ch.qos.logback.core.joran.spi.ElementPath;
19
20 import org.xml.sax.Attributes;
21
22 import ch.qos.logback.core.joran.spi.InterpretationContext;
23 import ch.qos.logback.core.joran.util.PropertySetter;
24 import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
25 import ch.qos.logback.core.util.AggregationType;
26
27
34 public class NestedBasicPropertyIA extends ImplicitAction {
35
36
37
38
39
40
41
42 Stack<IADataForBasicProperty> actionDataStack = new Stack<IADataForBasicProperty>();
43
44 private final BeanDescriptionCache beanDescriptionCache;
45
46 public NestedBasicPropertyIA(BeanDescriptionCache beanDescriptionCache) {
47 this.beanDescriptionCache = beanDescriptionCache;
48 }
49
50 public boolean isApplicable(ElementPath elementPath, Attributes attributes, InterpretationContext ec) {
51
52
53 String nestedElementTagName = elementPath.peekLast();
54
55
56 if (ec.isEmpty()) {
57 return false;
58 }
59
60 Object o = ec.peekObject();
61 PropertySetter parentBean = new PropertySetter(beanDescriptionCache, o);
62 parentBean.setContext(context);
63
64 AggregationType aggregationType = parentBean.computeAggregationType(nestedElementTagName);
65
66 switch (aggregationType) {
67 case NOT_FOUND:
68 case AS_COMPLEX_PROPERTY:
69 case AS_COMPLEX_PROPERTY_COLLECTION:
70 return false;
71
72 case AS_BASIC_PROPERTY:
73 case AS_BASIC_PROPERTY_COLLECTION:
74 IADataForBasicProperty ad = new IADataForBasicProperty(parentBean, aggregationType, nestedElementTagName);
75 actionDataStack.push(ad);
76
77 return true;
78 default:
79 addError("PropertySetter.canContainComponent returned " + aggregationType);
80 return false;
81 }
82 }
83
84 public void begin(InterpretationContext ec, String localName, Attributes attributes) {
85
86 }
87
88 public void body(InterpretationContext ec, String body) {
89
90 String finalBody = ec.subst(body);
91
92 IADataForBasicProperty actionData = (IADataForBasicProperty) actionDataStack.peek();
93 switch (actionData.aggregationType) {
94 case AS_BASIC_PROPERTY:
95 actionData.parentBean.setProperty(actionData.propertyName, finalBody);
96 break;
97 case AS_BASIC_PROPERTY_COLLECTION:
98 actionData.parentBean.addBasicProperty(actionData.propertyName, finalBody);
99 break;
100 default:
101 addError("Unexpected aggregationType " + actionData.aggregationType);
102 }
103 }
104
105 public void end(InterpretationContext ec, String tagName) {
106
107 actionDataStack.pop();
108 }
109 }
110