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        boolean done = false;
050
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        String elementName;
070        String namespace;
071        while (!done) {
072            XmlPullParser.Event eventType = parser.next();
073            elementName = parser.getName();
074            namespace = parser.getNamespace();
075            if (eventType == XmlPullParser.Event.START_ELEMENT) {
076                if (elementName.equals("file")) {
077                    name = parser.getAttributeValue("", "name");
078                    size = parser.getAttributeValue("", "size");
079                    hash = parser.getAttributeValue("", "hash");
080                    date = parser.getAttributeValue("", "date");
081                } else if (elementName.equals("desc")) {
082                    desc = parser.nextText();
083                } else if (elementName.equals("range")) {
084                    isRanged = true;
085                } else if (elementName.equals("x")
086                        && namespace.equals("jabber:x:data")) {
087                    form = dataFormProvider.parse(parser);
088                }
089            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
090                if (elementName.equals("si")) {
091                    done = true;
092                } else if (elementName.equals("file")) {
093                    long fileSize = 0;
094                    if (size != null && size.trim().length() != 0) {
095                        try {
096                            fileSize = Long.parseLong(size);
097                        }
098                        catch (NumberFormatException e) {
099                            LOGGER.log(Level.SEVERE, "Failed to parse file size from " + fileSize, e);
100                        }
101                    }
102
103                    Date fileDate = new Date();
104                    if (date != null) {
105                        try {
106                            fileDate = XmppDateTime.parseDate(date);
107                        } catch (ParseException e) {
108                            // couldn't parse date, use current date-time
109                        }
110                    }
111
112                    File file = new File(name, fileSize);
113                    file.setHash(hash);
114                    file.setDate(fileDate);
115                    file.setDesc(desc);
116                    file.setRanged(isRanged);
117                    initiation.setFile(file);
118                }
119            }
120        }
121
122        initiation.setSessionID(id);
123        initiation.setMimeType(mimeType);
124
125        initiation.setFeatureNegotiationForm(form);
126
127        return initiation;
128    }
129
130}