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