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