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     * 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 final EventElementType type;
048    private final NodeExtension ext;
049
050    public EventElement(EventElementType eventType, NodeExtension eventExt) {
051        type = eventType;
052        ext = eventExt;
053    }
054
055    public EventElementType getEventType() {
056        return type;
057    }
058
059    @Override
060    public List<ExtensionElement> getExtensions() {
061        return Arrays.asList(new ExtensionElement[] {getEvent()});
062    }
063
064    public NodeExtension getEvent() {
065        return ext;
066    }
067
068    @Override
069    public String getElementName() {
070        return "event";
071    }
072
073    @Override
074    public String getNamespace() {
075        return PubSubNamespace.event.getXmlns();
076    }
077
078    @Override
079    public XmlStringBuilder toXML(String enclosingNamespace) {
080        XmlStringBuilder xml = new XmlStringBuilder(this);
081        xml.rightAngleBracket();
082        xml.append(ext.toXML(null));
083        xml.closeElement(this);
084        return xml;
085    }
086
087    public static EventElement from(Stanza stanza) {
088        return stanza.getExtension(ELEMENT, NAMESPACE);
089    }
090}