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.classic.sift;
015
016import java.util.ArrayList;
017import java.util.List;
018import java.util.Map;
019
020import org.xml.sax.Attributes;
021
022import ch.qos.logback.core.joran.action.Action;
023import ch.qos.logback.core.joran.event.InPlayListener;
024import ch.qos.logback.core.joran.event.SaxEvent;
025import ch.qos.logback.core.joran.spi.ActionException;
026import ch.qos.logback.core.joran.spi.InterpretationContext;
027
028public class SiftAction extends Action implements InPlayListener {
029    List<SaxEvent> seList;
030
031    @Override
032    public void begin(InterpretationContext ic, String name, Attributes attributes) throws ActionException {
033        seList = new ArrayList<SaxEvent>();
034        ic.addInPlayListener(this);
035    }
036
037    @Override
038    public void end(InterpretationContext ic, String name) throws ActionException {
039        ic.removeInPlayListener(this);
040        Object o = ic.peekObject();
041        if (o instanceof SiftingAppender) {
042            SiftingAppender sa = (SiftingAppender) o;
043            Map<String, String> propertyMap = ic.getCopyOfPropertyMap();
044            AppenderFactoryUsingJoran appenderFactory = new AppenderFactoryUsingJoran(seList, sa.getDiscriminatorKey(), propertyMap);
045            sa.setAppenderFactory(appenderFactory);
046        }
047    }
048
049    public void inPlay(SaxEvent event) {
050        seList.add(event);
051    }
052
053    public List<SaxEvent> getSeList() {
054        return seList;
055    }
056
057}