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.packet.XmlEnvironment;
026import org.jivesoftware.smack.parsing.SmackParsingException;
027import org.jivesoftware.smack.provider.IQProvider;
028import org.jivesoftware.smack.xml.XmlPullParser;
029import org.jivesoftware.smack.xml.XmlPullParserException;
030
031import org.jivesoftware.smackx.si.packet.StreamInitiation;
032import org.jivesoftware.smackx.si.packet.StreamInitiation.File;
033import org.jivesoftware.smackx.xdata.packet.DataForm;
034import org.jivesoftware.smackx.xdata.provider.DataFormProvider;
035
036import org.jxmpp.util.XmppDateTime;
037
038/**
039 * The StreamInitiationProvider parses StreamInitiation packets.
040 *
041 * @author Alexander Wenckus
042 *
043 */
044public class StreamInitiationProvider extends IQProvider<StreamInitiation> {
045    private static final Logger LOGGER = Logger.getLogger(StreamInitiationProvider.class.getName());
046
047    @Override
048    public StreamInitiation parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
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        outerloop: while (true) {
068            XmlPullParser.Event eventType = parser.next();
069            if (eventType == XmlPullParser.Event.START_ELEMENT) {
070                String elementName = parser.getName();
071                String namespace = parser.getNamespace();
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.Event.END_ELEMENT) {
086                if (parser.getDepth() == initialDepth) {
087                    break outerloop;
088                }
089                if (parser.getName().equals("file")) {
090                    long fileSize = 0;
091                    if (size != null && size.trim().length() != 0) {
092                        try {
093                            fileSize = Long.parseLong(size);
094                        }
095                        catch (NumberFormatException e) {
096                            LOGGER.log(Level.SEVERE, "Failed to parse file size from " + fileSize, e);
097                        }
098                    }
099
100                    Date fileDate = new Date();
101                    if (date != null) {
102                        try {
103                            fileDate = XmppDateTime.parseDate(date);
104                        } catch (ParseException e) {
105                            // couldn't parse date, use current date-time
106                        }
107                    }
108
109                    File file = new File(name, fileSize);
110                    file.setHash(hash);
111                    file.setDate(fileDate);
112                    file.setDesc(desc);
113                    file.setRanged(isRanged);
114                    initiation.setFile(file);
115                }
116            }
117        }
118
119        initiation.setSessionID(id);
120        initiation.setMimeType(mimeType);
121
122        initiation.setFeatureNegotiationForm(form);
123
124        return initiation;
125    }
126
127}