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 037public class RegistrationProvider extends IqProvider<Registration> { 038 039 @Override 040 public Registration parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { 041 String instruction = null; 042 Map<String, String> fields = new HashMap<>(); 043 List<XmlElement> packetExtensions = new ArrayList<>(); 044 outerloop: 045 while (true) { 046 XmlPullParser.Event eventType = parser.next(); 047 if (eventType == XmlPullParser.Event.START_ELEMENT) { 048 // Any element that's in the jabber:iq:register namespace, 049 // attempt to parse it if it's in the form <name>value</name>. 050 if (parser.getNamespace().equals(Registration.NAMESPACE)) { 051 String name = parser.getName(); 052 String value = ""; 053 054 if (parser.next() == XmlPullParser.Event.TEXT_CHARACTERS) { 055 value = parser.getText(); 056 } 057 // Ignore instructions, but anything else should be added to the map. 058 if (!name.equals("instructions")) { 059 fields.put(name, value); 060 } 061 else { 062 instruction = value; 063 } 064 } 065 // Otherwise, it must be a packet extension. 066 else { 067 PacketParserUtils.addExtensionElement(packetExtensions, parser, xmlEnvironment); 068 } 069 } 070 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 071 if (parser.getName().equals(IQ.QUERY_ELEMENT)) { 072 break outerloop; 073 } 074 } 075 } 076 Registration registration = new Registration(instruction, fields); 077 registration.addExtensions(packetExtensions); 078 return registration; 079 } 080 081}