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
016import java.util.ArrayList;
017import java.util.List;
018
019import ch.qos.logback.core.joran.event.BodyEvent;
020import ch.qos.logback.core.joran.event.EndEvent;
021import ch.qos.logback.core.joran.event.SaxEvent;
022import ch.qos.logback.core.joran.event.StartEvent;
023
024public class EventPlayer {
025
026    final Interpreter interpreter;
027    List<SaxEvent> eventList;
028    int currentIndex;
029
030    public EventPlayer(Interpreter interpreter) {
031        this.interpreter = interpreter;
032    }
033
034    /**
035     * Return a copy of the current event list in the player.
036     * @return
037     * @since 0.9.20
038     */
039    public List<SaxEvent> getCopyOfPlayerEventList() {
040        return new ArrayList<SaxEvent>(eventList);
041    }
042
043    public void play(List<SaxEvent> aSaxEventList) {
044        eventList = aSaxEventList;
045        SaxEvent se;
046        for (currentIndex = 0; currentIndex < eventList.size(); currentIndex++) {
047            se = eventList.get(currentIndex);
048
049            if (se instanceof StartEvent) {
050                interpreter.startElement((StartEvent) se);
051                // invoke fireInPlay after startElement processing
052                interpreter.getInterpretationContext().fireInPlay(se);
053            }
054            if (se instanceof BodyEvent) {
055                // invoke fireInPlay before characters processing
056                interpreter.getInterpretationContext().fireInPlay(se);
057                interpreter.characters((BodyEvent) se);
058            }
059            if (se instanceof EndEvent) {
060                // invoke fireInPlay before endElement processing
061                interpreter.getInterpretationContext().fireInPlay(se);
062                interpreter.endElement((EndEvent) se);
063            }
064
065        }
066    }
067
068    public void addEventsDynamically(List<SaxEvent> eventList, int offset) {
069        this.eventList.addAll(currentIndex + offset, eventList);
070    }
071}