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 */ 017package org.jivesoftware.smackx.iqregister.provider; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.jivesoftware.smack.packet.IQ; 026import org.jivesoftware.smack.packet.IqData; 027import org.jivesoftware.smack.packet.XmlElement; 028import org.jivesoftware.smack.packet.XmlEnvironment; 029import org.jivesoftware.smack.parsing.SmackParsingException; 030import org.jivesoftware.smack.provider.IqProvider; 031import org.jivesoftware.smack.util.PacketParserUtils; 032import org.jivesoftware.smack.xml.XmlPullParser; 033import org.jivesoftware.smack.xml.XmlPullParserException; 034 035import org.jivesoftware.smackx.iqregister.packet.Registration; 036 037import org.jxmpp.JxmppContext; 038 039public class RegistrationProvider extends IqProvider<Registration> { 040 041 @Override 042 public Registration parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException, SmackParsingException { 043 String instruction = null; 044 Map<String, String> fields = new HashMap<>(); 045 List<XmlElement> packetExtensions = new ArrayList<>(); 046 outerloop: 047 while (true) { 048 XmlPullParser.Event eventType = parser.next(); 049 if (eventType == XmlPullParser.Event.START_ELEMENT) { 050 // Any element that's in the jabber:iq:register namespace, 051 // attempt to parse it if it's in the form <name>value</name>. 052 if (parser.getNamespace().equals(Registration.NAMESPACE)) { 053 String name = parser.getName(); 054 String value = ""; 055 056 if (parser.next() == XmlPullParser.Event.TEXT_CHARACTERS) { 057 value = parser.getText(); 058 } 059 // Ignore instructions, but anything else should be added to the map. 060 if (!name.equals("instructions")) { 061 fields.put(name, value); 062 } 063 else { 064 instruction = value; 065 } 066 } 067 // Otherwise, it must be a packet extension. 068 else { 069 PacketParserUtils.addExtensionElement(packetExtensions, parser, xmlEnvironment, jxmppContext); 070 } 071 } 072 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 073 if (parser.getName().equals(IQ.QUERY_ELEMENT)) { 074 break outerloop; 075 } 076 } 077 } 078 Registration registration = new Registration(instruction, fields); 079 registration.addExtensions(packetExtensions); 080 return registration; 081 } 082 083}