001/** 002 * 003 * Copyright 2003-2007 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 */ 017 018package org.jivesoftware.smackx.disco.provider; 019 020import org.jivesoftware.smack.provider.IQProvider; 021import org.jivesoftware.smack.util.PacketParserUtils; 022import org.jivesoftware.smackx.disco.packet.DiscoverInfo; 023import org.xmlpull.v1.XmlPullParser; 024 025/** 026* The DiscoverInfoProvider parses Service Discovery information packets. 027* 028* @author Gaston Dombiak 029*/ 030public class DiscoverInfoProvider extends IQProvider<DiscoverInfo> { 031 032 @Override 033 public DiscoverInfo parse(XmlPullParser parser, int initialDepth) 034 throws Exception { 035 DiscoverInfo discoverInfo = new DiscoverInfo(); 036 boolean done = false; 037 DiscoverInfo.Identity identity = null; 038 String category = ""; 039 String identityName = ""; 040 String type = ""; 041 String variable = ""; 042 String lang = ""; 043 discoverInfo.setNode(parser.getAttributeValue("", "node")); 044 while (!done) { 045 int eventType = parser.next(); 046 if (eventType == XmlPullParser.START_TAG) { 047 final String name = parser.getName(); 048 final String namespace = parser.getNamespace(); 049 if (namespace.equals(DiscoverInfo.NAMESPACE)) { 050 switch (name) { 051 case "identity": 052 // Initialize the variables from the parsed XML 053 category = parser.getAttributeValue("", "category"); 054 identityName = parser.getAttributeValue("", "name"); 055 type = parser.getAttributeValue("", "type"); 056 lang = parser.getAttributeValue(parser.getNamespace("xml"), "lang"); 057 break; 058 case "feature": 059 // Initialize the variables from the parsed XML 060 variable = parser.getAttributeValue("", "var"); 061 break; 062 } 063 } 064 // Otherwise, it must be a packet extension. 065 else { 066 PacketParserUtils.addExtensionElement(discoverInfo, parser); 067 } 068 } else if (eventType == XmlPullParser.END_TAG) { 069 if (parser.getName().equals("identity")) { 070 // Create a new identity and add it to the discovered info. 071 identity = new DiscoverInfo.Identity(category, type, identityName, lang); 072 discoverInfo.addIdentity(identity); 073 } 074 if (parser.getName().equals("feature")) { 075 // Create a new feature and add it to the discovered info. 076 boolean notADuplicateFeature = discoverInfo.addFeature(variable); 077 assert(notADuplicateFeature); 078 } 079 if (parser.getName().equals("query")) { 080 done = true; 081 } 082 } 083 } 084 085 return discoverInfo; 086 } 087}