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.event.stax;
015
016import ch.qos.logback.core.joran.spi.ElementPath;
017
018import javax.xml.stream.Location;
019import javax.xml.stream.events.Attribute;
020import java.util.ArrayList;
021import java.util.Iterator;
022import java.util.List;
023
024public class StartEvent extends StaxEvent {
025
026    List<Attribute> attributes;
027    public ElementPath elementPath;
028
029    StartEvent(ElementPath elementPath, String name, Iterator<Attribute> attributeIterator, Location location) {
030        super(name, location);
031        populateAttributes(attributeIterator);
032        this.elementPath = elementPath;
033    }
034
035    private void populateAttributes(Iterator<Attribute> attributeIterator) {
036        while (attributeIterator.hasNext()) {
037            if (attributes == null) {
038                attributes = new ArrayList<Attribute>(2);
039            }
040            attributes.add(attributeIterator.next());
041        }
042    }
043
044    public ElementPath getElementPath() {
045        return elementPath;
046    }
047
048    public List<Attribute> getAttributeList() {
049        return attributes;
050    }
051
052    Attribute getAttributeByName(String name) {
053        if (attributes == null)
054            return null;
055
056        for (Attribute attr : attributes) {
057            if (name.equals(attr.getName().getLocalPart()))
058                return attr;
059        }
060        return null;
061    }
062
063    @Override
064    public String toString() {
065        return "StartEvent(" + getName() + ")  [" + location.getLineNumber() + "," + location.getColumnNumber() + "]";
066    }
067}