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.ext.history;
019
020import java.io.IOException;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.jivesoftware.smack.packet.IQ;
026import org.jivesoftware.smack.packet.XmlEnvironment;
027import org.jivesoftware.smack.provider.IQProvider;
028import org.jivesoftware.smack.xml.XmlPullParser;
029import org.jivesoftware.smack.xml.XmlPullParserException;
030
031import org.jivesoftware.smackx.workgroup.util.MetaDataUtils;
032
033public class ChatMetadata extends IQ {
034
035    /**
036     * Element name of the stanza extension.
037     */
038    public static final String ELEMENT_NAME = "chat-metadata";
039
040    /**
041     * Namespace of the stanza extension.
042     */
043    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
044
045
046    private String sessionID;
047
048    public ChatMetadata() {
049        super(ELEMENT_NAME, NAMESPACE);
050    }
051
052    public String getSessionID() {
053        return sessionID;
054    }
055
056    public void setSessionID(String sessionID) {
057        this.sessionID = sessionID;
058    }
059
060
061    private Map<String, List<String>> map = new HashMap<String, List<String>>();
062
063    public void setMetadata(Map<String, List<String>> metadata) {
064        this.map = metadata;
065    }
066
067    public Map<String, List<String>> getMetadata() {
068        return map;
069    }
070
071    @Override
072    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
073        buf.rightAngleBracket();
074        buf.append("<sessionID>").append(getSessionID()).append("</sessionID>");
075
076        return buf;
077    }
078
079    /**
080     * An IQProvider for Metadata packets.
081     *
082     * @author Derek DeMoro
083     */
084    public static class Provider extends IQProvider<ChatMetadata> {
085
086        @Override
087        public ChatMetadata parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
088                        throws XmlPullParserException, IOException {
089            final ChatMetadata chatM = new ChatMetadata();
090
091            boolean done = false;
092            while (!done) {
093                XmlPullParser.Event eventType = parser.next();
094                if (eventType == XmlPullParser.Event.START_ELEMENT) {
095                    if (parser.getName().equals("sessionID")) {
096                       chatM.setSessionID(parser.nextText());
097                    }
098                    else if (parser.getName().equals("metadata")) {
099                        Map<String, List<String>> map = MetaDataUtils.parseMetaData(parser);
100                        chatM.setMetadata(map);
101                    }
102                }
103                else if (eventType == XmlPullParser.Event.END_ELEMENT) {
104                    if (parser.getName().equals(ELEMENT_NAME)) {
105                        done = true;
106                    }
107                }
108            }
109
110            return chatM;
111        }
112    }
113}
114
115
116
117