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