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