View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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.conditional;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  import java.util.Stack;
19  
20  import org.xml.sax.Attributes;
21  
22  import ch.qos.logback.core.joran.action.Action;
23  import ch.qos.logback.core.joran.event.InPlayListener;
24  import ch.qos.logback.core.joran.event.SaxEvent;
25  import ch.qos.logback.core.joran.spi.ActionException;
26  import ch.qos.logback.core.joran.spi.InterpretationContext;
27  
28  abstract public class ThenOrElseActionBase extends Action {
29  
30      Stack<ThenActionState> stateStack = new Stack<ThenActionState>();
31  
32      @Override
33      public void begin(InterpretationContext ic, String name, Attributes attributes) throws ActionException {
34  
35          if (!weAreActive(ic))
36              return;
37  
38          ThenActionState state = new ThenActionState();
39          if (ic.isListenerListEmpty()) {
40              ic.addInPlayListener(state);
41              state.isRegistered = true;
42          }
43          stateStack.push(state);
44      }
45  
46      boolean weAreActive(InterpretationContext ic) {
47          Object o = ic.peekObject();
48          if (!(o instanceof IfAction))
49              return false;
50          IfAction ifAction = (IfAction) o;
51          return ifAction.isActive();
52      }
53  
54      @Override
55      public void end(InterpretationContext ic, String name) throws ActionException {
56          if (!weAreActive(ic))
57              return;
58  
59          ThenActionState state = stateStack.pop();
60          if (state.isRegistered) {
61              ic.removeInPlayListener(state);
62              Object o = ic.peekObject();
63              if (o instanceof IfAction) {
64                  IfAction ifAction = (IfAction) o;
65                  removeFirstAndLastFromList(state.eventList);
66                  registerEventList(ifAction, state.eventList);
67              } else {
68                  throw new IllegalStateException("Missing IfAction on top of stack");
69              }
70          }
71      }
72  
73      abstract void registerEventList(IfAction ifAction, List<SaxEvent> eventList);
74  
75      void removeFirstAndLastFromList(List<SaxEvent> eventList) {
76          eventList.remove(0);
77          eventList.remove(eventList.size() - 1);
78      }
79  
80  }
81  
82  class ThenActionState implements InPlayListener {
83  
84      List<SaxEvent> eventList = new ArrayList<SaxEvent>();
85      boolean isRegistered = false;
86  
87      public void inPlay(SaxEvent event) {
88          eventList.add(event);
89      }
90  }