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