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