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.spi;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import ch.qos.logback.core.joran.event.BodyEvent;
20  import ch.qos.logback.core.joran.event.EndEvent;
21  import ch.qos.logback.core.joran.event.SaxEvent;
22  import ch.qos.logback.core.joran.event.StartEvent;
23  
24  public class EventPlayer {
25  
26      final Interpreter interpreter;
27      List<SaxEvent> eventList;
28      int currentIndex;
29  
30      public EventPlayer(Interpreter interpreter) {
31          this.interpreter = interpreter;
32      }
33  
34      /**
35       * Return a copy of the current event list in the player.
36       * @return
37       * @since 0.9.20
38       */
39      public List<SaxEvent> getCopyOfPlayerEventList() {
40          return new ArrayList<SaxEvent>(eventList);
41      }
42  
43      public void play(List<SaxEvent> aSaxEventList) {
44          eventList = aSaxEventList;
45          SaxEvent se;
46          for (currentIndex = 0; currentIndex < eventList.size(); currentIndex++) {
47              se = eventList.get(currentIndex);
48  
49              if (se instanceof StartEvent) {
50                  interpreter.startElement((StartEvent) se);
51                  // invoke fireInPlay after startElement processing
52                  interpreter.getInterpretationContext().fireInPlay(se);
53              }
54              if (se instanceof BodyEvent) {
55                  // invoke fireInPlay before characters processing
56                  interpreter.getInterpretationContext().fireInPlay(se);
57                  interpreter.characters((BodyEvent) se);
58              }
59              if (se instanceof EndEvent) {
60                  // invoke fireInPlay before endElement processing
61                  interpreter.getInterpretationContext().fireInPlay(se);
62                  interpreter.endElement((EndEvent) se);
63              }
64  
65          }
66      }
67  
68      public void addEventsDynamically(List<SaxEvent> eventList, int offset) {
69          this.eventList.addAll(currentIndex + offset, eventList);
70      }
71  }