Shuyi Chen | d7955ce | 2013-05-22 14:51:55 -0700 | [diff] [blame] | 1 | /** |
| 2 | * $RCSfile$ |
| 3 | * $Revision: 7071 $ |
| 4 | * $Date: 2007-02-12 08:59:05 +0800 (Mon, 12 Feb 2007) $ |
| 5 | * |
| 6 | * Copyright 2003-2007 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 { |
| 37 | DiscoverInfo discoverInfo = new DiscoverInfo(); |
| 38 | boolean done = false; |
| 39 | DiscoverInfo.Feature feature = null; |
| 40 | DiscoverInfo.Identity identity = null; |
| 41 | String category = ""; |
| 42 | String name = ""; |
| 43 | String type = ""; |
| 44 | String variable = ""; |
| 45 | String lang = ""; |
| 46 | discoverInfo.setNode(parser.getAttributeValue("", "node")); |
| 47 | while (!done) { |
| 48 | int eventType = parser.next(); |
| 49 | if (eventType == XmlPullParser.START_TAG) { |
| 50 | if (parser.getName().equals("identity")) { |
| 51 | // Initialize the variables from the parsed XML |
| 52 | category = parser.getAttributeValue("", "category"); |
| 53 | name = parser.getAttributeValue("", "name"); |
| 54 | type = parser.getAttributeValue("", "type"); |
| 55 | lang = parser.getAttributeValue(parser.getNamespace("xml"), "lang"); |
| 56 | } |
| 57 | else if (parser.getName().equals("feature")) { |
| 58 | // Initialize the variables from the parsed XML |
| 59 | variable = parser.getAttributeValue("", "var"); |
| 60 | } |
| 61 | // Otherwise, it must be a packet extension. |
| 62 | else { |
| 63 | discoverInfo.addExtension(PacketParserUtils.parsePacketExtension(parser |
| 64 | .getName(), parser.getNamespace(), parser)); |
| 65 | } |
| 66 | } else if (eventType == XmlPullParser.END_TAG) { |
| 67 | if (parser.getName().equals("identity")) { |
| 68 | // Create a new identity and add it to the discovered info. |
| 69 | identity = new DiscoverInfo.Identity(category, name, type); |
| 70 | if (lang != null) |
| 71 | identity.setLanguage(lang); |
| 72 | discoverInfo.addIdentity(identity); |
| 73 | } |
| 74 | if (parser.getName().equals("feature")) { |
| 75 | // Create a new feature and add it to the discovered info. |
| 76 | discoverInfo.addFeature(variable); |
| 77 | } |
| 78 | if (parser.getName().equals("query")) { |
| 79 | done = true; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return discoverInfo; |
| 85 | } |
| 86 | } |