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