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.util.ParserUtils; 022import org.jxmpp.jid.Jid; 023import org.xmlpull.v1.XmlPullParser; 024import org.xmlpull.v1.XmlPullParserException; 025 026import java.io.IOException; 027import java.text.ParseException; 028import java.text.SimpleDateFormat; 029import java.util.ArrayList; 030import java.util.Date; 031import java.util.List; 032import java.util.TimeZone; 033 034/** 035 * An IQProvider for transcripts summaries. 036 * 037 * @author Gaston Dombiak 038 */ 039public class TranscriptsProvider extends IQProvider<Transcripts> { 040 041 private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"); 042 static { 043 UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0")); 044 } 045 046 @Override 047 public Transcripts parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { 048 Jid userID = ParserUtils.getJidAttribute(parser, "userID"); 049 List<Transcripts.TranscriptSummary> summaries = new ArrayList<Transcripts.TranscriptSummary>(); 050 051 boolean done = false; 052 while (!done) { 053 int eventType = parser.next(); 054 if (eventType == XmlPullParser.START_TAG) { 055 if (parser.getName().equals("transcript")) { 056 summaries.add(parseSummary(parser)); 057 } 058 } 059 else if (eventType == XmlPullParser.END_TAG) { 060 if (parser.getName().equals("transcripts")) { 061 done = true; 062 } 063 } 064 } 065 066 return new Transcripts(userID, summaries); 067 } 068 069 private Transcripts.TranscriptSummary parseSummary(XmlPullParser parser) throws IOException, 070 XmlPullParserException { 071 String sessionID = parser.getAttributeValue("", "sessionID"); 072 Date joinTime = null; 073 Date leftTime = null; 074 List<Transcripts.AgentDetail> agents = new ArrayList<Transcripts.AgentDetail>(); 075 076 boolean done = false; 077 while (!done) { 078 int eventType = parser.next(); 079 if (eventType == XmlPullParser.START_TAG) { 080 if (parser.getName().equals("joinTime")) { 081 try { 082 joinTime = UTC_FORMAT.parse(parser.nextText()); 083 } catch (ParseException e) {} 084 } 085 else if (parser.getName().equals("leftTime")) { 086 try { 087 leftTime = UTC_FORMAT.parse(parser.nextText()); 088 } catch (ParseException e) {} 089 } 090 else if (parser.getName().equals("agents")) { 091 agents = parseAgents(parser); 092 } 093 } 094 else if (eventType == XmlPullParser.END_TAG) { 095 if (parser.getName().equals("transcript")) { 096 done = true; 097 } 098 } 099 } 100 101 return new Transcripts.TranscriptSummary(sessionID, joinTime, leftTime, agents); 102 } 103 104 private List<Transcripts.AgentDetail> parseAgents(XmlPullParser parser) throws IOException, XmlPullParserException { 105 List<Transcripts.AgentDetail> agents = new ArrayList<Transcripts.AgentDetail>(); 106 String agentJID = null; 107 Date joinTime = null; 108 Date leftTime = null; 109 110 boolean done = false; 111 while (!done) { 112 int eventType = parser.next(); 113 if (eventType == XmlPullParser.START_TAG) { 114 if (parser.getName().equals("agentJID")) { 115 agentJID = parser.nextText(); 116 } 117 else if (parser.getName().equals("joinTime")) { 118 try { 119 joinTime = UTC_FORMAT.parse(parser.nextText()); 120 } catch (ParseException e) {} 121 } 122 else if (parser.getName().equals("leftTime")) { 123 try { 124 leftTime = UTC_FORMAT.parse(parser.nextText()); 125 } catch (ParseException e) {} 126 } 127 else if (parser.getName().equals("agent")) { 128 agentJID = null; 129 joinTime = null; 130 leftTime = null; 131 } 132 } 133 else if (eventType == XmlPullParser.END_TAG) { 134 if (parser.getName().equals("agents")) { 135 done = true; 136 } 137 else if (parser.getName().equals("agent")) { 138 agents.add(new Transcripts.AgentDetail(agentJID, joinTime, leftTime)); 139 } 140 } 141 } 142 return agents; 143 } 144}