1/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * Copyright 2003-2006 Jive Software.
7 *
8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20package org.jivesoftware.smackx.provider;
21
22import java.text.ParseException;
23import java.util.Date;
24
25import org.jivesoftware.smack.packet.IQ;
26import org.jivesoftware.smack.provider.IQProvider;
27import org.jivesoftware.smack.util.StringUtils;
28import org.jivesoftware.smackx.packet.DataForm;
29import org.jivesoftware.smackx.packet.StreamInitiation;
30import org.jivesoftware.smackx.packet.StreamInitiation.File;
31import org.xmlpull.v1.XmlPullParser;
32
33/**
34 * The StreamInitiationProvider parses StreamInitiation packets.
35 *
36 * @author Alexander Wenckus
37 *
38 */
39public class StreamInitiationProvider implements IQProvider {
40
41	public IQ parseIQ(final XmlPullParser parser) throws Exception {
42		boolean done = false;
43
44		// si
45		String id = parser.getAttributeValue("", "id");
46		String mimeType = parser.getAttributeValue("", "mime-type");
47
48		StreamInitiation initiation = new StreamInitiation();
49
50		// file
51		String name = null;
52		String size = null;
53		String hash = null;
54		String date = null;
55		String desc = null;
56		boolean isRanged = false;
57
58		// feature
59		DataForm form = null;
60		DataFormProvider dataFormProvider = new DataFormProvider();
61
62		int eventType;
63		String elementName;
64		String namespace;
65		while (!done) {
66			eventType = parser.next();
67			elementName = parser.getName();
68			namespace = parser.getNamespace();
69			if (eventType == XmlPullParser.START_TAG) {
70				if (elementName.equals("file")) {
71					name = parser.getAttributeValue("", "name");
72					size = parser.getAttributeValue("", "size");
73					hash = parser.getAttributeValue("", "hash");
74					date = parser.getAttributeValue("", "date");
75				} else if (elementName.equals("desc")) {
76					desc = parser.nextText();
77				} else if (elementName.equals("range")) {
78					isRanged = true;
79				} else if (elementName.equals("x")
80						&& namespace.equals("jabber:x:data")) {
81					form = (DataForm) dataFormProvider.parseExtension(parser);
82				}
83			} else if (eventType == XmlPullParser.END_TAG) {
84				if (elementName.equals("si")) {
85					done = true;
86				} else if (elementName.equals("file")) {
87                    long fileSize = 0;
88                    if(size != null && size.trim().length() !=0){
89                        try {
90                            fileSize = Long.parseLong(size);
91                        }
92                        catch (NumberFormatException e) {
93                            e.printStackTrace();
94                        }
95                    }
96
97                    Date fileDate = new Date();
98                    if (date != null) {
99                        try {
100                            fileDate = StringUtils.parseXEP0082Date(date);
101                        } catch (ParseException e) {
102                            // couldn't parse date, use current date-time
103                        }
104                    }
105
106                    File file = new File(name, fileSize);
107					file.setHash(hash);
108					file.setDate(fileDate);
109					file.setDesc(desc);
110					file.setRanged(isRanged);
111					initiation.setFile(file);
112				}
113			}
114		}
115
116		initiation.setSesssionID(id);
117		initiation.setMimeType(mimeType);
118
119		initiation.setFeatureNegotiationForm(form);
120
121		return initiation;
122	}
123
124}
125