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 org.jivesoftware.smack.provider.IQProvider;
021import org.jivesoftware.smack.packet.Stanza;
022import org.jivesoftware.smack.util.PacketParserUtils;
023import org.xmlpull.v1.XmlPullParser;
024
025import java.util.ArrayList;
026import java.util.List;
027
028/**
029 * An IQProvider for transcripts.
030 *
031 * @author Gaston Dombiak
032 */
033public class TranscriptProvider extends IQProvider<Transcript> {
034
035    @Override
036    public Transcript parse(XmlPullParser parser, int initialDepth) throws Exception {
037        String sessionID = parser.getAttributeValue("", "sessionID");
038        List<Stanza> packets = new ArrayList<Stanza>();
039
040        boolean done = false;
041        while (!done) {
042            int eventType = parser.next();
043            if (eventType == XmlPullParser.START_TAG) {
044                if (parser.getName().equals("message")) {
045                    packets.add(PacketParserUtils.parseMessage(parser));
046                }
047                else if (parser.getName().equals("presence")) {
048                    packets.add(PacketParserUtils.parsePresence(parser));
049                }
050            }
051            else if (eventType == XmlPullParser.END_TAG) {
052                if (parser.getName().equals("transcript")) {
053                    done = true;
054                }
055            }
056        }
057
058        return new Transcript(sessionID, packets);
059    }
060}