001/** 002 * 003 * Copyright 2003-2006 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 */ 017package org.jivesoftware.smackx.si.provider; 018 019import java.text.ParseException; 020import java.util.Date; 021import java.util.logging.Level; 022import java.util.logging.Logger; 023 024import org.jivesoftware.smack.provider.IQProvider; 025 026import org.jivesoftware.smackx.si.packet.StreamInitiation; 027import org.jivesoftware.smackx.si.packet.StreamInitiation.File; 028import org.jivesoftware.smackx.xdata.packet.DataForm; 029import org.jivesoftware.smackx.xdata.provider.DataFormProvider; 030 031import org.jxmpp.util.XmppDateTime; 032import org.xmlpull.v1.XmlPullParser; 033 034/** 035 * The StreamInitiationProvider parses StreamInitiation packets. 036 * 037 * @author Alexander Wenckus 038 * 039 */ 040public class StreamInitiationProvider extends IQProvider<StreamInitiation> { 041 private static final Logger LOGGER = Logger.getLogger(StreamInitiationProvider.class.getName()); 042 043 @Override 044 public StreamInitiation parse(XmlPullParser parser, int initialDepth) 045 throws Exception { 046 boolean done = false; 047 048 // si 049 String id = parser.getAttributeValue("", "id"); 050 String mimeType = parser.getAttributeValue("", "mime-type"); 051 052 StreamInitiation initiation = new StreamInitiation(); 053 054 // file 055 String name = null; 056 String size = null; 057 String hash = null; 058 String date = null; 059 String desc = null; 060 boolean isRanged = false; 061 062 // feature 063 DataForm form = null; 064 DataFormProvider dataFormProvider = new DataFormProvider(); 065 066 int eventType; 067 String elementName; 068 String namespace; 069 while (!done) { 070 eventType = parser.next(); 071 elementName = parser.getName(); 072 namespace = parser.getNamespace(); 073 if (eventType == XmlPullParser.START_TAG) { 074 if (elementName.equals("file")) { 075 name = parser.getAttributeValue("", "name"); 076 size = parser.getAttributeValue("", "size"); 077 hash = parser.getAttributeValue("", "hash"); 078 date = parser.getAttributeValue("", "date"); 079 } else if (elementName.equals("desc")) { 080 desc = parser.nextText(); 081 } else if (elementName.equals("range")) { 082 isRanged = true; 083 } else if (elementName.equals("x") 084 && namespace.equals("jabber:x:data")) { 085 form = dataFormProvider.parse(parser); 086 } 087 } else if (eventType == XmlPullParser.END_TAG) { 088 if (elementName.equals("si")) { 089 done = true; 090 } else if (elementName.equals("file")) { 091 long fileSize = 0; 092 if (size != null && size.trim().length() != 0) { 093 try { 094 fileSize = Long.parseLong(size); 095 } 096 catch (NumberFormatException e) { 097 LOGGER.log(Level.SEVERE, "Failed to parse file size from " + fileSize, e); 098 } 099 } 100 101 Date fileDate = new Date(); 102 if (date != null) { 103 try { 104 fileDate = XmppDateTime.parseDate(date); 105 } catch (ParseException e) { 106 // couldn't parse date, use current date-time 107 } 108 } 109 110 File file = new File(name, fileSize); 111 file.setHash(hash); 112 file.setDate(fileDate); 113 file.setDesc(desc); 114 file.setRanged(isRanged); 115 initiation.setFile(file); 116 } 117 } 118 } 119 120 initiation.setSessionID(id); 121 initiation.setMimeType(mimeType); 122 123 initiation.setFeatureNegotiationForm(form); 124 125 return initiation; 126 } 127 128}