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