001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.pubsub;
018
019import java.util.Arrays;
020import java.util.List;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.packet.Stanza;
024import org.jivesoftware.smack.util.XmlStringBuilder;
025import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
026
027/**
028 * Represents the top level element of a pubsub event extension.  All types of pubsub events are
029 * represented by this class.  The specific type can be found by {@link #getEventType()}.  The 
030 * embedded event information, which is specific to the event type, can be retrieved by the {@link #getEvent()}
031 * method.
032 * 
033 * @author Robin Collier
034 */
035public class EventElement implements EmbeddedPacketExtension
036{
037    /**
038     * The constant String "event".
039     */
040    public static final String ELEMENT = "event";
041
042    /**
043     * The constant String "http://jabber.org/protocol/pubsub#event".
044     */
045    public static final String NAMESPACE = PubSubNamespace.EVENT.getXmlns();
046
047    private EventElementType type;
048    private NodeExtension ext;
049
050    public EventElement(EventElementType eventType, NodeExtension eventExt)
051    {
052        type = eventType;
053        ext = eventExt;
054    }
055
056    public EventElementType getEventType()
057    {
058        return type;
059    }
060
061    @Override
062    public List<ExtensionElement> getExtensions()
063    {
064        return Arrays.asList(new ExtensionElement[]{getEvent()});
065    }
066
067    public NodeExtension getEvent()
068    {
069        return ext;
070    }
071
072    @Override
073    public String getElementName()
074    {
075        return "event";
076    }
077
078    @Override
079    public String getNamespace()
080    {
081        return PubSubNamespace.EVENT.getXmlns();
082    }
083
084    @Override
085    public XmlStringBuilder toXML() {
086        XmlStringBuilder xml = new XmlStringBuilder(this);
087        xml.rightAngleBracket();
088        xml.append(ext.toXML());
089        xml.closeElement(this);
090        return xml;
091    }
092
093    public static EventElement from(Stanza stanza) {
094        return stanza.getExtension(ELEMENT, NAMESPACE);
095    }
096}