001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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 */
017
018package org.jivesoftware.smackx.workgroup.packet;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.provider.ExtensionElementProvider;
024
025import org.xmlpull.v1.XmlPullParser;
026import org.xmlpull.v1.XmlPullParserException;
027
028public class SessionID implements ExtensionElement {
029
030    /**
031     * Element name of the stanza extension.
032     */
033    public static final String ELEMENT_NAME = "session";
034
035    /**
036     * Namespace of the stanza extension.
037     */
038    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
039
040    private final String sessionID;
041
042    public SessionID(String sessionID) {
043        this.sessionID = sessionID;
044    }
045
046    public String getSessionID() {
047        return this.sessionID;
048    }
049
050    @Override
051    public String getElementName() {
052        return ELEMENT_NAME;
053    }
054
055    @Override
056    public String getNamespace() {
057        return NAMESPACE;
058    }
059
060    @Override
061    public String toXML(String enclosingNamespace) {
062        StringBuilder buf = new StringBuilder();
063
064        buf.append('<').append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\" ");
065        buf.append("id=\"").append(this.getSessionID());
066        buf.append("\"/>");
067
068        return buf.toString();
069    }
070
071    public static class Provider extends ExtensionElementProvider<SessionID> {
072
073        @Override
074        public SessionID parse(XmlPullParser parser, int initialDepth)
075                        throws XmlPullParserException, IOException {
076            String sessionID = parser.getAttributeValue("", "id");
077
078            // Advance to end of extension.
079            parser.next();
080
081            return new SessionID(sessionID);
082        }
083    }
084}