KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > provider > DiscoverInfoProvider


1 /**
2  * $RCSfile$
3  * $Revision: 2407 $
4  * $Date: 2004-11-02 20:37:00 -0300 (Tue, 02 Nov 2004) $
5  *
6  * Copyright 2003-2004 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  */

20
21 package org.jivesoftware.smackx.provider;
22
23 import org.jivesoftware.smack.packet.IQ;
24 import org.jivesoftware.smack.provider.IQProvider;
25 import org.jivesoftware.smack.util.PacketParserUtils;
26 import org.jivesoftware.smackx.packet.DiscoverInfo;
27 import org.xmlpull.v1.XmlPullParser;
28
29 /**
30 * The DiscoverInfoProvider parses Service Discovery information packets.
31 *
32 * @author Gaston Dombiak
33 */

34 public class DiscoverInfoProvider implements IQProvider {
35
36     public IQ parseIQ(XmlPullParser parser) throws Exception JavaDoc {
37         DiscoverInfo discoverInfo = new DiscoverInfo();
38         boolean done = false;
39         DiscoverInfo.Feature feature = null;
40         DiscoverInfo.Identity identity = null;
41         String JavaDoc category = "";
42         String JavaDoc name = "";
43         String JavaDoc type = "";
44         String JavaDoc variable = "";
45         discoverInfo.setNode(parser.getAttributeValue("", "node"));
46         while (!done) {
47             int eventType = parser.next();
48             if (eventType == XmlPullParser.START_TAG) {
49                 if (parser.getName().equals("identity")) {
50                     // Initialize the variables from the parsed XML
51
category = parser.getAttributeValue("", "category");
52                     name = parser.getAttributeValue("", "name");
53                     type = parser.getAttributeValue("", "type");
54                 }
55                 else if (parser.getName().equals("feature")) {
56                     // Initialize the variables from the parsed XML
57
variable = parser.getAttributeValue("", "var");
58                 }
59                 // Otherwise, it must be a packet extension.
60
else {
61                     discoverInfo.addExtension(PacketParserUtils.parsePacketExtension(parser
62                             .getName(), parser.getNamespace(), parser));
63                 }
64             } else if (eventType == XmlPullParser.END_TAG) {
65                 if (parser.getName().equals("identity")) {
66                     // Create a new identity and add it to the discovered info.
67
identity = new DiscoverInfo.Identity(category, name);
68                     identity.setType(type);
69                     discoverInfo.addIdentity(identity);
70                 }
71                 if (parser.getName().equals("feature")) {
72                     // Create a new feature and add it to the discovered info.
73
discoverInfo.addFeature(variable);
74                 }
75                 if (parser.getName().equals("query")) {
76                     done = true;
77                 }
78             }
79         }
80
81         return discoverInfo;
82     }
83 }
Popular Tags