View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.joran.action;
15  
16  import java.util.Stack;
17  
18  import org.xml.sax.Attributes;
19  
20  import ch.qos.logback.core.joran.spi.InterpretationContext;
21  import ch.qos.logback.core.joran.spi.Pattern;
22  import ch.qos.logback.core.joran.util.PropertySetter;
23  import ch.qos.logback.core.util.AggregationType;
24  
25  /**
26   * This action is responsible for tying together a parent object with one of its
27   * <em>simple</em> properties specified as an element but for which there is
28   * no explicit rule.
29   * 
30   * @author Ceki G&uuml;lc&uuml;
31   */
32  public class NestedBasicPropertyIA extends ImplicitAction {
33  
34  
35    // We use a stack of IADataForBasicProperty objects in order to 
36    // support nested elements which are handled by the same NestedBasicPropertyIA instance.
37    // We push a IADataForBasicProperty instance in the isApplicable method (if the
38    // action is applicable) and pop it in the end() method.
39    // The XML well-formedness property will guarantee that a push will eventually
40    // be followed by the corresponding pop.
41    Stack<IADataForBasicProperty> actionDataStack = new Stack<IADataForBasicProperty>();
42  
43    public boolean isApplicable(Pattern pattern, Attributes attributes,
44        InterpretationContext ec) {
45      // System.out.println("in NestedSimplePropertyIA.isApplicable [" + pattern +
46      // "]");
47      String nestedElementTagName = pattern.peekLast();
48  
49      // no point in attempting if there is no parent object
50      if (ec.isEmpty()) {
51        return false;
52      }
53  
54      Object o = ec.peekObject();
55      PropertySetter parentBean = new PropertySetter(o);
56      parentBean.setContext(context);
57  
58      AggregationType aggregationType = parentBean
59          .computeAggregationType(nestedElementTagName);
60  
61      switch (aggregationType) {
62      case NOT_FOUND:
63      case AS_COMPLEX_PROPERTY:
64      case AS_COMPLEX_PROPERTY_COLLECTION:
65        return false;
66  
67      case AS_BASIC_PROPERTY:
68      case AS_BASIC_PROPERTY_COLLECTION:
69        IADataForBasicProperty ad = new IADataForBasicProperty(parentBean,
70            aggregationType, nestedElementTagName);
71        actionDataStack.push(ad);
72        // addInfo("NestedSimplePropertyIA deemed applicable [" + pattern + "]");
73        return true;
74      default:
75        addError("PropertySetter.canContainComponent returned " + aggregationType);
76        return false;
77      }
78    }
79  
80    public void begin(InterpretationContext ec, String localName,
81        Attributes attributes) {
82      // NOP
83    }
84  
85    public void body(InterpretationContext ec, String body) {
86  
87      String finalBody = ec.subst(body);
88      // System.out.println("body "+body+", finalBody="+finalBody);
89      // get the action data object pushed in isApplicable() method call
90      IADataForBasicProperty actionData = (IADataForBasicProperty) actionDataStack.peek();
91      switch (actionData.aggregationType) {
92      case AS_BASIC_PROPERTY:
93        actionData.parentBean.setProperty(actionData.propertyName, finalBody);
94        break;
95      case AS_BASIC_PROPERTY_COLLECTION:
96        actionData.parentBean
97            .addBasicProperty(actionData.propertyName, finalBody);
98      }
99    }
100 
101   public void end(InterpretationContext ec, String tagName) {
102     // pop the action data object pushed in isApplicable() method call
103     actionDataStack.pop();
104   }
105 }