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