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