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