001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.spi;
015
016
017import java.util.ArrayList;
018import java.util.List;
019
020import ch.qos.logback.core.joran.event.BodyEvent;
021import ch.qos.logback.core.joran.event.EndEvent;
022import ch.qos.logback.core.joran.event.SaxEvent;
023import ch.qos.logback.core.joran.event.StartEvent;
024
025public class EventPlayer {
026
027    final SaxEventInterpreter interpreter;
028    final List<SaxEvent> saxEvents;
029    int currentIndex;
030
031    public EventPlayer(SaxEventInterpreter interpreter, List<SaxEvent> saxEvents) {
032        this.interpreter = interpreter;
033        this.saxEvents = saxEvents;
034    }
035
036    /**
037     * Return a copy of the current event list in the player.
038     * 
039     * @return
040     * @since 0.9.20
041     */
042    public List<SaxEvent> getCopyOfPlayerEventList() {
043        return new ArrayList<SaxEvent>(saxEvents);
044    }
045
046    public void play() {
047         
048        for (currentIndex = 0; currentIndex < saxEvents.size(); currentIndex++) {
049            SaxEvent se = saxEvents.get(currentIndex);
050
051            if (se instanceof StartEvent) {
052                interpreter.startElement((StartEvent) se);
053                continue;
054            }
055            if (se instanceof BodyEvent) {
056                interpreter.characters((BodyEvent) se);
057                continue;
058            }
059            if (se instanceof EndEvent) {
060                interpreter.endElement((EndEvent) se);
061                continue;
062            }
063
064        }
065    }
066
067    public void addEventsDynamically(List<SaxEvent> eventList, int offset) {
068        this.saxEvents.addAll(currentIndex + offset, eventList);
069    }
070}