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;
025
026import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
027
028/**
029 * Represents the top level element of a PubSub event extension.  All types of PubSub events are
030 * represented by this class.  The specific type can be found by {@link #getEventType()}.  The 
031 * embedded event information, which is specific to the event type, can be retrieved by the {@link #getEvent()}
032 * method.
033 * 
034 * @author Robin Collier
035 */
036public class EventElement implements EmbeddedPacketExtension
037{
038    /**
039     * The constant String "event".
040     */
041    public static final String ELEMENT = "event";
042
043    /**
044     * The constant String "http://jabber.org/protocol/pubsub#event".
045     */
046    public static final String NAMESPACE = PubSubNamespace.EVENT.getXmlns();
047
048    private final EventElementType type;
049    private final NodeExtension ext;
050
051    public EventElement(EventElementType eventType, NodeExtension eventExt)
052    {
053        type = eventType;
054        ext = eventExt;
055    }
056
057    public EventElementType getEventType()
058    {
059        return type;
060    }
061
062    @Override
063    public List<ExtensionElement> getExtensions()
064    {
065        return Arrays.asList(new ExtensionElement[] {getEvent()});
066    }
067
068    public NodeExtension getEvent()
069    {
070        return ext;
071    }
072
073    @Override
074    public String getElementName()
075    {
076        return "event";
077    }
078
079    @Override
080    public String getNamespace()
081    {
082        return PubSubNamespace.EVENT.getXmlns();
083    }
084
085    @Override
086    public XmlStringBuilder toXML() {
087        XmlStringBuilder xml = new XmlStringBuilder(this);
088        xml.rightAngleBracket();
089        xml.append(ext.toXML());
090        xml.closeElement(this);
091        return xml;
092    }
093
094    public static EventElement from(Stanza stanza) {
095        return stanza.getExtension(ELEMENT, NAMESPACE);
096    }
097}