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.smackx.pubsub.packet.PubSubNamespace;
024
025/**
026 * Represents the top level element of a pubsub event extension.  All types of pubsub events are
027 * represented by this class.  The specific type can be found by {@link #getEventType()}.  The 
028 * embedded event information, which is specific to the event type, can be retrieved by the {@link #getEvent()}
029 * method.
030 * 
031 * @author Robin Collier
032 */
033public class EventElement implements EmbeddedPacketExtension
034{
035        private EventElementType type;
036        private NodeExtension ext;
037        
038        public EventElement(EventElementType eventType, NodeExtension eventExt)
039        {
040                type = eventType;
041                ext = eventExt;
042        }
043        
044        public EventElementType getEventType()
045        {
046                return type;
047        }
048
049        public List<ExtensionElement> getExtensions()
050        {
051                return Arrays.asList(new ExtensionElement[]{getEvent()});
052        }
053
054        public NodeExtension getEvent()
055        {
056                return ext;
057        }
058
059        public String getElementName()
060        {
061                return "event";
062        }
063
064        public String getNamespace()
065        {
066                return PubSubNamespace.EVENT.getXmlns();
067        }
068
069        public String toXML()
070        {
071                StringBuilder builder = new StringBuilder("<event xmlns='" + PubSubNamespace.EVENT.getXmlns() + "'>");
072
073                builder.append(ext.toXML());
074                builder.append("</event>");
075                return builder.toString();
076        }
077}