1/**
2 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 *     http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14package org.jivesoftware.smackx.bytestreams.socks5.provider;
15
16import org.jivesoftware.smack.packet.IQ;
17import org.jivesoftware.smack.provider.IQProvider;
18import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
19import org.xmlpull.v1.XmlPullParser;
20
21/**
22 * Parses a bytestream packet.
23 *
24 * @author Alexander Wenckus
25 */
26public class BytestreamsProvider implements IQProvider {
27
28    public IQ parseIQ(XmlPullParser parser) throws Exception {
29        boolean done = false;
30
31        Bytestream toReturn = new Bytestream();
32
33        String id = parser.getAttributeValue("", "sid");
34        String mode = parser.getAttributeValue("", "mode");
35
36        // streamhost
37        String JID = null;
38        String host = null;
39        String port = null;
40
41        int eventType;
42        String elementName;
43        while (!done) {
44            eventType = parser.next();
45            elementName = parser.getName();
46            if (eventType == XmlPullParser.START_TAG) {
47                if (elementName.equals(Bytestream.StreamHost.ELEMENTNAME)) {
48                    JID = parser.getAttributeValue("", "jid");
49                    host = parser.getAttributeValue("", "host");
50                    port = parser.getAttributeValue("", "port");
51                }
52                else if (elementName.equals(Bytestream.StreamHostUsed.ELEMENTNAME)) {
53                    toReturn.setUsedHost(parser.getAttributeValue("", "jid"));
54                }
55                else if (elementName.equals(Bytestream.Activate.ELEMENTNAME)) {
56                    toReturn.setToActivate(parser.getAttributeValue("", "jid"));
57                }
58            }
59            else if (eventType == XmlPullParser.END_TAG) {
60                if (elementName.equals("streamhost")) {
61                    if (port == null) {
62                        toReturn.addStreamHost(JID, host);
63                    }
64                    else {
65                        toReturn.addStreamHost(JID, host, Integer.parseInt(port));
66                    }
67                    JID = null;
68                    host = null;
69                    port = null;
70                }
71                else if (elementName.equals("query")) {
72                    done = true;
73                }
74            }
75        }
76
77        toReturn.setMode((Bytestream.Mode.fromName(mode)));
78        toReturn.setSessionID(id);
79        return toReturn;
80    }
81
82}
83