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.IqData;
026import org.jivesoftware.smack.packet.XmlEnvironment;
027import org.jivesoftware.smack.parsing.SmackParsingException;
028import org.jivesoftware.smack.provider.IqProvider;
029import org.jivesoftware.smack.xml.XmlPullParser;
030import org.jivesoftware.smack.xml.XmlPullParserException;
031
032import org.jivesoftware.smackx.si.packet.StreamInitiation;
033import org.jivesoftware.smackx.si.packet.StreamInitiation.File;
034import org.jivesoftware.smackx.xdata.packet.DataForm;
035import org.jivesoftware.smackx.xdata.provider.DataFormProvider;
036
037import org.jxmpp.util.XmppDateTime;
038
039/**
040 * The StreamInitiationProvider parses StreamInitiation packets.
041 *
042 * @author Alexander Wenckus
043 *
044 */
045public class StreamInitiationProvider extends IqProvider<StreamInitiation> {
046    private static final Logger LOGGER = Logger.getLogger(StreamInitiationProvider.class.getName());
047
048    @SuppressWarnings("JavaUtilDate")
049    @Override
050    public StreamInitiation parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
051        // si
052        String id = parser.getAttributeValue("", "id");
053        String mimeType = parser.getAttributeValue("", "mime-type");
054
055        StreamInitiation initiation = new StreamInitiation();
056
057        // file
058        String name = null;
059        String size = null;
060        String hash = null;
061        String date = null;
062        String desc = null;
063        boolean isRanged = false;
064
065        // feature
066        DataForm form = null;
067        DataFormProvider dataFormProvider = new DataFormProvider();
068
069        outerloop: while (true) {
070            XmlPullParser.Event eventType = parser.next();
071            if (eventType == XmlPullParser.Event.START_ELEMENT) {
072                String elementName = parser.getName();
073                String namespace = parser.getNamespace();
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.Event.END_ELEMENT) {
088                if (parser.getDepth() == initialDepth) {
089                    break outerloop;
090                }
091                if (parser.getName().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}