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